Simple Moving Average Cross Over Open and Close Order

 

Hello,


Please I need help with getting the code correct. i am new to the metatrading EA


I have two moving average and an ordersend for buy when one cross the other and ordersend to sell when the other crosses the first one.

what i want to do is get the first order to close when the other lines crosses it , while the other position in the sell direction opens.


That is when A crosses B, buy order

When B crosses A, close first order while the second order in the sell direction opens.

This happens if the sell order opens as well or vie versa


the part which contains the while statement is not correct by the way.




//+------------------------------------------------------------------+
//|                                      Simple Moving Average 1.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property version   "1.00"
#property description "Simple Moving Average "


//+------------------------------------------------------------------+
//| Expert Variables                                   |
//+------------------------------------------------------------------+

extern int TakeProfit = 100;
extern int StopLoss=25;

extern int FastMa=21;
extern int FastMaShift=0;
extern int FastMaMethod=0;
extern int FastMaAppliedTo=0;


extern int SlowMa=55;
extern int SlowMaShift=0;
extern int SlowMaMethod=0;
extern int SlowMaAppliedTo=0;

extern double LotSize = 0.01;
extern int MagicNumber = 1234;
double pips;
int ticket;
int result;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
         double ticksize = MarketInfo(Symbol(), MODE_TICKSIZE);
       if (ticksize == 0.00001 || ticksize == 0.001)
     pips = ticksize*10;
       else pips = ticksize;
       
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int start()
  {
//---
   double PreviousFast = iMA(NULL,0,FastMa,FastMaShift,FastMaMethod,FastMaAppliedTo,2);
   //double MediumFast = iMA(NULL,0,MediumMa,MediumMaShift,MediumMaMethod,MediumMaAppliedTo,0);
    double CurrentFast = iMA(NULL,0,FastMa,FastMaShift,FastMaMethod,FastMaAppliedTo,1);
   
   
   
    double PreviousSlow = iMA(NULL,0,SlowMa,SlowMaShift,SlowMaMethod,SlowMaAppliedTo,2);
    //double CurrentMedium = iMA(NULL,0,MediumMa,MediumMaShift,MediumMaMethod,MediumMaAppliedTo,0);
    double CurrentSlow = iMA(NULL,0,SlowMa,SlowMaShift,SlowMaMethod,SlowMaAppliedTo,1);
   
   
        if(PreviousFast<PreviousSlow && CurrentFast>CurrentSlow && OrdersTotal()==0)
        ticket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-(StopLoss*pips),Ask+(TakeProfit*pips),NULL,0,Green);
       
        while(true)
        {
        double StateToClose;
        StateToClose=PreviousFast>PreviousSlow && CurrentFast<CurrentSlow;
        if(OrderType()==OP_BUY && StateToClose== true)
           result = OrderClose(ticket, OrderLots(), Bid, 0,Orange);
         break;
        }
         
           
    if(PreviousFast>PreviousSlow && CurrentFast<CurrentSlow && OrdersTotal()==0)
        ticket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+(StopLoss*pips),Bid-(TakeProfit*pips),NULL,0,Red);
       
        while(true)
        {
        double StateToOpen;
        StateToOpen=PreviousFast>PreviousSlow && CurrentFast<CurrentSlow;
        if(OrderType()==OP_SELL && StateToOpen== true)
           result = OrderClose(ticket, OrderLots(), Ask, 0,Purple);
         break;
        }
         
       
        return(0);
  }
//+------------------------------------------------------------------+




Here is the code

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
Ask questions on technical analysis, discuss trading systems and improve your MQL5 programming skills to develop your own trading strategies. Communicate and share your experience with traders from anywhere in the world, answer questions and help beginners — MQL5.community is developing along with you. Experts: Grid Template EA Grid Template...
Reason: