stoploss on a moving average

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #209529 quote
    phoentzs
    Participant
    Master

    Hello, I know that this rejection exists, but unfortunately I can’t find it anymore. I’m looking for the code how to put a stoploss directly on a moving average. For example, I enter the trade long and set the stop loss below an SMA. How exactly do you code this?

    #209533 quote
    robertogozzi
    Moderator
    Master

    There you go:

    MA = average[20,0](close)
    If MyLongConditions then
       Buy at Market
       Set Stop Price MA - 1*PipSize
    Endif
    phoentzs thanked this post
    #213784 quote
    phoentzs
    Participant
    Master

    In order to achieve the minimum distance of the SL I use the following for long only:

    Set Stop Price MA  4*PipSize

    For short only this:

    Set Stop Price MA + 4*PipSize

    But if I want to pack both long and short in one system. How do I write the SL so it doesn’t get mixed up? Do I need to use a for loop somehow?
    I have to say I use it outside of the buy/sell block. As a kind of trailing stop.

    #213788 quote
    GraHal
    Participant
    Master

    How about …

    If LongonMarket Then
    Set Stop Price MA - 4*pipsize
    Endif

    You mean all these Trailing Stop Codes and above is all we need – 3 lines of code!? 😉

    I’ll give it a go myself!

    phoentzs thanked this post
    #213794 quote
    phoentzs
    Participant
    Master

    No of course not. But there is a way to tighten the sling without a coded trailing stop. 😉

    #213805 quote
    robertogozzi
    Moderator
    Master

    This way (not tested):

    MA = average[20,0](close)
    If MyLongConditions then
       Buy at Market
       SL = MA - 4*PipSize
    ELSIf MyShortConditions then
       SellShort at Market
       SL = MA + 4*PipSize
    Endif
    IF LongOnMarket THEN
       SL = max(SL, MA - 4*PipSize)
    ELSIF ShortOnMarket THEN
       SL = min(SL, MA + 4*PipSize)
    ENDIF
    Set Stop Price SL
    GraHal, phoentzs and ZeroCafeine thanked this post
    #213954 quote
    ZeroCafeine
    Participant
    Senior

    @phoentzs
    I prefer to use two different codes for Long and Short, Don’t forget that the SellShort function can make you get out of a Long position as if you executed a normal Sell in case you mismanage the entry and exit level (or an unexpected conflict in programming), moreover the SellShort will make you get out of your Long position but it will not open a Short position, you will still have to execute the SellShort function to open a Short

    #219007 quote
    phoentzs
    Participant
    Master

    I would like to add a maximum SL to this code. That is, if the price is very far from the SMA20 and going against me, I don’t want a huge loss to the SMA, just 0.25%. In this way you lose a maximum of 0.25% per trade. If the position turns into profit, the MA-SL secures the profits. The function can be equated with the following function, which I use in TF M1:

    Set stop %loss 0.25

    if longonmarket and close

    #219341 quote
    robertogozzi
    Moderator
    Master

    This version will set a 0.25% SL when the distance between the SL and the entry price is greater than 50 pips:

    MyLongConditions  = close CROSSES OVER  average[20,0](close)
    MyShortConditions = close CROSSES UNDER average[20,0](close)
    MA = average[20,0](close)
    IF Not OnMarket THEN
       SL = 0
       SET STOP LOSS 0
    ENDIF
    If MyLongConditions then
       Buy at Market
       SL = MA - 4*PipSize
    ELSIf MyShortConditions then
       SellShort at Market
       SL = MA + 4*PipSize
    Endif
    IF LongOnMarket THEN
       SL = max(SL, MA - 4*PipSize)
    ELSIF ShortOnMarket THEN
       SL = min(SL, MA + 4*PipSize)
    ENDIF
    IF abs(SL - TradePrice) > 50*PipSize THEN
       SET STOP %LOSS 0.25
    ELSE
       SET STOP PRICE SL
    ENDIF
    phoentzs thanked this post
Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.

stoploss on a moving average


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
phoentzs @phoentzs Participant
Summary

This topic contains 8 replies,
has 4 voices, and was last updated by robertogozzi
2 years, 6 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 02/10/2023
Status: Active
Attachments: No files
Logo Logo
Loading...