一.文件操作
1.對應(yīng)文件路徑
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返回整行的數(shù)據(jù)(2017.05.03 02:00,1.09307)
string fr=FileReadString(f_h);
}
FileClose(f_h);
//3.在文件底部添加數(shù)據(jù),而不是直接覆蓋
//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ù)制(文件夾和目錄)
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文件夾創(chuàng)建
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.操作如下圖表
2.代碼實現(xiàn)
//+------------------------------------------------------------------+
//| 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獲取當(dāng)前圖表價格邊界
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設(shè)置圖表屬
ChartSetInteger(0,CHART_SHOW_ONE_CLICK,true);
//3.遍歷所有窗口,可以關(guān)閉指定類型的貨幣窗口
long first_chartId=ChartFirst();
for(int i=0;i<200;i++)
{
string symbol=ChartSymbol(first_chartId);
//3.1 關(guān)閉指定貨幣的窗口
if(symbol=="EURUSD")
{
ChartClose(first_chartId);
}
first_chartId=ChartNext(first_chartId);
if(first_chartId<0)
{
break;
}
}
//3.1當(dāng)前寬口價值指標(biāo)
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遍歷指標(biāo)
int indicatorsTotal=ChartIndicatorsTotal(0,0);
for(int i=indicatorsTotal-1;i>=0;i--)
{
string indicator_name=ChartIndicatorName(0,0,i);
//3.3刪除指標(biāo)
//ChartIndicatorDelete(0,0,indicator_name);
//3.4獲得指標(biāo)句柄
int indicator_h=ChartIndicatorGet(0,0,indicator_name);
}
//4.模板操作
//4.1加載模板
ChartApplyTemplate(0,"ADX");
//4.2保存當(dāng)前頁面為模板
ChartSaveTemplate(0,"testTemplate");
//6.屏幕截圖(交易的時候可以截圖留底)
ChartScreenShot(0,"1.jpg",800,800);
//7.獲取最近操作的錯誤碼
printf(GetLastError());
}
//+------------------------------------------------------------------+
2017/5/4
三.通知郵件發(fā)送
1.發(fā)送郵件功能
1.1設(shè)置移動139郵箱想罕,有短信提醒功能(http://mail.10086.cn/)
1.2發(fā)送郵件即可
SendMail("郵件標(biāo)題","郵件內(nèi)容");
2.發(fā)送文件到FTP
2.1設(shè)置
2.2 上傳
SendFTP("testFtp.txt");
3.抓取網(wǎng)頁內(nèi)容
3.1先添加要抓取的頁面
4.消息推送到手機(待研究)
4.1設(shè)置
4.2如下代碼即可
SendNotification("發(fā)送通知到手機");
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.發(fā)送郵件
SendMail("郵件標(biāo)題","郵件內(nèi)容");
//2.上傳文件到ftp
SendFTP("testFtp.txt");
//3.抓起網(wǎng)頁的內(nèi)容
//3.1上傳一個自定義的web
SendFTP("web.html");
//3.2抓取網(wǎng)頁內(nèi)容
string url="https://www.baidu.com";
char data[]; // HTTP 信息主體的數(shù)組
char result[]; // 包含服務(wù)器反應(yīng)數(shù)據(jù)的數(shù)組
string result_headers; // 服務(wù)器響應(yīng)標(biāo)題
WebRequest("GET",url,NULL,NULL,2000,data,0,result,result_headers);
string res=CharArrayToString(result);
//4.發(fā)送通知到手機
SendNotification("發(fā)送通知到手機");
int a=0;
}
//+------------------------------------------------------------------
如果您發(fā)現(xiàn)本文對你有所幫助悠栓,如果您認為其他人也可能受益,請把它分享出去弧呐。