MT4货币图表转换指标|在一张图表上使用多货币Symbol changer|切换图和周期




原理:在MT4图表上使用多货币Symbol changer,切换周期与货币对源码

点击下载程序与源码 文件大小:3.90 KB

必须读我.txt 策汇在线.url MQL4/ MQL4/Indicators/ MQL4/Libraries/ MQL4/Indicators/Symbol Changer.mq4

MQL4/Indicators/Symbol Changer.mq4代码片段:

//+------------------------------------------------------------------+ //| | //| Symbol Changer.mq4 | //| 货币图表转换指标 | //| http://www.fxchs.com | //| | //+------------------------------------------------------------------+ #property copyright "货币图表转换指标" #property link "策汇在线 http://www.fxchs.com" #property version "1.00" #property indicator_chart_window #property indicator_buffers 0 #property strict extern string Symbols = "EURUSD;GBPUSD;USDJPY;GBPJPY;EURJPY;XAUUSD"; // 货币列表(多个使用分号";"分隔) extern string UniqueID = "SymbolChanger By FXCHS"; // 指标唯一ID extern ENUM_BASE_CORNER Corner = 1; // 在图表的哪个角显示 默认1右上角 extern int XShift = 100; // 水平位置 extern int YShift = 40; // 垂直位置 extern int ButtonsInARow = 1; // 每行显示几个按钮 extern int XSize = 82; // 按钮宽度 extern int YSize = 28; // 按钮高度 extern int FSize = 10; // 按钮字体大小 extern color Bcolor = clrRoyalBlue; // 按钮颜色 extern color Dcolor = clrCornflowerBlue; // 按钮边框颜色 extern color Tncolor = clrBlack; // 文本颜色(常规) extern color Sncolor = clrWhite; // 文本颜色(选中) extern bool Transparent = false; // 按钮是否透明 //--------------------------------------------------------------------------------- // // 全局变量 // //--------------------------------------------------------------------------------- // 货币对数组 string garSymbols[]; // 周期数组 const string garsPriodText[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"}; const int gariPeriod[] = {1,5,15,30,60,240,1440,10080,43200}; //--------------------------------------------------------------------------------- // 初始化 int OnInit() { // 删除前后空格 Symbols = StringTrimLeft(StringTrimRight(Symbols)); // 删除最后的分号 if (StringSubstr(Symbols,StringLen(Symbols)-1,1) != ";") { Symbols = StringConcatenate(Symbols,";"); } // 将货币对字符串转为货币对数组 int s=0,i=StringFind(Symbols,";",s); string current; while (i > 0) { // 取货币对 current = StringSubstr(Symbols,s,i-s); // 自适应数组 ArrayResize(garSymbols,ArraySize(garSymbols)+1); // 赋值跳转到获取下个货币对 garSymbols[ArraySize(garSymbols)-1] = current; s = i + 1; i = StringFind(Symbols,";",s); } // // // // // // 画货币对按钮 int xpos=0,ypos=0,maxx=0,maxy=0; for (i = 0; i<ArraySize(garSymbols); i++) { // 按钮换行处理 if (i>0 && MathMod(i,ButtonsInARow)==0) { xpos=0; ypos+=YSize+1; } // 画按钮 DrawButton(UniqueID+":symbol:"+string(i),garSymbols[i],XShift+xpos,YShift+ypos); xpos +=XSize+1; } // 画周期按钮 xpos = 0; ypos += YSize*2; for (i = 0; i<ArraySize(garsPriodText); i++) { // 按钮换行处理 if (i>0 && MathMod(i,ButtonsInARow)==0) { xpos=0; ypos+=YSize+1; } // 画按钮 DrawButton(UniqueID+":time:"+string(i),garsPriodText[i],XShift+xpos,YShift+ypos); xpos +=XSize+1; } // // // // // // 设置货币对按钮颜色 SetSymbolButtonColor(); // 设置周期按钮颜色 SetTimeFrameButtonColor(); return(0); } // // // // // //+------------------------------------------------------------------+ //| 按钮事件 //+------------------------------------------------------------------+ void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam) { // 点击按钮时 if (id==CHARTEVENT_OBJECT_CLICK && ObjectGet(sparam,OBJPROP_TYPE)==OBJ_BUTTON) { // 转换货币 if (StringFind(sparam,UniqueID+":symbol:",0)==0) { ChartSetSymbolPeriod(0,ObjectGetString(0,sparam,OBJPROP_TEXT),_Period); } // 转换周期 if (StringFind(sparam,UniqueID+":time:",0)==0) { ChartSetSymbolPeriod(0,_Symbol,StringToTimeFrame(ObjectGetString(0,sparam,OBJPROP_TEXT))); } // 按钮状态 禁止 if (StringFind(sparam,UniqueID+":back:",0)==0) { ObjectSet(sparam,OBJPROP_STATE,false); } } } // // // // // //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int start() { return(0); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| 移除指标时 //+------------------------------------------------------------------+ void OnDeinit(const int reason) { switch(reason) { case REASON_CHARTCHANGE : case REASON_RECOMPILE : case REASON_CLOSE : break; default : { // 删除按钮对象 string lookFor = UniqueID+":"; int lookForLength = StringLen(lookFor); for (int i=ObjectsTotal()-1; i>=0; i--) { string objectName = ObjectName(i); if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName); } } } } // // // // // //----------------------------------------------------------------------------------- // //----------------------------------------------------------------------------------- // // // 自定义函数 // // // // 画按钮 void DrawButton(string name, string caption, int xpos, int ypos) { if (ObjectFind(name)!=0) ObjectCreate(name,OBJ_BUTTON,0,0,0); ObjectSet(name,OBJPROP_CORNER,Corner); ObjectSet(name,OBJPROP_XDISTANCE,xpos); ObjectSet(name,OBJPROP_YDISTANCE,ypos); ObjectSet(name,OBJPROP_XSIZE,XSize); ObjectSet(name,OBJPROP_YSIZE,YSize); ObjectSetText(name,caption,FSize,"Arial",Tncolor); ObjectSet(name,OBJPROP_FONTSIZE,FSize); ObjectSet(name,OBJPROP_BORDER_TYPE,BORDER_FLAT); ObjectSet(name,OBJPROP_COLOR,Tncolor); ObjectSet(name,OBJPROP_BGCOLOR,Bcolor); ObjectSet(name,OBJPROP_BACK,Transparent); ObjectSet(name,OBJPROP_BORDER_COLOR,Dcolor); ObjectSet(name,OBJPROP_STATE,false); ObjectSet(name,OBJPROP_HIDDEN,true); } //+------------------------------------------------------------------+ //| 设置货币对按钮颜色 | //+------------------------------------------------------------------+ void SetSymbolButtonColor() { string lookFor = UniqueID+":symbol:"; int lookForLength = StringLen(lookFor); for (int i=ObjectsTotal()-1; i>=0; i--) { string objectName = ObjectName(i); if (StringSubstr(objectName,0,lookForLength) == lookFor) { string symbol = ObjectGetString(0,objectName,OBJPROP_TEXT); if (symbol != _Symbol) ObjectSet(objectName,OBJPROP_COLOR,Tncolor); else ObjectSet(objectName,OBJPROP_COLOR,Sncolor); } } } //+------------------------------------------------------------------+ //| 设置货币对按钮颜色 | //+------------------------------------------------------------------+ void SetTimeFrameButtonColor() { string lookFor = UniqueID+":time:"; int lookForLength = StringLen(lookFor); for (int i=ObjectsTotal()-1; i>=0; i--) { string objectName = ObjectName(i); if (StringSubstr(objectName,0,lookForLength) == lookFor) { int time = StringToTimeFrame(ObjectGetString(0,objectName,OBJPROP_TEXT)); if (time != _Period) ObjectSet(objectName,OBJPROP_COLOR,Tncolor); else ObjectSet(objectName,OBJPROP_COLOR,Sncolor); } } } // // // // // // //+------------------------------------------------------------------+ //| 将周期转为字符串 //+------------------------------------------------------------------+ string TimeFrameToString(int tf) { for (int i=ArraySize(gariPeriod)-1; i>=0; i--) { if (tf==gariPeriod[i]) return(garsPriodText[i]); } return(""); } //+------------------------------------------------------------------+ //| 将字符串转周期 //+------------------------------------------------------------------+ int StringToTimeFrame(string tf) { for (int i=ArraySize(garsPriodText)-1; i>=0; i--) { if (tf==garsPriodText[i]) return(gariPeriod[i]); } return(0); }