Hello Friends! what i'm doing wrong? please help me..

To add comments, please log in or register
Tolga TANIS
30
Tolga TANIS  

Thank you already for helping!!

 i cant modify pending orders..

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//

//|                        Copyright 2017, MetaQuotes Software Corp.                        |//

//|                                 https://www.mql5.com                                    |//

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxXXXXXXXX//


#property copyright "Copyright 2017, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

extern int Tral_Stop=50;

int buys=0;

int sells=0;

int buystops=0;

int sellstops=0;

double buystopprice;

double sellstopprice;

int Ticket;

  




//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//

//XXXXXXXXXXXXXXXX                   START                 XXXXXXXXXXXXXXXX//

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//


int start()  {

  

      double high=iHigh(Symbol(),PERIOD_CURRENT,iHighest(Symbol(),PERIOD_CURRENT,MODE_HIGH,4,1));

      double low =iLow(Symbol(),PERIOD_CURRENT,iLowest(Symbol(),PERIOD_CURRENT,MODE_LOW,4,1));

      

      buystopprice=low+50*Point;

      sellstopprice=high-50*Point;

    

           if (OpenOrdersCount(Symbol())==0 && OpenPendingOrdersCount(Symbol())==0)

               {

               OpenBuySellStop();    //if there is no order open one BUYSTOP and one SELLSTOP on high and low level

               }


           else if ( OpenOrdersCount(Symbol())==-1 &&  OpenPendingOrdersCount(Symbol())==1 && OrderType()==OP_BUYSTOP)  

                {

                ModifyBuyStop(); // if one of them became ORDER and if there is one SELL ORDER and one BUYSTOP, then modify BUYSTOP price to new buystopprice

                }

                

           else if ( OpenOrdersCount(Symbol())==1 &&  OpenPendingOrdersCount(Symbol())==1 && OrderType()==OP_SELLSTOP)  

                {

                ModifySellStop();// if one of them became ORDER and if there is one BUY ORDER and one SELLSTOP, then modify SELLSTOP price to new sellstopprice

                }    

    

       TrailingStop();

    


//Comment( "Open Orders = ",OpenOrders(Symbol())+" Open Pending = "+OpenPendingOrders(Symbol())+" new price = "+buystopprice);


return(0);  }





//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//

//XXXXXXXXXXXXXXXX           OPEN FIRST ORDERS             XXXXXXXXXXXXXXXX//

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//

  

  void OpenBuySellStop()

{

      double high=iHigh(Symbol(),PERIOD_CURRENT,iHighest(Symbol(),PERIOD_CURRENT,MODE_HIGH,3,1));

      double low =iLow(Symbol(),PERIOD_CURRENT,iLowest(Symbol(),PERIOD_CURRENT,MODE_LOW,3,1));

      double BuyStopLoss=high;

      double SellStopLoss=low;


      int a=OrderSend(Symbol(),OP_BUYSTOP,0.1, high+50*Point,3,high-20*Point,0,NULL,0,0,clrBlue);

      int b=OrderSend(Symbol(),OP_SELLSTOP,0.1, low-50*Point,3,low+20*Point,0,NULL,0,0,  clrRed);

}




//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//

//XXXXXXXXXXXXXXXX        CHECK ALL OPEN ORDERS            XXXXXXXXXXXXXXXX//

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//


int OpenOrdersCount(string open)

{

     buys=0;

     sells=0;

  

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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;

        if(OrderSymbol()==Symbol())

        {

         if(OrderType()==OP_BUY)  buys++;

         if(OrderType()==OP_SELL) sells++;

        }

     }  

//--- return orders volume

   if(buys>0) return(buys);

   else       return(-sells);

    

}

  

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//

//XXXXXXXXXXXXXXXX     CHECK ALL PENDING ORDERS            XXXXXXXXXXXXXXXX//

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//


