Debugging exit 2 which use stochastic (simple logic)

MQL5 Indicateurs Experts

Spécifications

1) Core strategy logic (state machine)

States:

  • BEGINNING_00 (idle)

  • SETUP_00 → SETUP_01 → SETUP_02 → SETUP_03

  • SELL mirror required

Donchian touch triggers:

  • BUY_SETUP_00 triggers ONLY when price touches Donchian LOW

  • SELL_SETUP_00 triggers ONLY when price touches Donchian HIGH

  • Special case: if a candle touches BOTH Donchian high and low, use WAD trend on that candle to decide BUY vs SELL on that candle.

Restart-on-touch (important):

  • If there is no position at the close of candle n, and candle n touches Donchian, then the setup must restart to Setup_00 from candle n (even if previously in Setup_01/02/03).

  • This affects which candle is the “first setup candle” and therefore affects fixed cut-loss, retracement reference, and TP1 base.

Valid setup bar count rules:

  • setup candle t, t+1, t+2, t+3 are valid setup candles

  • t+4 is last chance for a pending stop order fill (same as Python logic)

Fixed stop loss rule (big requirement):

  • In SETUP_01, the cut loss (SL) must be fixed at the FIRST Setup01 candle.

  • Entry trigger level can trail each new setup candle.

  • Intrabar special case:

    • If price breaks the stop-loss side first (e.g., long breaks below stop level) then breaks the trigger level later in the same candle, then the SL is adjusted to the new extreme (and sizing adjusted if needed). Mirror for short.

Entry types (user selectable)
A) Normal stop entry:

  • Long: Buy Stop at setup candle High + 1 pip + spread (bid-stream model)

  • Short: Sell Stop at setup candle Low - 1 pip (bid-stream model)

B) Retracement entry:

  • Virtual breakout trigger starts retrace logic when breakout trigger is hit

  • Long: Buy Limit at % retracement from swing high toward swing ref

  • Short: Sell Limit at % retracement from swing low toward swing ref

  • CAP rule:

    • Long: if limit > breakout stop-entry price, cap it at breakout stop-entry

    • Short: if limit < breakout stop-entry price, cap it at breakout stop-entry

TP1 / TP2 exits
Two-position hedging mode:

  • At entry, open two separate positions:

    • TP1 leg = 50% volume, has server-side TP at TP1

    • Runner leg = 50% volume, no TP; TP2 logic activates only after TP1 is closed

  • Preferred over partial close

TP1:

  • Default TP1 = 1R, but in retracement mode TP1 is based on virtual breakout entry (not the retrace fill price)

TP2 method A — Stochastic trailing (this is the main area that still has issues):
Scanning start:

  • If TP1 hits on candle t, start scanning TP2 on candle t+1

Activation (whichever happens first after scanning starts):

  • Long: %K ≥ 80 OR %K crosses below %D

  • Short: %K ≤ 20 OR %K crosses above %D
    Important:

  • If TP1 hit and %K is already beyond threshold when scanning starts, activate immediately (no need to cross into it).

Trailing start:

  • If activation happens on candle a, trailing begins on candle a+1:

    • Long: stop = low(a) − 1 pip (applied from a+1 onward)

    • Short: stop = high(a) + 1 pip (applied from a+1 onward)

  • Tighten-only trailing (never loosens)

TP2 method B — PSAR trailing:

  • PSAR(t-1) trailing after TP1 closed

  • Freeze when PSAR flips to wrong side; resume when PSAR flips back
    (PSAR seems okay)

Risk / sizing:

  • Position size is risk n% of capital m (both inputs)

  • For JPY pairs etc, sizing uses OrderCalcProfit-based calculation to avoid tick-value conversion issues

Execution model:

  • Bid-stream core logic

  • Spread adjustments applied where required (e.g., buy entries, sell SL/TP, etc.)

2) What’s currently wrong

Most state machine is correct now, but TP2 Stochastic trailing sometimes:

  • does not activate when it should

  • or trails using the wrong candle reference

  • or appears inconsistent (sometimes correct, sometimes wrong)

I suspect a combination of:

  • activation gating timing (TP1 close time vs when EA detects it)

  • bar indexing / shift

  • position selection context (PositionSelectByTicket affecting subsequent PositionGet* calls)

  • SL modify failures due to stops/freeze (needs explicit logging)


Répondu

1
Développeur 1
Évaluation
(1)
Projets
0
0%
Arbitrage
2
0% / 50%
En retard
0
Travail
2
Développeur 2
Évaluation
(1)
Projets
2
0%
Arbitrage
1
0% / 100%
En retard
0
Gratuit
Publié : 2 codes
3
Développeur 3
Évaluation
Projets
0
0%
Arbitrage
0
En retard
0
Travail
4
Développeur 4
Évaluation
(3)
Projets
1
0%
Arbitrage
5
0% / 100%
En retard
0
Gratuit
5
Développeur 5
Évaluation
Projets
0
0%
Arbitrage
0
En retard
0
Gratuit
6
Développeur 6
Évaluation
Projets
0
0%
Arbitrage
0
En retard
0
Gratuit
7
Développeur 7
Évaluation
Projets
0
0%
Arbitrage
0
En retard
0
Gratuit
8
Développeur 8
Évaluation
(305)
Projets
548
35%
Arbitrage
79
32% / 42%
En retard
197
36%
Chargé
9
Développeur 9
Évaluation
(2)
Projets
2
0%
Arbitrage
2
0% / 50%
En retard
1
50%
Chargé
10
Développeur 10
Évaluation
(6)
Projets
14
43%
Arbitrage
2
0% / 100%
En retard
1
7%
Travail
11
Développeur 11
Évaluation
(15)
Projets
34
24%
Arbitrage
4
0% / 50%
En retard
2
6%
Travail
12
Développeur 12
Évaluation
(4)
Projets
3
33%
Arbitrage
2
0% / 100%
En retard
0
Gratuit
13
Développeur 13
Évaluation
(15)
Projets
18
6%
Arbitrage
8
38% / 38%
En retard
2
11%
Travail
Commandes similaires
EA should trade Gann indicator arrows filtered by Autotrendline indicator. Two orders per trade and when first level of Gann indicator is reached SL of second order should be moved to breakeven
I want to find a Developer to perform this work and settle payments in this Application. I undertake not to communicate with Applicants anywhere else except this Application, including third-party messengers, personal correspondence or emails. I understand that violators will be banned from publishing Orders in the Freelance service. I have read the Rules of using the Freelance service and accept them. I agree
I’m looking for an experienced developer to build an EA that executes trades automatically based on signals posted in my Telegram channel . Signal Execution Logic Example signal format: SELL GOLD 4669–4673 SL: 4676 TP1: 4667 TP2: 4664 TP3: Open (with trailing stop) EA requirements: Automatically read and parse signals from my Telegram channel Apply money management based on a fixed risk percentage (e.g. 5% of account
Moving forward 100+ USD
Position Size = (Account Risk %) / (Stop Loss %) Example: $1000 account, 1% risk = $10 If SL = 0.75%, position = $10 / 0.0075 = $1333 moving average
Hello, can you help me with my custom dashboard? I’m sending all the necessary files. Let me know if you can work on it. Please check the indicator carefully—some people declined, and I don’t have the source code. I’ll also need a sample file before I can select you as the developer
Hello, I have a strategy written in Pine Script (TradingView) that used to send signals to MetaTrader 5 via PineConnector. Now I want a native MT5 Expert Advisor (EA) written in MQL5, so I can do full backtesting and trading inside MetaTrader without any external bridge. I will provide: - Full Pine Script code - Entry and exit rules (based on BOS and OB logic) - SL/TP and dynamic risk management (R:R) - Breakeven
Job Title: HFT-Style M1 Gold Scalper EA for Exness Broker – 100-200 Trades/Day Description: Need a high-frequency trading Expert Advisor for scalping XAUUSD (Gold) on M1 timeframe only. This is for personal live account on Exness broker (ECN type preferred). Requirements: • 100-200+ trades per day during high liquidity sessions (London/New York overlap). • Ultra-fast tick-by-tick execution, optimized for low latency
EA for Gold 30+ USD
I am looking for a good EA robot for gold that will be taking trades on 1 min chart and takes profits on 1$ move on gold. that can take as many entries as possible in a day. example: ENTRY ON 3000.00 AND TAKE PROFIT AT 3001.00. The robot will have all the trading knowledge and strategy with a high win rate
The void 40 - 300 USD
An AI, with a clear understanding, analysis and predictions of the market, based on the right information and experience on years of market structure, an AI that is ready to face and survive the hardest moments of the market and emerge victorious. Simply but the perfect tool for a day trader
Here are my confirmations for Milestone 1: Trading hours: RTH only (9:30–16:00 ET). Data period: Please test the maximum available history. A longer sample (ideally 10 years) is preferred, as it strengthens the diagnostic and regime analysis. This is for robustness of the assessment, not for optimization. Positioning: One trade at a time (no stacking). Directions: Long and short, strictly as defined by the rules

Informations sur le projet

Budget
30 - 100 USD

Client

Commandes passées1
Nombre d'arbitrages0