protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = "Adaptive trading strategy using AMA, ATR, and RSI.";
Name = "AdaptiveMarketStrategy";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
IsStopSupported = true;
IsExitOnSessionCloseStrategy = false;
}
else if (State == State.Configure)
{
AddDataSeries(Data.BarsPeriodType.Minute, 1); // Using 1-minute bars
AddAMA(10, 30); // Fast AMA
AddAMA(30, 60); // Slow AMA
AddATR(14);
AddRSI(14, 3);
}
}
private AMA fastAMA;
private AMA slowAMA;
private ATR atr;
private RSI rsi;
protected override void OnBarUpdate()
{
if (CurrentBars[0] < 60) return; // Wait for enough bars
double fastAmaValue = fastAMA[0];
double slowAmaValue = slowAMA[0];
double atrValue = atr[0];
double rsiValue = rsi[0];
// Bullish trade entry
if (CrossAbove(fastAMA, slowAMA, 1) && rsiValue > 60)
{
EnterLong("LongEntry");
SetStopLoss("LongEntry", CalculationMode.Price, Close[0] - 1.5 * atrValue, false);
SetProfitTarget("LongEntry", CalculationMode.Price, Close[0] + 3 * atrValue);
}
// Bearish trade entry
if (CrossBelow(fastAMA, slowAMA, 1) && rsiValue < 40)
{
EnterShort("ShortEntry");
SetStopLoss("ShortEntry", CalculationMode.Price, Close[0] + 1.5 * atrValue, false);
SetProfitTarget("ShortEntry", CalculationMode.Price, Close[0] - 3 * atrValue);
}
// Trailing stop logic for long positions
if (Position.MarketPosition == MarketPosition.Long)
{
double highestHigh = MAX(High, 20)[0]; // Adjust period as needed
SetStopLoss("LongEntry", CalculationMode.Price, highestHigh - atrValue, true);
}
// Trailing stop logic for short positions
if (Position.MarketPosition == MarketPosition.Short)
{
double lowestLow = MIN(Low, 20)[0]; // Adjust period as needed
SetStopLoss("ShortEntry", CalculationMode.Price, lowestLow + atrValue, true);
}
}

Comment