Spread costs in MQL5

niceno

Trader
Jun 28, 2023
6
0
7
55
Dear all,

I am new to trading and am developing my first strategies in MQL5. Up to now, I am only testing different strategies in Strategy Tester. Whenever I open a position, I am essentially buying, I only "go long", because "going short" is still a bit nebulous in my mind.

Imagine I start with 10'000. I open a position (buy it) and use these functions to check the account status in a log file:
MQL5:
FileWrite(LogFile, " - balance:                 ", AccountInfoDouble(ACCOUNT_BALANCE));
FileWrite(LogFile, " - equity:                  ", AccountInfoDouble(ACCOUNT_EQUITY));
FileWrite(LogFile, " - profit:                  ", AccountInfoDouble(ACCOUNT_PROFIT));
double spreadCost = SymbolInfoInteger(_Symbol, SYMBOL_SPREAD) * _Point * Lots * CONTRACT_SIZE;
FileWrite(LogFile, " - spread cost:             ", NormalizeDouble(spreadCost, _Digits));

After that purchase I see the following lines in the log file:
Code:
- balance:     10000.0
- equity:       9988.9
- profit:        -11.1
- spread cost:    11.1

Equity is balance plus profit, 10000 - 11.1 = 9988.9, all is fine and logical. The profit (-11.1) is due to spread cost, which I double check with last two lines of the code snippet above and third and fourth line in the log file. So far all is still clear.

However, when I close the position (sell it in my case), which I sold for mere 2.7 more than I bough it, I get the following output in my log file:
Code:
- balance:      9996.02
- equity:       9996.02
- profit:          0.0
- spread cost:    11.4

Although I earned 2.7, I also had to pay for keeping the position over one night. I checked it with a call to: HistoryDealGetDouble(closeTicket, DEAL_SWAP); and it amounts to 6.68. So, I started with 10000, earned 2.7, but spent 6.68 for deal swap, so balance is 10000 - 6.68 + 2.7 = 9996.02, but I don't see the spread costs ever accounted for? Where did all the spread costs go?

I am trying to be cautions / conservative in the assessment of my strategies, but I can't see how did spread costs dissapear from the MQL5's AccountInfoDouble functions?

Could anyone please shed light on that?

Cheers
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,210
1,504
144
Odesa
www.earnforex.com
The spread cost didn't go anywhere. When you opened the trade you had a floating loss of -11.10 in terms of spread cost. Since you closed the trade at +2.70 profit, it means that the price went up by 13.80 from the level it had been when you opened the trade. So, it's basically:
10,000.00 - 11.10 + 13.80 - 6.68 = 9,996.02.
 

niceno

Trader
Jun 28, 2023
6
0
7
55
The spread cost didn't go anywhere. When you opened the trade you had a floating loss of -11.10 in terms of spread cost. Since you closed the trade at +2.70 profit, it means that the price went up by 13.80 from the level it had been when you opened the trade. So, it's basically:
10,000.00 - 11.10 + 13.80 - 6.68 = 9,996.02.

Thanks Enivid. So, if you allow me to put it different words, at the moment I opened a trade I immediately lose on the spread, because if I sold it at the same moment, that's how much I would lost. However, at the time I close the position it doesn't matter any more, because I sold it at the price I sold it, and that's all. If you allow me to draw an analogy, as if I walked into a car dealer and bough a car for 35'000$, I would immediately lose (say) 3'000.$ But that doesn't matter at all when I sell the car. When I sell it, I get what a buyer is ready to pay for?
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,210
1,504
144
Odesa
www.earnforex.com
Thanks Enivid. So, if you allow me to put it different words, at the moment I opened a trade I immediately lose on the spread, because if I sold it at the same moment, that's how much I would lost. However, at the time I close the position it doesn't matter any more, because I sold it at the price I sold it, and that's all. If you allow me to draw an analogy, as if I walked into a car dealer and bough a car for 35'000$, I would immediately lose (say) 3'000.$ But that doesn't matter at all when I sell the car. When I sell it, I get what a buyer is ready to pay for?
Yes, that's right. Your analogy is spot on!
 
  • 👍
Reactions: niceno