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>
/// Arttrader v1.5 strategy (simplified).
/// Uses EMA slope on a higher timeframe to filter entries,
/// with candle pattern confirmation on the trading timeframe.
/// </summary>
public class ArttraderV15Strategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<DataType> _trendCandleType;
private readonly StrategyParam<int> _emaPeriod;
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public DataType TrendCandleType
{
get => _trendCandleType.Value;
set => _trendCandleType.Value = value;
}
public int EmaPeriod
{
get => _emaPeriod.Value;
set => _emaPeriod.Value = value;
}
public ArttraderV15Strategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Trading candles", "General");
_trendCandleType = Param(nameof(TrendCandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Trend Candle Type", "Trend candles for EMA", "General");
_emaPeriod = Param(nameof(EmaPeriod), 11)
.SetGreaterThanZero()
.SetDisplay("EMA Period", "EMA period on trend timeframe", "Indicators");
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var ema = new ExponentialMovingAverage { Length = EmaPeriod };
decimal currentEma = 0;
decimal previousEma = 0;
bool hasCurrentEma = false;
bool hasPreviousEma = false;
// Subscribe to trend timeframe for EMA slope
var trendSub = SubscribeCandles(TrendCandleType);
trendSub
.Bind(ema, (ICandleMessage candle, decimal emaVal) =>
{
if (candle.State != CandleStates.Finished)
return;
if (hasCurrentEma)
{
previousEma = currentEma;
hasPreviousEma = true;
}
currentEma = emaVal;
hasCurrentEma = true;
})
.Start();
// Subscribe to trading timeframe for signals
var tradeSub = SubscribeCandles(CandleType);
tradeSub
.Bind((ICandleMessage candle) =>
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (!hasPreviousEma)
return;
var emaSlope = currentEma - previousEma;
var close = candle.ClosePrice;
var open = candle.OpenPrice;
// Long: EMA slope positive, bearish candle (close < open), close near low
if (emaSlope > 0 && close <= open && Position <= 0)
BuyMarket();
// Short: EMA slope negative, bullish candle (close > open), close near high
else if (emaSlope < 0 && close >= open && Position >= 0)
SellMarket();
})
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, tradeSub);
DrawIndicator(area, ema);
DrawOwnTrades(area);
}
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class arttrader_v15_strategy(Strategy):
def __init__(self):
super(arttrader_v15_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle Type", "Trading candles", "General")
self._trend_candle_type = self.Param("TrendCandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Trend Candle Type", "Trend candles for EMA", "General")
self._ema_period = self.Param("EmaPeriod", 11) \
.SetDisplay("EMA Period", "EMA period on trend timeframe", "Indicators")
self._current_ema = 0.0
self._previous_ema = 0.0
self._has_current_ema = False
self._has_previous_ema = False
@property
def CandleType(self):
return self._candle_type.Value
@property
def TrendCandleType(self):
return self._trend_candle_type.Value
@property
def EmaPeriod(self):
return self._ema_period.Value
def OnReseted(self):
super(arttrader_v15_strategy, self).OnReseted()
self._current_ema = 0.0
self._previous_ema = 0.0
self._has_current_ema = False
self._has_previous_ema = False
def OnStarted2(self, time):
super(arttrader_v15_strategy, self).OnStarted2(time)
self._current_ema = 0.0
self._previous_ema = 0.0
self._has_current_ema = False
self._has_previous_ema = False
ema = ExponentialMovingAverage()
ema.Length = self.EmaPeriod
trend_sub = self.SubscribeCandles(self.TrendCandleType)
trend_sub.Bind(ema, self._on_trend_candle).Start()
trade_sub = self.SubscribeCandles(self.CandleType)
trade_sub.Bind(self._on_trade_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, trade_sub)
self.DrawIndicator(area, ema)
self.DrawOwnTrades(area)
def _on_trend_candle(self, candle, ema_value):
if candle.State != CandleStates.Finished:
return
ev = float(ema_value)
if self._has_current_ema:
self._previous_ema = self._current_ema
self._has_previous_ema = True
self._current_ema = ev
self._has_current_ema = True
def _on_trade_candle(self, candle):
if candle.State != CandleStates.Finished:
return
if not self._has_previous_ema:
return
ema_slope = self._current_ema - self._previous_ema
close = float(candle.ClosePrice)
open_p = float(candle.OpenPrice)
if ema_slope > 0 and close <= open_p and self.Position <= 0:
self.BuyMarket()
elif ema_slope < 0 and close >= open_p and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return arttrader_v15_strategy()