Trailing Take Profit Linked to Equity

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #237580 quote
    Suzan
    Participant
    New

    Hello Team,

    I’m new to (PRT) and have been thoroughly enjoying learning and improving my skills on the platform.

    I’ve written a code for a strategy but would greatly appreciate your support and advice. My goal is to implement a trailing take profit that is tied to my equity, specifically based on the initial capital used to enter a position. However, as I am new to coding in PRT, I may have used incorrect keywords or syntax.

    What I am trying to achieve is to secure the gains made while in the market by establishing a trailing take profit system that locks in profits at specified increments (e.g., secure profits every $5,000). The idea is that as my profits increase, the trailing stop would adjust accordingly and ensure that profits are locked in and the position remains open. The position should only be exited when the profit falls below the trailing take profit level.

    // Define Parameters 
    
    Equitytrailing = 0 // Trailing Equity Level 
    Equity = 30000 // Initial equity (Starting Balance)
    Trailingamount = 5000 // Trailing profit amount (5000$) 
    
    // Conditions to enter long positions
    IF NOT LongOnMarket AND YourConditions THEN
    BUY 1 CONTRACTS AT MARKET
    Equity = Equity + POSITIONPERF // Update equity based on the performance of the open position 
    ENDIF
    
    // Trailing take profit
    IF ONMARKET Then
    // Set initial trailing equity level once postion is open 
    IF equitytrailing = 0 THEN
    equitytrailing = Equtity + trailingamount 
    ENDIF 
    ENDIF
    
    // Adjust trailing equity level if current profit exceeds previouse target 
    IF (equity + POSITIONPERF) >= (equitytrailing + Trailingamount)THEN
    equitytrailing = equity + POSITIONPERF // Look in profits as position
    ENDIF
    
    // Set target price based on trailing equity 
    Set Target Price equitytrailing - Equity // Ensure the target profit is dynamically linked to equity growth 
    
    // Exit postion if profit falls below trailing stop level 
    IF (Equity + POSITIONPERF)> Equitytrailing THEN 
    SELL AT MARKET 
    
    //Reset equitytrailing
    equitytrailing = 0
    ENDIF
    
    

     

    Thank you for your assistance

    #237658 quote
    Suzan
    Participant
    New

    Anyone can advise in this regards please

     

    Thank you

    #237665 quote
    robertogozzi
    Moderator
    Master

    PositionPerf is NOT the current gains, it’s a multiplier to be applied to PositionPrice (the average opening price, same as the opening price when not accumulating positions),  so that PositionPrice * PositionPerf  is the base for the current gain (loss, if negative), but it must be additionally adjusted according to the value of each pip and to the size of the position, so that the complete formula is MyGain = PositionPrice * POSITIONPERF * PipValue * abs(CountOfPosition).

    This the amended code (I also changed several lines and got rid of some of them):

    // Define Parameters
    
    ONCE Equitytrailing = 0        // Trailing Equity Level
    ONCE Capital        = 30000    // Initial equity (Starting Balance)
    ONCE Equity         = Capital  // Initial equity (Starting Balance)
    ONCE Trailingamount = 5000     // Trailing profit amount (5000$)
    
    YourConditions = close CROSSES OVER Average[20,0](close)
    
    // Conditions to enter long positions
    IF NOT LongOnMarket AND YourConditions THEN
    BUY 1 CONTRACTS AT MARKET
    equitytrailing = Capital + trailingamount
    ENDIF
    
    // Trailing take profit
    IF ONMARKET Then
    Pips = Trailingamount / PipValue / abs(CountOfPosition)
    Equity = Capital + (PositionPrice * POSITIONPERF * PipValue * abs(CountOfPosition)) // Update equity based on the performance of the open position
    // Set initial trailing equity level once postion is open
    
    // Exit postion if profit falls below trailing stop level
    IF Equity > Equitytrailing THEN
    SELL AT MARKET
    ENDIF
    
    // Adjust trailing equity level if current profit exceeds previouse target
    //IF equity >= (equitytrailing + Trailingamount)THEN
    //equitytrailing = equity  // Look in profits as position
    //ENDIF
    
    // Set target price based on trailing equity
    Set Target Profit Pips//$profit Trailingamount // Ensure the target profit is dynamically linked to equity growth
    ENDIF
    graph PositionPrice * POSITIONPERF * PipValue * abs(CountOfPosition) AS "Current Gain"

    Im not pretty sure it is exactly what you want, please test it and give me some feedback.

    Iván González thanked this post
    #237736 quote
    Suzan
    Participant
    New

    Thanks Rbertogozzi for your feedback

    Kindly, I need some clarification before test it,  dose the (Pips = Trailingamount /PipValue/abs(CountOfPosition) work to ensure that profits are locked in and the position remains open?

    My idea is as long as the direction moving to my favor it secure the new profits and move to next trailing amount point. What I want is the position only be exited when the profit falls below the trailing take profit level which suppose to be updated each time profit increase by (5000$) .  The idea is similar to use dynamic take profit strategy but linked to amount of my equity instead of points or %.

    // Set target price based on trailing equity
    Set Target Profit Pips//$profit Trailingamount // Ensure the target profit is dynamically linked to equity growth

     

    Correct me if I’m wrong please. I think should be  (Equity < Equitytrailing) in order to exit the position.

    // Exit postion if profit falls below trailing stop level
    IF Equity > Equitytrailing THEN
    SELL AT MARKET
    ENDIF

    Thanks a lot

    #237762 quote
    robertogozzi
    Moderator
    Master

    Kindly, I need some clarification before test it, dose the (Pips = Trailingamount /PipValue/abs(CountOfPosition) work to ensure that profits are locked in and the position remains open?

    No, actually it converts the money into pips, because to me it seems to work better.

    Correct me if I’m wrong please. I think should be (Equity < Equitytrailing) in order to exit the position.

    When exactly do you want to exit?

    #237797 quote
    Suzan
    Participant
    New

    Hi Rberto,

    The code is good so far but the result of testing the above code is , it give me a fixed target profit , while I intend to implement a dynamic one which secure the profit without exit the position and continue moving and exit the position only when profit drop below  trailing amount. In the current code the position exited through take profit when reach my trailing amount (after 250 points which is (20$ *250=5000$ in NASDAQ).

    I  tried to implement this code to get a dynamic take profit , but it doesn’t work well.

    // Trailing take profit
    IF ONMARKET Then
    Pips = Trailingamount / PipValue / abs(CountOfPosition)
    Equity = Capital + (PositionPrice * POSITIONPERF * PipValue * abs(CountOfPosition))
    if Equity – Pips > Pips THEN
    Equitytrailing = Equitytrailing + Pips
    ENDIF
    // Set target price based on trailing equity
    Set TARGET PRICE Equitytrailing
    ENDIF

    Any idea or enhancement?

     

    Thanks

    #237869 quote
    robertogozzi
    Moderator
    Master

    This versions both seem to work as you expect.

    This is version 1  (normal, exiting as soon as the gain drops below equitytrailing:

    // Define Parameters
    ONCE Equitytrailing = 0        // Trailing Equity Level
    ONCE Capital        = 30000    // Initial equity (Starting Balance)
    ONCE Equity         = Capital  // Initial equity (Starting Balance)
    ONCE Trailingamount = 5000     // Trailing profit amount (5000$)
    
    YourConditions = close CROSSES OVER Average[20,0](close)
    
    // Conditions to enter long positions
    IF NOT LongOnMarket AND YourConditions THEN
       BUY 1 CONTRACTS AT MARKET
       //reset the variables for the newly opened trade
       equitytrailing = 0
       NextStep       = trailingamount
    ENDIF
    
    // Trailing take profit
    IF ONMARKET Then
       Equity = (PositionPrice * POSITIONPERF * PipValue * abs(CountOfPosition))
       IF Equity >= NextStep THEN
          //lock in trailingamount profits
          equitytrailing = equitytrailing + trailingamount
          NextStep       = equitytrailing + trailingamount
       ENDIF
       // Exit postion if profit falls below trailing stop level
       IF (Equity < Equitytrailing) THEN//AND (PositionPerf > 0) THEN
          SELL AT MARKET
       ENDIF
    ENDIF
    //
    graph PositionPrice * POSITIONPERF * PipValue * abs(CountOfPosition) AS "Current Gain"
    graph equitytrailing
    graph NextStep

    Version 2 is almost the same, I only changed line 25 to make sure profits never drops below 0, so that your trades will always be wins (but DrawDown increases and trades last much longer, so that you will incur in higher rollover):

    // Define Parameters
    ONCE Equitytrailing = 0        // Trailing Equity Level
    ONCE Capital        = 30000    // Initial equity (Starting Balance)
    ONCE Equity         = Capital  // Initial equity (Starting Balance)
    ONCE Trailingamount = 5000     // Trailing profit amount (5000$)
    
    YourConditions = close CROSSES OVER Average[20,0](close)
    
    // Conditions to enter long positions
    IF NOT LongOnMarket AND YourConditions THEN
       BUY 1 CONTRACTS AT MARKET
       //reset the variables for the newly opened trade
       equitytrailing = 0
       NextStep       = trailingamount
    ENDIF
    
    // Trailing take profit
    IF ONMARKET Then
       Equity = (PositionPrice * POSITIONPERF * PipValue * abs(CountOfPosition))
       IF Equity >= NextStep THEN
          //lock in trailingamount profits
          equitytrailing = equitytrailing + trailingamount
          NextStep       = equitytrailing + trailingamount
       ENDIF
       // Exit postion if profit falls below trailing stop level
       IF (Equity < Equitytrailing) AND (PositionPerf > 0) THEN
          SELL AT MARKET
       ENDIF
    ENDIF
    //
    graph PositionPrice * POSITIONPERF * PipValue * abs(CountOfPosition) AS "Current Gain"
    graph equitytrailing
    graph NextStep
    #237928 quote
    Suzan
    Participant
    New

    Thank you, Roberto, for your outstanding efforts and support. I tested the code, and it works well. Here’s my feedback for potential users:

    The code is particularly effective in short time frames (1 minute), as it relies on the candlestick close for exiting positions. For instance, with an initial trailing amount of $1,500, the equity trailing activates when gains exceed this threshold, moving to $3,000 next. If current gains reach $3,700, Next step then becomes $4,500. However, if the price begins to dip below $3,000, the position won’t exit until the current candlestick closes, which could see gains drop to $2,000 or more. Therefore, quicker exits in small time frames could enhance performance significantly.

    If it will exit the position in the exact time when it touches the equity trailing which is (3000$) will be more safe

    Thanks once again, Roberto!

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

Trailing Take Profit Linked to Equity


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Suzan @suzan Participant
Summary

This topic contains 7 replies,
has 2 voices, and was last updated by Suzan
1 year, 4 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 09/15/2024
Status: Active
Attachments: No files
Logo Logo
Loading...