But for some reason the SetProfitTarget() Rule will execute before the BarsSinceExecution(), while the SetStopLoss Rule works properly. Even if I remove the SetStopLoss Rule, the SetProfitTarget still doesn't work.
//**** at bottom, everything else is for reference if it impacts the bottom problem
protected override void OnBarUpdate() { if (Position.MarketPosition == MarketPosition.Flat) { // Set stop loss to 6 ticks in case of reversals SetStopLoss("ShortEntry", CalculationMode.Ticks, 6, false); } // Declares position Short else if (Position.MarketPosition == MarketPosition.Short) { // if price is more than entry by 5 ticks, raise Stop Loss to 1 tick if (Position.AveragePrice >= Close[0] + (5 * TickSize)) { SetStopLoss("ShortEntry", CalculationMode.Ticks, -1, false); } // if price is over entry by 9 ticks (2.25 points), raise Stop Loss to 1.25 points (5 ticks) if (Position.AveragePrice >= Close[0] + (9 * TickSize)) { SetStopLoss("ShortEntry", CalculationMode.Ticks, -5, false); } } // Going Short by Cross Below if (CrossBelow(EMA1, SMA1, 1)) { // Enter Short EnterShort(Convert.ToInt32(DefaultQuantity), "ShortEntry"); } // Needs both statements to be true in order to execute // This rule will prevent false signals during ranges/reversals if (Position.AveragePrice >= Close[0] + (5 * TickSize) && CrossAbove(EMA1, SMA1, 1)) { ExitShort(Convert.ToInt32(DefaultQuantity), "ShortEntry", ""); } //******************************************************************************************************************** if (BarsSinceEntryExecution() >= 8 && (Position.AveragePrice >= Close[0])) { // if current price is under entry price -> Set 1 tick StopLoss SetStopLoss("ShortEntry", CalculationMode.Ticks, -1, false); } // Why is this executing before 8 bars? else { SetProfitTarget("ShortEntry", CalculationMode.Ticks, 1, false); } } //********************************************************************************************************************
1. Call the method with OnStateChange
2. Call the method with OnBarUpdate
3. StopTargetHandling
4. Or something else
I see the documentation states that I can call the method OnStateChange() or OnBarUpdate(), but because I already declared the position flat at the beginning through OnBarUpdate() so I don't see that as the problem here, unless it's with OnStateChange. (Then how do you do that?)
I also read How to close a position under Manage Approach about to use a ByStrategyPosition or a PerEntryExecution. Would this fix the problem? If so how does it & where would I put it?
Again I'm not sure what the problem is/what route to take.
Please explain which route & why it fixes it.
Thanks in advance.
Comment