Halfway point exit without trailing stop loss

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #252316 quote
    AhimsaAhimsa
    Participant
    New

    Hi everyone

    Does anyone know how to code the following condition which must only apply after I have been in a trade for x bars to prevent premature exits:

    EXIT IF price is halfway (or lower) between the ENTRYPRICE, and the highest price reached during the trade?

    I do not want to use a trailing stop loss because there is a bug in ProRealTime which is causing trailing stop losses to get pushed back in the opposite direction.

    Many thanks in advance for your help.

    #252320 quote
    Iván GonzálezIván González
    Moderator
    Legend

    Hi! here you have an example:

    // ===========================
    // apply after x bars in trade to prevent premature exits
    // ===========================
    // ----- General settings
    DEFPARAM CumulateOrders = False
    rrmultiple = 3
    x = 20
    // ===========================
    // Example
    // ===========================
    atr = averagetruerange[14](close)
    sma10 = average[10](close)
    sma60 = average[60](close)
    setupLong = sma10 crosses over sma60
    
    // ===========================
    // Entry rules
    // ===========================
    IF NOT longonmarket and setupLong THEN
       
       BUY 1 CONTRACTS AT MARKET
       
       stopprice   = close - 1.5*atr
       targetdist  = rrmultiple * (close - stopprice)
       targetprice = close + targetdist
       entrybar = barindex
       highestPrice = close
       SET STOP pRICE stopprice
       SET TARGET pRICE targetprice
       
    ENDIF
    
    // ===========================
    // Exit after X bars if price < limit
    // ===========================
    if longonmarket then
       bars=bars+1
       highestPrice =  max(high,highestPrice)
       if bars>x then
          stopprice = 0.5*(highestPrice+tradeprice)
          SET STOP pRICE stopprice
       endif
    else
       bars = 0
       highestPrice = close
    endif
    // ===========================
    // Graphs for monitoring
    // ===========================
    GRAPH bars COLOURED("red")
    GRAPH x coloured("blue")
    GRAPHONPRICE stopprice COLOURED("red")
    GRAPHONPRICE targetprice COLOURED("green")
    GRAPHONPRICE highestPrice COLOURED("blue")
    GRAPHONPRICE sma10 COLOURED("fuchsia")
    GRAPHONPRICE sma60 COLOURED("orange")
    
    Ahimsa thanked this post
    #252342 quote
    AhimsaAhimsa
    Participant
    New

    This is fantastic, thank you Ivan!

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

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Halfway point exit without trailing stop loss


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Ahimsa @ahimsa Participant
Summary

This topic contains 2 replies,
has 2 voices, and was last updated by AhimsaAhimsa
11 months, 2 weeks ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 10/08/2025
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...