using System;
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>
/// Simplified conversion of the FxNode Safe Tunnel EA.
/// Uses Highest/Lowest channel (tunnel) with ATR-based stops.
/// Buys near the lower boundary and sells near the upper boundary.
/// </summary>
public class FxNodeSafeTunnelStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _channelPeriod;
private readonly StrategyParam<int> _atrPeriod;
private readonly StrategyParam<decimal> _touchPct;
private decimal _entryPrice;
private int _cooldown;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int ChannelPeriod { get => _channelPeriod.Value; set => _channelPeriod.Value = value; }
public int AtrPeriod { get => _atrPeriod.Value; set => _atrPeriod.Value = value; }
public decimal TouchPct { get => _touchPct.Value; set => _touchPct.Value = value; }
public FxNodeSafeTunnelStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Timeframe", "General");
_channelPeriod = Param(nameof(ChannelPeriod), 100)
.SetGreaterThanZero()
.SetDisplay("Channel Period", "Lookback for Highest/Lowest channel", "Indicator");
_atrPeriod = Param(nameof(AtrPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("ATR Period", "ATR lookback for stops", "Indicator");
_touchPct = Param(nameof(TouchPct), 0.02m)
.SetDisplay("Touch %", "How close price must be to channel boundary (0-1)", "Indicator");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
yield return (Security, CandleType);
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_entryPrice = 0;
_cooldown = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_entryPrice = 0;
var highest = new Highest { Length = ChannelPeriod };
var lowest = new Lowest { Length = ChannelPeriod };
var atr = new AverageTrueRange { Length = AtrPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(highest, lowest, atr, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, highest);
DrawIndicator(area, lowest);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal high, decimal low, decimal atrVal)
{
if (candle.State != CandleStates.Finished)
return;
if (_cooldown > 0)
{
_cooldown--;
return;
}
var channelWidth = high - low;
if (channelWidth <= 0)
return;
var touchZone = channelWidth * TouchPct;
var close = candle.ClosePrice;
// Check stop/take for active positions
if (Position > 0)
{
// Exit long: price near upper channel or stop loss
if (close >= high - touchZone || (_entryPrice > 0 && close < _entryPrice - atrVal * 2))
{
SellMarket();
_entryPrice = 0;
_cooldown = 10;
return;
}
}
else if (Position < 0)
{
// Exit short: price near lower channel or stop loss
if (close <= low + touchZone || (_entryPrice > 0 && close > _entryPrice + atrVal * 2))
{
BuyMarket();
_entryPrice = 0;
_cooldown = 10;
return;
}
}
// Entry signals
if (Position <= 0 && close <= low + touchZone)
{
// Price near lower boundary - buy
if (Position < 0) BuyMarket(); // close short first
BuyMarket();
_entryPrice = close;
_cooldown = 10;
}
else if (Position >= 0 && close >= high - touchZone)
{
// Price near upper boundary - sell
if (Position > 0) SellMarket(); // close long first
SellMarket();
_entryPrice = close;
_cooldown = 10;
}
}
}
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, AverageTrueRange
from StockSharp.Algo.Strategies import Strategy
class fx_node_safe_tunnel_strategy(Strategy):
def __init__(self):
super(fx_node_safe_tunnel_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Timeframe", "General")
self._channel_period = self.Param("ChannelPeriod", 100) \
.SetGreaterThanZero() \
.SetDisplay("Channel Period", "Lookback for Highest/Lowest channel", "Indicator")
self._atr_period = self.Param("AtrPeriod", 14) \
.SetGreaterThanZero() \
.SetDisplay("ATR Period", "ATR lookback for stops", "Indicator")
self._touch_pct = self.Param("TouchPct", 0.02) \
.SetDisplay("Touch %", "How close price must be to channel boundary (0-1)", "Indicator")
self._entry_price = 0.0
self._cooldown = 0
@property
def candle_type(self):
return self._candle_type.Value
@property
def channel_period(self):
return self._channel_period.Value
@property
def atr_period(self):
return self._atr_period.Value
@property
def touch_pct(self):
return self._touch_pct.Value
def OnReseted(self):
super(fx_node_safe_tunnel_strategy, self).OnReseted()
self._entry_price = 0.0
self._cooldown = 0
def OnStarted2(self, time):
super(fx_node_safe_tunnel_strategy, self).OnStarted2(time)
self._entry_price = 0.0
highest = Highest()
highest.Length = self.channel_period
lowest = Lowest()
lowest.Length = self.channel_period
atr = AverageTrueRange()
atr.Length = self.atr_period
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(highest, lowest, atr, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, highest)
self.DrawIndicator(area, lowest)
self.DrawOwnTrades(area)
def process_candle(self, candle, high, low, atr_val):
if candle.State != CandleStates.Finished:
return
if self._cooldown > 0:
self._cooldown -= 1
return
high = float(high)
low = float(low)
atr_val = float(atr_val)
channel_width = high - low
if channel_width <= 0:
return
touch_zone = channel_width * float(self.touch_pct)
close = float(candle.ClosePrice)
if self.Position > 0:
if close >= high - touch_zone or (self._entry_price > 0 and close < self._entry_price - atr_val * 2):
self.SellMarket()
self._entry_price = 0.0
self._cooldown = 10
return
elif self.Position < 0:
if close <= low + touch_zone or (self._entry_price > 0 and close > self._entry_price + atr_val * 2):
self.BuyMarket()
self._entry_price = 0.0
self._cooldown = 10
return
if self.Position <= 0 and close <= low + touch_zone:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
self._entry_price = close
self._cooldown = 10
elif self.Position >= 0 and close >= high - touch_zone:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._entry_price = close
self._cooldown = 10
def CreateClone(self):
return fx_node_safe_tunnel_strategy()