Esta página ainda não está disponível no seu idioma; mostrando outra versão.
Strategies
Overview
The Strategy class is the base class for creating trading strategies in StockSharp. It provides a complete set of tools for subscribing to market data, managing orders and positions, calculating statistics, and generating reports.
Key capabilities of the Strategy class:
- Subscribing to candles, order books, ticks, and other market data
- Placing, modifying, and canceling orders
- Target position management
- PnL, commission, and statistics calculation
- Risk management
- Timer and rule system
- Alerts
- Report generation
Warning
Child strategies functionality (ChildStrategies) has been declared obsolete and is no longer supported. The ChildStrategies property is marked with the [Obsolete("Child strategies no longer supported.")] attribute. If your code uses child strategies, it is recommended to refactor -- run each strategy as an independent instance.
Documentation Sections
- Target Position Management -- declarative position size management via
SetTargetPosition - Trading Modes -- restricting trading activity via
StrategyTradingModes - Alert System -- sending notifications (popup, sound, log, Telegram)
- Timer System -- periodic action execution
- Risk Management -- risk management rules
- High-Level Subscriptions -- simplified market data subscriptions
- Strategy Reports -- generating trade result reports
- Advanced Features -- order comments, schedules, risk-free rate, indicator source
Minimal Strategy
public class MyStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public MyStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame());
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle)
{
if (!IsFormedAndOnlineAndAllowTrading())
return;
// Trading logic
}
}
Strategy Lifecycle
- Creation -- constructor, declaring parameters via
Param<T>. - Configuration -- setting
Security,Portfolio,Connector, and parameters. - Start -- calling
Start(), transition toProcessStates.Startedstate, invocation ofOnStarted2(DateTime). - Running -- processing market data, placing orders.
- Stop -- calling
Stop(), transition throughProcessStates.StoppingtoProcessStates.Stopped, invocation ofOnStopped().