Hi Everyone,
I’m very new to PRT and coding but decided to try my hand at writing a simple trailing stop. I’d be thrilled if anyone could take a look at what I’ve done and let me know why it’s not working. At this stage, the code enters a position but doesn’t seem to exit where the trailing stop probably should. Any guidance appreciated.
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
indicator1 = ExponentialAverage[20](close)
c1 = (close < indicator1)
If c1 Then
buy 1 contract at market
endif
if BarIndex - TRADEINDEX >0 and close > tradeprice then
slprice = highest[(BarIndex - TRADEINDEX)](close) - 2
else
slprice = tradeprice - 20
endif
if onmarket and close[1] > slprice and close < slprice then
sell at market
endif
Try this one:
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
indicator1 = ExponentialAverage[20](close)
c1 = (close > indicator1)
If c1 Then
buy 1 contract at market
slprice = 0
endif
if BarIndex - TRADEINDEX >0 and close > tradeprice then
slprice = max(highest[(BarIndex - TRADEINDEX)](close) - 2,slprice)
else
slprice = max(tradeprice - 20,slprice)
endif
if onmarket and close[1] > slprice and close < slprice then
sell at market
endif
graphonprice slprice coloured(0,128,0,155)
Thanks Roberto, really appreciate your response but that version doesn’t seem to work either. It still won’t exit trades and for some reason your version buys at way over the market rate.
Please post:
- Instrument traded
- TF used
- date & time of the bar where an incorrect exit occurred or did not but should have
so I can specifically check it.
Hey Roberto,
Thanks for your reply, sorry for not getting back to you, I’ve just been trying to learn a bit more about the coding. I might have another go and then come back with more questions in the future.
Cheers
Steve