Another try at a trailing stop

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #195831 quote
    RGTurner
    Participant
    New

    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
    #195881 quote
    robertogozzi
    Moderator
    Master

    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)
    #195901 quote
    RGTurner
    Participant
    New

    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.

    #195942 quote
    robertogozzi
    Moderator
    Master

    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.

    #196350 quote
    RGTurner
    Participant
    New

    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

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.

Another try at a trailing stop


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
RGTurner @rgturner Participant
Summary

This topic contains 4 replies,
has 2 voices, and was last updated by RGTurner
3 years, 7 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 06/22/2022
Status: Active
Attachments: No files
Logo Logo
Loading...