using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Moving average crossover strategy with price crossing the SMA line.
/// Buys when candle opens below MA and closes above it, sells vice versa.
/// </summary>
public class Maybeawo222Strategy : Strategy
{
private readonly StrategyParam<int> _movingPeriod;
private readonly StrategyParam<DataType> _candleType;
private ExponentialMovingAverage _ema;
private decimal? _prevClose;
private decimal? _prevMa;
public int MovingPeriod
{
get => _movingPeriod.Value;
set => _movingPeriod.Value = value;
}
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public Maybeawo222Strategy()
{
_movingPeriod = Param(nameof(MovingPeriod), 20)
.SetDisplay("MA Period", "Simple moving average period", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
.SetDisplay("Candle Type", "Primary candle series", "General");
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_ema = new ExponentialMovingAverage { Length = MovingPeriod };
_prevClose = null;
_prevMa = null;
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(_ema, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, _ema);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal maValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!_ema.IsFormed)
{
_prevClose = candle.ClosePrice;
_prevMa = maValue;
return;
}
var close = candle.ClosePrice;
if (_prevClose is null || _prevMa is null)
{
_prevClose = close;
_prevMa = maValue;
return;
}
// Buy signal: candle crosses MA from below to above
var buySignal = _prevClose <= _prevMa && close > maValue;
// Sell signal: candle crosses MA from above to below
var sellSignal = _prevClose >= _prevMa && close < maValue;
if (buySignal && Position <= 0)
{
BuyMarket(Position < 0 ? Math.Abs(Position) + 1 : 1);
}
else if (sellSignal && Position >= 0)
{
SellMarket(Position > 0 ? Math.Abs(Position) + 1 : 1);
}
_prevClose = close;
_prevMa = maValue;
}
/// <inheritdoc />
protected override void OnReseted()
{
_ema = null;
_prevClose = null;
_prevMa = null;
base.OnReseted();
}
}
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 maybeawo222_strategy(Strategy):
"""
EMA crossover: buy when price crosses MA from below, sell from above.
"""
def __init__(self):
super(maybeawo222_strategy, self).__init__()
self._moving_period = self.Param("MovingPeriod", 20).SetDisplay("MA Period", "EMA period", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(30))).SetDisplay("Candle Type", "Candles", "General")
self._prev_close = None
self._prev_ma = None
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(maybeawo222_strategy, self).OnReseted()
self._prev_close = None
self._prev_ma = None
def OnStarted2(self, time):
super(maybeawo222_strategy, self).OnStarted2(time)
ema = ExponentialMovingAverage()
ema.Length = self._moving_period.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(ema, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, ema)
self.DrawOwnTrades(area)
def _process_candle(self, candle, ma_val):
if candle.State != CandleStates.Finished:
return
close = float(candle.ClosePrice)
ma = float(ma_val)
if self._prev_close is None or self._prev_ma is None:
self._prev_close = close
self._prev_ma = ma
return
buy_signal = self._prev_close <= self._prev_ma and close > ma
sell_signal = self._prev_close >= self._prev_ma and close < ma
if buy_signal and self.Position <= 0:
self.BuyMarket()
elif sell_signal and self.Position >= 0:
self.SellMarket()
self._prev_close = close
self._prev_ma = ma
def CreateClone(self):
return maybeawo222_strategy()