Need help to code a new stop loss

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #100346 quote
    jebus89
    Participant
    Master

    Backstory: While making mean reversion its pretty usual to exit when close crosses under/over the mean. Sometimes close crosses over/under and the trade is pretty much perfect, other times price just keeps on running and you miss out on alot of profit.

     

    I would love a kind of trailing stop loss that moves the stop to the price of the Moving average when the crossover happens. As you see in the picture:

    1. I enter short trade when price is high, hoping price will revert back to its mean.

    2. When in the short trade, and price crosses under the MA, the stop loss will move to that spesific price of the moving average, when that crossover happens. So if the moving average = 12 123, then thats my new stop loss.

    mm.png mm.png
    #100351 quote
    robertogozzi
    Moderator
    Master

    It’s just abs(TRADEPRICE – CLOSE) of the candle when the crossing occurs.

    Divide it by PIPSIZE if you need pips instead of a price difference.

    If you need the price of the average, you decide which one and replace CLOSE with it.

    #100355 quote
    jebus89
    Participant
    Master

    not sure i understand what u mean Roberto, sorry.

    Im not sure how i can keep the stop loss at the same level regardless of how long the trade continues?

    #100374 quote
    robertogozzi
    Moderator
    Master

    You just need to save the value of your SL and, once set, never change it again. You’ll then place a pending STOP order each candlestick while On Market.

    When Not OnMarket clear your SL value resetting it to zero.

    #100382 quote
    jebus89
    Participant
    Master

    Im stupid, cant get this to work, sorry.

     

    Im someone wanna help me write the code id be grateful.

    #100407 quote
    robertogozzi
    Moderator
    Master

    Try this:

    IF Not OnMarket THEN
       MyStop = 0
    ENDIF
    Sma20 = average[20,0[(close)
    IF close CROSSES UNDER Sma20 AND MyStop = 0 THEN
       MyStop = Sma20
    ENDIF
    IF MyStop AND ShortOnMarket THEN
       EXITSHORT AT MyStop STOP
    ENDIF
    #100411 quote
    jebus89
    Participant
    Master

    I have not tested it yet Roberto, but this was my intial thought as well. The problem (and please correct me if im wrong) is that i dont want “a MA 20” to be my new stop level. i want “the MA 20” level where price crossed under the MA 20.

     

    I dont think the code u posted does this? it just has “if price crosses over MA20 then sell” basicly?

    #100414 quote
    GraHal
    Participant
    Master

    My head is buzzing trying to work out if the code is doing what I think it is supposed to! 🙂

    More coffee then look at it again, but attached is what I am looking at.

    The last orange bar was where it exit the Short trade.

    Jebus89.jpg Jebus89.jpg
    #100416 quote
    robertogozzi
    Moderator
    Master

    Line 6 does the job. The level where CLOSE crosses SMA20 is… SMA20!

    GraHal thanked this post
    #100419 quote
    robertogozzi
    Moderator
    Master

    In case you need the moment when close FIRST crosses under SMA20, then you’ll have to resort to MTF so that you can tell exactly that very moment, but it’s not granted that by the closure of the bar the price won’t retrace so, indeed, no crossing could have occurred at all, despite your strategy may have assumed it!

    #100420 quote
    GraHal
    Participant
    Master

    Surely the exit was on the next bar / bar not shown (at the stop level exit) and you can see that the exit price was the same price as MyStop shown on the previous bar?

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

Need help to code a new stop loss


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
jebus89 @jebus89 Participant
Summary

This topic contains 10 replies,
has 3 voices, and was last updated by GraHal
6 years, 8 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 06/10/2019
Status: Active
Attachments: 3 files
Logo Logo
Loading...