Moving Average Cross Over

 
int init(){
            RealPoint = RealPipPoint(Symbol());  
           }
           
           
 void OnTick(){
               double MidMovingAverage = iMA(NULL,PERIOD_H4,7 , NULL, MODE_EMA,PRICE_CLOSE,0);

               double SlowMovingAverage = iMA(NULL ,PERIOD_H4,10,NULL ,MODE_EMA,PRICE_CLOSE,0);

               double FastMovingAverage = iMA(NULL ,PERIOD_H4,21,NULL ,MODE_EMA,PRICE_CLOSE,0);

               double lastMidMovingAverage = iMA(NULL,PERIOD_H4,7 , NULL, MODE_EMA,PRICE_CLOSE,1);

               double LastSlowMovingAverage = iMA(NULL ,PERIOD_H4, 10,NULL , MODE_EMA, PRICE_CLOSE,1);

               double LastFastMovingAverage = iMA(NULL ,PERIOD_H4, 21,NULL , MODE_EMA, PRICE_CLOSE,1);
          
              

 if((SlowMovingAverage < MidMovingAverage) && (LastSlowMovingAverage>lastMidMovingAverage))
                                { 
                                  double s = iLow(Symbol(),PERIOD_H4,1); // candle low function  
                        
                                  string time = TimeToStr(TimeCurrent(),TIME_SECONDS);
                             Print("master " +s);
                             Print("time is " +time);
                          
            }
            }
            

if((SlowMovingAverage > FastMovingAverage )&& (LastSlowMovingAverage < LastFastMovingAverage) && (OrdersTotal()==0))
            {
           
             int Buyorder = OrderSend(Symbol(),OP_BUY, 0.1 , Ask , 0 , initialSl ,initialTp,NULL,0,0,Green);
             OrderSelect(Buyorder,SELECT_BY_TICKET); 
                                 double OpenPrice = OrderOpenPrice();
                        
             //line for TP
             if(initialTp == 0)  LongTakeProfit = OpenPrice + (TakeProfit * RealPoint);
             Print("long tp price is  : " +LongTakeProfit);
             Print ("long sl price is : " +s);
             //Print ("long sl price is a: " +a);
             
            
             //modify trade code.
                                 if(s>0 && LongTakeProfit > 0) 
                                {
                                 Print("Final sl is " +s);
             bool LongMod = OrderModify(Buyorder,OpenPrice,s, LongTakeProfit,0);
             Print("bye bye order");
                                }                                       
         }

hello Guys i am basic coder of EA and i am facing some problem in making of EA .
i making a EA on cross over strategy how ever my strategy is based on 2 cross in 3 moving average.
my problem is i need to save the first cross over for the Stop loss and the 2nd cross over will be my entry with sl of previous cross over.

so can any body tell me how to save the previous cross over in the variable for the stop loss.

now i have made this code but in this code S variable is saving the current market state.



now can any one help me ???

Basic Principles - Trading Operations - MetaTrader 5 Help
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels. is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the...
Files:
 
sufiyansalam:

hello Guys i am basic coder of EA and i am facing some problem in making of EA .
i making a EA on cross over strategy how ever my strategy is based on 2 cross in 3 moving average.
my problem is i need to save the first cross over for the Stop loss and the 2nd cross over will be my entry with sl of previous cross over.

so can any body tell me how to save the previous cross over in the variable for the stop loss.

now i have made this code but in this code S variable is saving the current market state.



now can any one help me ???

There are so many ways to calculate and or triggering SL,for example previous higher high in sell trade and previous lower low for buy,that can be seen by fractals even,the other way to adjusting and configuring stoch,adx and so on a list - but i dont know which 3 ma cross indicator you are trying to emerge for creating EA

1

 
sufiyansalam: my problem is i need to save … can any one help me ???
Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
          No free help
          urgent help.

Or pay someone. Top of every page is the link Freelance.
          Hiring to write script - General - MQL5 programming forum

 
sufiyansalam:

...now can any one help me ???

please use code buttons instead of showing images of your code

 
lippmaje:

please use code buttons instead of showing images of your code


soory sir i am new here that's why it's was difficult to find the code button but now i have mentioned the code to please have a look ?
if just want to save the s variable value . but when i use S variable in next IF condition if print zero value ...

 
William Roeder:
Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
          No free help
          urgent help.

