What's new

General Grid trading doesn't work to Multi symbol in MQL5 Programming

vik20010219

New Member
Sir,
I want to practice this Example EA MT5.

It has some working into grid trading to 4 multi symbol..but it doesn't work to trade in this Second symbol "EURUSD" (Please see this 1st photo attached)

It has 3 Trade in "GBPUSD" symbol (Please see this 2nd photo attached) but I want to only one Trade in GPBUSD.

I tested but It has some problem.

Please Solve this code

Code:
//+------------------------------------------------------------------+

//|                                                       MA_PipStep.mq5 |

//|                                            vik20011902@gmail.com|

//+------------------------------------------------------------------+

#property link      "vik20011902@gmail.com"

#property version   "1.00"



// Moving Average

int MA_Handle[];

double MA[];



// Point

double point = 0 ;



double StopLoss_point[] ;

double TakeProfit_point[];



double Ask[];

double Bid[];

double Spread[];


int AmountSymbols = 4;



string symbol[] = {"AUDUSD.s", "EURUSD.s", "GBPUSD.s", "USDCAD.s"};



input int MagicNumber = 12345;

input int MA_Period = 20;

input int MA_Shift = 1;



input int TakeProfit = 1000;

input int StopLoss = 1000;



input int MaxTrade = 3;

input int PipStep = 5;

input ENUM_TIMEFRAMES Time_Frame = PERIOD_M1;

double iclose[];


int OnInit()

  {



   EventSetTimer(1);



   for(int i = 0; i < AmountSymbols; i++)

     {

      ArrayResize(MA_Handle, AmountSymbols);

      MA_Handle[i] = iMA(symbol[i], PERIOD_CURRENT, MA_Period, MA_Shift, MODE_SMA, PRICE_CLOSE);



      if(MA_Handle[i] == INVALID_HANDLE)

        {

         Print("Failed ", symbol[i], " Loading this Indicator ");

         return (INIT_FAILED);

        }



      //Digits

      int digits = 1;

      if(Digits() == 3 || Digits() == 5)

        {

         digits = 10;

         point = Point() * digits;

        }

      else

         if(Digits() <= 2 || Digits() == 4)

           {

            digits = 10;

            point = Point() * 1;

           }



      ArrayResize(StopLoss_point, AmountSymbols);

      ArrayResize(TakeProfit_point, AmountSymbols);



      StopLoss_point[i] = StopLoss * point ;

      TakeProfit_point[i] = TakeProfit * point ;



     }



//---

   return(INIT_SUCCEEDED);

  }



void OnDeinit(const int reason)

  {

   for(int s = 0; s < AmountSymbols; s++)

     {

      ArrayResize(MA_Handle, AmountSymbols);



      if(MA_Handle[s] != INVALID_HANDLE)

        {

         IndicatorRelease(MA_Handle[s]);

        }

     }



   EventKillTimer();

  }



void OnTimer()

  {



   for(int i = 0; i < AmountSymbols; i++)

     {

      // Moving Average Buffer

      ArraySetAsSeries(MA, true);



      int count = 3;



      if(!CopyBuffer(MA_Handle[i], 0, 1, count, MA))

        {

         Print("Failed to create Indicator");

        }





      //Bars

      if(Bars(symbol[i], PERIOD_CURRENT) < 100)

        {

         Print("Bars less than 100");

         return;

        }







      ArrayResize(Spread, AmountSymbols);



      Spread[i] = (double)SymbolInfoInteger(symbol[i], SYMBOL_SPREAD);



      Comment("Spread = ", Spread[i]);



      ArrayResize(Ask, AmountSymbols);

      ArrayResize(Bid, AmountSymbols);



      Ask[i] = SymbolInfoDouble(symbol[i], SYMBOL_ASK);

      Bid[i] = SymbolInfoDouble(symbol[i], SYMBOL_BID);





      // Maximum Trade

      int total = 0;



      for(int i = CalculateAllPositions(symbol[i]) - 1; i >= 0; i--)

        {

         ulong Position_Ticket = PositionGetTicket(i);

         if(PositionSelectByTicket(Position_Ticket))

           {





            string check_Symbol = PositionGetString(POSITION_SYMBOL);



            if(check_Symbol == symbol[i])

              {

               total++;

              }

            if(total > MaxTrade)

              {

               return;

              }

           }

        }





      // New Current Bar

      if(New_Current_Bar(symbol[i]))

        {

         ArrayResize(iclose, AmountSymbols);



         iclose[i] = iClose(symbol[i], PERIOD_CURRENT, 1);



         if(CalculateAllPositions(symbol[i]) == 0)

           {

            if(MA[0] < Ask[i] && iclose[i] > MA[0])

              {

               // Buy

               Buy(symbol[i], MagicNumber, Ask[i], StopLoss_point[i], TakeProfit_point[i]);

              }

            else

               if(MA[0] > Bid[i] && iclose[i] < MA[0])

                 {

                  // Sell

                  Sell(symbol[i], MagicNumber, Bid[i], StopLoss_point[i], TakeProfit_point[i]);

                 }

           }



         if(CalculateAllPositions(symbol[i]) >= 1)

           {

            Pip_Step(symbol[i], MagicNumber, StopLoss_point[i], TakeProfit_point[i]);

           }



        }

     }



  }



