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;
  }
//+------------------------------------------------------------------+

如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。

你可能感兴趣的:(MT5入门到精通之五(MQL5初级语法 ))