//+------------------------------------------------------------------+ //| HistoryInfo.mqh | //| Gunther Schulz | //| https://github.com/khramkov/MQL5-JSON-API | //+------------------------------------------------------------------+ #property copyright "Gunther Schulz" #property link "https://github.com/khramkov/MQL5-JSON-API" //+------------------------------------------------------------------+ //| Get historical data | //+------------------------------------------------------------------+ void HistoryInfo(CJAVal &dataObject) { string actionType = dataObject["actionType"].ToStr(); string chartTF = dataObject["chartTF"].ToStr(); string symbol=dataObject["symbol"].ToStr(); // bool correctTickHistory=dataObject["correctTickHistory"].ToBool(); // Write CVS fle to local directory if(actionType=="WRITE" && chartTF=="TICK") { CJAVal data, d, msg; MqlTick tickArray[]; string fileName=symbol + "-" + chartTF + ".csv"; // file name string directoryName="Data"; // directory name string outputFile=directoryName+"\\"+fileName; ENUM_TIMEFRAMES period=GetTimeframe(chartTF); datetime fromDate=(datetime)dataObject["fromDate"].ToInt(); datetime toDate=TimeCurrent(); if(dataObject["toDate"].ToInt()!=NULL) toDate=(datetime)dataObject["toDate"].ToInt(); Print("Fetching HISTORY"); Print("1) Symbol: "+symbol); Print("2) Timeframe: Ticks"); Print("3) Date from: "+TimeToString(fromDate)); if(dataObject["toDate"].ToInt()!=NULL) Print("4) Date to:"+TimeToString(toDate)); int tickCount = 0; ulong fromDateM = StringToTime(fromDate); ulong toDateM = StringToTime(toDate); tickCount=CopyTicksRange(symbol,tickArray,COPY_TICKS_ALL,1000*(ulong)fromDateM,1000*(ulong)toDateM); if(tickCount < 0) { mControl.mSetUserError(65541, GetErrorID(65541)); } CheckError(__FUNCTION__); Print("Preparing data of ", tickCount, " ticks for ", symbol); /* if(correctTickHistory) CorrectTicks(symbol,tickArray); */ int file_handle=FileOpen(outputFile, FILE_WRITE | FILE_CSV); if(file_handle!=INVALID_HANDLE) { msg["status"] = (string) "CONNECTED"; msg["type"] = (string) "NORMAL"; msg["data"] = (string) StringFormat("Writing to: %s\\%s", TerminalInfoString(TERMINAL_DATA_PATH), outputFile); if(liveStream) InformClientSocket(liveSocket, msg.Serialize()); ActionDoneOrError(ERR_SUCCESS, __FUNCTION__, "ERR_SUCCESS"); //ActionDoneOrError(ERR_SUCCESS , __FUNCTION__, dataSocket); // Inform client that file is avalable for writing PrintFormat("%s file is available for writing",fileName); PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH)); //--- write the time and values of signals to the file for(int i=0; i0) { tradeInfo.Ticket(ticket); data["ticket"]=(long) tradeInfo.Ticket(); data["time"]=(long) tradeInfo.Time(); data["price"]=(double) tradeInfo.Price(); data["volume"]=(double) tradeInfo.Volume(); data["symbol"]=(string) tradeInfo.Symbol(); data["type"]=(string) tradeInfo.TypeDescription(); data["entry"]=(long) tradeInfo.Entry(); data["profit"]=(double) tradeInfo.Profit(); trades["trades"].Add(data); } } } else { trades["trades"].Add(data); } string t=trades.Serialize(); if(debug) Print(t); InformClientSocket(dataSocket,t); } else { mControl.mSetUserError(65538, GetErrorID(65538)); CheckError(__FUNCTION__); } } //+------------------------------------------------------------------+