Sure-Fire Flip-at-Stop Strategy (STOP Orders Only, No Hedging)

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #256522 quote
    Nicolas
    Keymaster
    Master

    This topic presents a Sure-Fire flip-at-stop recovery strategy coded for ProRealTime / ProOrder.

    Main characteristics:

    • No hedging (only one position at a time)
    • STOP orders only for reversals
    • When a position hits its stop, an opposite order is triggered at the exact same price
    • Position size is recalculated at each level to:
    • recover all previous losses
    • reach a fixed net profit target
    • Fully parameterized
    • Modular architecture using CALL functions

    

    This code is fully functional and intended for advanced users who understand recovery systems and risk management.


    Strategy Logic (High Level):

    > The strategy starts with an initial direction (Dir0)

    > An entry is triggered using:

    • a breakout STOP order, or
    • a moving average crossover (optional)

    > Once in position:

    • a Take Profit is set
    • a reverse STOP order is placed at the stop level

    >If Take Profit is hit:

    • the cycle ends
    • level and losses are reset

    >If Stop Loss is hit:

    • the reverse order opens a new position
    • direction flips
    • lot size increases
    • level increments

    >The process repeats until:

    • profit is achieved
    • or Nmax recovery levels are reached


    User Parameters:

    Strategy         = 1        // 0 = breakout, 1 = MA cross
    
    
    once Dir0        =  1       // Initial direction: 1 = long, -1 = short
    once D0          = 20.0     // Initial stop distance (points)
    once rTPoverSL   =  1.2     // Take Profit / Stop Loss ratio
    once eScale      =  1.0     // Distance scaling (1.0 = constant)
    once GtargetEUR  = 10.0     // Net profit target per cycle (€)
    once Nmax        =  5       // Maximum recovery levels
    
    
    once MinLot      = 1.0
    once LotStep     = 1.0
    once LotMax      = 100.0
    
    
    once nBreak      = 20       // Breakout lookback
    once bufferPts   = 1.0      // Breakout buffer (points)
    

    With eScale = 1.0, stop and target distances remain constant at all levels.


    Main Strategy Code:

    DEFPARAM CumulateOrders = False
    
    
    ONCE level = 0
    ONCE CprevEUR = 0.0
    ONCE lastEntryPrice = 0.0
    once ipointvalue = pointvalue
    
    
    longWasOn  = LONGONMARKET[1]
    shortWasOn = SHORTONMARKET[1]
    
    
    // --- Current level setup
    dirK = CALL "SF_DirectionAtLevel"[Dir0, level]
    SLpts, TPpts = CALL "SF_LevelMetrics"[D0, rTPoverSL, eScale, level]
    lotK = CALL "SF_LotSizer"[CprevEUR, GtargetEUR, TPpts, ipointvalue, MinLot, LotStep, LotMax]
    
    
    // --- End of cycle reset
    IF NOT ONMARKET AND (longWasOn OR shortWasOn) THEN
     level = 0
     CprevEUR = 0
     dirK = Dir0
     traded = 0
    ENDIF
    
    
    // --- Entry logic
    IF NOT ONMARKET THEN
    
    
     IF Strategy = 0 THEN
      entryStopPrice = CALL "SF_EntryStopPrice"[dirK, nBreak, bufferPts]
      IF dirK = 1 THEN
       BUY lotK CONTRACTS AT entryStopPrice STOP
      ELSE
       SELLSHORT lotK CONTRACTS AT entryStopPrice STOP
      ENDIF
    
    
     ELSIF Strategy = 1 THEN
      maf = average[7,1]
      mas = average[21,1]
    
    
      IF maf CROSSES OVER mas THEN
       dirK = 1
       BUY lotK CONTRACTS AT MARKET
       SET TARGET PPROFIT TPpts
       IF traded = 0 THEN Dir0 = dirK : traded = 1 ENDIF
    
    
      ELSIF maf CROSSES UNDER mas THEN
       dirK = -1
       SELLSHORT lotK CONTRACTS AT MARKET
       SET TARGET PPROFIT TPpts
       IF traded = 0 THEN Dir0 = dirK : traded = 1 ENDIF
      ENDIF
     ENDIF
    
    
    ENDIF
    
    
    // --- Position management
    IF ONMARKET THEN
    
    
     SET TARGET PPROFIT TPpts
    
    
     revPrice = CALL "SF_ReversePrice"[positionprice, dirK, SLpts]
    
    
     CprevNextEUR = CprevEUR + lotK * SLpts * pointvalue
     SLptsNext, TPptsNext = CALL "SF_LevelMetrics"[D0, rTPoverSL, eScale, level + 1]
     lotNext = CALL "SF_LotSizer"[CprevNextEUR, GtargetEUR, TPptsNext, ipointvalue, MinLot, LotStep, LotMax]
    
    
     IF level < Nmax - 1 THEN
      IF dirK = 1 THEN
       SELLSHORT lotNext CONTRACTS AT revPrice STOP
      ELSE
       BUY lotNext CONTRACTS AT revPrice STOP
      ENDIF
      SET STOP PRICE 0
     ELSE
      SET STOP PRICE revPrice
     ENDIF
    
    
     IF (longWasOn AND SHORTONMARKET) OR (shortWasOn AND LONGONMARKET) THEN
      CprevEUR = CprevNextEUR
      level = level + 1
      lastEntryPrice = positionprice
     ENDIF
    
    
    ENDIF
    


    Functions (Called via CALL)


    CALL “SF_LevelMetrics”

    Calculates stop and target distances per recovery level.

    SLpts = D0 * POW(e, level)
    TPpts = r * SLpts
    RETURN SLpts, TPpts
    


    CALL “SF_LotSizer”

    Computes the minimum lot size required to recover losses and reach the profit target.

    raw = (CprevEUR + GtargetEUR) / (TPpts * iPointValue)
    q = raw / LotStep
    qInt = round(q, 0)
    ceilq = qInt + (q > qInt)
    ilot = ceilq * LotStep
    
    
    IF ilot < MinLot THEN ilot = MinLot ENDIF
    IF ilot > LotMax THEN ilot = LotMax ENDIF
    
    
    RETURN ilot
    


    CALL “SF_EntryStopPrice”

    Determines the STOP entry price based on a breakout.

    IF dir = 1 THEN
     entryStopPrice = HIGHEST[nBreak](HIGH) + bufferPts * pointsize
    ELSE
     entryStopPrice = LOWEST[nBreak](LOW) - bufferPts * pointsize
    ENDIF
    RETURN entryStopPrice
    


    CALL “SF_ReversePrice”

    Returns the exact price where the position must flip.

    reversePrice = entryPrice - dir * SLpts * pointsize
    RETURN reversePrice
    


    CALL “SF_DirectionAtLevel”

    Defines trade direction depending on the recovery level.

    IF level MOD 2 = 0 THEN
     dir = Dir0
    ELSE
     dir = -Dir0
    ENDIF
    RETURN dir
    


    Important Notes

    āš ļø This is a recovery system with increasing exposure

    • Lot size can grow very fast
    • Margin requirements may explode
    • Slippage and gaps can invalidate assumptions
    • Always use:
    • Nmax
    • LotMax
    • session filters
    • volatility filters
    • manual emergency stop

    This strategy is not a free lunch and must be tested thoroughly.


    Good Luck! šŸ™‚

    #256524 quote
    Nicolas
    Keymaster
    Master

    To make it easier to recreate the code and test it, here is the complete strategy file, which will also install all dependencies in CALL. I can’t wait for the functions to be added to ProBuilder! In the meantime, CALL works just fine! 

    GraHal thanked this post
    #256766 quote
    Nicolas
    Keymaster
    Master

    ok, let’s explain what the code is doing in simple words šŸ™‚

    A “Surefire” hedging strategy is a trading technique designed to profit regardless of which way the market moves. It avoids predicting the future (market analysis) and relies entirely on mathematics and position management.

    The basic premise is: “If I lose money in one direction, I will open a larger position in the other direction to cover the loss and make a profit.

    It has an extreme winning rates in trending market, but it is very difficult to maintain the equity and margin in ranging market because of the possibility of many losses in a single row. In that case, the “eScale” parameter is of help, because it increases the range between 2 consecutive orders opening and therefore can avoid successive opening when market makes small moves (whipsaws).. The other parameters can also improve the net profit while reducing the risk of ruin.

    So the trigger of opening the first order must rely on a very good “go up” or “go down” signals.

    Also, since hedging is not yet possible with ProOrder, we have to close the losing order and recover its loss with the next triggered order, while in pure hedging mode, the loss could be net off, here we have to suffer it. Anyway, this is a very good exercise of coding and analysis and a vast field of improvements.

    Attached below is a backtest on NASDAQ 20-minutes, 1 point spread with default settings from the code shared earlier in the topic. Of course, long only in an uptrend makes it somehow biases..

    GraHal thanked this post
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

šŸ“ā€ā˜ ļø The Dark Side: Grids & Martingales

New Reply
Author
author-avatar
Nicolas @nicolas Keymaster
Summary

This topic contains 2 replies,
has 1 voice, and was last updated by Nicolas
1 week, 6 days ago.

Topic Details
Forum: šŸ“ā€ā˜ ļø The Dark Side: Grids & Martingales Forum
Started: 01/20/2026
Status: Active
Attachments: 2 files
Logo Logo
Loading...