Thanks for the detailed report! From your description:
- Open position stays active for about an hour
- Trailing stop does not move even though price changes a lot
- Swing lines on chart disappear
- After manually closing position, swing lines reappear and presumably trailing starts working again
Possible causes and debugging avenues:
1. Swing line disappearance when position is open
Your EA creates two horizontal lines in OnInit():
ObjectCreate(0, swingHighLineName, OBJ_HLINE, 0, 0, 0);
ObjectCreate(0, swingLowLineName, OBJ_HLINE, 0, 0, 0);
They set their prices continuously in OnTick() here:
ObjectSetDouble(0, swingHighLineName, OBJPROP_PRICE, lastSwingHigh);
ObjectSetDouble(0, swingLowLineName, OBJPROP_PRICE, lastSwingLow);
Hypothesis:
EA code likely never deletes these objects when a position is open, nor when position closes. But line disappearance implies that either:
- The objects got deleted externally or by another EA/script/template/chart event
- Your EA calls
ObjectDelete() somewhere else (you do it only in OnDeinit, so unlikely unless EA restarts).
Alternatively:
-
Possibly the lastSwingHigh or lastSwingLow returns 0 or invalid values causing lines to be hidden/moved out of visible chart area.
-
Chart timeframe updates or incorrect timeframe usage in iHigh/iLow calls cause invalid or no swing bars found.
2. Trailing stop doesn’t move despite price changes
Trailing stop update call depends on:
enableTrailingSL == true
trailingStopMode != TrailingNone
- Positions selected by magic number and symbol
CanUpdateTrailing() returns true (to throttle updates)
- Swing bars found on current timeframe inside trailing functions
- Checks profit distance vs
TrailingProfitTriggerPerc
If trailing stop never moves, possibilities:
-
The trailing conditions do not allow an update (for example price did not move past profit trigger threshold)
-
The trailing update function is never called for the position (because position is not selected or magic/symbol mismatch, or the loop terminates early)
-
The trailing stop update attempts fail silently (you have logging, so errors should appear)
-
CanUpdateTrailing() throttling is blocking updates (timeout too long / last_update incorrect)
-
iLowest / iHighest return -1 (no bars), so trailing stop update functions immediately return.
Step-by-step debugging steps I suggest:
A. Check that the swing bars are correctly found for the trailing stop update:
In your trailing stop functions (UpdateTrailingStopByMarketStructure() etc.), you do:
int lowestBar = iLowest(symbol, PERIOD_CURRENT, MODE_LOW, ms_swingLookback, 0);
int highestBar = iHighest(symbol, PERIOD_CURRENT, MODE_HIGH, ms_swingLookback, 0);
- Is
PERIOD_CURRENT corresponding to the chart timeframe, or to the swingTF input?
In your entry logic you use:
int highestBar = iHighest(_Symbol, swingTF, MODE_HIGH, swingLookback, 1);
int lowestBar = iLowest(_Symbol, swingTF, MODE_LOW, swingLookback, 1);
which is consistent with swingTF.
If your entry swing bars use swingTF but trailing uses PERIOD_CURRENT, there may be mismatch or the PERIOD_CURRENT timeframe has too few bars, or bars are incomplete or 0 sometimes, leading iHighest/iLowest to return -1, causing trailing functions to exit early and no SL move.
Fix: In trailing stop update functions, use swingTF (or a dedicated trailing TF input if desired) instead of PERIOD_CURRENT.
Example, replace:
int lowestBar = iLowest(symbol, PERIOD_CURRENT, MODE_LOW, ms_swingLookback, 0);
by
int lowestBar = iLowest(symbol, swingTF, MODE_LOW, ms_swingLookback, 0);
Similarly for highestBar.
B. Check the CanUpdateTrailing() timestamps:
Confirm that the timestamp logic isn’t blocking frequent updates, especially for open positions kept for a long time.
Try logging in CanUpdateTrailing():
PrintFormat("Trailing check position %I64d, now=%d, last_update=%d, diff=%d", position_ticket, now, g_trailingUpdateInfo[i].last_update, now - g_trailingUpdateInfo[i].last_update);
If you see the difference always less than TrailUpdateIntervalSec (e.g., 15 sec), trailing updates will not trigger.
Try temporarily reducing TrailUpdateIntervalSec to 1 or 0 to test.
C. Verify if price moves enough past TrailingProfitTriggerPerc * initial SL distance
In UpdateTrailingStopHybrid() you have:
double move_in_profit = (pos_type == POSITION_TYPE_BUY) ? (current_price - entry_price) : (entry_price - current_price);
if(move_in_profit < TrailingProfitTriggerPerc * initial_sl_dist)
return; // not enough profit, no trailing
-
Check your definition of profit trigger (TrailingProfitTriggerPerc = 0.5 means 50% of initial stop distance).
-
If price never moves that far in profit, trailing stops won’t update.
Try temporarily setting TrailingProfitTriggerPerc = 0.0 for debug.
Log the profit move:
PrintFormat("Trailing - position %I64d, move_in_profit=%.5f, trigger=%.5f", position_ticket, move_in_profit, TrailingProfitTriggerPerc * initial_sl_dist);
D. Swing lines disappearing — why?
Swing line prices are set each tick to lastSwingHigh and lastSwingLow (calculated with swing lookback):
ObjectSetDouble(0, swingHighLineName, OBJPROP_PRICE, lastSwingHigh);
ObjectSetDouble(0, swingLowLineName, OBJPROP_PRICE, lastSwingLow);
If lastSwingHigh or lastSwingLow == 0 or invalid number, lines could visually disappear or move off-chart.
It can happen if iHighest or iLowest return negative or invalid bar indices, or iHigh / iLow return 0 or error.
You call:
int highestBar = iHighest(_Symbol, swingTF, MODE_HIGH, swingLookback, 1);
int lowestBar = iLowest(_Symbol, swingTF, MODE_LOW, swingLookback, 1);
with start offset 1 (exclude current forming bar).
Suggestion:
-
Add checks if highestBar < 0 || lowestBar < 0 - you already do - but if this condition is true, you print error and return, so lines won't get updated.
-
If your EA skips updating lines, they keep their previous price. But if somehow your EA restarts or chart reloads, the objects may lose persistence or get reset.
-
Also check if your chart bar count on swingTF is sufficient, and that the chart data is fully loaded.
E. Possibly handle object recreation on EA restart or chart change
Try adding a function to ensure lines exist every OnTick if disappeared:
void EnsureSwingLinesExist()
{
if(!ObjectFind(0, swingHighLineName))
{
ObjectCreate(0, swingHighLineName, OBJ_HLINE, 0, 0, 0);
ObjectSetInteger(0, swingHighLineName, OBJPROP_COLOR, clrGreen);
ObjectSetInteger(0, swingHighLineName, OBJPROP_WIDTH, 2);
ObjectSetInteger(0, swingHighLineName, OBJPROP_BACK, true);
ObjectSetInteger(0, swingHighLineName, OBJPROP_RAY_RIGHT, true);
}
if(!ObjectFind(0, swingLowLineName))
{
ObjectCreate(0, swingLowLineName, OBJ_HLINE, 0, 0, 0);
ObjectSetInteger(0, swingLowLineName, OBJPROP_COLOR, clrRed);
ObjectSetInteger(0, swingLowLineName, OBJPROP_WIDTH, 2);
ObjectSetInteger(0, swingLowLineName, OBJPROP_BACK, true);
ObjectSetInteger(0, swingLowLineName, OBJPROP_RAY_RIGHT, true);
}
}
Call this at the beginning of OnTick().
This will eliminate accidental object deletions and ensure lines always stay present.
Summary Checklist:
| Possible Issue | Suggested Fix / Debug |
|---|
Trailing stop uses PERIOD_CURRENT instead of swingTF for swing bars | Replace with swingTF |
| Current price or swing bars unavailable causing no update | Add debug print and check bars available |
| Throttling blocking updates due to last update timestamp close | Print and/or reduce TrailUpdateIntervalSec |
| Trailing profit trigger percentage too high | Set TrailingProfitTriggerPerc = 0 temporarily |
| Swing lines disappear on position open possibly due to invalid swing values | Ensure swing bars found or recreate lines every tick |
| Objects deleted/reset by external reason | Add logic to recreate lines every tick |
If after these steps, problem persists, I can help you adjust or add detailed debug prints to trace exact flow.
Quick patch for trailing using consistent swingTF instead of PERIOD_CURRENT:
In trailing stop update functions, e.g., UpdateTrailingStopByMarketStructure:
Replace this:
int lowestBar = iLowest(symbol, PERIOD_CURRENT, MODE_LOW, ms_swingLookback, 0);
int highestBar = iHighest(symbol, PERIOD_CURRENT, MODE_HIGH, ms_swingLookback, 0);
with:
int lowestBar = iLowest(symbol, swingTF, MODE_LOW, ms_swingLookback, 0);
int highestBar = iHighest(symbol, swingTF, MODE_HIGH, ms_swingLookback, 0);
Make sure swingTF is an accessible global or EF input in these functions' scope.
If needed, add extern ENUM_TIMEFRAMES swingTF as parameter or global.
If you want, I can prepare such patch/edit directly for you. Just ask!