The strategy appears to be syncing correctly, but it’s not actually pushing orders to be executed. Am I missing something?
#region Using declarations
using System;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.NinjaScript.Indicators;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class SuperTrendStrategy : Strategy
{
// Input parameters
private double AtrMult = 1.0;
private int ATR_Periods = 4;
private int posSize = 1;
// Variables
private double ATR;
private double UP;
private double DN;
private double ST;
// SuperTrend Series for storing values
private Series<double> SuperTrendSeries;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"SuperTrend Strategy";
Name = "SuperTrendStrategy";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
}
else if (State == State.DataLoaded)
{
SuperTrendSeries = new Series<double>(this);
}
}
protected override void OnBarUpdate()
{
// Calculate the ATR
ATR = ATR(ATR_Periods)[0];
// Calculate the SuperTrend UP and DN levels
UP = (High[0] + Low[0]) / 2 + (AtrMult * ATR);
DN = (High[0] + Low[0]) / 2 - (AtrMult * ATR);
// Calculate the SuperTrend value
if (CurrentBar == 0)
{
ST = UP; // Default to UP for the first bar
}
else
{
if (Close[0] < ST)
ST = UP;
else
ST = DN;
}
// Assign the SuperTrend value to the series
SuperTrendSeries[0] = ST;
// Define the conditions for SuperTrend crossing below (buy condition)
bool SuperTrendUp = CrossBelow(ST, Close, 1);
bool SuperTrendDn = CrossAbove(ST, Close, 1);
// Execute orders based on the conditions
if (SuperTrendUp)
EnterLong(Convert.ToInt32(posSize), @"Long Entry");
if (SuperTrendDn)
EnterShort(Convert.ToInt32(posSize), @"SELL");
}
// The SuperTrend plot (used for visualization)
public Series<double> SuperTrend
{
get { return SuperTrendSeries; }
}
}
}