LANZ Strategy 6.0 [Backtest]
Implements LANZ Strategy 6.0: trades the 09:00 New York one-hour candle with configurable risk parameters. Positions are closed at 15:00 New York time.
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;
using StockSharp.Algo;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// LANZ Strategy 6.0: trades the 09:00 New York hour candle with risk management.
/// </summary>
public class Lanz60BacktestStrategy : Strategy
{
private readonly StrategyParam<decimal> _slPercent;
private readonly StrategyParam<decimal> _riskReward;
private readonly StrategyParam<decimal> _accountSizeUsd;
private readonly StrategyParam<decimal> _riskPercent;
private readonly StrategyParam<bool> _useManualPipValue;
private readonly StrategyParam<decimal> _manualPipValue;
private readonly StrategyParam<bool> _enableBuy;
private readonly StrategyParam<bool> _enableSell;
private readonly StrategyParam<DataType> _candleType;
private readonly TimeZoneInfo _nyZone = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
/// <summary>
/// Stop-loss as percent of candle range.
/// </summary>
public decimal SlPercent
{
get => _slPercent.Value;
set => _slPercent.Value = value;
}
/// <summary>
/// Risk-reward ratio.
/// </summary>
public decimal RiskReward
{
get => _riskReward.Value;
set => _riskReward.Value = value;
}
/// <summary>
/// Account capital in USD.
/// </summary>
public decimal AccountSizeUsd
{
get => _accountSizeUsd.Value;
set => _accountSizeUsd.Value = value;
}
/// <summary>
/// Risk percent per trade.
/// </summary>
public decimal RiskPercent
{
get => _riskPercent.Value;
set => _riskPercent.Value = value;
}
/// <summary>
/// Use manual pip value.
/// </summary>
public bool UseManualPipValue
{
get => _useManualPipValue.Value;
set => _useManualPipValue.Value = value;
}
/// <summary>
/// Manual pip value.
/// </summary>
public decimal ManualPipValue
{
get => _manualPipValue.Value;
set => _manualPipValue.Value = value;
}
/// <summary>
/// Enable buy entries.
/// </summary>
public bool EnableBuy
{
get => _enableBuy.Value;
set => _enableBuy.Value = value;
}
/// <summary>
/// Enable sell entries.
/// </summary>
public bool EnableSell
{
get => _enableSell.Value;
set => _enableSell.Value = value;
}
/// <summary>
/// Candle type used for processing.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes parameters.
/// </summary>
public Lanz60BacktestStrategy()
{
_slPercent = Param(nameof(SlPercent), 0.05m)
.SetDisplay("SL %", "Stop loss percent of candle range", "Risk")
;
_riskReward = Param(nameof(RiskReward), 5m)
.SetDisplay("Risk Reward", "Risk reward ratio", "Risk")
;
_accountSizeUsd = Param(nameof(AccountSizeUsd), 10000m)
.SetDisplay("Account Size USD", "Account capital", "Money");
_riskPercent = Param(nameof(RiskPercent), 1m)
.SetDisplay("Risk %", "Risk percent per trade", "Money")
;
_useManualPipValue = Param(nameof(UseManualPipValue), true)
.SetDisplay("Manual Pip Value", "Use manual pip value", "Money");
_manualPipValue = Param(nameof(ManualPipValue), 1m)
.SetDisplay("Pip Value", "Manual pip value", "Money");
_enableBuy = Param(nameof(EnableBuy), true)
.SetDisplay("Enable Buy", "Allow long entries", "General");
_enableSell = Param(nameof(EnableSell), false)
.SetDisplay("Enable Sell", "Allow short entries", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
var nyTime = TimeZoneInfo.ConvertTime(candle.OpenTime, _nyZone);
if (nyTime.Hour == 15 && nyTime.Minute == 0)
{
if (Position > 0)
SellMarket(Position);
else if (Position < 0)
BuyMarket(-Position);
return;
}
var isBull = nyTime.Hour == 9 && candle.ClosePrice > candle.OpenPrice;
var isBear = nyTime.Hour == 9 && candle.ClosePrice < candle.OpenPrice;
if ((!isBull || !EnableBuy) && (!isBear || !EnableSell))
return;
var entryPrice = candle.ClosePrice;
var hi = candle.HighPrice;
var lo = candle.LowPrice;
var candleRange = hi - lo;
var slPrice = SlPercent == 0m
? (isBull ? lo : hi)
: (isBull ? lo - candleRange * SlPercent : hi + candleRange * SlPercent);
var risk = Math.Abs(entryPrice - slPrice);
var tpPrice = isBull ? entryPrice + risk * RiskReward : entryPrice - risk * RiskReward;
var step = Security.PriceStep ?? 1m;
var pipSize = step * 10m;
var slPips = pipSize > 0m ? risk / pipSize : 0m;
var pipValue = UseManualPipValue || Security.Board?.Code != "FX"
? ManualPipValue
: (Security.Code.EndsWith("USD", StringComparison.OrdinalIgnoreCase)
? 10m
: Security.Code.StartsWith("USD", StringComparison.OrdinalIgnoreCase)
? 100000m * pipSize / candle.ClosePrice
: ManualPipValue);
var riskUsd = AccountSizeUsd * (RiskPercent / 100m);
var qty = slPips > 0m && pipValue > 0m ? riskUsd / (slPips * pipValue) : 0m;
var volume = qty + Math.Abs(Position);
if (isBull && EnableBuy && Position <= 0)
{
BuyMarket(volume);
}
else if (isBear && EnableSell && Position >= 0)
{
SellMarket(volume);
}
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, TimeZoneInfo
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Strategies import Strategy
class lanz_6_0_backtest_strategy(Strategy):
def __init__(self):
super(lanz_6_0_backtest_strategy, self).__init__()
self._enable_buy = self.Param("EnableBuy", True) \
.SetDisplay("Enable Buy", "Allow long entries", "General")
self._enable_sell = self.Param("EnableSell", False) \
.SetDisplay("Enable Sell", "Allow short entries", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle Type", "Candle timeframe", "General")
self._ny_zone = TimeZoneInfo.FindSystemTimeZoneById("America/New_York")
self._bar_count = 0
@property
def candle_type(self):
return self._candle_type.Value
@candle_type.setter
def candle_type(self, value):
self._candle_type.Value = value
def OnReseted(self):
super(lanz_6_0_backtest_strategy, self).OnReseted()
self._bar_count = 0
def OnStarted2(self, time):
super(lanz_6_0_backtest_strategy, self).OnStarted2(time)
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self.OnProcess).Start()
def OnProcess(self, candle):
if candle.State != CandleStates.Finished:
return
self._bar_count += 1
close = float(candle.ClosePrice)
open_p = float(candle.OpenPrice)
ny_time = TimeZoneInfo.ConvertTime(candle.OpenTime, self._ny_zone)
hour = ny_time.Hour
if hour == 15 and self.Position != 0:
if self.Position > 0:
self.SellMarket()
elif self.Position < 0:
self.BuyMarket()
return
is_bull = hour == 9 and close > open_p
is_bear = hour == 9 and close < open_p
if is_bull and self._enable_buy.Value and self.Position <= 0:
self.BuyMarket()
elif is_bear and self._enable_sell.Value and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return lanz_6_0_backtest_strategy()