Download MetaTrader 5
To add comments, please log in or register
Added support for the Spanish language. Come and check it out!
Black_Jack822
57
Black_Jack822 2015.10.02 12:55 

Hi all. I've made an on chart button which will run a specific indicator and remove it when released. Can somebody tell me please what is missing to keep the button pressed when I switch between Time Frames?


#property indicator_chart_window
extern bool value_to_toggle = false; 
string sButtonName = "Fibo generator";

#import "user32.dll"
void keybd_event(int bVk, int bScan, int dwFlags,int dwExtraInfo);

#define  VK_G 0x47               //--- letter G
#define  VK_MENU 0x12            //--- Alt key  
#define  KEYEVENTF_KEYUP 0x0002  //--- key is released (not pressed)
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   Toggle();
   return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
{
   ObjectDelete(sButtonName);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   if(sparam==sButtonName) Toggle(); 
  }
//+------------------------------------------------------------------+
void Toggle()
{
   color cColor  = Gray;
   ChartIndicatorDelete(0,0,"Fibo generator");
   
   if(value_to_toggle == true)
   {
      PlaySound("Alert.wav");
      cColor = Blue;
      
  keybd_event(VK_MENU, 0, 0, 0);                   //--- press Alt key
  keybd_event(VK_G, 0, 0, 0);                      //--- press G key

  keybd_event(VK_G, 0, KEYEVENTF_KEYUP, 0);        //--- release G key
  keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);     //--- release Alt key
      
   }
   CreateButton(sButtonName,cColor,"Arial",0);
   value_to_toggle = !value_to_toggle;
   
}
void CreateButton(string sName, color cColor, string sFont = "Arial", string sText = "")
{
   if(ObjectFind(sName)< 0)
   
   ObjectCreate(0,sName,OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,sName,OBJPROP_XDISTANCE,63);
   ObjectSetInteger(0,sName,OBJPROP_YDISTANCE,0);
   ObjectSetInteger(0,sName,OBJPROP_XSIZE,25);
   ObjectSetInteger(0,sName,OBJPROP_YSIZE,25);
   ObjectSetInteger(0,sName,OBJPROP_CORNER,4);
   ObjectSetString(0,sName,OBJPROP_TEXT,"FG");
   ObjectSetInteger(0,sName,OBJPROP_COLOR, cColor);
   ObjectSetString(0,sName,OBJPROP_FONT,sFont);
   ObjectSetInteger(0,sName,OBJPROP_FONTSIZE,9);

}
Black_Jack822
57
Black_Jack822 2015.10.04 12:12  
Black_Jack822:

Hi all. I've made an on chart button which will run a specific indicator and remove it when released. Can somebody tell me please what is missing to keep the button pressed when I switch between Time Frames?


I've done it :)Topic closed.
Stuart Browne
5711
Stuart Browne 2015.10.05 00:43  
Black_Jack822:
I've done it :)Topic closed.
Would be nice if you could post your solution in case others have a similar problem
/
To add comments, please log in or register