//+------------------------------------------------------------------+ //| Very Simple Intersection iMA Open Position.mq5 | //| Copyright © 2022, Vladimir Karputov | //| https://www.mql5.com/en/users/barabashkakvn | //+------------------------------------------------------------------+ #property copyright "Copyright © 2022, Vladimir Karputov" #property link "https://www.mql5.com/en/users/barabashkakvn" #property version "1.000" //--- #include #include //--- CPositionInfo m_position; // object of CPositionInfo class CTrade m_trade; // object of CTrade class //--- input parameters input group "Position size management (lot calculation)" input double InpLots = 0.01; // Lots input group "MA Fast" input int Inp_MA_Fast_ma_period = 5; // MA Fast: averaging period input int Inp_MA_Fast_ma_shift = 0; // MA Fast: horizontal shift input ENUM_MA_METHOD Inp_MA_Fast_ma_method = MODE_SMA; // MA Fast: smoothing type input ENUM_APPLIED_PRICE Inp_MA_Fast_applied_price = PRICE_CLOSE; // MA Fast: type of price input group "MA Slow" input int Inp_MA_Slow_ma_period = 15; // MA Slow: averaging period input int Inp_MA_Slow_ma_shift = 0; // MA Slow: horizontal shift input ENUM_MA_METHOD Inp_MA_Slow_ma_method = MODE_SMA; // MA Slow: smoothing type input ENUM_APPLIED_PRICE Inp_MA_Slow_applied_price = PRICE_CLOSE; // MA Slow: type of price input group "Additional features" input ulong InpMagic = 200; // Magic number //--- int handle_iMA_Fast; // variable for storing the handle of the iMA indicator int handle_iMA_Slow; // variable for storing the handle of the iMA indicator datetime m_prev_bars = 0; // "0" -> D'1970.01.01 00:00'; bool m_init_error = false; // error on InInit //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- forced initialization of variables m_prev_bars = 0; // "0" -> D'1970.01.01 00:00'; m_init_error = false; // error on InInit //--- m_trade.SetExpertMagicNumber(InpMagic); m_trade.SetMarginMode(); m_trade.SetTypeFillingBySymbol(Symbol()); m_trade.SetDeviationInPoints(10); //--- //--- create handle of the indicator iMA handle_iMA_Fast=iMA(Symbol(),Period(),Inp_MA_Fast_ma_period,Inp_MA_Fast_ma_shift, Inp_MA_Fast_ma_method,Inp_MA_Fast_applied_price); //--- if the handle is not created if(handle_iMA_Fast==INVALID_HANDLE) { //--- tell about the failure and output the error code PrintFormat("Failed to create handle of the iMA indicator ('Fast') for the symbol %s/%s, error code %d", Symbol(), EnumToString(Period()), GetLastError()); //--- the indicator is stopped early m_init_error=true; return(INIT_SUCCEEDED); } //--- create handle of the indicator iMA handle_iMA_Slow=iMA(Symbol(),Period(),Inp_MA_Slow_ma_period,Inp_MA_Slow_ma_shift, Inp_MA_Slow_ma_method,Inp_MA_Slow_applied_price); //--- if the handle is not created if(handle_iMA_Slow==INVALID_HANDLE) { //--- tell about the failure and output the error code PrintFormat("Failed to create handle of the iMA indicator ('Slow') for the symbol %s/%s, error code %d", Symbol(), EnumToString(Period()), GetLastError()); //--- the indicator is stopped early m_init_error=true; return(INIT_SUCCEEDED); } //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- we work only at the time of the birth of new bar datetime time_0=iTime(Symbol(),Period(),0); if(time_0==m_prev_bars) return; m_prev_bars=time_0; //--- double ma_fast[],ma_slow[]; ArraySetAsSeries(ma_fast,true); ArraySetAsSeries(ma_slow,true); int start_pos=0,count=6; if(!iGetArray(handle_iMA_Fast,0,start_pos,count,ma_fast) || !iGetArray(handle_iMA_Slow,0,start_pos,count,ma_slow)) { m_prev_bars=0; return; } //--- if(ma_fast[2]ma_slow[1]) { //--- Close all 'SELL' ... for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties if(m_position.Symbol()==Symbol() && m_position.Magic()==InpMagic) if(m_position.PositionType()==POSITION_TYPE_SELL) if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol Print(__FILE__," ",__FUNCTION__,", ERROR: ","SELL PositionClose ",m_position.Ticket(),", ",m_trade.ResultRetcodeDescription()); //--- ... and open one 'BUY' m_trade.Buy(InpLots); //--- return; } if(ma_fast[2]>ma_slow[2] && ma_fast[1]=0; i--) // returns the number of current positions if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties if(m_position.Symbol()==Symbol() && m_position.Magic()==InpMagic) if(m_position.PositionType()==POSITION_TYPE_BUY) if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol Print(__FILE__," ",__FUNCTION__,", ERROR: ","BUY PositionClose ",m_position.Ticket(),", ",m_trade.ResultRetcodeDescription()); //--- ...and open one 'SELL' m_trade.Sell(InpLots); //--- return; } } //+------------------------------------------------------------------+ //| Get value of buffers | //+------------------------------------------------------------------+ bool iGetArray(const int handle,const int buffer,const int start_pos, const int count,double &arr_buffer[]) { bool result=true; if(!ArrayIsDynamic(arr_buffer)) { PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__); return(false); } ArrayFree(arr_buffer); //--- reset error code ResetLastError(); //--- fill a part of the iBands array with values from the indicator buffer int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer); if(copied!=count) { //--- if the copying fails, tell the error code PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d", __FILE__,__FUNCTION__,count,copied,GetLastError()); //--- quit with zero result - it means that the indicator is considered as not calculated return(false); } return(result); } //+------------------------------------------------------------------+