incorporate spread in calculating stoploss and take profit

 

Hi, I wan to ask about buy and sell or entry price. I've heard about the word 'buy at the ask and sell at the bid' but there's no one talking about spread. Using the code below I want to know which one is correct between adding spread or not.

void OnStart() {
   int distance = 100;
   
   double entry = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   double sl = SymbolInfoDouble(_Symbol, SYMBOL_BID) + iSpread(_Symbol,PERIOD_CURRENT,0) * _Point - distance * _Point;
   double tp = SymbolInfoDouble(_Symbol, SYMBOL_BID) + iSpread(_Symbol,PERIOD_CURRENT,0) * _Point + distance * _Point;
   
   Print("sl distance: ", NormalizeDouble((entry - sl),Digits()));
   Print("tp distance: ", NormalizeDouble((tp - entry),Digits()));

   double entry = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   double sl = SymbolInfoDouble(_Symbol, SYMBOL_BID) - distance * _Point;
   double tp = SymbolInfoDouble(_Symbol, SYMBOL_BID) + distance * _Point;
   
   Print("sl distance (w/o spread): ", NormalizeDouble((entry - sl),Digits()));
   Print("tp distance (w/o spread): ", NormalizeDouble((tp - entry),Digits()));
}

the return value that I got from the code is like this

2024.02.22 16:00:26.218 Test (AUDCAD,M15)       sl distance: 0.00102

2024.02.22 16:00:26.218 Test (AUDCAD,M15)       tp distance: 0.00098


2024.02.22 16:00:45.099 Test (AUDCAD,M15)       sl distance (w/o spread): 0.00122

2024.02.22 16:00:45.099 Test (AUDCAD,M15)       tp distance(w/o spread): 0.00078

Clearly the one using spread closer to initial distance which is 100 points but still there's a 2 points different from the initial value. Where this 2 points differrent coming from?

 
I suggest you calculate spread independently, use ask - bid instead of iSpread function. iSpread return static value to me it usually 10 for most of the time. If you calculate it using ask - bid the value will be different between iSpread and calculated spread and that's why the value distance isn't 100 points away.
 

1. Spread is often a floating value not fixed. You cannot speculate it based on a specific moment.

2. You do not need to count for spread for SL/TP. A buy is closed with a sell(vice versa), so the spread is already calculated.

 
Yashar Seyyedin #:
Spread is often a floating value not fixed. You cannot speculate it based on a specific moment.

I think OP confusing why the distance between SL and TP not 100 points like distance var value.

 
Luandre Ezra #:
I think OP confusing why the distance between SL and TP not 100 points like distance var value.

Yes that's confusing me because the value is different between iSpread and ask - bid price. Thanks for letting me know this been dealing with this problem for days.

Yashar Seyyedin #:
You do not need to count for spread for SL/TP. A buy is closed with a sell(vice versa), so the spread is already calculated.

I tried to do calculation using bid price and ask price.

double sl = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID) - distance * _Point, Digits());
double tp = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID) + distance * _Point, Digits());

If I use this calculation the result would be like this

2024.02.23 09:39:59.675 Test (GBPUSD,M15) sl distance: 0.00111

2024.02.23 09:39:59.675 Test (GBPUSD,M15) tp distance: 0.00089

but if I use this calculation the result is like what I want

double sl = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK) - distance * _Point, Digits());
double tp = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK) + distance * _Point, Digits());

2024.02.23 09:41:03.733 Test (GBPUSD,M15) sl distance: 0.001

2024.02.23 09:41:03.733 Test (GBPUSD,M15) tp distance: 0.001

I think I need to use the entry price which is the ask price so that the distance is the same. I think I understand where the problem is. Thank you
 
your calculation is correct in the distance but in reality it different. If you buy at the ask you already pay the spread in the front so use bid price in calculating the sl and tp price. If you sell you pay the spread at the end when you close it so use ask price as to calculate sl and tp price.
 
Luandre Ezra #:
your calculation is correct in the distance but in reality it different. If you buy at the ask you already pay the spread in the front so use bid price in calculating the sl and tp price. If you sell you pay the spread at the end when you close it so use ask price as to calculate sl and tp price.
Actually, you always pay the spread upfront.

A trade will always have the shortee distance between open and close as profit.

Buy opens on ask and closes in bid. Sell opens on bid and closes ask.

For a profitable position, you first ha e to overcome the spread. While for a loosing position, you will always be facing the spread added in already.
 
My understanding is that spread is paid when the close price use ask and the reason is that candle bar use bid price. When we open short position we buy at the bid price which is the price that chart shows and close short at the ask price which is bid+spread price. That's why I said that short position paid the spread at the close or end of the trade.
 
Luandre Ezra #:
My understanding is that spread is paid when the close price use ask and the reason is that candle bar use bid price. When we open short position we buy at the bid price which is the price that chart shows and close short at the ask price which is bid+spread price. That's why I said that short position paid the spread at the close or end of the trade.
I understand your conclusions, but if you think this through, then a sell position (which does not get bought at bid price, but sold), would be break even at exactly that moment, and only by closing the position, the loss would be seen.

But that's not what is happening. You sell at the bid price and instantly you are at a loss, due to the spread. Because at the moment you sell, you are by contract obligated to buy back your lending (the actual contract size, lot volume), and therefore you are instantly in a need to fulfill your contractual obligation. So as a matter of fact, you are forced to buy at the ask price the volume you have currently lended. The broker grants you this obligation as long as your account balance is high enough to actually fulfill this obligation.

This is also true for a buy position.

What you see on the chart has little to do with what you actually sign up to in the moment you open a position.
Reason: