Swing Cyborg is a discretionary helper that automates execution based on a trader's own trend forecast. The user defines the expected trend direction and the time window when it should be valid. The strategy then confirms entries with the RSI indicator and manages exits with fixed targets.
Parameters
Volume – order volume in lots.
TrendPrediction – expected trend direction (Uptrend or Downtrend).
TrendTimeframe – timeframe used for RSI and trading (M30, H1 or H4).
TrendStart – beginning of the user defined trend period.
TrendEnd – end of the user defined trend period.
Aggressiveness – money management preset:
Low: take profit 300 pips, stop loss 200 pips.
Medium: take profit 500 pips, stop loss 250 pips.
High: take profit 600 pips, stop loss 300 pips.
Trading Logic
Wait for a new candle on the selected timeframe.
Trade only if current time is between TrendStart and TrendEnd.
Calculate RSI(14).
If there is no open position:
If TrendPrediction is Uptrend and RSI ≤ 65 → buy.
If TrendPrediction is Downtrend and RSI ≥ 35 → sell.
StartProtection automatically closes the position when profit or loss reaches the predefined level.
The strategy operates on finished candles and does not open a new position while an existing one is active.
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>
/// SwingCyborg strategy using RSI overbought/oversold levels.
/// </summary>
public class SwingCyborgStrategy : Strategy
{
private readonly StrategyParam<int> _rsiPeriod;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevRsi;
private bool _hasPrev;
public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public SwingCyborgStrategy()
{
_rsiPeriod = Param(nameof(RsiPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("RSI Period", "RSI period", "Parameters");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Candle type", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_prevRsi = 0;
_hasPrev = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
SubscribeCandles(CandleType)
.Bind(rsi, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal rsiVal)
{
if (candle.State != CandleStates.Finished) return;
if (!_hasPrev)
{
_prevRsi = rsiVal;
_hasPrev = true;
return;
}
// Buy when RSI exits oversold
if (_prevRsi <= 30m && rsiVal > 30m && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
// Sell when RSI exits overbought
else if (_prevRsi >= 70m && rsiVal < 70m && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
_prevRsi = rsiVal;
}
}
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 RelativeStrengthIndex
from StockSharp.Algo.Strategies import Strategy
class swing_cyborg_strategy(Strategy):
def __init__(self):
super(swing_cyborg_strategy, self).__init__()
self._rsi_period = self.Param("RsiPeriod", 14) \
.SetDisplay("RSI Period", "RSI period", "Parameters")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Candle type", "General")
self._prev_rsi = 0.0
self._has_prev = False
@property
def rsi_period(self):
return self._rsi_period.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(swing_cyborg_strategy, self).OnReseted()
self._prev_rsi = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(swing_cyborg_strategy, self).OnStarted2(time)
rsi = RelativeStrengthIndex()
rsi.Length = self.rsi_period
self.SubscribeCandles(self.candle_type) \
.Bind(rsi, self.process_candle) \
.Start()
def process_candle(self, candle, rsi_val):
if candle.State != CandleStates.Finished:
return
rsi_val = float(rsi_val)
if not self._has_prev:
self._prev_rsi = rsi_val
self._has_prev = True
return
if self._prev_rsi <= 30.0 and rsi_val > 30.0 and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif self._prev_rsi >= 70.0 and rsi_val < 70.0 and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_rsi = rsi_val
def CreateClone(self):
return swing_cyborg_strategy()