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
Anyone can advise in this regards please
Thank you
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.
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
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.
|
|
// Exit postion if profit falls below trailing stop level
IF Equity > Equitytrailing THEN
SELL AT MARKET
ENDIF
|
When exactly do you want to exit?
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
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