The issues I'm seeing is that,
(1) I only see 2 contracts triggered for the trade and not seeing 3rd contract in the trade at all
(2) 2nd issue is that I do not see the negative condition triggered. It either exits with a stop loss or exits at 1st target.
Appreciate your feedback.
Quantity = 3;
StopLoss = 40;
ProfitTarget = 30;
ExitQuantity = 1;
ProfitTarget2 = 90;
protected override void OnBarUpdate()
{
if(CurrentBars[0] == 0 && bar0FT == null)
{
bar0FT = new FirstTick(CurrentBars[0]);
}
int idx = State == State.Historical || Calculate == Calculate.OnBarClose ? 0 : 1;
bar0FT.Update(CurrentBars[0]);
if(bar0FT.IsFirstTick &&
ema9[idx] > ema21[idx] && //<--logic to enter the trade
Position.MarketPosition == MarketPosition.Flat)
{
Entry1IsClosed = false;
SetStopLoss(CalculationMode.Ticks, StopLoss);
SetProfitTarget("EnterLong 1", CalculationMode.Ticks, ProfitTarget);
EnterLong(Quantity - ExitQuantity, "EnterLong 1");
if(ExitQuantity > 0)
{
EnterLong(ExitQuantity, "EnterLong 2");
SetProfitTarget("EnterLong 2", CalculationMode.Ticks, ProfitTarget2);
}
}
if(bar0FT.IsFirstTick &&
ema9[idx] < ema21[idx] && //<--logic to enter the trade
Position.MarketPosition == MarketPosition.Flat)
{
Entry1IsClosed = false;
SetStopLoss(CalculationMode.Ticks, StopLoss);
SetProfitTarget("EnterShort 1", CalculationMode.Ticks, ProfitTarget);
EnterShort(Quantity - ExitQuantity, "EnterShort 1");
if(ExitQuantity > 0)
{
EnterShort(ExitQuantity, "EnterShort 2");
}
SetProfitTarget("EnterShort 2", CalculationMode.Ticks, ProfitTarget2);
}
if(bar0FT.IsFirstTick )
{
if(Position.MarketPosition == MarketPosition.Long && ema9[idx] < ema21[idx])
{
if(!Entry1IsClosed) ExitLong("EnterLong 1");
ExitLong("EnterLong 2");
}
else if(Position.MarketPosition == MarketPosition.Short && ema9[idx] > ema21[idx] )
{
if(!Entry1IsClosed) ExitLong("EnterShort 1");
ExitShort("EnterShort 2");
}
}
}
Comment