I create a strategy on Tradingview using Trade Volume Index. It is simple one. Buy when Volume hit HHV (Highest High Volume) and Sell as in Vice Versa.
However I am a newbie into this pinescript. I adapt from kodify strategy example in pinescript V3 on position sizing strategy into the current V4. However It seems run find on BTC/USD as photo attached. BTC/USD But not the case for EURO/USD with Study Error as photo attached EURO/USD
Could you please kinldy help me in 2 parts
Many Many Thanks !
//@version=4
// Step 1) Define strategy settings
strategy(title='Yotyord - TVI', overlay=false,pyramiding=0,initial_capital=100000,commission_type=strategy.commission.cash_per_contract,commission_value=25,slippage=2)
//Session Input Options
smooth = input(title='Signal Smoothness, (<0 to hide):', type=input.integer, defval=4)
length = input(10,title='Highest High Volume')
// Position sizing inputs
usePosSize = input(title="Use Position Sizing?", type=input.bool, defval=true)
maxRisk = input(title="Max Position Risk %", type=input.float, defval=2, step=.25)
maxExposure = input(title="Max Position Exposure %", type=input.float, defval=10, step=1)
marginPerc = input(title="Margin %", type=input.integer, defval=10)
// Stop inputs
atrLen = input(title="ATR Length", type=input.integer, defval=10)
stopOffset = input(title="Stop Offset Multiple", type=input.float, defval=4, step=.25)
// Step 2) Calculate strategy values
//Writing Trade Volume Index graph (TVI)
f_tvi()=>
float _direction = na
float _return_tvi = na
_min_tick=syminfo.mintick
_price_change = close - open
if _price_change > _min_tick
_direction :=1
if _price_change < -_min_tick
_direction :=-1
if abs(_price_change) <= _min_tick
_direction :=_direction[1]
if na(_return_tvi[1])
_return_tvi := volume
else if _direction > 0
_return_tvi := _return_tvi[1] + volume
else if _direction < 0
_return_tvi := _return_tvi[1] - volume
else
_return_tvi[1]
TVI=f_tvi()
tradeWindow = time <= timenow - (86400000 * 3)
stopValue = atr(atrLen) * stopOffset
//Get ATR Values
atrValue = atr(atrLen)
// Calculate position size
riskEquity = (maxRisk * 0.01) * strategy.equity
riskTrade = stopValue * syminfo.pointvalue
maxPos = ((maxExposure * 0.01) * strategy.equity) /
((marginPerc * 0.01) * (close * syminfo.pointvalue))
posSize = usePosSize ? min(floor(riskEquity / riskTrade), maxPos) : 1
//Buy when TVI = HHV and Sell When TVI = LLV
hhV = highest(TVI,length)
llV = lowest(TVI,length)
BuySig = hhV == TVI ? hhV : na
SellSig = llV == TVI ? llV : na
eLong = hhV == TVI
eShort = llV == TVI
plotshape(BuySig, style=shape.circle,title = "Buy Signal",color = color.green,location=location.absolute)
plotshape(SellSig, style=shape.circle,title = "Sell Signal",color = color.red,location=location.absolute)
// Step 3) Output strategy data
plot(TVI,color=color.white,title='TVI')
plot(hhV , color=color.green, title="HHV")
plot(llV , color=color.red, title="LLV")
// Step 4) Determine long trading conditions
enterLong = eLong and tradeWindow
longStop = 0.0
longStop := enterLong ? close - (stopOffset * atrValue) :
longStop[1]
// Step 5) Code short trading conditions
enterShort = eShort and tradeWindow
plotshape(enterLong, style=shape.circle,
title = "Enter Long",
location = location.belowbar,
color = color.green)
plotshape(enterShort, style=shape.circle,
title = "Enter Short",
location=location.abovebar,
color = color.red)
shortStop = 0.0
shortStop := enterShort ? close + (stopOffset * atrValue) :
shortStop[1]
//plot(series=strategy.position_size > 0 ? longStop : na,color=color.green, linewidth=2, style=shape.circle)
//plot(series=strategy.position_size < 0 ? shortStop : na,color=color.red, linewidth=2, style=shape.circle)
// Step 6) Submit entry orders
if (enterLong)
strategy.entry(id="EL", long=true,qty=posSize)
if (enterShort)
strategy.entry(id="ES", long=false,qty=posSize)
// Step 7) Send exit orders
if (strategy.position_size > 0)
strategy.exit(id="XL", from_entry="EL", stop=longStop)
if (strategy.position_size < 0)
strategy.exit(id="XS", from_entry="ES", stop=shortStop)
strategy.close_all(when=not tradeWindow)
有指示如何从松V3自动转换为V4。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.