Problem generating Oninit code?
-
Hi everyone,
I'm trying to create a csv file in OnInit() and not having much luck. It's a basic as it gets atm but it doesn't seem to be generating the code in the mq5 file. Any ideas?
-
you just need a pass block above it, single blocks don't work on FX
-
@jstap Ahhh....got it! Thanks jstap
I'm considering buying your book. Do you have a pdf copy that I'd be able to read on a ipad?
-
No mate, only the Amazon version, you can get the app to view it on the ipad.
-
Great, I’ll look into it. Thanks.
-
-
Use the "Write to File" Block
files should be saved to the MetaTrader Files folder (typically MQL5/Files/).Step-by-Step Setup:-
-Add the "Write to File" Block into your event handler (e.g., OnInit, OnTick, or OnTimer)
-Set the filename (e.g., data.csv)
-Define Data to Export: Connect data blocks (variables, OHLC data, indicator values) to the 'Write to File' block
-Set Write Mode: Choose between REWRITE (overwrite) or APPEND (add to existing file).
-
Data Export Limitations
For example, exporting custom indicator data may require additional configuration beyond basic OHLC and volume data. -
Thanks for the help everyone. I'm using the custom mql code block as I can code a little. I'm trying to output all deals (ENTRY_IN and ENTRY_OUT) and eventually I will be logging the indicator values at time of entry and exit.
At the moment it is only logging DEAL_ENTRY_OUT deals. Can anyone see what I'm doing wrong? The code is a follows and under On trade.
//=========================================== // FXDreema On Trade Custom Block Logger //=========================================== // static variable to track last logged deal static ulong last_logged_deal = 0; // get total number of deals int total_deals = HistoryDealsTotal(); if(total_deals <= 0) return; // get the latest deal ticket ulong deal_ticket = HistoryDealGetTicket(total_deals - 1); if(deal_ticket == 0) return; // avoid logging the same deal twice if(deal_ticket == last_logged_deal) return; last_logged_deal = deal_ticket; // select the deal if(!HistoryDealSelect(deal_ticket)) return; //------------------------------------------- // get deal info //------------------------------------------- ulong deal_order = HistoryDealGetInteger(deal_ticket, DEAL_ORDER); ulong position_id = HistoryDealGetInteger(deal_ticket, DEAL_POSITION_ID); string symbol = HistoryDealGetString(deal_ticket, DEAL_SYMBOL); long deal_type_i = HistoryDealGetInteger(deal_ticket, DEAL_TYPE); long deal_entry_i = HistoryDealGetInteger(deal_ticket, DEAL_ENTRY); long deal_reason_i = HistoryDealGetInteger(deal_ticket, DEAL_REASON); datetime deal_time = (datetime)HistoryDealGetInteger(deal_ticket, DEAL_TIME); double volume = HistoryDealGetDouble(deal_ticket, DEAL_VOLUME); double profit = HistoryDealGetDouble(deal_ticket, DEAL_PROFIT); double price = HistoryDealGetDouble(deal_ticket, DEAL_PRICE); double swap = HistoryDealGetDouble(deal_ticket, DEAL_SWAP); double commission = HistoryDealGetDouble(deal_ticket, DEAL_COMMISSION); long magic = HistoryDealGetInteger(deal_ticket, DEAL_MAGIC); string comment = HistoryDealGetString(deal_ticket, DEAL_COMMENT); //------------------------------------------- // get current market prices //------------------------------------------- double bid = SymbolInfoDouble(symbol, SYMBOL_BID); double ask = SymbolInfoDouble(symbol, SYMBOL_ASK); double spread = ask - bid; //------------------------------------------- // convert enums to readable strings //------------------------------------------- string deal_type = ""; switch(deal_type_i) { case DEAL_TYPE_BUY: deal_type="DEAL_TYPE_BUY"; break; case DEAL_TYPE_SELL: deal_type="DEAL_TYPE_SELL"; break; case DEAL_TYPE_BALANCE: deal_type="DEAL_TYPE_BALANCE"; break; case DEAL_TYPE_CREDIT: deal_type="DEAL_TYPE_CREDIT"; break; default: deal_type="OTHER"; } string deal_entry = ""; switch(deal_entry_i) { case DEAL_ENTRY_IN: deal_entry="DEAL_ENTRY_IN"; break; case DEAL_ENTRY_OUT: deal_entry="DEAL_ENTRY_OUT"; break; case DEAL_ENTRY_INOUT: deal_entry="DEAL_ENTRY_INOUT"; break; default: deal_entry="OTHER"; } string deal_reason = ""; switch(deal_reason_i) { case DEAL_REASON_EXPERT: deal_reason="DEAL_REASON_EXPERT"; break; case DEAL_REASON_SL: deal_reason="DEAL_REASON_SL"; break; case DEAL_REASON_TP: deal_reason="DEAL_REASON_TP"; break; case DEAL_REASON_SO: deal_reason="DEAL_REASON_SO"; break; case DEAL_REASON_CLIENT: deal_reason="DEAL_REASON_CLIENT"; break; default: deal_reason=IntegerToString(deal_reason_i); } //------------------------------------------- // write to CSV //------------------------------------------- int file = FileOpen("MyStrategy.csv", FILE_READ|FILE_WRITE|FILE_CSV|FILE_SHARE_WRITE|FILE_COMMON, ","); if(file != INVALID_HANDLE) { FileSeek(file,0,SEEK_END); FileWrite(file, deal_ticket, deal_order, position_id, symbol, deal_type, deal_entry, deal_reason, TimeToString(deal_time,TIME_DATE|TIME_SECONDS), DoubleToString(volume,2), DoubleToString(profit,2), DoubleToString(price,_Digits), DoubleToString(swap,2), DoubleToString(commission,2), magic, comment, DoubleToString(bid,_Digits), DoubleToString(ask,_Digits), DoubleToString(spread,_Digits) ); FileClose(file); } -
Ask AI:
Looking at the code, I can spot a few issues:
- static variable won't work in a Custom Block
static ulong last_logged_deal = 0; — Static variables inside FXDreema custom blocks don't persist between calls the way they would in a native MQL5 function. This means last_logged_deal will reset every tick, so the duplicate-deal guard will never actually work.
Fix: Use a Global Variable instead:
mql5ulong last_logged_deal = (ulong)GlobalVariableGet("LastLoggedDeal");
And after updating it:
mql5GlobalVariableSet("LastLoggedDeal", (double)deal_ticket); - No HistorySelect call before accessing history
HistoryDealsTotal() and HistoryDealGetTicket() require history to be loaded first. Without this, you may get 0 deals even when history exists.
Fix: Add before your history calls:
mql5HistorySelect(0, TimeCurrent()); - Missing CSV header row
The file is opened in append mode (SEEK_END) every time, so there's no header written. This makes the CSV harder to work with in Excel etc. You'd want to check if the file is new/empty and conditionally write a header row first. - _Digits used for spread
Spread is typically displayed as points, not price digits. Using _Digits gives you a raw price difference like 0.00010 rather than the more readable 1.0 (in points). Consider:
mql5DoubleToString(spread / SymbolInfoDouble(symbol, SYMBOL_POINT), 1) - FILE_SHARE_WRITE without FILE_SHARE_READ
If anything else tries to read the file while it's open, it'll be blocked. Adding FILE_SHARE_READ is safer:
mql5FILE_READ|FILE_WRITE|FILE_CSV|FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_COMMON
The most critical of these is #2 (missing HistorySelect) and #1 (the static variable not persisting) — both could cause the logger to silently log nothing or log duplicates.
- static variable won't work in a Custom Block
-
Thanks @jstap . I have managed to get it to work by manually adding some code to the OnTradeTransaction() event function after fxdreema has generated its code. Is there a way of specifying the OnTradeTransaction function from fxdreema?/ I thought maybe it would be possible using a custom function but I'm not familiar with that part of fxdreema yet.
-
Yes (possibly) you could generate a custom block, but this gets quite difficult, but if you understand how you may manage it, the easiest way is to ask AI a few questions, on trade may go into here, in here you will see how the sections work, for this I think you need to treat the setting section as on trade which is where you would use the block, ask ai what code should go in what section. Save and use the block in a project, any errors that come up will be from what is written in "create custom blocks, keep changing/saving/refreshing project.
-
It's annoying. When accessing deals and writing to csv I only seem to get DEAL_ENTRY_OUT deals when using the custom mql block in onTrade(). When I manually put the code into ontradeTransaction() it works fine. This is after making sure I use HistorySelect first.
FYI that code was generated by chatGPT.
-
Of cause it was, even great coders will use AI, is much quicker than Writing it yourself. This will give you an idea of what goes where: https://fxdreema.com/forum/topic/21603/close-all-mt5-trades-quickly/14
