Hi
I now use this in one of my programs
SET TARGET pPROFIT 425
Is there a way to make it so if 425 is hit my position is not sold but a trailing stoploss kicks in?
Simlply remove SET TARGET pPROFIT 425 and append this code snippet to your strategy. You only need to set the TRAILINGSTART (I set it to 425 as you rquested) and the TRAILINGSTEP (the pace at which the trailing stop will be updated):
//*********************************************************************************
// https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/
// (lines 17- 56)
//
//trailing stop function
trailingstart = 425 //trailing will start @trailinstart points profit
trailingstep = 5 //trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//*********************************************************************************
Tank you Roberto for your fast answer.
I have put in the code in one of my long robots and one of my short, but the results I get from backtesting makes no sence. For example changing the trailingstep from 5 to 6 could change the backtesting results by 40% and that is just not possible with such a small change, I trade the DAX and 1 piont is nothing. Every number I used as trailingstop gave a worse result the just useing target profit. I even tried to set trailingstep to 0 to see if I would get the same results as just “SET TARGET pPROFIT 425” and it did not give the same result.
If you have any ide where I went wrong it would be much appreciated.
Optimise the start and the step using …
Start: min 25 max 500 increment 25
Step min 10 max 100 increment 10
Tell us how you get on
Tried both my short and long with alot of different numbers. My short gave great results where all test improved backtesting with 5-10%, but the long gives alot of strange numbers.
Made a matrix in excel and attached
EXITSHORT AT MARKET
ENDIF
// Stops and targets
SET STOP pLOSS 165
//trailing stop function
trailingstart = 405 //trailing will start @trailinstart points profit
trailingstep = 30 //trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
EXITSHORT AT newSL STOP
ENDIF
SELL AT MARKET
ENDIF
// Stops and targets
trailingstart = 425 //trailing will start @trailinstart points profit
trailingstep = 25 //trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
ENDIF
The first one is my short that is working fine, and the second one is my long that I have problems with.
You still have SET STOP pLOSS 165 in yuor Short so how do you know exits are not due to SET STOP pLOSS 165?
It is quite time consuming going over trades and only yourself will have the motivation.
Enter as a last line of code …
GRAPH newSL as “trailing”
and try and work out what is happening re exits … zoom in 1 or 2 trades and look at price levels together with the line for NewSL and fit it altogether in your mind … it can be interesting and enlightening! 🙂
Yes this is really fun, and having you guys help is priceless. I have one more Q before I dive deep in grafs and numbers, that is close to my other Q.
I have this 3th robot that end like this
// Conditions to exit long positions
indicator3 = ExponentialAverage[20](close)
indicator4 = ExponentialAverage[100](close)
c2 = (indicator3[2] CROSSES OVER indicator4[2])
IF c2 THEN
SELL AT MARKET
ENDIF
// Stops and targets
SET STOP pLOSS 500
Is there some way to make c2 to not trigger a “sell at market” but to trigger a Trailing stoploss?
You cannot have twice the same code, as it’s read sequentially and the second one will override the first one.
Try this …
If c2 Then
// Stops and targets
trailingstart = 425 //trailing will start @trailinstart points profit
trailingstep = 25 //trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
ENDIF
Endif
Tank you for taking your time to help me. In this one I’m not intrested in 425 starting point, but would like it to start trailing at c2. Would this work, just changing 425 to c2?
If c2 Then
// Stops and targets
trailingstart = c2 //trailing will start @trailinstart points profit
trailingstep = 25 //trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
ENDIF
Endif
Would this work, just changing 425 to c2?
No, because C2 can only be True / 1 or False / 0. You can’t put 1 or 0 in place of 425.
c2 = (indicator3[2] CROSSES OVER indicator4[2])
- If indicator3[2] CROSSES OVER indicator4[2] then C2 = True / 1.
- If indicator3[2] CROSSES OVER indicator4[2] does NOT occur then C2 = False / 0.
Ok ,but how would I then go about if I want a trailing stop to start at just that level where indicator3<span class=”crayon-o”>[</span><span class=”crayon-cn”>2</span><span class=”crayon-o”>]</span> <span class=”crayon-st”>CROSSES OVER</span> indicator4<span class=”crayon-o”>[</span><span class=”crayon-cn”>2</span><span class=”crayon-o”>]? Is it possible to store this value in a constant that I can later use in “trailingstarts?</span>
I want a trailing stop to start at just that level
To achieve above, I provided you with the code in the post below
Trigger Trailing SL if target is met