using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Logs the current position on every candle. Simplified from the original multi-position listing.
/// When position changes direction based on candle close, trades accordingly.
/// </summary>
public class ListPositionsStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _logInterval;
private int _candleCount;
private decimal? _prevClose;
/// <summary>
/// Candle type for monitoring.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Number of candles between position log entries.
/// </summary>
public int LogInterval
{
get => _logInterval.Value;
set => _logInterval.Value = value;
}
/// <summary>
/// Initializes strategy parameters.
/// </summary>
public ListPositionsStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe for monitoring", "General");
_logInterval = Param(nameof(LogInterval), 10)
.SetGreaterThanZero()
.SetDisplay("Log Interval", "Log position every N candles", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
yield return (Security, CandleType);
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_candleCount = 0;
_prevClose = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
SubscribeCandles(CandleType)
.Bind(ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormed)
return;
_candleCount++;
if (_prevClose != null)
{
if (candle.ClosePrice > _prevClose.Value && Position <= 0)
BuyMarket();
else if (candle.ClosePrice < _prevClose.Value && Position >= 0)
SellMarket();
}
if (_candleCount % LogInterval == 0)
{
LogInfo($"Position: {Position}, Price: {candle.ClosePrice:0.#####}, Equity: {Portfolio?.CurrentValue:0.##}");
}
_prevClose = candle.ClosePrice;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Math
from StockSharp.Messages import DataType, CandleStates, Unit, UnitTypes
from StockSharp.Algo.Strategies import Strategy
class list_positions_strategy(Strategy):
def __init__(self):
super(list_positions_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4)))
self._log_interval = self.Param("LogInterval", 10)
self._candle_count = 0
self._prev_close = None
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
@property
def LogInterval(self):
return self._log_interval.Value
@LogInterval.setter
def LogInterval(self, value):
self._log_interval.Value = value
def OnStarted2(self, time):
super(list_positions_strategy, self).OnStarted2(time)
self._candle_count = 0
self._prev_close = None
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(self.ProcessCandle).Start()
def ProcessCandle(self, candle):
if candle.State != CandleStates.Finished:
return
if not self.IsFormed:
return
close = float(candle.ClosePrice)
self._candle_count += 1
if self._prev_close is not None:
if close > self._prev_close and self.Position <= 0:
self.BuyMarket()
elif close < self._prev_close and self.Position >= 0:
self.SellMarket()
self._prev_close = close
def OnReseted(self):
super(list_positions_strategy, self).OnReseted()
self._candle_count = 0
self._prev_close = None
def CreateClone(self):
return list_positions_strategy()