using System;
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SupertrendStrategy : Robot
{
private Supertrend supertrend10;
private Supertrend supertrend11;
private Supertrend supertrend12;
private ExponentialMovingAverage ema200;
private AverageTrueRange atr;
private DateTime lastTradeTime;
private BollingerBands bollingerBands;
private RelativeStrengthIndex rsi;
[Parameter("Supertrend Period (ATR 10)", DefaultValue = 10)]
public int SupertrendPeriod10 { get; set; }
[Parameter("Supertrend Multiplier (ATR 10)", DefaultValue = 1.0)]
public double SupertrendMultiplier10 { get; set; }
[Parameter("Supertrend Period (ATR 11)", DefaultValue = 11)]
public int SupertrendPeriod11 { get; set; }
[Parameter("Supertrend Multiplier (ATR 11)", DefaultValue = 2.0)]
public double SupertrendMultiplier11 { get; set; }
[Parameter("Supertrend Period (ATR 12)", DefaultValue = 12)]
public int SupertrendPeriod12 { get; set; }
[Parameter("Supertrend Multiplier (ATR 12)", DefaultValue = 3.0)]
public double SupertrendMultiplier12 { get; set; }
[Parameter("RSI Period", DefaultValue = 14)]
public int RSIPeriod { get; set; }
[Parameter("RSI Overbought Level", DefaultValue = 70)]
public int RSIOverboughtLevel { get; set; }
[Parameter("RSI Oversold Level", DefaultValue = 30)]
public int RSIOversoldLevel { get; set; }
[Parameter("EMA 200 Period", DefaultValue = 200)]
public int EMA200Period { get; set; }
[Parameter("ATR Period", DefaultValue = 14)]
public int ATRPeriod { get; set; }
[Parameter("Volume (Lots)", DefaultValue = 1.0)]
public double Volume { get; set; }
[Parameter("Trade Cooldown (Minutes)", DefaultValue = 360)]
public int TradeCooldownMinutes { get; set; }
[Parameter("Bollinger Bands Period", DefaultValue = 20)]
public int BollingerPeriod { get; set; }
[Parameter("Bollinger Bands Deviation", DefaultValue = 2.0)]
public double BollingerDeviation { get; set; }
protected override void OnStart()
{
supertrend10 = Indicators.Supertrend(SupertrendPeriod10, SupertrendMultiplier10);
supertrend11 = Indicators.Supertrend(SupertrendPeriod11, SupertrendMultiplier11);
supertrend12 = Indicators.Supertrend(SupertrendPeriod12, SupertrendMultiplier12);
bollingerBands = Indicators.BollingerBands(MarketSeries.Close, BollingerPeriod, BollingerDeviation, MovingAverageType.Simple);
ema200 = Indicators.ExponentialMovingAverage(MarketSeries.Close, EMA200Period);
atr = Indicators.AverageTrueRange(ATRPeriod, MovingAverageType.Simple);
lastTradeTime = MarketSeries.OpenTime.LastValue;
rsi = Indicators.RelativeStrengthIndex(MarketSeries.Close, RSIPeriod);
}
private bool ShouldTradeNow()
{
// Check if enough time has passed since the last trade
return (MarketSeries.OpenTime.LastValue - lastTradeTime).TotalMinutes >= TradeCooldownMinutes;
}
protected override void OnBar()
{
int lastIndex = MarketSeries.Close.Count - 1;
// Check conditions for Supertrend 10 (ATR 10, Multiplier 1)
bool isUpTrend10 = supertrend10.UpTrend.IsRising();
bool isDownTrend10 = supertrend10.DownTrend.IsFalling();
// Check conditions for Supertrend 11 (ATR 11, Multiplier 2)
bool isUpTrend11 = supertrend11.UpTrend.IsRising();
bool isDownTrend11 = supertrend11.DownTrend.IsFalling();
// Check conditions for Supertrend 12 (ATR 12, Multiplier 3)
bool isUpTrend12 = supertrend12.UpTrend.IsRising();
bool isDownTrend12 = supertrend12.DownTrend.IsFalling();
// Check if the last 10 bars were above SMA for buy trade
bool isAboveSMA10 = IsAboveSMA(MarketSeries.Close, lastIndex, 10);
// Check if the last 10 bars were below SMA for sell trade
bool isBelowSMA10 = IsBelowSMA(MarketSeries.Close, lastIndex, 10);
// Check Bollinger Bands conditions
bool isAboveUpperBand = MarketSeries.Close[lastIndex] > bollingerBands.Top[lastIndex];
bool isBelowLowerBand = MarketSeries.Close[lastIndex] < bollingerBands.Bottom[lastIndex];
double targets = 100;
// Close buy positions if at least one Supertrend turns red or if the last 10 bars were below SMA
if (Positions.Count > 0 && (isDownTrend10 || isDownTrend11 || isDownTrend12 || isBelowSMA10))
{
foreach (var position in Positions)
{
if (position.TradeType == TradeType.Buy)
ClosePosition(position);
}
}
// Close sell positions if at least one Supertrend turns green or if the last 10 bars were above SMA
if (Positions.Count > 0 && (isUpTrend10 || isUpTrend11 || isUpTrend12 || isAboveSMA10))
{
foreach (var position in Positions)
{
if (position.TradeType == TradeType.Sell)
ClosePosition(position);
}
}
// Open new positions for buy trade if all Supertrends are rising, and the last 10 bars were above SMA
if (Positions.Count == 0 &&
isUpTrend10 && isUpTrend11 && isUpTrend12 &&
isAboveSMA10 &&
isAboveUpperBand
// && rsi.Result[lastIndex] < 0
)
{
// Buy signal (All Supertrends are rising, and the last 10 bars were above SMA)
ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.NormalizeVolumeInUnits(Symbol.LotSize), "Buy Order", targets, targets);
}
// Open new positions for sell trade if all Supertrends are falling, and the last 10 bars were below SMA
else if (Positions.Count == 0 &&
isDownTrend10 && isDownTrend11 && isDownTrend12 &&
isBelowSMA10 &&
isBelowLowerBand
//&& rsi.Result[lastIndex] > 0
)
{
// Sell signal (All Supertrends are falling, and the last 10 bars were below SMA)
ExecuteMarketOrder(TradeType.Sell, SymbolName, Symbol.NormalizeVolumeInUnits(Symbol.LotSize), "Sell Order", targets, targets);
}
}
private bool IsAboveSMA(DataSeries series, int index, int period)
{
for (int i = 0; i < period; i++)
{
if (series[index - i] <= ema200.Result[index - i])
{
return false;
}
}
return true;
}
private bool IsBelowSMA(DataSeries series, int index, int period)
{
for (int i = 0; i < period; i++)
{
if (series[index - i] >= ema200.Result[index - i])
{
return false;
}
}
return true;
}
}
}