Hello
I’m trying to code a Trailing Stop the starts only if the strategy is in profit.
I’ve even requested help from PRT support team.
The improved code that they suggested does not work.
What I mean by that is, the Trailing Stop is created, but it’s not moving at all.
It just stuck there like a Stop Loss.
The following is the code for executing the Trailing Stop.
Can you spot the problem?
IF not longonmarket and (c1 OR c2) THEN
Buy 2 CONTRACT AT MARKET
SET STOP pTRAILING 0
ENDIF
LongPositionValue = (close - tradeprice(1))*abs(COUNTOFPOSITION)/pointsize*pointvalue
IF longonmarket and LongPositionValue >= 100 THEN
SET STOP pTRAILING 60
ENDIF
Thanks for the help.
A
Hi Algotrader.
There are a few strategies on the site that does what you want.
it not an easy task to do the trailing stop
Perhaps start here in the blog to see if you can use it
Cheers Kasper
Hi Elsborgtrading
Thanks, I am familiar with this post..
There are 2 problems here
1. This snippets supply by PRT support!! This is their suggestion.
And it doesn’t work!
If the creator of the software gives you a solution that doesn’t work..
What else left out there??
I find it disturbing. Wouldn’t you!?
2. On that post, I am referring to ‘Risk related trailing stop’
Unfortunately, it’s very basic, no details.
Cannot really understand it.
🙁
Hi Nicolas
- It runs with no errors.. so yes it works on backtest.
- No rejected orders.
- South Africa 40 Cash. Spread is 16.
As you can see on the attached images,
The strategy execute the stop loss and the trailing.
The price moved over 800 point in the favor direction,
But the trailing did not move at all.
(IG user with PRT V10.2)
Did you GRAPH “longpositionvalue” on backtest if the calculation is correctly made?
If spread is 16 points, it’s possible that the stoploss can’t move because the new calculated stoploss level is somewhere in the spread (seen this problem already with SAF index, that’s why I asked).
It’s always painful to debug the internal trailing function of proorder language, I never use it in my own development and for customers, if you already known the posts I made in the blog, why don’t you use these snippets?
Hi Nicolas
A. I never thought about that, Will give it a try. (Does GRAPH works on the V10.2?)
B. So the solution here is to add the spread into the calculation? so if my minimum stop loss is at 60,
so the new one should be 60+16=76?
C. So I’m not the only one who struggle with implementing the trailing stop into a strategy, Haha, I’m in good hands..
I do check your coding, the thing is you mainly code for FX so I need to tweak and change parameters to be compatible for SA40
SA40 is very different. I have manage to run your breakeven code successfully.
Q: Would you say the code for calculating the strategy value is right?
LongPositionValue = (close - tradeprice(1))*abs(COUNTOFPOSITION)/pointsize*pointvalue
You see, for SA40:
Pointsize is always 1`.
`
Pointvalue is always 2 (The micro contract is 2 rand per point).
abs(CountOfPosition), I guess PRT puts the ‘abs’ here because if you go SHORT it’ll be -2.
Other issue here is the TradePrice(1)
The way I understand it, it refers to the previous order.
However, I am always trading one order only, so as there is no previous trade,
there is only the current trade. I guess I should use TradePrice(0)
Formula will be calculated like this:
LongPositionValue = (close – tradeprice(0))*abs(COUNTOFPOSITION)/pointsize*pointvalue
LongPositionValue = (46080 – 46000)*abs(2)/1*2 = 80
But that is wrong!
it should be 320
Seams like PRT support have a mistake here, it should be: (46080 – 46000)*abs(2)*1*2 = 320
Is there a better way to calculate ‘PositionValue’?
Either way, as you can see from the attached images, the strategy went far and beyond that
And yet, it didn’t move the trailing stop in the favor direction at all.
The main problem still lies in the ‘Trailing Stop’ coding more then anything else..
What do you think?
GRAPH is the only way to debug your code, it works only in backtest and yes it is compatible with 10.2 (it was introduced in 10.2). You may have already read my blog article about the GRAPH function.
You should begin to debug the LongPositionValue state with GRAPH, before making any conclusion. Trust me, this is where to start 🙂
I use this calculation myself, to find the floating profit:
//floating profit
floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gains