I need time ,I need money, I need more patience
Automatic history check, download and refresh 10 replies
MT5 - show trade history on chart 5 replies
Solutions for forcing 100% history data download? 3 replies
MT5 For Download 6 replies
Alpari's History Data Download! 2 replies
//+------------------------------------------------------------------+ //| Export_History_Positions_V2.mq5 | //| Copyright :copyright: 2018, Amr Ali | //| https://www.mql5.com/en/users/amrali | //+------------------------------------------------------------------+ #property copyright "Copyright :copyright: 2018, Amr Ali" #property link "https://www.mql5.com/en/users/amrali" #property version "2.000" #property description "The script exports history of closed positions ordered by close time from a retail hedging account to .csv file" #property script_show_inputs #include <CHistoryPositionInfo.mqh> //--- input variables input datetime InpStartDate = 0; // Start date input datetime InpEndDate = D'2038.01.01'; // End date input string InpFileName = "MT5Data.csv"; // Filename //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { if(AccountInfoInteger(ACCOUNT_MARGIN_MODE)!=ACCOUNT_MARGIN_MODE_RETAIL_HEDGING) { Alert("This script can be started only on a retail hedging account (Forex)."); return; } //--- if(InpStartDate>InpEndDate) { Alert("Error: The start date must be earlier than the end date"); return; } if(ExportHistoryPositions(InpStartDate,InpEndDate,InpFileName)) { //--- open the .csv file with the associated Windows program Execute(TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL5\\Files\\"+InpFileName); Print("History is exported to ",TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL5\\Files\\"+InpFileName); } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool ExportHistoryPositions(datetime from_date,datetime to_date,string filename) { //--- ResetLastError(); //--- use FILE_ANSI to correctly display .csv files within Excel 2013 and above. int handle= FileOpen(filename,FILE_WRITE|FILE_SHARE_READ|FILE_CSV|FILE_ANSI,','); if(handle == INVALID_HANDLE) { Alert("File open failed, error ",_LastError); return(false); } FileWrite(handle, "Open Time", "Ticket", "Symbol", "Type", "Volume", "Open Price", "S/L", "T/P", "Close Time", "Close Price", "Commission", "Swap", "Profit", "Profit Points", "Balance", "Magic Number", "Duration", "Open Reason", "Close Reason", "Open Comment", "Close Comment", "Deal Tickets"); ulong s_time=GetMicrosecondCount(); double Balance=0; int allCnts = 0; double allVols = 0; double allComm = 0; double allSwap = 0; double allProf = 0; //--- variable to hold the history position info CHistoryPositionInfo hist_position; //--- Retrieve the history of closed positions for the specified period if(!hist_position.HistorySelect(from_date,to_date)) { Alert("CHistoryPositionInfo::HistorySelect() failed!"); return(false); } //--- now process the list of closed positions int total = hist_position.PositionsTotal(); for(int i = 0; i < total; i++) { //--- Select a closed position by its index in the list if(hist_position.SelectByIndex(i)) { datetime time_open = hist_position.TimeOpen(); datetime time_close = hist_position.TimeClose(); long type = hist_position.PositionType(); string type_desc = hist_position.TypeDescription(); long magic = hist_position.Magic(); long pos_id = hist_position.Identifier(); double volume = hist_position.Volume(); double price_open = hist_position.PriceOpen(); double price_sl = hist_position.StopLoss(); double price_tp = hist_position.TakeProfit(); double price_close = hist_position.PriceClose(); double commission = hist_position.Commission(); double swap = hist_position.Swap(); double profit = hist_position.Profit(); string symbol = hist_position.Symbol(); string open_comment = hist_position.OpenComment(); string close_comment = hist_position.CloseComment(); string open_reason_desc = hist_position.OpenReasonDescription(); string close_reason_desc = hist_position.CloseReasonDescription(); string deal_tickets = hist_position.DealTickets(","); int deals_count = HistoryDealsTotal(); // of the selected position //--- sums Balance+=profit+swap+commission; allVols+=volume; allComm+=commission; allSwap+=swap; allProf+=profit; allCnts+=1; //--- SymbolSelect(symbol,true); int digits=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS); double point=SymbolInfoDouble(symbol,SYMBOL_POINT); //--- FileWrite(handle, (string)time_open, pos_id, symbol, type_desc, DoubleToString(volume,2), DoubleToString(price_open,digits), (price_sl ? DoubleToString(price_sl,digits) : ""), (price_tp ? DoubleToString(price_tp,digits) : ""), (string)time_close, DoubleToString(price_close,(deals_count==2 ? digits : digits+3)), DoubleToString(commission,2), DoubleToString(swap,2), DoubleToString(profit,2), MathRound((type == POSITION_TYPE_BUY ? price_close - price_open : price_open - price_close) / point), // DoubleToString(Balance,2), // magic, TimeElapsedToString(time_close - time_open), open_reason_desc, close_reason_desc, open_comment, close_comment, deal_tickets ); } } //--- footer FileWrite(handle,""); FileWrite(handle,"Closed Positions",IntegerToString(allCnts)); FileWrite(handle,"Total Volume",DoubleToString(allVols,2)); FileWrite(handle,"Total Commission",DoubleToString(allComm,2)); FileWrite(handle,"Total Swap",DoubleToString(allSwap,2)); FileWrite(handle,"Total Profit",DoubleToString(allProf,2)); FileWrite(handle,"Total Net P/L",DoubleToString(Balance,2)); //--- Print("Total closed positions = ",hist_position.PositionsTotal()); PrintFormat("Time elapsed = %I64u microsec",GetMicrosecondCount()-s_time); //--- FileClose(handle); //--- return(true); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ string TimeElapsedToString(const datetime pElapsedSeconds) { const long days = pElapsedSeconds / PeriodSeconds(PERIOD_D1); return((days ? (string)days + "d " : "") + TimeToString(pElapsedSeconds,TIME_SECONDS)); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ #import "shell32.dll" int ShellExecuteW(int hWnd,string Verb,string File,string Parameter,string Path,int ShowCommand); #import //+------------------------------------------------------------------+ //| Execute Windows command/program or open a document/webpage | //+------------------------------------------------------------------+ void Execute(const string command,const string parameters="") { //shell32::ShellExecuteW(0,"open",command,parameters,NULL,1); } //+------------------------------------------------------------------+