好的,你需要
1. 接收经纪商的Feed(希望您成功)
2.写入文件
**(两者 - python)**
3.读取并解析
4.添加到历史中心/marketWatch
**(两者 - mt5)**
因此,您会在之后收到字符串形式的数据I_want_money.get_candles(goal,60,111,time.time())
该字符串可能是 json 或 json-array。
重要的问题当然是您要放置数据的路径。 MQL45 专家只能访问两个文件夹(如果不应用 dll):
C:\Users\MY_NAME_IS_DANIEL_KNIAZ\AppData\Roaming\MetaQuotes\Terminal\MY_TERMINAL_ID_IN_HEX_FORMAT\MQL4\Files
和
C:\Users\MY_NAME_IS_DANIEL_KNIAZ\AppData\Roaming\MetaQuotes\Terminal\Common\Files
在后一种情况下,您需要使用 const int handle=FileOpen(,|*|文件公用);
为了解析 json,您可以使用 jason.mqhhttps://www.mql5.com/en/code/13663 https://www.mql5.com/en/code/13663库(还有其他一些库),但据我记得它有一个错误:它无法正确解析对象数组。为了克服这个问题,我建议将每个刻度写在单独的行上。
最后,您将随机从 python 应用程序接收数据,并将其写入 Common 或 direct 文件夹中。 MT5机器人将读取并删除。为了避免混淆,最好保证文件具有唯一的名称。随机 (random.randint(1,1000)) 或日期时间的毫秒数都可以提供帮助。
到目前为止,你已经有了 python 代码:
receivedString = I_want_money.get_candles(goal,60,111,time.time())
filePath = 'C:\Users\MY_NAME_IS_DANIEL_KNIAZ\AppData\Roaming\MetaQuotes\Terminal\MY_TERMINAL_ID_IN_HEX_FORMAT\MQL4\Files\iqoptionfeed'
fileName = os.path.join(filePath,"_"+goal+"_"+str(datetime.now())+".txt")
file = open(fileName, "w")
for string_ in receivedString:
file.write(string_)
file.close()
如果您创建了一个线程,每次您从该线程收到答案时,您都会编写一个这样的文件。
接下来,您需要 MT5 中的数据。
最简单的方法是循环遍历现有文件,确保可以读取它们并读取(如果不能读取则放弃)并在读取后删除,然后继续处理接收到的数据。
最简单、更快的方法当然是使用 0MQ,但让我们在没有 dll 的情况下实现它。
为了读取文件,您需要设置一个可以尽可能快地工作的计时器,然后放开它。由于您无法使 Windows 应用程序的休眠时间少于 15.6 毫秒,因此您的计时器应该休眠这个时间。
string path;
int OnInit()
{
EventSetMillisecondTimer(16);
path="iqoptionfeed\\*";
}
void OnDeinit(const int reason) { EventKillTimer(); }
string _fileName;
long _search_handle;
void OnTimer()
{
_search_handle=FileFindFirst(path,_fileName);
if(_search_handle!=INVALID_HANDLE)
{
do
{
ResetLastError();
FileIsExist(_fileName);
if(GetLastError()!=ERR_FILE_IS_DIRECTORY)
processFile(path+_fileName);
}
while(FileFindNext(_search_handle,_fileName));
FileFindClose(_search_handle);
}
}
这段代码循环文件夹并处理它设法找到的每个文件。
现在读取文件(两个函数)并处理其中的消息:
void processFile(const string fileName)
{
string message;
if(ReadFile(fileName,message))
processMessage(message,fileName);
}
bool ReadFile(const string fileName,string &result,const bool common=false)
{
const int handle = FileOpen(fileName,common?(FILE_COMMON|FILE_READ):FILE_READ);
if(handle==INVALID_HANDLE)
{
printf("%i - failed to find file %s (probably doesnt exist!). error=%d",__LINE__,fileName,GetLastError());
return(false);
}
Read(handle,result);
FileClose(handle);
if(!FileDelete(fileName,common?FILE_COMMON:0))
printf("%i - failed to delete file %s/%d. error=%d",__LINE__,fileName,common,GetLastError());
return(true);
}
void Read(const int handle,string &message)
{
string text="";
while(!FileIsEnding(handle) && !IsStopped())
{
text=StringConcatenate(text,FileReadString(handle),"\n");
}
//printf("%i %s - %s.",__LINE__,__FUNCTION__,text);
message=text;
}
最后但并非最不重要的一点是:处理获得的文件。
正如上面所建议的,每个新刻度都有一个 json 格式的刻度,以 \r\n 分隔。
我们的目标是将其添加到符号中。为了解析 json,jason.mqh 是一个可用的解决方案,但您当然可以手动解析它。
void processMessage(const string message,const string fileName)
{
string symbolName=getSymbolFromFileName(fileName);
if(!SymbolSelect(symbolName,true))
{
if(!CustomSymbolCreate(symbolName))
return;
}
string lines[];
int size=StringSplit(message,(ushort)'\n',lines);
for(int i=0;i<size;i++)
{
if(StringLen(lines[i])==0)
continue;
CJAVal jLine(jtUNDEF,NULL);
jLine.Deserialize(lines[i]);
MqlTick mql;
//here I assume that you receive a json file like " { "time":2147483647,"bid":1.16896,"ask":1.16906,"some_other_data":"someOtherDataThatYouMayAlsoUse" } "
mql.time=(datetime)jLine["time"].ToInt();
mql.bid=(double)jLine["bid"].ToDbl();
mql.ask=(double)jLine["ask"].ToDbl();
ResetLastError();
if(CustomTicksAdd(symbolName,mql)<0)
printf("%i %s - failed to upload tick: %s %s %.5f %.5f. error=%d",__LINE__,__FILE__,symbolName,TimeToString(mql.time),mql.bid,mql.ask,GetLastError());
}
}
string getSymbolFromFileName(const string fileName)
{
string elements[];
int size=StringSplit(fileName,(ushort)'_',elements);
if(size<2)
return NULL;
return elements[1];
}
不要忘记添加调试信息和请求GetLastError()
由于某种原因你会收到错误。
这可以在回测器中工作吗?当然不是。拳头,OnTimer()
MQL 测试器不支持。接下来,您需要一些历史记录才能使其运行。如果您没有任何历史记录 - 没有人可以帮助您,除非经纪人可以将其提供给您;最好的想法可能是立即开始收集和存储它,当项目准备好时(可能再过几个月),您将准备好并能够使用可用数据集测试和优化策略。您可以将收集到的数据集应用到测试器中(与 MQL4 相比,MQL5 实际上是算法交易开发的下一步),可以手动使用,也可以使用像 tickDataSuite 及其 Csv2Fxt.ex4 文件这样的东西,该文件生成测试器可以读取和处理的 HST 二进制文件;无论如何,这是另一个问题,没有人可以告诉您您的经纪人是否将其数据存储在某个地方以提供给您。