一.MQL5初級(jí)語法(學(xué)過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 //彈出確認(rèn)框
//7.外部參數(shù)
input double lots=0.1;
//8.全局變量
int magicNum=170422;
//2常量(宏定義)
#define lotSize 0.1
#define EAName "旺仔2488"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
/*
只執(zhí)行一次
*/
void OnStart()
{
//1.數(shù)據(jù)類型
int aI=11234;
double bD = 1.23;
string cS = "字符串";
color dC=clrBlue;
datetime eT=D'2017.04.22 12:46:27';
//3.1變量(預(yù)定義變量)
int pI=_Digits;
int P2I=Digits();
//4.1函數(shù)使用
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.字符串操作函數(shù)
//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); //注意一定要單引號(hào)
}
//+------------------------------------------------------------------+
//4.函數(shù)
int add(int x,int y)
{
return(x+y);
}
//+------------------------------------------------------------------+
//9.函數(shù)引用參數(shù)(返回多個(gè)參數(shù)用)
int addReference(int &x,int &y)
{
x = 2*x;
y = 2*y;
return(x+y);
}
//+------------------------------------------------------------------+
二.MQL5中級(jí)語法
1.客戶端全局變量(軟件重啟值也不會(huì)變)
//+------------------------------------------------------------------+
//| 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.客戶端全局變量(軟件重啟值也不會(huì)變)
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.其他枚舉,結(jié)構(gòu)體,數(shù)組
//+------------------------------------------------------------------+
//| 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 系統(tǒng)枚舉類型
input ENUM_TIMEFRAMES timePeroid=PERIOD_H1;
//3.結(jié)構(gòu)體類型
//3.1自定義結(jié)構(gòu)體
struct kBar
{
double open;
double close;
double high;
double low;
datetime time;
int vol;
};
//4.數(shù)組
//固定數(shù)組
double b[10];
//可變數(shù)組
double a[];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
///////
//4.1固定數(shù)組使用
double tempD=b[0];
//4.2 可變數(shù)組使用
ArrayResize(a,2);
tempD=a[0];
//4.3數(shù)組初始化
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結(jié)構(gòu)體使用
kBar kb;
kb.open=1.1;
kb.close=1.2;
kb.high = 1.3;
kb.low=1.0;
kb.time= TimeCurrent();
kb.vol = 100;
}
//+------------------------------------------------------------------+
如果您發(fā)現(xiàn)本文對你有所幫助,如果您認(rèn)為其他人也可能受益,請把它分享出去。