Button on Chart to trigger Script

 

Hi everyone,

I am playing around with buttons on the chart and I thought it would be good to have a button that moves the stoploss to breakeven.

What I want to do: I want to make an indicator that plots a button on my chart. When I push the button it triggers a script that moves my stop to break even.

I will send the ticket and the new position to the script so it knows which stop to move an where.


My question: Is there a way to trigger a script from an indicator? If yes, how?

Thanks for reading. If I can do it I will put it out so everyone can use it.

 

Short answer: No.

You'll need to code the button and the trade management into an EA and do it that way.

 
honest_knave:

Short answer: No.

You'll need to code the button and the trade management into an EA and do it that way.

Thanks, the short answer is allways the best. :-)
 
//+------------------------------------------------------------------+
//| Create the Buy button                                                |
//+------------------------------------------------------------------+
bool Create_BuyButton(const long              chart_ID=0,               // chart's ID
                      const string            name="Button_Buy",        // button name
                      const int               x=0,                      // X coordinate
                      const int               y=20,// Y coordinate
                      const int               width=60,                 // button width
                      const int               height=20,                // button height
                      const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring
                      const string            text="Buy",               // text
                      const string            font="Courier New",       // font
                      const int               font_size=10,             // font size
                      const color             clr=clrBlack,             // text color
                      const color             back_clr=clrGray,         // background color
                      const bool              back=false                // in the background
                      )
  {
//--- reset the error value
   ResetLastError();
//--- create the button
   ObjectCreate(chart_ID,name,OBJ_BUTTON,0,0,0);
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- successful execution
   return(true);
  }
//+------------------------------------------------------------------+
//| Create the Sell button                                                |
//+------------------------------------------------------------------+
bool Create_SellButton(const long              chart_ID=0,               // chart's ID
                       const string            name="Button_Sell",       // button name
                       const int               sub_window=0,             // subwindow index
                       const int               x=65,// X coordinate
                       const int               y=20,                     // Y coordinate
                       const int               width=60,                 // button width
                       const int               height=20,                // button height
                       const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring
                       const string            text="Sell",              // text
                       const string            font="Courier New",       // font
                       const int               font_size=10,             // font size
                       const color             clr=clrBlack,             // text color
                       const color             back_clr=clrGray,         // background color
                       const bool              back=false                // in the background
                       )
  {
   ResetLastError();
//--- create the button
   ObjectCreate(chart_ID,name,OBJ_BUTTON,sub_window,0,0);
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- successful execution
   return(true);
  }
//+------------------------------------------------------------------+
//| Create the Reverse button                                                |
//+------------------------------------------------------------------+
bool Create_ReverseButton(const long              chart_ID=0,               // chart's ID
                          const string            name="Button_Reverse",    // button name
                          const int               sub_window=0,             // subwindow index
                          const int               x=130,// X coordinate
                          const int               y=20,                     // Y coordinate
                          const int               width=60,                 // button width
                          const int               height=20,                // button height
                          const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring
                          const string            text="Reverse",           // text
                          const string            font="Courier New",       // font
                          const int               font_size=10,             // font size
                          const color             clr=clrBlack,             // text color
                          const color             back_clr=clrGray,         // background color
                          const bool              back=false                // in the background
                          )
  {
//--- reset the error value
   ResetLastError();
//--- create the button
   ObjectCreate(chart_ID,name,OBJ_BUTTON,sub_window,0,0);
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- successful execution
   return(true);
  }
