Change Chart Type _ HOTKEY ... help please!

To add comments, please log in or register
aphong
122
aphong  

Hi all pro coders,

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 it should works 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 :)

//------------------------------------------------------------------
#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);
}
aphong
122
aphong  
aphong:

Hi all pro coders,

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 works 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 :)

little Bug fixed! ... version :  ChartType_HotKey  C

// ChartType.HotKey
//............................
#property indicator_chart_window
#define C 67

extern bool indicator_ON = true;

int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
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);
  }
 ///
void OnChartEvent(const int id,const long& lparam,const double& dparam,const string& sparam)
  {
   if(id == CHARTEVENT_KEYDOWN && lparam == C) 
   {press_C();}
   
  }
///////
void press_C()
{
//
  if (indicator_ON)
  {
   updateCHARTTYPE();
   ChartRedraw(0);
  }                              
}
///////
void updateCHARTTYPE ()
{
long     Chart_ID       = ChartID();
//
double CHARTTYPE = ChartGetInteger(Chart_ID,CHART_MODE);
if          (CHARTTYPE !=1) ChartSetInteger(Chart_ID,CHART_MODE,1); 
else if     (CHARTTYPE !=0) ChartSetInteger(Chart_ID,CHART_MODE,0);
}
aphong
122
aphong  

another cool Toy for those who like!  version :  ChartType_Button

// ChartType.Button
//...................
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0
  
input string  Button_Text     = ":)";             // Button Text
input int     XShift          = 20;               // Horizontal shift
input int     YShift          = 20;               // Vertical shift
input int     XSize           = 30;               // Width of buttons
input int     YSize           = 20;               // Height of buttons
input int     FSize           = 10;               // Fort size
input color   Bcolor          = clrGainsboro;     // Button color
input color   Dcolor          = clrDarkGray;      // Button border color
input color   Tncolor         = clrBlack;         // Text color - normal
input color   Sncolor         = clrRed;           // Text color - selected
input bool    Transparent     = false;            // Transparent buttons?
input ENUM_BASE_CORNER corner = CORNER_LEFT_UPPER;// Chart corner

//
//
//
//
//

int  OnInit() { createButton("CHARTTYPE",Button_Text,XShift+10,YShift+10,corner); return(INIT_SUCCEEDED); }
void OnDeinit(const int reason)
{ 
   switch(reason)
   {
      case REASON_CHARTCHANGE :
      case REASON_RECOMPILE   :
      case REASON_CLOSE       : break;
      default : ObjectDelete(0,"CHARTTYPE");
   }
}
void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam)
{
   if (id==CHARTEVENT_OBJECT_CLICK && ObjectGetInteger(0,sparam,OBJPROP_TYPE)==OBJ_BUTTON)
   {
      if (StringFind(sparam,"CHARTTYPE",0)==0)
      {
         int set = !ChartGetInteger(0,CHART_MODE,CHART_BARS);
            ChartSetInteger(0,CHART_MODE,CHART_BARS,set);
            ChartSetInteger(0,CHART_MODE,CHART_CANDLES,set);
            ChartRedraw(0);
      }
   }      
}     
void createButton(string name, string caption, int xpos, int ypos, int corn)
{
       ObjectCreate(0,name,OBJ_BUTTON,0,0,0);
          ObjectSetInteger(0,name,OBJPROP_CORNER,corn);
          ObjectSetInteger(0,name,OBJPROP_XDISTANCE,xpos);
          ObjectSetInteger(0,name,OBJPROP_YDISTANCE,ypos);
          ObjectSetInteger(0,name,OBJPROP_XSIZE,XSize);
          ObjectSetInteger(0,name,OBJPROP_YSIZE,YSize);
          ObjectSetString(0,name,OBJPROP_TEXT,caption);
              ObjectSetInteger(0,name,OBJPROP_FONTSIZE,FSize);
              ObjectSetInteger(0,name,OBJPROP_BORDER_TYPE,BORDER_FLAT);
              ObjectSetInteger(0,name,OBJPROP_COLOR,Tncolor); 
              ObjectSetInteger(0,name,OBJPROP_BGCOLOR,Bcolor); 
              ObjectSetInteger(0,name,OBJPROP_BACK,Transparent); 
              ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR,Dcolor); 
              ObjectSetInteger(0,name,OBJPROP_STATE,false);
              ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);
}

//
//
//
//
//

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);
}
William Roeder
19849
William Roeder  
aphong: Could anyone please find BUGs in my simple indi? 
#define C 67
   if(id == CHARTEVENT_KEYDOWN && lparam == C) 
   {press_C();}
Hard coding values is never good practice. Unnecessary use of macros is never good practice. Try:
const char C='C';
   if(id == CHARTEVENT_KEYDOWN && lparam == C) 
   {press_C();}

or:

input string C="C";
   if(id == CHARTEVENT_KEYDOWN && lparam == StringGetChar(C,0)) 
   {press_C();}
aphong
122
aphong  
William Roeder:
Hard coding values is never good practice. Unnecessary use of macros is never good practice. Try:

or:

Great! ... thank you so much for your code ... never known that before!

have a good weekend Sir! :)

To add comments, please log in or register