stoploss on a moving average
Forums › ProRealTime English forum › ProOrder support › stoploss on a moving average
- This topic has 8 replies, 4 voices, and was last updated 1 year ago by robertogozzi.
-
-
02/10/2023 at 9:07 PM #209529
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?
02/11/2023 at 8:52 AM #209533There you go:
12345MA = average[20,0](close)If MyLongConditions thenBuy at MarketSet Stop Price MA - 1*PipSizeEndif1 user thanked author for this post.
04/26/2023 at 7:40 PM #213784In 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.04/26/2023 at 9:05 PM #213788How about …
123If LongonMarket ThenSet Stop Price MA - 4*pipsizeEndifYou mean all these Trailing Stop Codes and above is all we need – 3 lines of code!? 😉
I’ll give it a go myself!
1 user thanked author for this post.
04/27/2023 at 9:25 AM #21379404/27/2023 at 3:37 PM #213805This way (not tested):
1234567891011121314MA = average[20,0](close)If MyLongConditions thenBuy at MarketSL = MA - 4*PipSizeELSIf MyShortConditions thenSellShort at MarketSL = MA + 4*PipSizeEndifIF LongOnMarket THENSL = max(SL, MA - 4*PipSize)ELSIF ShortOnMarket THENSL = min(SL, MA + 4*PipSize)ENDIFSet Stop Price SL3 users thanked author for this post.
05/01/2023 at 11:26 PM #213954@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 Short08/13/2023 at 9:36 PM #219007I 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
08/19/2023 at 3:20 PM #219341This version will set a 0.25% SL when the distance between the SL and the entry price is greater than 50 pips:
123456789101112131415161718192021222324MyLongConditions = close CROSSES OVER average[20,0](close)MyShortConditions = close CROSSES UNDER average[20,0](close)MA = average[20,0](close)IF Not OnMarket THENSL = 0SET STOP LOSS 0ENDIFIf MyLongConditions thenBuy at MarketSL = MA - 4*PipSizeELSIf MyShortConditions thenSellShort at MarketSL = MA + 4*PipSizeEndifIF LongOnMarket THENSL = max(SL, MA - 4*PipSize)ELSIF ShortOnMarket THENSL = min(SL, MA + 4*PipSize)ENDIFIF abs(SL - TradePrice) > 50*PipSize THENSET STOP %LOSS 0.25ELSESET STOP PRICE SLENDIF1 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on