//+------------------------------------------------------------------+
//| Create the Close button                                                |
//+------------------------------------------------------------------+
bool Create_CloseButton(const long              chart_ID=0,               // chart's ID
                        const string            name="Button_Close",      // button name
                        const int               sub_window=0,             // subwindow index
                        const int               x=195,// X coordinate
                        const int               y=20,// Y coordinate
                        const int               width=60,                 // button width
                        const int               height=20,                // button height
                        const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring
                        const string            text="Close",             // text
                        const string            font="Courier New",       // font
                        const int               font_size=10,             // font size
                        const color             clr=clrBlack,             // text color
                        const color             back_clr=clrGray,         // background color
                        const bool              back=false                // in the background
                        )
  {
//--- reset the error value
   ResetLastError();
//--- create the button
   ObjectCreate(chart_ID,name,OBJ_BUTTON,sub_window,0,0);
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- successful execution
   return(true);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void PlaceLongOrder()
  {
//-------------------------------------------------------------------------------------------------
   LongOrder_Open=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"",0);
//Error Control
   if(GetLastError()>=3){Alert("Buy ..Error  : "+ErrorDescription(GetLastError()));}
//-------------------------------------------------------------------------------------------------
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void PlaceShortOrder()
  {
//-------------------------------------------------------------------------------------------------
   ShortOrder_Open=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"",0);
//Error Control
   if(GetLastError()>=3){Alert("Buy ..Error  : "+ErrorDescription(GetLastError()));}
//-------------------------------------------------------------------------------------------------
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseOrder()
  {
//Close Long Order
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
           {
            if(OrderType()==OP_BUY)
              {
               //-------------------------------------------------------------------------------------------------
               double CloseBuy=OrderClose(OrderTicket(),OrderLots(),Bid,0);
               //-------------------------------------------------------------------------------------------------
               ChartSetInteger(0,CHART_COLOR_FOREGROUND,clrSteelBlue);
               //-------------------------------------------------------------------------------------------------
              }
            if(OrderType()==OP_SELL)
              {
               //-------------------------------------------------------------------------------------------------
               double CloseSell=OrderClose(OrderTicket(),OrderLots(),Ask,0);
               //-------------------------------------------------------------------------------------------------
               ChartSetInteger(0,CHART_COLOR_FOREGROUND,clrSteelBlue);
               //-------------------------------------------------------------------------------------------------
              }
           }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void ReverseOrder()
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
           {
            if(OrderType()==OP_BUY)
              {
               //------------------------------------------------------------------------------------------------- 
               //Close Long Order
               double CloseLong_Rev=OrderClose(OrderTicket(),OrderLots(),Bid,0);
               //------------------------------------------------------------------------------------------------- 
               //Open New Short Order with Risk Management setting LotSize
               double OpenShort_Rev=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"",0);
               //------------------------------------------------------------------------------------------------- 
              }
            if(OrderType()==OP_SELL)
              {
               //------------------------------------------------------------------------------------------------- 
               //Close Short Order
               double CloseShort_Rev=OrderClose(OrderTicket(),OrderLots(),Ask,0);
               //------------------------------------------------------------------------------------------------- 
               //Open New Long Order with Risk Management setting LotSize
               double OpenLong_Rev=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"",0);
               //------------------------------------------------------------------------------------------------- 
              }
           }
      //Error Control
      Print("ReverseOrder Error  : "+ErrorDescription(GetLastError()));
     }
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   if(id==CHARTEVENT_OBJECT_CLICK)
     {
      if(sparam=="Button_Buy")
        {
         PlaceLongOrder();
         ObjectSetInteger(0,"Button_Buy",OBJPROP_STATE,false);
        }
      if(sparam=="Button_Sell")
        {
         PlaceShortOrder();
         ObjectSetInteger(0,"Button_Sell",OBJPROP_STATE,false);
        }
      if(sparam=="Button_Close")
        {
         CloseOrder();
         ObjectSetInteger(0,"Button_Close",OBJPROP_STATE,false);
        }
      if(sparam=="Button_Reverse")
        {
         ReverseOrder();
         ObjectSetInteger(0,"Button_Reverse",OBJPROP_STATE,false);
        }
     }
  }
MBeck
:

Thanks, the short answer is allways the best. :-)

you cant trigger a script but you can code a button to do whatever you want to do.

i have  open/close/reverse buttons in my ea, one day i will get around to making a button for pending orders.

a button for moving a stoploss to breakeven, or even trailing a stoploss for that matter is possible.

all the info you need and the info i used is in the documentation and a certain search engine.

good luck 

 
Donald Gibson:

you cant trigger a script but you can code a button to do whatever you want to do.

i have  open/close/reverse buttons in my ea, one day i will get around to making a button for pending orders.

a button for moving a stoploss to breakeven, or even trailing a stoploss for that matter is possible.

all the info you need and the info i used is in the documentation and a certain search engine.

good luck 


Hi,
And is it possible to hide show arrow symbol by button (for example) with true/false bool, rather than always open the indicator pop-up (CRTL+I) ?

Can you give me simple and short example please ?

Regards.

 
Hello, honestly it can not be done but if you can code a button to do other things. 
 
Thierry Ramaniraka #:

Hi,
And is it possible to hide show arrow symbol by button (for example) with true/false bool, rather than always open the indicator pop-up (CRTL+I) ?

Can you give me simple and short example please ?

Regards.

Filterthis:

Hi everyone,

I am playing around with buttons on the chart and I thought it would be good to have a button that moves the stoploss to breakeven.

What I want to do: I want to make an indicator that plots a button on my chart. When I push the button it triggers a script that moves my stop to break even.

I will send the ticket and the new position to the script so it knows which stop to move an where.


My question: Is there a way to trigger a script from an indicator? If yes, how?

Thanks for reading. If I can do it I will put it out so everyone can use it.

Yes, you can call a script from an indicator by a button.
You need to assign a keyboard shortcut to the script and in the button you need to emulate the keyboard events.