NathanYsp: My goal is get the Position history of my account into excel spreadsheet automatically. No export to CSV or stuff like that. I found a lot of examples to get real time price, open and close trades, bid, ask and etc... nothing about retrieve the trading history. The same way you get the automatic price and orders by the second I would like to have the history as soon and I open nor close the trade the excel is updated with new data. Thank you.
Not possible via standard MQL. You will have to use DLL calls. You will have to build your own bridge between the two.
NathanYsp: My goal is get the Position history of my account into excel spreadsheet automatically. No export to CSV or stuff like that.
That is exactly what you should do. Write at CSV file and have Excel import it.
I went in that route. I used Expot_History_Position_V2 to Mql5 and I import into Excel with PowerQuery.
Now Im trying to use a EA to run the export code automatically every x minutes, so the data get refreshed automatically.
This is the script code, do you know if its possible to adapt that for a EA?
**This is not my script, I only made a few changes to fit my need**
Now Im trying to use a EA to run the export code automatically every x minutes, so the data get refreshed automatically.
This is the script code, do you know if its possible to adapt that for a EA?
**This is not my script, I only made a few changes to fit my need**
//+------------------------------------------------------------------+ //| Export_History_Positions_V2.mq5 | //| Copyright © 2018, Amr Ali | //| https://www.mql5.com/en/users/amrali | //+------------------------------------------------------------------+ #property 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); } //+------------------------------------------------------------------+

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
My goal is get the Position history of my account into excel spreadsheet automatically. No export to CSV or stuff like that. I found a lot of examples to get real time price, open and close trades, bid, ask and etc... nothing about retrieve the trading history.
The same way you get the automatic price and orders by the second I would like to have the history as soon and I open nor close the trade the excel is updated with new data.
Thank you.