1 (edited by geektrader 2019-01-17 03:01:05)

Topic: Logging the spread in the comment field of each trade [MT4 CODE]


I just thought I would share how I log the spread of every trade in the order comment field in MT4, which I am doing since a long time already to always being able to check at what spread each trade of each strategy was really executed at on average. I am using that data to adjust the spread for future backtests / optimizations of each strategy.

All you´ll have to do is to open the exported MQ4 code from EA Studio and replace this in the "OpenPosition" function:

string comment    = IntegerToString(Magic_Number);


string comment    = IntegerToString(Magic_Number) + ", Spread (points): " + DoubleToString((Ask - Bid) / _Point, 0);

Make sure that your Magic_Number (in the EA input settings) is not too long, as the order comment field at your broker can usually only hold 31 chars at the most. Alternatively, you can remove the Magic_Number from the order comment field as well, as it is anyway assigned in the Magic Number field of the trade already. Then the final code would look like this:

string comment    = "Spread (points): " + DoubleToString((Ask - Bid) / _Point, 0);

Have fun :-)