I have this issue where my strategy doubles down properly, but then only closes one of the positions. The strategy is for Forex and goes up to 2 min lots, (20000). That part works, but when it's tim to close the strategy the strategy only closes 1 lot (10000). Can someone please tell me what I'm doing wrong? Everything looks fine historically when the strategy is applied to a chart. Code is below:
namespace NinjaTrader.NinjaScript.Strategies
{
public class StochasticSlowDoubleDown : Strategy
{
private EMA EMA1;
private double currentPnL = 0;
private double unrealizedPL = 0;
private StochasticsFast StochasticsFast1;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "StochasticSlowDoubleDown";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
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)
{
}
else if (State == State.DataLoaded)
{
EMA1 = EMA(Close, 50);
EMA1.Plots[0].Brush = Brushes.Goldenrod;
AddChartIndicator(EMA1);
//SetStopLoss("", CalculationMode.Currency, 200, false);
StochasticsFast1 = StochasticsFast(Close, 3, 5);
AddChartIndicator(StochasticsFast1);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
// Set 1
if ((Close[0] > EMA1[0])
&& (CrossBelow(StochasticsFast1.D, 10, 1)))
{
EnterLong(Convert.ToInt32(10000), "");
}
///Gets Strategy Unrealized PL
currentPnL = SystemPerformance.AllTrades.TradesPerformance.NetP rofit;
// Set 2
if ((CrossBelow(StochasticsFast1.D, 90, 1) && currentPnL > 0))
{
ExitLong(Convert.ToInt32(10000), "", "");
}
//else if (CrossBelow(StochasticsFast1.D, 10, 1) && Close[0] > EMA1[0])
else if (CrossBelow(StochasticsFast1.D, 10, 1) && Position.MarketPosition != MarketPosition.Flat)
{
EnterLong(Convert.ToInt32(10000), "");
}
if (CrossBelow(StochasticsFast1.D, 90, 1))
{
ExitLong(Convert.ToInt32(20000), "", "");
}
}
}
}
Comment