Or pay someone. Top of every page is the link Freelance.
          Hiring to write script - General - MQL5 programming forum

sir i am beginner and learning the coding . how ever i have mentioned the code above please have a look .
 

Two approaches are possible - one is to keep track of the mid MA cross in a global variable so that later when the fast MA cross happens you just read them out, the second is more favorable and shown below. The problem with approach 1 is that the content of global variables may be lost when the EA is restarted, especially on H4 when there might be several hours between fast and mid MA upcross.

The idea of the 2nd approach is to wait until the fast MA upcross happens, then look back N (say 10) candles in the chart history to find the mid MA upcross:

double LastMidMovingAverage, LastSlowMovingAverage, LastFastMovingAverage; // global variables

void OnTick() {
   bool isNewBar=IsNewBar();
   double FastMovingAverage = iMA(_Symbol,_Period, 7,0,MODE_EMA,PRICE_CLOSE,0);
   double MidMovingAverage  = iMA(_Symbol,_Period,10,0,MODE_EMA,PRICE_CLOSE,0);
   double SlowMovingAverage = iMA(_Symbol,_Period,21,0,MODE_EMA,PRICE_CLOSE,0);

   if(isNewBar) {
      LastFastMovingAverage = iMA(_Symbol,_Period, 7,0,MODE_EMA,PRICE_CLOSE,1);
      LastMidMovingAverage  = iMA(_Symbol,_Period,10,0,MODE_EMA,PRICE_CLOSE,1);
      LastSlowMovingAverage = iMA(_Symbol,_Period,21,0,MODE_EMA,PRICE_CLOSE,1);
   }
   if((FastMovingAverage>MidMovingAverage && LastFastMovingAverage<=LastMidMovingAverage) // fast MA upcross
        && MyTotalOrders()==0) {

      // find the mid MA upcross
      double midMA  = MidMovingAverage;
      double slowMA = SlowMovingAverage;

      for(int shift=0; shift<10; shift++) {
         double midMAprev  = iMA(_Symbol,_Period,10,0,MODE_EMA,PRICE_CLOSE,shift+1);
         double slowMAprev = iMA(_Symbol,_Period,21,0,MODE_EMA,PRICE_CLOSE,shift+1);

         if(midMA>slowMA && midMAprev<=slowMAprev) {
            // mid MA upcross at shift-th candle
            double stoploss = CalculateCross(midMA,slowMA,midMAprev,slowMAprev);
            OpenBuyOrder(stoploss);
            break;
         }
         midMA  = midMAprev;
         slowMA = slowMAprev;
      }
      // no mid MA upcross within the last 10 candles = no buy
   }
}

The function IsNewBar checks for a new bar, and MyTotalOrders counts your EA's orders indentified by its Magic ID.

The function CalculateCross should compute the exact price at which the cross happened, this is the geometric operation that finds the intersection of the two straight lines midMAprev->midMA and slowMAprev->slowMA. A quick and easy solution would be an approximation by the total average of all values:

double CalculateCross(double midMA,double slowMA,double midMAprev,double slowMAprev) {
   return (midMA + slowMA + midMAprev + slowMAprev) / 4;
}
 

hello I am not sure if I understand properly, 

I will not answer your question but will give a hint that will make you solve future problems of his nature. 

 try and fix the logic of the code on paper. draw boxes and lines to show he whole structure of your code <NB: this is yours> then when the first cross happens you will have a variable that holds the Value of the MAs at point of Cross. 



 
lippmaje:

Two approaches are possible - one is to keep track of the mid MA cross in a global variable so that later when the fast MA cross happens you just read them out, the second is more favorable and shown below. The problem with approach 1 is that the content of global variables may be lost when the EA is restarted, especially on H4 when there might be several hours between fast and mid MA upcross.

The idea of the 2nd approach is to wait until the fast MA upcross happens, then look back N (say 10) candles in the chart history to find the mid MA upcross:

The function IsNewBar checks for a new bar, and MyTotalOrders counts your EA's orders indentified by its Magic ID.

The function CalculateCross should compute the exact price at which the cross happened, this is the geometric operation that finds the intersection of the two straight lines midMAprev->midMA and slowMAprev->slowMA. A quick and easy solution would be an approximation by the total average of all values:

Thankx Brother that's working for me ..
i was actually searching the condition which you have told me to search the previous 10 candle . 
really genius piece of Code .
appreciated :)

Reason: