Attachments forums

List of attachments posted on this forum.


All files on forums: 161137

Re: Coding Help

mrtools, Sun Aug 04, 2019 4:49 pm

aphong wrote: Sun Aug 04, 2019 2:07 pm Hi all pro coders,
Im new to mql
Im trying to code an simple indicator to quickly change chart type between bar charts & candlestick chart when press C key... but something weird happened, my indi seems only work when there is a tick comming, i think i should work right away after I press C key! Dont know what is the problem here? ... one more thing, I notice that when I press C key then the bars/ candlestick icon is changed immediately but the display on main chart is not =))

Could anyone please find BUGs in my simple indi? attached is the codes

thanks & have a good weekend :)

Code: Select all

//------------------------------------------------------------------
#property indicator_chart_window
#define C 67

extern bool indicator_ON = true;

double CHARTTYPE;

int OnInit()
  {
   CHARTTYPE = getCHARTTYPE_glovar();
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   if (reason==REASON_REMOVE) GlobalVariableDel("ChangeChartType"+ Symbol()+string(ChartID()));
  }
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[])
  {
   if (indicator_ON) updateCHARTTYPE(CHARTTYPE);
   return(rates_total);
  }
 ///
void OnChartEvent(const int id,const long& lparam,const double& dparam,const string& sparam)
  {
   
   if(id == CHARTEVENT_KEYDOWN && lparam == C) 
   {press_C();}
  }
//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
double getCHARTTYPE_glovar()
{
double result;
//
   if (indicator_ON) 
   {
         if (GlobalVariableCheck("ChangeChartType"+ Symbol()+string(ChartID())))
         {
            if (  (GlobalVariableGet("ChangeChartType"+ Symbol()+string(ChartID())) == 0)         
               || (GlobalVariableGet("ChangeChartType"+ Symbol()+string(ChartID())) == 1)  )
            {result = GlobalVariableGet("ChangeChartType"+ Symbol()+string(ChartID()));} 
         }      
         else
         {  
            result = 1;
            GlobalVariableSet("ChangeChartType"+ Symbol()+string(ChartID()),1);// Creat & set initial value for global variable
         }
   }
return(result);
}
///////
void press_C()
{
double   charttype      = GlobalVariableGet("ChangeChartType"+ Symbol()+string(ChartID()));
//
              if (indicator_ON)
              {
               if(charttype ==0)
                 {
                  CHARTTYPE = 1;
                  GlobalVariableSet("ChangeChartType"+ Symbol()+string(ChartID()),1);
                  //
                 }
               else if(charttype ==1)
                 {
                  CHARTTYPE = 0;
                  GlobalVariableSet("ChangeChartType"+ Symbol()+string(ChartID()),0);
                  //
                 }
               }
//              
updateCHARTTYPE(CHARTTYPE);                                
}
///////
void updateCHARTTYPE (double ChartType)
{
long     Chart_ID       = ChartID();
//
ChartSetInteger(Chart_ID,CHART_MODE,ChartType);
}
Maybe this code will help with it you can change from bars to candles, with an on chart button.
All files in topic