namespace StockSharp.Samples.Strategies;
using System;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;
/// <summary>
/// Strategy that submits a sequence of market buy orders on each candle close.
/// After a configurable number of orders have been placed, it stops.
/// </summary>
public class TimedBuyOrderStrategy : Strategy
{
private readonly StrategyParam<int> _ordersToPlace;
private readonly StrategyParam<DataType> _candleType;
private int _ordersPlaced;
/// <summary>
/// Initializes a new instance of the <see cref="TimedBuyOrderStrategy"/> class.
/// </summary>
public TimedBuyOrderStrategy()
{
_ordersToPlace = Param(nameof(OrdersToPlace), 60)
.SetGreaterThanZero()
.SetDisplay("Orders To Place", "Number of sequential buy orders before stopping", "Trading");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle type", "Candle type for strategy calculation.", "General");
}
/// <summary>
/// Total number of buy orders to submit before the strategy stops.
/// </summary>
public int OrdersToPlace
{
get => _ordersToPlace.Value;
set => _ordersToPlace.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_ordersPlaced = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_ordersPlaced = 0;
var sma = new SMA { Length = 5 };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(sma, OnProcess)
.Start();
}
private void OnProcess(ICandleMessage candle, decimal smaValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (_ordersPlaced >= OrdersToPlace)
return;
BuyMarket();
_ordersPlaced++;
}
}
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 SimpleMovingAverage
from StockSharp.Algo.Strategies import Strategy
class timed_buy_order_strategy(Strategy):
def __init__(self):
super(timed_buy_order_strategy, self).__init__()
self._orders_to_place = self.Param("OrdersToPlace", 60) \
.SetDisplay("Orders To Place", "Number of sequential buy orders before stopping", "Trading")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle type", "Candle type for strategy calculation.", "General")
self._orders_placed = 0
@property
def OrdersToPlace(self):
return self._orders_to_place.Value
@property
def CandleType(self):
return self._candle_type.Value
def OnReseted(self):
super(timed_buy_order_strategy, self).OnReseted()
self._orders_placed = 0
def OnStarted2(self, time):
super(timed_buy_order_strategy, self).OnStarted2(time)
self._orders_placed = 0
sma = SimpleMovingAverage()
sma.Length = 5
subscription = self.SubscribeCandles(self.CandleType)
subscription \
.Bind(sma, self._on_process) \
.Start()
def _on_process(self, candle, sma_value):
if candle.State != CandleStates.Finished:
return
if self._orders_placed >= self.OrdersToPlace:
return
self.BuyMarket()
self._orders_placed += 1
def CreateClone(self):
return timed_buy_order_strategy()