int OpenPendingOrdersCount(string pending)

  {

    buystops=0;

    sellstops=0;



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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;

        {

         if(OrderType()==OP_BUYSTOP)  buystops++;

         if(OrderType()==OP_SELLSTOP) sellstops++;

        }

     }  

//--- return orders volume

   if (buystops==1 && sellstops==1) return(2);

   else if(buystops>0) return(buystops);

   else       return(-sellstops);

    

  }  

  



//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//

//XXXXXXXXXXXXXXXX      MODIFY PENDING BUYSTOP ORDER       XXXXXXXXXXXXXXXX//

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//


int ModifyBuyStop()



   {

    bool   result;

    int    total;



   total=OrdersTotal();

   double point=MarketInfo(Symbol(),MODE_POINT);

   double buystopstoploss=buystopprice-50*point;

  

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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

        {

                  

         if(OrderType()==OP_BUYSTOP)

           {

            

            OrderPrint();

            //---- modify first pending order

          

            result=OrderModify(OrderTicket(),buystopprice,buystopstoploss,0,0,CLR_NONE);

            if(result!=TRUE) Print("LastError = ", GetLastError());

            //---- print modified order (it still selected after modify)

            else if(OrderSelect(OrderTicket(),SELECT_BY_TICKET)) OrderPrint();

            break;

           }

        }

      else { Print( "Error when order select ", GetLastError()); break; }

     }

//----

   return(0);

  

   }



//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//

//XXXXXXXXXXXXXXXX      MODIFY PENDING SELLSTOP ORDER      XXXXXXXXXXXXXXXX//

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//


  

  int ModifySellStop()



   {

    bool   result;

    int    total;



//----

   total=OrdersTotal();

   double point=MarketInfo(Symbol(),MODE_POINT);

   double sellstopstoploss=sellstopprice+50*point;

  

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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

        {

                  

         if(OrderType()==OP_BUYSTOP)

           {

            

            OrderPrint();

            //---- modify first pending order

          

            result=OrderModify(OrderTicket(),sellstopprice,sellstopstoploss,0,0,CLR_NONE);

            if(result!=TRUE) Print("LastError = ", GetLastError());

            //---- print modified order (it still selected after modify)

            else if(OrderSelect(OrderTicket(),SELECT_BY_TICKET)) OrderPrint();

            break;

           }

        }

      else { Print( "Error when order select ", GetLastError()); break; }

     }

//----

   return(0);

  

   }

  

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//

