Hi. I have the following code from the `TrailingMA.mqh` file that comes with Metatrader:
My question is the following. What is happening if while opening the long position the MA (Moving Average) is above it? It can happen, right?
From my understanding is that there will not be set any Stoploss until the current price of the position minus the StopLevel() * Point() is above the moving average (the level > MA). In that case the new Stop level calculated will be StopLevel() points above the moving average which means that it will probably be hit almost instantly? I say probably because the prices can easily fluctuate the minimum level allowed from the broker.
Am I understanding correctly? If yes, then the code is a bit problematic...
Thanks.
the function returns false because the ma is not above the level which is the minimum distance below the open price required for a valid stop loss
the function returns false because the ma is not above the level which is the minimum distance below the open price required for a valid stop loss
Sure! But this doesn't answer the question fully. Once it reaches that level the possibility of the position being closed is really high, right?
What I mean is that once the stop-loss is set (once MA ends up being below the level which is the minimum distance for a valid stop loss from the current bid price), it is risking that the price can fall an amount of points equal to StopLevel() and getting stopped immediately.
If yes, then the code is a bit problematic since it will be hunting the MA in order to get stopped...
Sure! But this doesn't answer the question fully. Once it reaches that level the possibility of the position being closed is really high, right?
What I mean is that once the stop-loss is set (once MA ends up being below the level which is the minimum distance for a valid stop loss from the current bid price), it is risking that the price can fall an amount of points equal to StopLevel() and getting stopped immediately.
If yes, then the code is a bit problematic since it will be hunting the MA in order to get stopped...
think of it as a regular trailing stop function that is designed should be updated for a long position. it's not wise to activate the trailing immediately after buy, give it some space... set some threshold and activate the trailing function after threshold has been exceeded and/or the easiest way is to use a factor...
double min_SL_buffer = m_symbol.StopsLevel() * Stop_factor * m_symbol.Point();
think of it as a regular trailing stop function that is designed should be updated for a long position. it's not wise to activate the trailing immediately after buy, give it some space... set some threshold and activate the trailing function after threshold has been exceeded and/or the easiest way is to use a factor...
double min_SL_buffer = m_symbol.StopsLevel() * Stop_factor * m_symbol.Point();
This code is from the Standard Library, not written by me. There are cases that an expert might open a long position on the bottom side of the moving average.
eg. based on documentation https://www.mql5.com/en/docs/standardlibrary/expertclasses/csignal/signal_ma you can have a moving average Failed breakout signal. This happens when the open price is above MA, the close price below MA and the indicator value rises. This is a weak signal for buying but the expert might open a long position if the signal exceeds the necessary threshold. If that happens, in the case you use a MA trailing stop-loss (the original one in the Standard Library of Metatrader), you might get stopped when you touch the MA value (which is higher at the current time).
This seems like a bug to me even though I haven't tested it completely. I will create an EA and verify it...
Part of the code for that signal can be seen below:
//+------------------------------------------------------------------+ //| "Voting" that price will grow. | //+------------------------------------------------------------------+ int CSignalMA::LongCondition(void) { int result=0; int idx =StartIndex(); //--- analyze positional relationship of the close price and the indicator at the first analyzed bar if(DiffCloseMA(idx)<0.0) { //--- the close price is below the indicator if(IS_PATTERN_USAGE(1) && DiffOpenMA(idx)>0.0 && DiffMA(idx)>0.0) { //--- the open price is above the indicator (i.e. there was an intersection), but the indicator is directed upwards result=m_pattern_1; //--- consider that this is an unformed "piercing" and suggest to enter the market at the current price m_base_price=0.0; } } else { ... ...
- www.mql5.com
Sure! But this doesn't answer the question fully. Once it reaches that level the possibility of the position being closed is really high, right?
What I mean is that once the stop-loss is set (once MA ends up being below the level which is the minimum distance for a valid stop loss from the current bid price), it is risking that the price can fall an amount of points equal to StopLevel() and getting stopped immediately.
If yes, then the code is a bit problematic since it will be hunting the MA in order to get stopped...
Thats why it returns false so that you don't update the stop loss
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi. I have the following code from the `TrailingMA.mqh` file that comes with Metatrader:
My question is the following. What is happening if while opening the long position the MA (Moving Average) is above it? It can happen, right?
From my understanding is that there will not be set any Stoploss until the current price of the position minus the StopLevel() * Point() is above the moving average (the level > MA). In that case the new Stop level calculated will be StopLevel() points above the moving average which means that it will probably be hit almost instantly? I say probably because the prices can easily fluctuate the minimum level allowed from the broker.
Am I understanding correctly? If yes, then the code is a bit problematic...
Thanks.