/*

I removed this code because Iam Unabe to send this message forum Forex forum if it says "Message may not exceed 10000 characters"

New Bar function

*/

int CalculateAllPositions(string symbol_name)

  {

   int total = 0;



   for(int i = PositionsTotal() - 1; i >= 0; i--)

     {

      ulong Position_Ticket = PositionGetTicket(i);

      if(PositionSelectByTicket(Position_Ticket))

        {

         ENUM_POSITION_TYPE Position_Type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);

         string check_Symbol = PositionGetString(POSITION_SYMBOL);



         if(check_Symbol == symbol_name)

           {

            total++;

           }

        }

      //---



     }



   return(total);

  }


/*

I removed this code because Iam Unabe to send this message forum Forex forum if it says "Message may not exceed 10000 characters"

Buy & Sell Trade function

*/



// PipStep  Grid

int Pip_Step(string sparam, int magic_2, double StopLoss_3, double TakeProfit_3)

  {

   double   Open_Price  =  0;

   bool     LastIsBuy   =  false;

   int      TotalBuy   =  0;

   int      TotalSell  =  0;



   double Spread_1 = (double)SymbolInfoInteger(sparam, SYMBOL_SPREAD);



   double Ask_1 = SymbolInfoDouble(sparam, SYMBOL_ASK);

   double Bid_1 = SymbolInfoDouble(sparam, SYMBOL_BID);



   double iclose_1 = iClose(sparam, PERIOD_CURRENT, 1);





   for(int iCount = CalculateAllPositions(sparam) - 1; iCount >= 0; iCount--)

     {

      ulong Position_Ticket = PositionGetTicket(iCount);

      if(PositionSelectByTicket(Position_Ticket))

        {

         int Position_Type = (int)PositionGetInteger(POSITION_TYPE);

         string check_Symbol = PositionGetString(POSITION_SYMBOL);

         long magic = PositionGetInteger(POSITION_MAGIC);

         double open_price = PositionGetDouble(POSITION_PRICE_OPEN);



         if(Position_Type == POSITION_TYPE_BUY && check_Symbol == sparam && magic == magic_2)

           {



            if(Open_Price == 0)

              {

               Open_Price = open_price;

              }

            if(Open_Price > open_price)

              {

               Open_Price = open_price;

              }



            LastIsBuy = true;

            TotalBuy++;



            if(TotalBuy == MaxTrade)

              {

               Print("Maximum Trade is reached");

               return(0);

              }



           }



         if(Position_Type == POSITION_TYPE_SELL && check_Symbol == sparam && magic == magic_2)

           {



            if(Open_Price == 0)

              {

               Open_Price = open_price;

              }

            if(Open_Price < open_price)

              {

               Open_Price = open_price;

              }



            LastIsBuy = false; // Next Sell won't Trade

            TotalSell++;



            if(TotalSell == MaxTrade)

              {

               Print("Maximum Trade is reached");

               return(0);

              }



           }

        }

     }



   /* If the Price is downtrend to Buy Order, check the Bid */

   if(LastIsBuy)

     {



      if(Bid_1 <= Open_Price - (Spread_1 * point) - (PipStep * point))



        {





         if(MA[0] < Ask_1 && iclose_1 > MA[0])

           {

            Buy(sparam, magic_2, Ask_1, StopLoss_3, TakeProfit_3);

           }



         LastIsBuy = false;

         return(0);

        }

     }



   /* If the direction is Uptrend to Sell Order, check the value of Ask */

   else

      if(!LastIsBuy)

        {



         if(Ask_1 >= Open_Price + (Spread_1 * point) + (PipStep * point))



           {

            if(MA[0] > Bid_1 && iclose_1 < MA[0])

              {

               Sell(sparam, magic_2, Bid_1, StopLoss_3, TakeProfit_3);

              }



            return(0);

           }

        }

   return(0);

  }
 

Attachments

  • Screenshot (19).png
    Screenshot (19).png
    82 KB · Views: 2
  • Screenshot (19)_LI.jpg
    Screenshot (19)_LI.jpg
    656.3 KB · Views: 1

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

Similar threads

Users Who Are Viewing This Thread (Total: 2, Members: 0, Guests: 2)

Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock    No Thanks