Crypto Strategy SUSDT 10 min 策略
基于 EMA 的简单策略:当收盘价高于 EMA 且开盘价低于 EMA 时做多,反之做空。止损和止盈按照入场价的百分比设置。
详情
- 入场条件:
- 多头:
close > EMA且open < EMA - 空头:
close < EMA且open > EMA
- 多头:
- 多空方向:双向。
- 离场条件:止盈或止损。
- 止损/止盈:有,均为百分比。
- 默认值:
CandleType= 10 分钟EmaLength= 24TakeProfitPercent= 4StopLossPercent= 2OrderPercent= 30
- 过滤器:
- 类别:趋势跟随
- 方向:双向
- 指标:EMA
- 止损:是
- 复杂度:低
- 时间框架:日内
- 季节性:否
- 神经网络:否
- 背离:否
- 风险等级:中等
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Crypto SUSDT 10 min strategy.
/// A candle that opens below the EMA and closes above it buys, one that opens above the EMA and closes below it sells short,
/// reversing an opposite position. Every trade is closed by a TakeProfitPercent target or a StopLossPercent stop.
/// </summary>
public class CryptoSusdt10MinStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _emaLength;
private readonly StrategyParam<decimal> _takeProfitPercent;
private readonly StrategyParam<decimal> _stopLossPercent;
private readonly StrategyParam<decimal> _orderPercent;
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// EMA length.
/// </summary>
public int EmaLength
{
get => _emaLength.Value;
set => _emaLength.Value = value;
}
/// <summary>
/// Take profit percentage from entry price.
/// </summary>
public decimal TakeProfitPercent
{
get => _takeProfitPercent.Value;
set => _takeProfitPercent.Value = value;
}
/// <summary>
/// Stop loss percentage from entry price.
/// </summary>
public decimal StopLossPercent
{
get => _stopLossPercent.Value;
set => _stopLossPercent.Value = value;
}
/// <summary>
/// Percent of equity per order (informational; the strategy trades Volume).
/// </summary>
public decimal OrderPercent
{
get => _orderPercent.Value;
set => _orderPercent.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public CryptoSusdt10MinStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(10).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
_emaLength = Param(nameof(EmaLength), 24)
.SetGreaterThanZero()
.SetDisplay("EMA Length", "EMA length", "Indicators");
_takeProfitPercent = Param(nameof(TakeProfitPercent), 4m)
.SetNotNegative()
.SetDisplay("Take Profit %", "Take profit percentage from entry price", "Risk");
_stopLossPercent = Param(nameof(StopLossPercent), 2m)
.SetNotNegative()
.SetDisplay("Stop Loss %", "Stop loss percentage from entry price", "Risk");
_orderPercent = Param(nameof(OrderPercent), 30m)
.SetGreaterThanZero()
.SetDisplay("Order %", "Percent of equity per order", "Risk");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var ema = new ExponentialMovingAverage { Length = EmaLength };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(ema, ProcessCandle)
.Start();
StartProtection(
TakeProfitPercent > 0 ? new Unit(TakeProfitPercent, UnitTypes.Percent) : new Unit(),
StopLossPercent > 0 ? new Unit(StopLossPercent, UnitTypes.Percent) : new Unit(),
useMarketOrders: true);
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, ema);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal ema)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (candle.ClosePrice > ema && candle.OpenPrice < ema && Position <= 0)
BuyMarket(Volume + Math.Abs(Position));
else if (candle.ClosePrice < ema && candle.OpenPrice > ema && Position >= 0)
SellMarket(Volume + Math.Abs(Position));
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Decimal
from StockSharp.Messages import DataType, CandleStates, Unit, UnitTypes
from StockSharp.Algo.Indicators import ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class crypto_susdt_10_min_strategy(Strategy):
"""
Crypto SUSDT 10 min strategy.
A candle that opens below the EMA and closes above it buys, one that opens above the EMA and closes below it sells short,
reversing an opposite position. Every trade is closed by a TakeProfitPercent target or a StopLossPercent stop.
"""
def __init__(self):
super(crypto_susdt_10_min_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(10))).SetDisplay("Candle Type", "Type of candles to use", "General")
self._ema_length = self.Param("EmaLength", 24).SetGreaterThanZero().SetDisplay("EMA Length", "EMA length", "Indicators")
self._take_profit_percent = self.Param("TakeProfitPercent", 4.0).SetNotNegative().SetDisplay("Take Profit %", "Take profit percentage from entry price", "Risk")
self._stop_loss_percent = self.Param("StopLossPercent", 2.0).SetNotNegative().SetDisplay("Stop Loss %", "Stop loss percentage from entry price", "Risk")
self._order_percent = self.Param("OrderPercent", 30.0).SetGreaterThanZero().SetDisplay("Order %", "Percent of equity per order", "Risk")
@property
def candle_type(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(crypto_susdt_10_min_strategy, self).OnStarted2(time)
ema = ExponentialMovingAverage()
ema.Length = self._ema_length.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(ema, self._process_candle).Start()
take = float(self._take_profit_percent.Value)
stop = float(self._stop_loss_percent.Value)
self.StartProtection(
Unit(Decimal(take), UnitTypes.Percent) if take > 0 else Unit(),
Unit(Decimal(stop), UnitTypes.Percent) if stop > 0 else Unit(),
useMarketOrders=True)
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, ema)
self.DrawOwnTrades(area)
def _process_candle(self, candle, ema):
if candle.State != CandleStates.Finished:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
if candle.ClosePrice > ema and candle.OpenPrice < ema and self.Position <= 0:
self.BuyMarket(self.Volume + abs(self.Position))
elif candle.ClosePrice < ema and candle.OpenPrice > ema and self.Position >= 0:
self.SellMarket(self.Volume + abs(self.Position))
def CreateClone(self):
return crypto_susdt_10_min_strategy()