Trading Divergence

 
I am trying to create an EA that acknowledges when divergence (for short positions) and convergence (for long positions) between the MACD chart and price has occurred. My idea for identifying this for entering a long position is as follows with the same in reverse for entering a short position. 1) The current macd histogram bar is below zero and the macd has crossed above the signal line thus signaling a long position, however a long position will not be entered into at this point instead the current macd level will be assigned to a variable, eg peak1, and the current price will be assigned to a variable, eg price1. 2) The program then needs to identify when then next macd/signal line crossover occurs (macd still being below zero) and then assign a variable to the current macd level, eg peak2, and assign a variable to the current price level, eg price2. 3) If peak2 is greater than peak1 and price2 is less than price1 then this signifies that convergence has occurred at which point a long position may be entered however i feel that it may be wise to wait until then next oversold condition has occurred on the stochastic oscillator. I understand that the program requires cycle operators, ie "for" or "while" to halt the program until the above mentioned stages have been satisfied. I am not an experienced progammer but i have attempted to use the "for" cycle without success and to be honest the code i have probably needs rewriting from scratch which is why i have not uploaded it here. If anybody can help me with the correct code that would be great. Thanks
 

S


To keep a track of 'state', which I think would help you, check out Static variables https://book.mql4.com/variables/types


and Globals https://book.mql4.com/variables/globals


Good Luck

-BB-

Reason: