Button On Chart Want To Make It Available For Auto Trades Not Just Manual Trades

To add comments, please log in or register
CloudS JumperZ
67
CloudS JumperZ  

Hello all I'm new here this is my first topic, I already find all information about the subject in google so on in forex forum but I didn't find the right one can you all help me?

Below is my code I want to make it available to close auto trades like EA in EA not just manual trades.


input bool RunOnCurrentCurrencyPair = true;

input bool CloseOnlyManualTrades = true;

input bool DeletePendingOrders = true;

input int  MaxSlippage = 5;




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

//| Expert initialization function                                   |

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

int OnInit()

  {

//--- 

   

   ObjectCreate      (0,"CloseAll_btn",OBJ_BUTTON,0,0,0);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_XDISTANCE,450);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_YDISTANCE,5);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_XSIZE,100);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_YSIZE,50);


   ObjectSetString(0,"CloseAll_btn",OBJPROP_TEXT,"Close All");

      

   

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_COLOR, White);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_BGCOLOR, Red);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_BORDER_COLOR,Red);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_BORDER_TYPE,BORDER_FLAT);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_BACK,false);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_HIDDEN,true);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_STATE,false);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_FONTSIZE,12);


//---

   return;

  }

  

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---


   ObjectDelete(0,"CloseAll_btn");

   

  }

  

  

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

      

  }

  

  

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

//| ChartEvent function                                              |

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

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

//---

            

   if(sparam== "CloseAll_btn")

      {

      if(RunOnCurrentCurrencyPair == true && CloseOnlyManualTrades == true) CloseAll(DeletePendingOrders,MaxSlippage);

        

      ObjectSetInteger(0,"CloseAll_btn",OBJPROP_STATE,false);    

      }

         

//---      

  }

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



void CloseAll (bool boolPendingOrders, int intMaxSlippage)

  {   

   int ticket;

   if (OrdersTotal() == 0) return;

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

      {

       if (OrderSelect (i, SELECT_BY_POS, MODE_TRADES) == true)

         {

          if (OrderType() == OP_BUY)

            {

             ticket = OrderClose (OrderTicket(), OrderLots(), MarketInfo (OrderSymbol(), MODE_BID), 3, CLR_NONE);

             if (ticket == -1) Print ("Error: ", GetLastError());

             if (ticket >   0) Print ("Position ", OrderTicket() ," closed");

            }

          if (OrderType() == OP_SELL)

            {

             ticket = OrderClose (OrderTicket(), OrderLots(), MarketInfo (OrderSymbol(), MODE_ASK), 3, CLR_NONE);

             if (ticket == -1) Print ("Error: ",  GetLastError());

             if (ticket >   0) Print ("Position ", OrderTicket() ," closed");            

            }   

          if (OrderType() == OP_BUYLIMIT)

            {

             ticket = OrderDelete (OrderTicket(), CLR_NONE);

             if (ticket == -1) Print ("Error: ", GetLastError());

             if (ticket >   0) Print ("Position ", OrderTicket() ," closed");

            }

          if (OrderType() == OP_SELLLIMIT)

            {

             ticket = OrderDelete (OrderTicket(), CLR_NONE);

             if (ticket == -1) Print ("Error: ", GetLastError());

             if (ticket >   0) Print ("Position ", OrderTicket() ," closed");

            }           

         }

      }

   return;

  }

      

_____________________________________________________________________________________________________________________________________________________________________________________

      


nicholi shen
2392
nicholi shen  
CloudSJumperZ:

Hello all I'm new here this is my first topic, I already find all information about the subject in google so on in forex forum but I didn't find the right one can you all help me?

Below is my code I want to make it available to close auto trades like EA in EA not just manual trades.

You'd want to check that the chart event is object click, the object name is your button, and the confirm the button state. You also need to adjust your logic to check the input var criteria. Also you can massively reduce the amount of code by simplifying it to just... 

#property strict

input bool RunOnCurrentCurrencyPair = true;
input bool CloseOnlyManualTrades    = true;
input bool DeletePendingOrders      = true;
input int  MaxSlippage              = 5;

