//@version=4
study("TWAP Trend", "TWAP", true)
smoothing = input(14)
resolution = input("0", "Timeframe") //240 D
src = input(ohlc4)
res = resolution != "0" ? resolution : timeframe.period
weight = barssince(change(security(syminfo.tickerid, res, time, lookahead=barmerge.lookahead_on)))
price = 0.
price:= weight == 0 ? src : src + nz(price[1])
twap = price / (weight + 1)
ma = smoothing < 2 ? twap : sma(twap, smoothing)
bullish = iff(smoothing < 2, src >= ma, src > ma)
disposition = bullish ? color.lime : color.red
basis = plot(src, "OHLC4", disposition, linewidth=1, transp=100)
work = plot(ma, "TWAP", disposition, linewidth=2, transp=20)
fill(basis, work, disposition, transp=65)
showcross = input(false, title="Show Buy/Sell")
crossup = ma[1] < src[1] and ma > src
crossdn = ma[1] > src[1] and ma < src
plotshape(showcross and crossup and not crossup[1] ? src : na, location=location.absolute, style=shape.labeldown, color=color.red, size=size.tiny, text="Sell", textcolor=#ffffff, transp=0, offset=-1)
plotshape(showcross and crossdn and not crossdn[1] ? src : na, location=location.absolute, style=shape.labelup, color=color.lime, size=size.tiny, text="Buy", textcolor=#ffffff, transp=0, offset=-1)
In finance, time-weighted average price (TWAP) is the average price of a security over a specified time. Below are some contrasting differences between TWAP and VWAP:-
- TWAP is weighted based on time, VWAP is weighted based on time and volume.
- Small volume trades do not impact TWAP but it does impact VWAP
- TWAP calculation is fairly simple while VWAP calculation is complex
- VWAP trade will buy or sell 40% of a trade in the first half of the day and then the other 60% in the second half of the day. A TWAP trade would most likely execute an even 50/50 volume in the first and second half of the day.
I made a coding request for this one. I didnt know it would end up as a forum post, but this is a coding request. I wasnt sure how to convert this from pine script to pro real code.
Please post a screenshot of how it should look on a chart. Do you want it to work on v10.3? (other timeframe than the actual one is only possible with v11).
Below is a screenshot! I can work on the current timeframe. I use it personally on a 30 min. It doesnt matter so much the settings, it just to cut it in a nice way and then use the risk control.