Output "
Setting initial short stop loss: 17281.5 (Bid: 17274)
Average Position 17274.25
New best price for short position: 17274
New best price for short position: 17268
Activating trail stop for short position: 17280.5 (Bid: 17268)
Updating trailing stop for short position: 17269 (Bid: 17268)
"
These first three (Bolded) occurred within the entry bar, however the next 3 update AFTER the bar has already closed causing us to get worse exit and in many other instances causing us to lose on a trade when we should've exited for profit. Is this just a market replay thing or something wrong with my code? Thanks!!!
Code Snippets: (Code is set with Calculate.OnTickUpdate)
Entry and Initial Stop Loss Logic:
if (shortCondition && Position.MarketPosition == MarketPosition.Flat)
{
EnterShort("Short");
currentStopLoss = Open[1] + InitialSL * TickSize;
if (currentStopLoss > GetCurrentBid())
{
Print($"Setting initial short stop loss: {currentStopLoss} (Bid: {GetCurrentBid()})");
SetStopLoss("Short", CalculationMode.Ticks, InitialSL, false);
}
trailStopActivated = false;
bestPriceInFavor = Position.AveragePrice;
Print($"Average Position {bestPriceInFavor}");
shortOpened = true;
longOpened = false;
lastEntryBar = CurrentBar;
}
Trail Stop Logic:
if (Position.MarketPosition == MarketPosition.Short)
{
double currentPrice = GetCurrentBid(0);
if (currentPrice < bestPriceInFavor)
{
bestPriceInFavor = currentPrice;
Print($"New best price for short position: {bestPriceInFavor}");
}
double unrealizedProfit = Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]);
if (!trailStopActivated && unrealizedProfit >= TpPoints * TickSize * 20)
{
currentStopLoss = bestPriceInFavor + InitialSL * TickSize;
Print($"Activating trail stop for short position: {currentStopLoss} (Bid: {GetCurrentBid()})");
SetStopLoss("Short", CalculationMode.Price, currentStopLoss, false);
trailStopActivated = true;
}
if (trailStopActivated)
{
double newStopLoss = bestPriceInFavor + TrailOffset * TickSize;
if (newStopLoss < currentStopLoss && newStopLoss > GetCurrentBid(0) && newStopLoss != lastUpdateStopLoss)
{
Print($"Updating trailing stop for short position: {newStopLoss} (Bid: {GetCurrentBid()})");
currentStopLoss = newStopLoss;
SetStopLoss("Short", CalculationMode.Price, currentStopLoss, false);
lastUpdateStopLoss = newStopLoss;

Comment