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>
/// Countertrend strategy based on the Open Source Forex oscillator.
/// </summary>
public class OsfCountertrendStrategy : Strategy
{
private readonly StrategyParam<int> _rsiPeriod;
private readonly StrategyParam<decimal> _volumePerPoint;
private readonly StrategyParam<decimal> _takeProfitPoints;
private readonly StrategyParam<int> _cooldownBars;
private readonly StrategyParam<DataType> _candleType;
private RelativeStrengthIndex _rsi;
private int _cooldown;
private decimal _longTarget;
private decimal _shortTarget;
/// <summary>
/// RSI period used to approximate the original OSF oscillator.
/// </summary>
public int RsiPeriod
{
get => _rsiPeriod.Value;
set => _rsiPeriod.Value = value;
}
/// <summary>
/// Volume traded per RSI point away from equilibrium (50 level).
/// </summary>
public decimal VolumePerPoint
{
get => _volumePerPoint.Value;
set => _volumePerPoint.Value = value;
}
/// <summary>
/// Take-profit distance expressed in instrument points.
/// </summary>
public decimal TakeProfitPoints
{
get => _takeProfitPoints.Value;
set => _takeProfitPoints.Value = value;
}
/// <summary>
/// Number of finished candles to wait before a new signal can trigger.
/// </summary>
public int CooldownBars
{
get => _cooldownBars.Value;
set => _cooldownBars.Value = value;
}
/// <summary>
/// Candle type used for calculations.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes a new instance of <see cref="OsfCountertrendStrategy"/>.
/// </summary>
public OsfCountertrendStrategy()
{
_rsiPeriod = Param(nameof(RsiPeriod), 14)
.SetRange(2, 200)
.SetDisplay("RSI Period", "RSI length used in oscillator", "General");
_volumePerPoint = Param(nameof(VolumePerPoint), 0.01m)
.SetRange(0.001m, 1m)
.SetDisplay("Volume per Point", "Order volume per RSI point from 50", "Risk");
_takeProfitPoints = Param(nameof(TakeProfitPoints), 150m)
.SetRange(0m, 1000m)
.SetDisplay("Take Profit", "Distance to take profit in points", "Risk");
_cooldownBars = Param(nameof(CooldownBars), 5)
.SetRange(0, 50)
.SetDisplay("Cooldown Bars", "Finished candles to wait after trading", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Data series for processing", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_rsi = null;
_cooldown = 0;
_longTarget = 0m;
_shortTarget = 0m;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_rsi = new RelativeStrengthIndex
{
Length = RsiPeriod
};
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(_rsi, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, _rsi);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal rsiValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!_rsi.IsFormed)
return;
// indicators already checked above
// Track active positions for manual take-profit handling.
if (Position > 0 && _longTarget > 0m && TakeProfitPoints > 0m)
{
if (candle.LowPrice <= _longTarget)
{
SellMarket();
_longTarget = 0m;
}
}
else if (Position < 0 && _shortTarget > 0m && TakeProfitPoints > 0m)
{
if (candle.HighPrice >= _shortTarget)
{
BuyMarket();
_shortTarget = 0m;
}
}
if (_cooldown > 0)
{
_cooldown--;
return;
}
var diff = rsiValue - 50m;
if (diff == 0m)
return;
var absDiff = Math.Abs(diff);
var volume = absDiff * VolumePerPoint;
if (volume <= 0m)
return;
var step = Security?.PriceStep ?? 1m;
if (diff > 0m && Position <= 0m)
{
// RSI above 50: countertrend short trade sized by oscillator distance.
var volumeToSell = volume + Math.Max(0m, Position);
if (volumeToSell <= 0m)
return;
SellMarket();
_shortTarget = TakeProfitPoints > 0m
? candle.ClosePrice - step * TakeProfitPoints
: 0m;
_longTarget = 0m;
_cooldown = CooldownBars;
}
else if (diff < 0m && Position >= 0m)
{
// RSI below 50: countertrend long trade sized by oscillator distance.
var volumeToBuy = volume + Math.Max(0m, -Position);
if (volumeToBuy <= 0m)
return;
BuyMarket();
_longTarget = TakeProfitPoints > 0m
? candle.ClosePrice + step * TakeProfitPoints
: 0m;
_shortTarget = 0m;
_cooldown = CooldownBars;
}
}
}
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
from StockSharp.Algo.Indicators import RelativeStrengthIndex
from StockSharp.Algo.Strategies import Strategy
class osf_countertrend_strategy(Strategy):
"""RSI countertrend strategy. Sells when RSI is above 50 (overbought), buys when
below 50 (oversold). Includes cooldown bars and virtual take-profit per direction."""
def __init__(self):
super(osf_countertrend_strategy, self).__init__()
self._rsi_period = self.Param("RsiPeriod", 14) \
.SetDisplay("RSI Period", "RSI length used in oscillator", "General")
self._volume_per_point = self.Param("VolumePerPoint", 0.01) \
.SetDisplay("Volume per Point", "Order volume per RSI point from 50", "Risk")
self._take_profit_points = self.Param("TakeProfitPoints", 150.0) \
.SetDisplay("Take Profit", "Distance to take profit in points", "Risk")
self._cooldown_bars = self.Param("CooldownBars", 5) \
.SetDisplay("Cooldown Bars", "Finished candles to wait after trading", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Data series for processing", "General")
self._rsi = None
self._cooldown = 0
self._long_target = 0.0
self._short_target = 0.0
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
@property
def RsiPeriod(self):
return self._rsi_period.Value
@property
def VolumePerPoint(self):
return self._volume_per_point.Value
@property
def TakeProfitPoints(self):
return self._take_profit_points.Value
@property
def CooldownBars(self):
return self._cooldown_bars.Value
def OnReseted(self):
super(osf_countertrend_strategy, self).OnReseted()
self._rsi = None
self._cooldown = 0
self._long_target = 0.0
self._short_target = 0.0
def OnStarted2(self, time):
super(osf_countertrend_strategy, self).OnStarted2(time)
self._rsi = RelativeStrengthIndex()
self._rsi.Length = self.RsiPeriod
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(self._rsi, self._process_candle).Start()
def _process_candle(self, candle, rsi_value):
if candle.State != CandleStates.Finished:
return
if self._rsi is None or not self._rsi.IsFormed:
return
step = 1.0
if self.Security is not None and self.Security.PriceStep is not None:
ps = float(self.Security.PriceStep)
if ps > 0:
step = ps
tp_points = float(self.TakeProfitPoints)
# Check active take-profit levels
if self.Position > 0 and self._long_target > 0 and tp_points > 0:
if float(candle.LowPrice) <= self._long_target:
self.SellMarket()
self._long_target = 0.0
elif self.Position < 0 and self._short_target > 0 and tp_points > 0:
if float(candle.HighPrice) >= self._short_target:
self.BuyMarket()
self._short_target = 0.0
if self._cooldown > 0:
self._cooldown -= 1
return
diff = float(rsi_value) - 50.0
if diff == 0:
return
abs_diff = abs(diff)
volume = abs_diff * float(self.VolumePerPoint)
if volume <= 0:
return
close_price = float(candle.ClosePrice)
if diff > 0 and self.Position <= 0:
# RSI above 50: countertrend short
self.SellMarket()
self._short_target = close_price - step * tp_points if tp_points > 0 else 0.0
self._long_target = 0.0
self._cooldown = self.CooldownBars
elif diff < 0 and self.Position >= 0:
# RSI below 50: countertrend long
self.BuyMarket()
self._long_target = close_price + step * tp_points if tp_points > 0 else 0.0
self._short_target = 0.0
self._cooldown = self.CooldownBars
def CreateClone(self):
return osf_countertrend_strategy()