I am a complete beginner and to learn, I am trying to develop the stupidest strategy possible: When a new candle appears, we take a sale if the previous one is bullish and a purchase if the previous one is bearish.
When I test with the strategy analyzer, everything works.
When I put it live on my demo account, a first trade is taken. It is not closed on the new candle but later, whatever the time frame used and it does not resume a trade before many candles.
Could you tell me what is wrong with my code because I think the logic is respected.
But it may also be the parameters more than the logic.
Thank you all.
namespace NinjaTrader.NinjaScript.Strategies
{
public class SimpleCandleStrategy : Strategy
{
private double lastBarOpen = 0;
private double lastBarClose = 0;
private int lastBarIndex = -1;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"SimpleCandleStrategy SimpleCandleStrategy SimpleCandleStrategy ";
Name = "SimpleCandleStrategy";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 4;
StartBehavior = StartBehavior.ImmediatelySubmit;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
}
else if (State == State.Configure)
{
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBar < 2) return;
if (CurrentBar != lastBarIndex)
{
lastBarIndex = CurrentBar;
double previousOpen = lastBarOpen;
double previousClose = lastBarClose;
lastBarOpen = Open[0];
lastBarClose = Close[1];
if (Position.MarketPosition == MarketPosition.Flat)
{
if (previousClose > previousOpen)
{
EnterShort();
}
else if (previousClose < previousOpen)
{
EnterLong();
}
}
else if (Position.MarketPosition == MarketPosition.Long)
{
if (previousClose > previousOpen)
{
ExitLong();
EnterShort();
}
}
else if (Position.MarketPosition == MarketPosition.Short)
{
if (previousClose < previousOpen)
{
ExitShort();
EnterLong();
}
}
}
}
}
}

Comment