MT5入門到精通之五(MQL5初級語法 )

一.MQL5初級語法(學過c++的簡單看10分鐘就可以)

//+------------------------------------------------------------------+
//|                                                     Script接口.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property script_show_inputs //彈出確認框

//7.外部參數
input double lots=0.1;

//8.全局變量
int magicNum=170422;

//2常量(宏定義)
#define lotSize 0.1
#define EAName "旺仔2488"
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
/*
只執行一次
*/
void OnStart()
  {
//1.數據類型
   int aI=11234;
   double bD = 1.23;
   string cS = "字符串";
   color dC=clrBlue;
   datetime eT=D'2017.04.22 12:46:27';

//3.1變量(預定義變量)
   int pI=_Digits;
   int P2I=Digits();

//4.1函數使用
   int res=add(5,6);
   int a = 5;
   int b =6;
   int res2=addReference(a,b);

//5.字符串格式化
   string eS=StringFormat("nihao test %s:\n,age=%d,height=%.1f",_Symbol,20,176.5);
   printf("nihao test %s:\n,age=%d,height=%.1f",_Symbol,20,176.5);

//6.字符串操作函數
//6.1字符串查找
   string soureS= "wang,li,zhang,san";
   string findS ="san";
   int fIndex=StringFind(soureS,findS,0);
//6.2獲取子字符串
   string subS=StringSubstr(soureS,0,4);
//6.3字符串分割
   string sArr[];
   StringSplit(soureS,',',sArr); //注意一定要單引號

  }
//+------------------------------------------------------------------+
//4.函數
int add(int x,int y)
  {
   return(x+y);
  }
//+------------------------------------------------------------------+
//9.函數引用參數(返回多個參數用)
int addReference(int &x,int &y)
  {
   x = 2*x;
   y = 2*y;
   return(x+y);
  }
//+------------------------------------------------------------------+

二.MQL5中級語法
1.客戶端全局變量(軟件重啟值也不會變)

//+------------------------------------------------------------------+
//|                                                       MQL5語法.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

int magic=170422;
string preClientVarName;
//客戶端變量
#define IsOpenBuy "isOpenBuy"
string isOpenBuyCV;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//1.客戶端全局變量(軟件重啟值也不會變)
   preClientVarName=MQLInfoString(MQL_PROGRAM_NAME)+Symbol()+IntegerToString(magic);
   isOpenBuyCV = preClientVarName+IsOpenBuy;
   if(GlobalVariableCheck(isOpenBuyCV)==false)
     {
      GlobalVariableSet(isOpenBuyCV,0);
     }
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(GlobalVariableGet(isOpenBuyCV)==0)
     {
      //do something
      GlobalVariableSet(isOpenBuyCV,1);
     }
  }
//+------------------------------------------------------------------+

2.其他枚舉,結構體,數組

//+------------------------------------------------------------------+
//|                                                       MQL5語法.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//2.枚舉類型
enum openType
  {
   buy,//只開多單
   sell,//只開空單
   buyAndSell,//多空都可以
  };
input openType oType=buy;
//2.1 系統枚舉類型
input ENUM_TIMEFRAMES timePeroid=PERIOD_H1;
//3.結構體類型
//3.1自定義結構體
struct kBar
  {
   double            open;
   double            close;
   double            high;
   double            low;
   datetime          time;
   int               vol;
  };

//4.數組
//固定數組
double b[10];
//可變數組
double a[];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
///////
//4.1固定數組使用
   double tempD=b[0];

//4.2 可變數組使用
   ArrayResize(a,2);
   tempD=a[0];

//4.3數組初始化
   int aArr[10];
   ArrayInitialize(aArr,1);
//4.3.1部分元素初始化賦值
   ArrayFill(aArr,2,2,2);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
//3.1結構體使用
   kBar kb;
   kb.open=1.1;
   kb.close=1.2;
   kb.high = 1.3;
   kb.low=1.0;
   kb.time= TimeCurrent();
   kb.vol = 100;
  }
//+------------------------------------------------------------------+

如果您發現本文對你有所幫助,如果您認為其他人也可能受益,請把它分享出去。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容