//XXXXXXXXXXXXXXXX              TRAILING STOP              XXXXXXXXXXXXXXXX//

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//



    int TrailingStop()                                     // Special function 'start'

  {

   string Symb=Symbol();  

   string Text;                     // Symbol

//------------------------------------------------------------------------------- 2 --

   for(int i=1; i<=OrdersTotal(); i++)          // Cycle searching in orders

     {

      if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available

        {                                       // Analysis of orders:

         int Tip=OrderType();                   // Order type

         if(OrderSymbol()!=Symb||Tip>1)continue;// The order is not "ours"

         double SL=OrderStopLoss();             // SL of the selected order

         //---------------------------------------------------------------------- 3 --

         while(true)                            // Modification cycle

           {

            double TS=Tral_Stop;                // Initial value

            double Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL);//Min. distance

            if (TS < Min_Dist)                  // If less than allowed

               TS=Min_Dist;                     // New value of TS

            //------------------------------------------------------------------- 4 --

            bool Modify=false;                  // Not to be modified

            switch(Tip)                         // By order type

              {

               case 0 :                         // Order Buy

                  if (NormalizeDouble(SL,Digits)< // If it is lower than we want

                     NormalizeDouble(Bid-TS*Point,Digits))

                    {

                     SL=Bid-TS*Point;           // then modify it

                       Text="Buy ";        // Text for Buy

                     Modify=true;               // To be modified

                    }

                  break;                        // Exit 'switch'

               case 1 :                         // Order Sell

                  if (NormalizeDouble(SL,Digits)> // If it is higher than we want

                     NormalizeDouble(Ask+TS*Point,Digits)

                     || NormalizeDouble(SL,Digits)==0)//or equal to zero

                    {

                     SL=Ask+TS*Point;           // then modify it

                     Text="Sell ";              // Text for Sell

                     Modify=true;               // To be modified

                    }

              }                                 // End of 'switch'

            if (Modify==false)                  // If it is not modified

               break;                           // Exit 'while'

            //------------------------------------------------------------------- 5 --

            double TP    =OrderTakeProfit();    // TP of the selected order

            double Price =OrderOpenPrice();     // Price of the selected order

             Ticket=OrderTicket();        // Ticket of the selected order



            Alert ("Modification ",Text,Ticket,". Awaiting response..");

            bool Ans=OrderModify(Ticket,Price,SL,TP,0);//Modify it!

            

            //------------------------------------------------------------------- 6 --

            if (Ans==true)                      // Got it! :)

              {

               Alert ("Order ",Text,Ticket," is modified:)");

               break;                           // From modification cycle.

              }

            //------------------------------------------------------------------- 7 --

            int Error=GetLastError();           // Failed :(

            switch(Error)                       // Overcomable errors

              {

               case 130:Alert("Wrong stops. Retrying.");

                  RefreshRates();               // Update data

                  continue;                     // At the next iteration

               case 136:Alert("No prices. Waiting for a new tick..");

                  while(RefreshRates()==false)  // To the new tick

                     Sleep(1);                  // Cycle delay

                  continue;                     // At the next iteration

               case 146:Alert("Trading subsystem is busy. Retrying ");

                  Sleep(500);                   // Simple solution

                  RefreshRates();               // Update data

                  continue;                     // At the next iteration

                  // Critical errors

               case 2 : Alert("Common error.");

                  break;                        // Exit 'switch'

               case 5 : Alert("Old version of the client terminal.");

                  break;                        // Exit 'switch'

               case 64: Alert("Account is blocked.");

                  break;                        // Exit 'switch'

               case 133:Alert("Trading is prohibited");

                  break;                        // Exit 'switch'

               default: Alert("Occurred error ",Error);//Other errors

              }

            break;                              // From modification cycle

           }                                    // End of modification cycle

      

         //---------------------------------------------------------------------- 8 --

        }                                       // End of order analysis

     }                                          // End of order search

//------------------------------------------------------------------------------- 9 --

   return(0);  }




//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//

//XXXXXXXXXXXXXXXX                   END                   XXXXXXXXXXXXXXXX//

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//
Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
Carl Schreiber
Moderator
7132
Carl Schreiber  

Please use the SRC-button to post you code!

Please edit you post accordingly

Sergey Golubev
Moderator
106134
Sergey Golubev  
I edited his post.
whroeder1
17940
whroeder1  
Tolga TANIS i cant modify pending orders..
  1. Print out your variables, and find out why.
  2. Don't hide information from us? Your error code was what?
Marco vd Heijden
Moderator
8176
Marco vd Heijden  

  int ModifySellStop()



   {

    bool   result;

    int    total;



//----

   total=OrdersTotal();

   double point=MarketInfo(Symbol(),MODE_POINT); This is not the Point for the OrderSymbol() ! But for the Chart Symbol()

   double sellstopstoploss=sellstopprice+50*point;

  

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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

        {

                  

         if(OrderType()==OP_BUYSTOP)

           {

            double point=MarketInfo(OrderSymbol(),MODE_POINT); <---it has to be here inside the orderloop

            OrderPrint();

            //---- modify first pending order

          

            result=OrderModify(OrderTicket(),sellstopprice,sellstopstoploss,0,0,CLR_NONE);

            if(result!=TRUE) Print("LastError = ", GetLastError());

            //---- print modified order (it still selected after modify)

            else if(OrderSelect(OrderTicket(),SELECT_BY_TICKET)) OrderPrint();

            break;

           }

        }

      else { Print( "Error when order select ", GetLastError()); break; }

     }

//----

   return(0);

  

   }
To add comments, please log in or register