MT编程语言MQL入门全接触_第1页
MT编程语言MQL入门全接触_第2页
MT编程语言MQL入门全接触_第3页
MT编程语言MQL入门全接触_第4页
MT编程语言MQL入门全接触_第5页
已阅读5页,还剩45页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、mt4编程语言全接触语法 syntax代码格式空格建、 tab 键、换行键和换页符都可以成为代码排版的分隔符,你能使用各种符号来增加代码的可读性。注释多行注释使用/* 作为开始到*/ 结束,在这之间不能够嵌套。单行注释使用示例:0a2f7c用来更精确的表示十进制数字。示例 :double a = ; double b = ; double c = ; double d = 16;浮点型的取值范围从到 . string 类型字符串型是用来表示连续的ascii码字符的使用连续的两个双引号来包括需要表示的内容如: character constant. 示例 :this is a character

2、 string copyright symbol txa9 this line with lf symbol n color 类型颜色类型可以使用以下示例里的几种方式进行定义。示例 :d12:30:27 关系运算符用返回 0(false) 或 1(true) 来表示两个量之间的关系。a 是否等于 b a = b; a 是否不等于 b a != b; a 是否小于 b a b; a 是否小于等于b a = b; 真假运算符否定运算符 (!),用来表示真假的反面的结果。.,xn 这样的方法将各种值传送到function中进行运算。示例: double sl=ask-25*point; double

3、 tp=ask+25*point; int ticket=ordersend(symbol(),op_buy,1,ask,3,sl,tp, my comment,123,0,red); 优先级规则下面是从上到下的运算优先规则,优先级高的将先被运算。() function call from left to right array element selection ! negation from left to right bitwise negation - sign changing operation * multiplication from left to right / divis

4、ion % module division + addition from left to right - subtraction right shift less than from left to right greater than = greater than or equals = equals from left to right != not equal & bitwise and operation from left to right bitwise exclusive or from left to right | bitwise or operation from

5、 left to right & logical and from left to right | logical or from left to right = assignment from right to left += assignment addition -= assignment subtraction *= assignment multiplication /= assignment division %= assignment module = assignment right shift = assignment left shift &= assign

6、ment bitwise and |= assignment bitwise or = assignment exclusive or , comma from left to right 操作符 operators 格式和嵌套格式 . 一个操作符可以占用一行或者多行,两个或多个操作符可以占用更多的行。嵌套 . 执行控制符 (if, if-else, switch, while and for)可以进行任意嵌套. 复合操作符一个复合操作符有一个( 一个区段 )和由一个或多个任何类型的操作符组成的的附件. 每个表达式使用分号作为结束(;) 示例 : if(x=0) x=1; y=2; z=3;

7、表达式操作符任何以分号 (;) 结束的表达式都被视为是一个操作符。assignment operator. identifier=expression;标识符=表达式 ;示例 : x=3; y=x=3; ., argumentn);函数名称 ( 参数 1,.,参数 n);示例 : fclose(file); 空操作符只有一个分号组成(;).我们用它来表示没有任何表达式的空操作符. 停止操作符一个 break; , 我们将其放在嵌套内的指定位置, 用来在指定情况下跳出循环操作. 示例 : . default: operators; break; 当表达式 expression 的值等于结果之一时

8、,执行其结果下的操作。不管结果如何都将执行 default中的操作。示例: case 3+4: 表达式 1 和表达式 3 都可以内嵌多个用逗号 (,) 分割的表达式。示例: for(i=0,j=n-l;in;i+,j-) a=aj; 函数 function 函数定义一个函数是由返回值、输入参数、内嵌操作所组成的。示例 : double .,xn)示例 : int somefunc() double a=linfunc, , 8); double linfunc(double x, double a, double b) return (a*x + b); 特殊函数 init()、deinit(

9、)和 start() init()在载入时调用,可以用此函数在开始自定义指标或者自动交易之前做初始化操作。deinit()在卸载时调用,可以用此函数在去处自定义指标或者自动交易之前做初始化操作。start()当数据变动时触发, 对于自定义指标或者自动交易的编程主要依靠此函数进行。变量 variables 定义变量定义基本类型基本类型包括?string - 字符串型 ; ?int - 整数型 ; ?double - 双精度浮点数型 ; ?bool - 布尔型示例 : string messagebox; int orders; double symbolprice; bool blog; 定义附

10、加类型附加类型包括?datetime - 时间型 , 使用无符号整型数字存储, 是 0:0:0开始的秒数?color - 颜色 , 使用三色的整型数字编码而成示例 : extern color cmodify_color = c0 x44,0 xb9,0 xe6; 定义数组类型示例 : int a50; . 函数的参数内的变量只能在函数内才生效,在函数外无法使用,而且在函数内对变量进行的修改在函数外无法生效。调用函数示例 : func(123, ; 如果有需要在变量传入由参数传入函数内操作后保留修改在函数外生效的情况的话,可以在参数定义的类型名称后加上修饰符(&) 。示例 : void

11、 func(int& x, double& y, double& z) . 静态变量定义在数据类型前加上static就可以将变量定义成静态变量示例 : static int flag 全局变量定义全局变量是指在整个程序中都能够调用的变量,只需将变量定义卸载所有嵌套之外即可。示例 : int global_flag; int start() . 附加变量定义附加变量可以允许由用户自己输入。示例 : extern double inputparameter1 = ; int init() . 初始化变量变量必须经过初始化才可以使用。基本类型示例 : int mt = 1;

12、编译参数定义#property identifier_value示例 : #property copyright metaquotes software corp. #property stacksize 1024 以下是所有的参数名称: 参数名称类型说明link string 设置一个链接到公司网站copyright string 公司名称stacksize int 堆栈大小indicator_chart_window void 显示在走势图窗口indicator_separate_window void 显示在新区块indicator_buffers int 显示缓存最高8 indicat

13、or_minimum int 图形区间最低点indicator_maximum int 图形区间最高点indicator_colorn color 第 n根线的颜色,最高8 根线indicator_leveln double predefined level n for separate window custom indicator show_confirm void 当程序执行之前是否经过确认show_inputs void before script run its property sheet appears; disables show_confirm property 嵌入文件#i

14、nclude 示例 : #include #include file_name 示例 : #include 引入函数或其他模块#import file_name func1(); func2(); #import示例 : #import int messageboxa(int hwnd,string lptext,string lpcaption, int utype); int messageboxexa(int hwnd,string lptext,string lpcaption, int utype,int wlanguageid); #import #import int getdc

15、(int hwnd); int releasedc(int hwnd,int hdc); #import 账户信息 account information double accountbalance() 返回账户余额示例 : print(account balance = ,accountbalance(); double accountcredit() 返回账户信用点数示例 : print(account number , accountcredit(); string accountcompany() 返回账户公司名示例 : print(account company name , acc

16、ountcompany(); string accountcurrency() 返回账户所用的通货名称示例 : print(account currency is , accountcurrency(); double accountequity() 返回资产净值示例 : print(account equity = ,accountequity(); double accountfreemargin() returns free margin value of the current account. 示例 : print(account free margin = ,accountfree

17、margin(); int accountleverage() 返回杠杆比率示例 : print(account #,accountnumber(), leverage is , accountleverage(); double accountmargin() returns margin value of the current account. 示例 : print(account margin , accountmargin(); string accountname() 返回账户名称示例 : print(account name , accountname(); int accoun

18、tnumber() 返回账户数字示例 : print(account number , accountnumber(); double accountprofit() 返回账户利润示例 : print(account profit , accountprofit(); 数组函数 array functions int arraybsearch( double array, double value, int count=whole_array, int start=0, int direction=mode_ascend) 搜索一个值在数组中的位置此函数不能用在字符型或连续数字的数组上. :

19、输入参数array - 需要搜索的数组value - 将要搜索的值count - 搜索的数量,默认搜索所有的数组start - 搜索的开始点,默认从头开始direction - 搜索的方向, mode_ascend 顺序搜索 mode_descend 倒序搜索示例 : datetime daytimes; int shift=10,dayshift; : 输入参数dest_array - 目标数组symbol - 标示,当前所需要的通货的标示timeframe - 图表的时间线示例 : double array16; arraycopyrates(array1,eurusd, period_h

20、1); print(current bar ,timetostr(array100),open, array101); int arraycopyseries( double& array, int series_index, string symbol=null, int timeframe=0) 复制一个系列的走势图数据到数组上注: 如果 series_index是 mode_time, 那么第一个参数必须是日期型的数组: 输入参数dest_array - 目标数组series_index - 想要取的系列的名称或编号,0-5 symbol - 标示,当前所需要的通货的标示time

21、frame - 图表的时间线示例 : datetime daytimes; int shift=10,dayshift; : 输入参数array - 需要检查的数组示例 : if(arrayisseries(array1)=false) arrayinitialize(array1,0); else print(series array cannot be initialized!); return(-1); int arraymaximum( double array, int count=whole_array, int start=0) 找出数组中最大值的定位: 输入参数array -

22、需要检查的数组count - 搜索数组中项目的个数start - 搜索的开始点示例 : double num_array15=4,1,6,3,9,4,1,6,3,9,4,1,6,3,9; int maxvalueidx=arraymaximum(num_array); print(max value = , num_arraymaxvalueidx); int arrayminimum( double array, int count=whole_array, int start=0) 找出数组中最小值的定位: 输入参数array - 需要检查的数组count - 搜索数组中项目的个数star

23、t - 搜索的开始点示例 : double num_array15=4,1,6,3,9,4,1,6,3,9,4,1,6,3,9; double minvalueidx=arrayminimum(num_array); print(min value = , num_arrayminvalueidx); int arrayrange( object array, int range_index) 取数组中指定维数中项目的数量。: 输入参数array - 需要检查的数组range_index - 指定的维数示例 : int dim_size; double num_array10,10,10; d

24、im_size=arrayrange(num_array, 1); int arrayresize( object& array, int new_size) 重定义数组大小: 输入参数array - 需要检查的数组new_size - 第一维中数组的新大小示例 : double array14; int element_count=arrayresize(array, 20); int arraysort( double& array, int count=whole_array, int start=0, int sort_dir=mode_ascend) 对数组进行排序,

25、系列数组不可进行排序: 输入参数array - 需要处理的数组count - 对多少个数组项进行排序start - 排序的开始点sort_dir - 排序方式, mode_ascend顺序排列 mode_descend倒序排列示例: double num_array5=4,1,6,3,9; . ) 弹出一个显示信息的警告窗口: 输入参数. - 任意值,如有多个可用逗号分割示例 : if(close0signallevel) alert(close price coming , close0,!); string clientterminalname() 返回客户终端名称示例 : print(t

26、erminal name is ,clientterminalname(); string companyname() 返回公司名称示例 : print(company name is ,companyname(); void comment( . ) 显示信息在走势图左上角: 输入参数. - 任意值,如有多个可用逗号分割示例 : double free=accountfreemargin(); comment(account free margin is ,doubletostr(free,2),n,current time is ,timetostr(curtime(); int getl

27、asterror() 取最后错误在错误中的索引位置示例 : int err; int handle=fileopen(, file_read|file_bin); if(handle1) err=getlasterror(); print(error(,err,): ,errordescription(err); return(0); int gettickcount() 取时间标记,函数取回用毫秒标示的时间标记。示例 : int start=gettickcount(); . print(calculation time is , gettickcount()-start, millisec

28、onds.); void hidetestindicators(bool hide) 使用此函数设置一个在expert advisor 的开关,在测试完成之前指标不回显示在图表上。: 输入参数hide - 是否隐藏 true 或者 false 示例 : hidetestindicators(true); bool isconnected() 返回客户端是否已连接示例 : if(!isconnected() print(connection is broken!); return(0); . bool isdemo() 返回是否是模拟账户示例 : if(isdemo() print(i am w

29、orking on demo account); else print(i am working on real account); bool isdllsallowed() 返回是否允许载入dll 文件示例 : #import int messageboxa(int hwnd ,string sztext, string szcaption,int ntype); . . if(isdllsallowed()=false) print(dll call is not allowed. experts cannot run.); return(0); . . if(islibrariesall

30、owed()=false) print(library call is not allowed. experts cannot run.); return(0); . bool istesting() 返回是否处于测试模式示例 : if(istesting() print(i am testing now); bool istradeallowed() 返回是否允许交易示例 : if(istradeallowed() print(trade allowed); double marketinfo( string symbol, int type) 返回市场当前情况: 输入参数symbol -

31、通货代码type - 返回结果的类型示例 : double var; var=marketinfo(eurusd,mode_bid); int messagebox( string text=null, string caption=null, int flags=empty) 弹出消息窗口,返回消息窗口的结果: 输入参数text - 窗口显示的文字caption - 窗口上显示的标题flags - 窗口选项开关示例 : #include int ret=messagebox(objectcreate() fails with code +getlasterror()+ncontinue, q

32、uestion, mb_yesno|mb_iconquestion); if(ret=idno) return(false); . ) 将文本打印在结果窗口内: 输入参数. - 任意值,复数用逗号分割示例 : print(account free margin is , accountfreemargin(); print(current time is , timetostr(curtime(); double pi=3 print(pi number is , doubletostr(pi,8); : 输入参数subject - 邮件标题some_text - 邮件内容示例: double

33、 lastclose=close0; if(lastclosemy_signal) sendmail(from your expert, price dropped down to +doubletostr(lastclose); string serveraddress()返回服务器地址示例: print(server address is , serveraddress(); void sleep( int milliseconds)设置线程暂停时间: 输入参数milliseconds - 暂停时间 1000 = 1 秒示例: sleep(5); void speechtext( stri

34、ng text, int lang_mode=speech_english)使用 speech进行语音输出: 输入参数text - 阅读的文字lang_mode - 语音模式 speech_english (默认的) 或 speech_native 示例: double lastclose=close0; speechtext(price dropped down to +doubletostr(lastclose); string symbol()返回当前当前通货的名称示例: int total=orderstotal(); for(int pos=0;pos=12 | hour()17)

35、is_siesta=true; datetime localtime() 返回当前电脑时间示例 : if(localtime()-orderopentime()360) return(0); int minute() 返回当前分钟示例 : if(minute()=15) return(first quarter); int month() 返回当前月份示例 : if(month()=5) return(first half of year); int seconds() 返回当前秒数示例 : if(seconds()0) . fileclose(handle); void filedelete

36、(string filename) 删除文件,如果发生错误可以通过getlasterror()来查询注:你只能操作terminal_direxpertsfiles目录下的文件: 输入参数filename - 目录和文件名示例 : . for(int i=0;ibars_count;i+) filewrite(handle, i+1,open,close,high, low); fileclose(handle); bool fileisending(int handle)检查是否到了文件尾 . : 输入参数handle - fileopen()返回的句柄示例: if(fileisending(

37、h1) fileclose(h1); return(false); bool fileislineending( int handle)检查行是否到了结束: 输入参数handle - fileopen()返回的句柄示例: if(fileislineending(h1) fileclose(h1); return(false); int fileopen( string filename, int mode, int delimiter=;)打开文件,如果失败返回值小于1,可以通过 getlasterror()获取错误注:只能操作 terminal_direxpertsfiles目录的文件: 输

38、入参数filename - 目录文件名mode - 打开模式 file_bin, file_csv, file_read, file_write. delimiter - csv型打开模式用的分割符,默认为分号 (;). 示例: int handle; handle=fileopen(,file_csv|file_read,;); if(handle1) print(file not found, the last error is , getlasterror(); return(false); int fileopenhistory( string filename, int mode,

39、int delimiter=;)打开历史数据文件,如果失败返回值小于1,可以通过 getlasterror()获取错误: 输入参数filename - 目录文件名mode - 打开模式 file_bin, file_csv, file_read, file_write. delimiter - csv型打开模式用的分割符,默认为分号 (;). 示例: int handle=fileopenhistory(,file_bin|file_write); if(handle0) filereadarray(handle, varray, 0, 10); fileclose(handle); doub

40、le filereaddouble( int handle, int size=double_value)从文件中读取浮点型数据,数字可以是8byte 的 double 型或者是 4byte 的 float型。: 输入参数handle - fileopen()返回的句柄size - 数字个是大小, double_value(8 bytes) 或者 float_value(4 bytes). 示例: int handle; double value; handle=fileopen(,file_bin); if(handle0) value=filereaddouble(handle,doubl

41、e_value); fileclose(handle); int filereadinteger( int handle, int size=long_value)从文件中读取整形型数据,数字可以是1,2,4byte的长度: 输入参数handle - fileopen()返回的句柄size - 数字个是大小, char_value(1 byte), short_value(2 bytes) 或者long_value(4 bytes). 示例: int handle; int value; handle=fileopen(, file_bin|file_read); if(handle0) value=filereadintege

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论