simon.neame
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: May 12th, 2023
Contact: TradingView Profile

Floating Spread

Hey Team, im Nemo, a budding pinescripter and below average trader lol

I have a general question about Floating Spread/Spread. What do people do to deal with Floating Spread? How do you manage automated trading where a Tradingview script has an alert with a fixed Spread input? Do you cancel the alert every so often and create a new alert with a new Spread?

My example here is;
I have a script that works fine, it is connected to MT4 via Pineconnector, everything there works fine, the signals are being generated, sent and received fine, I have the script running in Tradingview on USDSEK 1M through the Oanda broker and my MT4 is logged in to my Oanda demo account.
When the script generates a buy/sell signal at eg. 10.500 I have initially been letting the alert send a Market Order and found the price that MT4 enters at is off, generally above the price the script had, it has been a different price for every trade (I'm guessing this is to do with the bid/ask price which I am also still trying to get my head around).
When I let the alert send a Limit Order I find the price is still not in line with the scripts entry price, even with a 0.6 (I have tried many different number here) spread input.
After looking at the ticker specs in MT4 I came across the Floating Spread type and a bit of googling later I am confused as to how traders deal with Spread if it is constantly changing.

Any help here is greatly appreciated, all info will be gladly absorbed. Also big shout to Matt who has taught me everything I know so far with his fantastic content and lessons.

Nemo

Deep-Wave
Pine Script Master
Pine Script Master
Posts: 44
Joined: September 4th, 2020
Contact: TradingView Profile

Re: Floating Spread

Hi Simon, in your example when your trading view script generates a buy/sell signal and sends a market order to pine connector, there are several factor why you find the prices are off.
First off, TradingView does not account for spread in pine script. That means that programmatically you have one price instead of the bid and ask prices that are used in MT4 to buy (ask price) or sell (bid price) the assets.
As far as I know, the price that is used in TradingView is the mid price. It is the average between the ask and bid price (if you look at a chart and enable in the settings the dashed lines for ask and bid prices, you will see that the candle body is formed by the mid price while above the body you have the ask and below you have the bid. When a buy or sell condition becomes true in TradingView , it becomes true according to the mid price and the order is sent to pine connector. But on MT4 the buy order is executed on the ask and the sell order on the bid, so naturally there is a difference.

Second, if you use a different broker then Oanda on your MT4 terminal then you have slightly different price feeds in TradingView and MT4. That can also account for some of the difference.
You also have to consider latency. By that I mean, the time it takes until the TradingView order is generated, send via webhook, processed by the pine connector web server and expert advisor and finally placed as a trade. I imagine this is a couple of miliseconds so prices can also change during that time.
Last but not least you also might be affected by slippage.

When it comes to limit orders it is a bit different. The limit price you calculate in you trading view script gets sent to pine connector as the entry price. If it is a buy order then the order only gets executed when the ask price is equal to the requested limit price or better (in this case meaning lower). If it is a sell order then the order only gets executed when the bid price is equal to the requested limit price or better (in this case meaning higher).
For more info feel free to check out the following video: https://youtu.be/iE-0zNkdFXo
Cheers :cool:

Return to “Pine Script Q&A”