Simplistic Automatic Growth Models Strategy
The strategy forms cumulative average high and low bands and trades when price breaks those levels.
Details
- Entry Criteria:
- Close price above the upper band opens a long.
- Close price below the lower band opens a short.
- Long/Short: Both.
- Exit Criteria:
- Opposite signal closes the position.
- Stops: None.
- Default Values:
Length= 10
- Filters:
- Category: Trend
- Direction: Both
- Indicators: Highest, Lowest
- Stops: No
- Complexity: Low
- Timeframe: Any
- Seasonality: No
- Neural networks: No
- Divergence: No
- Risk level: Medium
using System;
using System.Linq;
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>
/// Simplistic Automatic Growth Models Strategy - trades when price crosses averaged growth bands.
/// </summary>
public class SimplisticAutomaticGrowthModelsStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _length;
private decimal _cumHigh;
private decimal _cumLow;
private int _count;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int Length { get => _length.Value; set => _length.Value = value; }
public SimplisticAutomaticGrowthModelsStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Type of candles for processing", "General");
_length = Param(nameof(Length), 10)
.SetGreaterThanZero()
.SetDisplay("Length", "Lookback length for bands", "Indicators");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_cumHigh = 0;
_cumLow = 0;
_count = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var highest = new Highest { Length = Length };
var lowest = new Lowest { Length = Length };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(highest, lowest, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal hi, decimal lo)
{
if (candle.State != CandleStates.Finished)
return;
_cumHigh += hi;
_cumLow += lo;
_count++;
var avgHi = _cumHigh / _count;
var avgLo = _cumLow / _count;
if (candle.ClosePrice > avgHi && Position <= 0)
BuyMarket();
else if (candle.ClosePrice < avgLo && Position >= 0)
SellMarket();
}
}
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 Highest, Lowest
from StockSharp.Algo.Strategies import Strategy
class simplistic_automatic_growth_models_strategy(Strategy):
def __init__(self):
super(simplistic_automatic_growth_models_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle Type", "Type of candles for processing", "General")
self._length = self.Param("Length", 10) \
.SetDisplay("Length", "Lookback length for bands", "Indicators")
self._cum_high = 0.0
self._cum_low = 0.0
self._count = 0
@property
def candle_type(self):
return self._candle_type.Value
@property
def length(self):
return self._length.Value
def OnReseted(self):
super(simplistic_automatic_growth_models_strategy, self).OnReseted()
self._cum_high = 0.0
self._cum_low = 0.0
self._count = 0
def OnStarted2(self, time):
super(simplistic_automatic_growth_models_strategy, self).OnStarted2(time)
highest = Highest()
highest.Length = self.length
lowest = Lowest()
lowest.Length = self.length
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(highest, lowest, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def on_process(self, candle, hi, lo):
if candle.State != CandleStates.Finished:
return
self._cum_high += hi
self._cum_low += lo
self._count += 1
avg_hi = self._cum_high / self._count
avg_lo = self._cum_low / self._count
if candle.ClosePrice > avg_hi and self.Position <= 0:
self.BuyMarket()
elif candle.ClosePrice < avg_lo and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return simplistic_automatic_growth_models_strategy()