MQL4一键切换所有图表

  • Post author:
  • Post category:其他


#MT4一键切换所有图表功能,源码如下:

//+------------------------------------------------------------------+
//|                                                 symbolSwitch.mq4 |
//|                                                         银河债王 |
//|                                    https://blog.csdn.net/iop02008|
//+------------------------------------------------------------------+
#property copyright "银河债王"
#property link      "https://blog.csdn.net/iop02008"
#property version   "1.00"
#property strict
input string inSymbols="XAUUSD,WTI";//品种
input color inColor=clrWhite;//字体颜色
//input int inDecimal=3;//小数位
input int inSize=13;//字体大小
input color buttonColor=clrRed;//按钮颜色
input color buttonFontColor=clrBlack;//按钮字体颜色
int Xpointer=0;


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnInit()
  {

//ObjectsDeleteAll(0);
   Xpointer=0;
   string symbols[];
   StringSplit(inSymbols,',',symbols);
   for(int i=0; i<ArraySize(symbols); i++)
     {
      ButtonCreate(symbols[i]);

     }
   SetPrice();

  }

//+------------------------------------------------------------------+
//|             事件列表                                                     |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
//Print("id",id);
//Print("lparam",lparam);
//Print("dparam",dparam);
//Print("sparam:\t",sparam);
//string text;
//Print("CHARTEVENT_CLICK:",CHARTEVENT_CLICK);

//Print(text);
   buttonProcess(sparam);
  }



//+------------------------------------------------------------------+
//|        按钮处理函数                                                          |
//+------------------------------------------------------------------+
void buttonProcess(string buttonName)
  {
   SymbolSet(buttonName);
   buttonRest(buttonName);

   /*
   if(buttonName == "XAUUSD"){
      SymbolSet("XAUUSD");
   }else if(buttonName == "OIL-FEB20"){
      SymbolSet("OIL-FEB20");
   }
   */

  }

//+------------------------------------------------------------------+
//|        按钮创建                                                          |
//+------------------------------------------------------------------+
void ButtonCreate(string name)
  {
   Xpointer+=50;
   ObjectCreate(name,OBJ_BUTTON,0,0,0);
   ObjectSetString(0,name,OBJPROP_TEXT,name);
//ObjectSetInteger(0,name,OBJPROP_XSIZE,100);
//ObjectSetInteger(0,name,OBJPROP_YSIZE,50);
   SetSize(name,80,50);
   SetXY(name,0,Xpointer);
   ObjectSetInteger(0,name,OBJPROP_COLOR,buttonFontColor);
   ObjectSetInteger(0,name,OBJPROP_BGCOLOR,buttonColor);//C'220,20,60'

//标签相关
   ObjectCreate(name+"-price",OBJ_LABEL,0,0,0);
   ObjectSetInteger(0,name+"-price",OBJPROP_XDISTANCE,80);
   ObjectSetInteger(0,name+"-price",OBJPROP_YDISTANCE,Xpointer+15);
//--- 设置文本字体
   ObjectSetString(0,name+"-price",OBJPROP_FONT,"微软雅黑");
//--- 设置字体大小
   ObjectSetInteger(0,name+"-price",OBJPROP_FONTSIZE,inSize);
//--- 设置颜色
   ObjectSetInteger(0,name+"-price",OBJPROP_COLOR,inColor);
   
   //禁止选择标签
   ObjectSetInteger(0,name+"-price",OBJPROP_SELECTABLE,false);
   ObjectSetInteger(0,name+"-price",OBJPROP_SELECTED,false);

  }

//+------------------------------------------------------------------+
//|          按钮状态重置                                                        |
//+------------------------------------------------------------------+
void buttonRest(string name,int sl=150)
  {
   Sleep(sl);
//ObjectDelete(name);
//myInit();
   ObjectSetInteger(0,name,OBJPROP_STATE,false);
  }
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//|       设置图表品种                                                           |
//+------------------------------------------------------------------+
void SymbolSet(string name)
  {
   long id=ChartFirst();

   while(id!=-1)
     {

      ChartSetSymbolPeriod(id,name,ChartPeriod(id));
      id=ChartNext(id);
     }
  }


//+------------------------------------------------------------------+
//|        设置按钮位置                                                          |
//+------------------------------------------------------------------+
void SetXY(string name,int x,int y)
  {
   ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);
  }

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


//+------------------------------------------------------------------+
//|         设置按钮大小                                                         |
//+------------------------------------------------------------------+
void SetSize(string name,int x,int y)
  {
   ObjectSetInteger(0,name,OBJPROP_XSIZE,x);
   ObjectSetInteger(0,name,OBJPROP_YSIZE,y);
  }

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

//+------------------------------------------------------------------+
//|       卸载函数                                                           |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   ObjectsDeleteAll(0);

  }


//+------------------------------------------------------------------+
//|      设置标签价格                                                            |
//+------------------------------------------------------------------+
void SetPrice()
  {
   string symbols[];
   StringSplit(inSymbols,',',symbols);
   for(int i=0; i<ArraySize(symbols); i++)
     {
      ObjectSetString(0,symbols[i]+"-price",OBJPROP_TEXT,DoubleToString(MarketInfo(symbols[i],MODE_BID),(int)MarketInfo(symbols[i],MODE_DIGITS)));
     }

  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   SetPrice();
  }
//+------------------------------------------------------------------+

class Button{
   public:
   Button(string name){
      this.Name=name;
   }
   private:
   string Name;
};

在这里插入图片描述



版权声明:本文为iop02008原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。