OPEN-SOURCE SCRIPT

Slippage Calculator

Hello everyone,


This is the first script I publish, also my English is not the best my apologies.

This simple study script is an attempt to estimate the slippage during a trade. By no means it is a precise calculation, it is just an approach that can be improved.

You are welcome to take this simple script and use it and change it at your convenience just ask your acknowledge.
My approach considers the total fluctuation the price gets as a consequence for the total volume on that specific bar.
The volume on each bar is given in BTC, thus you get that by using the formula.

vol = volume * vwap

The total variation in price is considered as twice the size of the bar plus the gap between to consecutive bars. that is the
total fluctuation of price on each bar = (2 * (high - low) + abs(open - close[1])) which is not totally true since inside each bar price can fluctuate a lot more.
The script considers you are trading your total equity (eq_BTC ) each time. The fraction of your equity of the volume bar is eq_BTC / vol
Then eq_BTC / vol is the portion or the total fluctuation in price that is due to your entry or exit from the market.

(2 * (high - low) + abs(open - close[1])) * eq_BTC / vol
is the average change in price due you enter o exit a position.

** the 2 factor accounts for the two directions in the market buying and selling. There is more behind this formula I can explain you in more detail if you like.

I haven't seen anywhere a formula like this one, so it is intended to be a first attempt to get a better approach.

Finally the output of the scripts is how many ticks the price might change due to your trade on each bar

round(slippage/syminfo.mintick)

The script is focused in crypto but it can be used as well on forex markets.

Take care,

yvponce


fluctuatingprice-volumeslippageVolatility

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer