MT5入门到精通之十五(文件操作,图表操作,通知邮件发送)
2017-05-04 本文已影响0人
旺仔2488
一.文件操作
1.对应文件路径
2.常用操作代码
//+------------------------------------------------------------------+
//| operateFileScript.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"
#include <wz\Kline.mqh>
Kline kl;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnStart()
{
//---
MqlRates rates[];
kl.getRates(rates,50);
int f_h;
//1.正常文件写入流程(一般打开这个属性FILE_SHARE_READ,其他进程也可以查看)
f_h=FileOpen("abc.txt",FILE_READ|FILE_WRITE|FILE_TXT|FILE_SHARE_READ,",");
FileWrite(f_h,TimeToString(rates[0].time),DoubleToString(rates[0].open,Digits()));
FileWrite(f_h,TimeToString(rates[1].time),DoubleToString(rates[1].open,Digits()));
//1.2Writes to a disk all data remaining in the input/output file buffer.
FileFlush(f_h);
FileClose(f_h);
//2.文件读取操作
f_h=FileOpen("abc.txt",FILE_READ|FILE_WRITE|FILE_TXT|FILE_SHARE_READ,",");
while(FileIsEnding(f_h)==false)
{
//2.1返回整行的数据(2017.05.03 02:00,1.09307)
string fr=FileReadString(f_h);
}
FileClose(f_h);
//3.在文件底部添加数据,而不是直接覆盖
//3.1推荐用csv格式,不需要有分割号,
f_h=FileOpen("abc.csv",FILE_READ|FILE_WRITE|FILE_CSV|FILE_SHARE_READ);
FileSeek(f_h,0,SEEK_END);
FileWrite(f_h,TimeToString(rates[0].time),DoubleToString(rates[0].open,Digits()));
FileWrite(f_h,TimeToString(rates[1].time),DoubleToString(rates[1].open,Digits()));
FileWrite(f_h,TimeToString(rates[2].time),DoubleToString(rates[2].open,Digits()));
FileFlush(f_h);
FileClose(f_h);
//4.删除文件
// FileDelete("abc.csv");
//5.二进制文件读取操作
f_h=FileOpen("test.bin",FILE_READ|FILE_WRITE|FILE_BIN|FILE_SHARE_READ);
FileWriteDouble(f_h,1.23456);
FileClose(f_h);
f_h=FileOpen("test.bin",FILE_READ|FILE_WRITE|FILE_BIN|FILE_SHARE_READ);
string valueD=FileReadDouble(f_h);
FileClose(f_h);
//6.文件的复制(文件夹和目录)
f_h=FileOpen("directory\\abc.txt",FILE_READ|FILE_WRITE|FILE_TXT|FILE_BIN|FILE_SHARE_READ);
FileWrite(f_h,TimeToString(rates[0].time),DoubleToString(rates[0].open,Digits()));
FileWrite(f_h,TimeToString(rates[1].time),DoubleToString(rates[1].open,Digits()));
FileFlush(f_h);
FileClose(f_h);
FileCopy("directory\\abc.txt",0,"directory\\dcd.txt",FILE_REWRITE);
//6.1文件夹创建
FolderCreate("test");
FolderDelete("test");
FolderClean("test");
//7.文件搜索遍历
string fName;
int s_h=FileFindFirst("directory\\a*.*",fName);
if(s_h!=INVALID_HANDLE)
{
do
{
if(FileIsExist(fName)==true)
{
printf(fName+"这是一个文件");
}
else
{
printf(fName+"这是一个文件夹");
}
}
while(FileFindNext(s_h,fName)==true);
}
}
//+------------------------------------------------------------------+
二.图表操作
1.操作如下图表
image.png
2.代码实现
//+------------------------------------------------------------------+
//| operateChartScript.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"
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//1.打开制定周期的图表
ChartOpen("GBPUSD",PERIOD_H1);
//2.1获取图表属性ChartGetInteger ChartGetDouble ChartGetString
int cMode=ChartGetInteger(0,CHART_MODE);
if(cMode==CHART_BARS)Alert("柱状图");
if(cMode==CHART_CANDLES)Alert("蜡烛图");
if(cMode==CHART_LINE)Alert("线形图");
//2.1.1获取当前图表价格边界
double min=ChartGetDouble(0,CHART_PRICE_MIN);
double max=ChartGetDouble(0,CHART_PRICE_MAX);
string eaName=ChartGetString(0,CHART_EXPERT_NAME);
string scriptName=ChartGetString(0,CHART_SCRIPT_NAME);
//2.2设置图表属
ChartSetInteger(0,CHART_SHOW_ONE_CLICK,true);
//3.遍历所有窗口,可以关闭指定类型的货币窗口
long first_chartId=ChartFirst();
for(int i=0;i<200;i++)
{
string symbol=ChartSymbol(first_chartId);
//3.1 关闭指定货币的窗口
if(symbol=="EURUSD")
{
ChartClose(first_chartId);
}
first_chartId=ChartNext(first_chartId);
if(first_chartId<0)
{
break;
}
}
//3.1当前宽口价值指标
int ma_h=iMA(Symbol(),0,5,0,0,0);
ChartIndicatorAdd(0,0,ma_h);
int bands=iBands(Symbol(),0,20,0,2.0,PRICE_CLOSE);
ChartIndicatorAdd(0,0,bands);
//3.2遍历指标
int indicatorsTotal=ChartIndicatorsTotal(0,0);
for(int i=indicatorsTotal-1;i>=0;i--)
{
string indicator_name=ChartIndicatorName(0,0,i);
//3.3删除指标
//ChartIndicatorDelete(0,0,indicator_name);
//3.4获得指标句柄
int indicator_h=ChartIndicatorGet(0,0,indicator_name);
}
//4.模板操作
//4.1加载模板
ChartApplyTemplate(0,"ADX");
//4.2保存当前页面为模板
ChartSaveTemplate(0,"testTemplate");
//6.屏幕截图(交易的时候可以截图留底)
ChartScreenShot(0,"1.jpg",800,800);
//7.获取最近操作的错误码
printf(GetLastError());
}
//+------------------------------------------------------------------+
2017/5/4
三.通知邮件发送
1.发送邮件功能
1.1设置移动139邮箱,有短信提醒功能(http://mail.10086.cn/)
1.2发送邮件即可
SendMail("邮件标题","邮件内容");
2.发送文件到FTP
2.1设置
2.2 上传
SendFTP("testFtp.txt");
3.抓取网页内容
3.1先添加要抓取的页面
4.消息推送到手机(待研究)
4.1设置
4.2如下代码即可
SendNotification("发送通知到手机");
5.以上完整代码如下
//+------------------------------------------------------------------+
//| operateNoticeScript.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"
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//1.发送邮件
SendMail("邮件标题","邮件内容");
//2.上传文件到ftp
SendFTP("testFtp.txt");
//3.抓起网页的内容
//3.1上传一个自定义的web
SendFTP("web.html");
//3.2抓取网页内容
string url="https://www.baidu.com";
char data[]; // HTTP 信息主体的数组
char result[]; // 包含服务器反应数据的数组
string result_headers; // 服务器响应标题
WebRequest("GET",url,NULL,NULL,2000,data,0,result,result_headers);
string res=CharArrayToString(result);
//4.发送通知到手机
SendNotification("发送通知到手机");
int a=0;
}
//+------------------------------------------------------------------
如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。