Trailing Take Profit Linked to Equity
Forums › ProRealTime English forum › ProOrder support › Trailing Take Profit Linked to Equity
- This topic has 7 replies, 2 voices, and was last updated 1 month ago by Suzan.
-
-
09/15/2024 at 1:57 AM #237580
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.
Below the code1234567891011121314151617181920212223242526272829303132333435// Define ParametersEquitytrailing = 0 // Trailing Equity LevelEquity = 30000 // Initial equity (Starting Balance)Trailingamount = 5000 // Trailing profit amount (5000$)// Conditions to enter long positionsIF NOT LongOnMarket AND YourConditions THENBUY 1 CONTRACTS AT MARKETEquity = Equity + POSITIONPERF // Update equity based on the performance of the open positionENDIF// Trailing take profitIF ONMARKET Then// Set initial trailing equity level once postion is openIF equitytrailing = 0 THENequitytrailing = Equtity + trailingamountENDIFENDIF// Adjust trailing equity level if current profit exceeds previouse targetIF (equity + POSITIONPERF) >= (equitytrailing + Trailingamount)THENequitytrailing = equity + POSITIONPERF // Look in profits as positionENDIF// Set target price based on trailing equitySet Target Price equitytrailing - Equity // Ensure the target profit is dynamically linked to equity growth// Exit postion if profit falls below trailing stop levelIF (Equity + POSITIONPERF)> Equitytrailing THENSELL AT MARKET//Reset equitytrailingequitytrailing = 0ENDIFThank you for your assistance
09/16/2024 at 10:24 PM #23765809/17/2024 at 4:15 AM #237665PositionPerf 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):
1234567891011121314151617181920212223242526272829303132333435// Define ParametersONCE Equitytrailing = 0 // Trailing Equity LevelONCE 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 positionsIF NOT LongOnMarket AND YourConditions THENBUY 1 CONTRACTS AT MARKETequitytrailing = Capital + trailingamountENDIF// Trailing take profitIF ONMARKET ThenPips = 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 levelIF Equity > Equitytrailing THENSELL AT MARKETENDIF// 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 equitySet Target Profit Pips//$profit Trailingamount // Ensure the target profit is dynamically linked to equity growthENDIFgraph 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.
1 user thanked author for this post.
09/18/2024 at 1:14 AM #237736Thanks 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 %.
12// Set target price based on trailing equitySet Target Profit Pips//$profit Trailingamount // Ensure the target profit is dynamically linked to equity growthCorrect me if I’m wrong please. I think should be (Equity < Equitytrailing) in order to exit the position.
1234// Exit postion if profit falls below trailing stop levelIF Equity > Equitytrailing THENSELL AT MARKETENDIFThanks a lot
09/18/2024 at 10:42 AM #237762Kindly, 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.
1234// Exit postion if profit falls below trailing stop levelIF Equity > Equitytrailing THENSELL AT MARKETENDIFWhen exactly do you want to exit?
09/19/2024 at 1:30 AM #237797Hi 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 profitIF ONMARKET ThenPips = Trailingamount / PipValue / abs(CountOfPosition)Equity = Capital + (PositionPrice * POSITIONPERF * PipValue * abs(CountOfPosition))if Equity – Pips > Pips THENEquitytrailing = Equitytrailing + PipsENDIF// Set target price based on trailing equitySet TARGET PRICE EquitytrailingENDIFAny idea or enhancement?
Thanks
09/20/2024 at 11:07 AM #237869This versions both seem to work as you expect.
This is version 1 (normal, exiting as soon as the gain drops below equitytrailing:
Version 1123456789101112131415161718192021222324252627282930313233// Define ParametersONCE Equitytrailing = 0 // Trailing Equity LevelONCE 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 positionsIF NOT LongOnMarket AND YourConditions THENBUY 1 CONTRACTS AT MARKET//reset the variables for the newly opened tradeequitytrailing = 0NextStep = trailingamountENDIF// Trailing take profitIF ONMARKET ThenEquity = (PositionPrice * POSITIONPERF * PipValue * abs(CountOfPosition))IF Equity >= NextStep THEN//lock in trailingamount profitsequitytrailing = equitytrailing + trailingamountNextStep = equitytrailing + trailingamountENDIF// Exit postion if profit falls below trailing stop levelIF (Equity < Equitytrailing) THEN//AND (PositionPerf > 0) THENSELL AT MARKETENDIFENDIF//graph PositionPrice * POSITIONPERF * PipValue * abs(CountOfPosition) AS "Current Gain"graph equitytrailinggraph NextStepVersion 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):
123456789101112131415161718192021222324252627282930313233// Define ParametersONCE Equitytrailing = 0 // Trailing Equity LevelONCE 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 positionsIF NOT LongOnMarket AND YourConditions THENBUY 1 CONTRACTS AT MARKET//reset the variables for the newly opened tradeequitytrailing = 0NextStep = trailingamountENDIF// Trailing take profitIF ONMARKET ThenEquity = (PositionPrice * POSITIONPERF * PipValue * abs(CountOfPosition))IF Equity >= NextStep THEN//lock in trailingamount profitsequitytrailing = equitytrailing + trailingamountNextStep = equitytrailing + trailingamountENDIF// Exit postion if profit falls below trailing stop levelIF (Equity < Equitytrailing) AND (PositionPerf > 0) THENSELL AT MARKETENDIFENDIF//graph PositionPrice * POSITIONPERF * PipValue * abs(CountOfPosition) AS "Current Gain"graph equitytrailinggraph NextStep09/22/2024 at 10:25 PM #237928Thank 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!
2 users thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on