#include <stdlib.mqh>
#include <ChartObjects\ChartObjectsTxtControls.mqh>
CChartObjectButton b;
//+------------------------------------------------------------------+
int OnInit()
{
   if(!b.Create(0,"__close__",0,450,5,100,50) 
   || !b.Color(clrWhite)
   || !b.BackColor(clrRed)
   || !b.FontSize(12)
   || !b.Description("Close All")
   )
      return INIT_FAILED;
   return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   if(id==CHARTEVENT_OBJECT_CLICK && sparam==b.Name() && b.State())
   {
      closeall();
      b.State(false);
   }
}
//+------------------------------------------------------------------+
void OnTick(){}

void closeall()
{
   for(int i=OrdersTotal()-1;i>=0;i--)
   {
      if(OrderSelect(i,SELECT_BY_POS)
      &&(!RunOnCurrentCurrencyPair || OrderSymbol()==_Symbol)
      &&(!CloseOnlyManualTrades || OrderMagicNumber()<=0)
      ){
         if(OrderType()<2)
         {
            RefreshRates();
            if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),MaxSlippage))
               Print("OrderCloseError: ", ErrorDescription(GetLastError()));
         }
         else if(DeletePendingOrders)
            if(!OrderDelete(OrderTicket()))
               Print("OrderCloseError: ", ErrorDescription(GetLastError()));
      }
   }
}

Finally, use the code format button when posting code!!! ;)

Alain Verleyen
38661
Alain Verleyen  
Emma Schwatson:

You'd want to check that the chart event is object click, the object name is your button, and the confirm the button state. You also need to adjust your logic to check the input var criteria. Also you can massively reduce the amount of code by simplifying it to just... 

Finally, use the code format button when posting code!!! ;)

      &&(!CloseOnlyManualTrades || OrderMagicNumber()<=0)

That's a little bug. Manual magic is 0 only. Negative magic can be set by EA.

nicholi shen
2392
nicholi shen  
Alain Verleyen:

That's a little bug. Manual magic is 0 only. Negative magic can be set by EA.

...or feature... sometimes it's nice to reserve negative integer magics for EA you want to always manually override. 

CloudS JumperZ
67
CloudS JumperZ  
Emma Schwatson:

You'd want to check that the chart event is object click, the object name is your button, and the confirm the button state. You also need to adjust your logic to check the input var criteria. Also you can massively reduce the amount of code by simplifying it to just... 

Finally, use the code format button when posting code!!! ;)

Sorry for late reply

Thanks for the code I already test it only on its own and executed has the button and can't close manual trade it is good but I can't test it if auto trades. I already put it on my own EA but nothing button on chart and even dependencies in the input EA and I already too using my code to create the button in below code and I get the button on chart but if I trade using my EA and want close the auto pending orders nothing happen IDK what to do now and 1 more thing if in void onInit return a value it is able or not or just warning not errors?

ObjectCreate      (0,"CloseAll_btn",OBJ_BUTTON,0,0,0);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_XDISTANCE,450);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_YDISTANCE,5);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_XSIZE,100);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_YSIZE,50);



   ObjectSetString(0,"CloseAll_btn",OBJPROP_TEXT,"Close All");

      

   

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_COLOR, White);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_BGCOLOR, Red);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_BORDER_COLOR,Red);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_BORDER_TYPE,BORDER_FLAT);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_BACK,false);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_HIDDEN,true);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_STATE,false);

   ObjectSetInteger(0,"CloseAll_btn",OBJPROP_FONTSIZE,12);

William Roeder
20207
William Roeder  
When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your posts.
          General rules and best pratices of the Forum. - General - MQL5 programming forum
          Messages Editor
CloudS JumperZ
67
CloudS JumperZ  

Sorry again for late reply

Where can I edit that comment? I only use mobile browsers

William Roeder
20207
William Roeder  
You can only edit posts for a few days.
Keith Watford
Moderator
20339
Keith Watford  

I have edited your post to show your code in the code box.

Please remember next time.

CloudS JumperZ
67
CloudS JumperZ  
whroeder1:
You can only edit posts for a few days.

Ohh I see thanks for info

CloudS JumperZ
67
CloudS JumperZ  
Keith Watford:

I have edited your post to show your code in the code box.

Please remember next time.

Thanks Keith, Sorry I'm so newbie

12
To add comments, please log in or register