DennisLi
DennisLi

Reputation: 4154

TradingView does not calculate and show the correct value of MFI indicator

My entry condition is simple, but TV does not order with the correct condition. The MFI is lower than 70 in fact.

enter image description here

Update

//@version=4

initial_capital = 30000

strategy("test", shorttitle="test", overlay=true, 
       default_qty_type=strategy.fixed, currency=currency.USD, calc_on_every_tick=false,
       initial_capital=initial_capital,commission_type=strategy.commission.percent, commission_value=0, pyramiding=1)
    

src = input(defval=close)
 
start_time = input(type=input.time, defval=timestamp("01 Jan 2018 00:00 +0800"), step=1)
end_time = input(type=input.time, defval=timestamp("10 Jul 2031 00:00 +0800"), step=1)

window() => time >= start_time and time <= end_time ? true : false

debug = input(false)


//MACD
[macdLine, signalLine, histLine] = macd(close, 12, 26, 9)


pap = nz(strategy.position_avg_price)
profit = 100*(close - nz(strategy.position_avg_price)) / nz(strategy.position_avg_price) 

var buy_signal_cnt = 0
var last_buy_signal_cnt = 0 
var last_sell_signal_cnt = 0
var last_short_price = 0.0 
var last_long_price = 0.0 

var buy_once = false
buy_once:=false

var sell_once = false
sell_once:=false

stopPer = input(defval=1.5, type=input.float, step=0.01)
takePer = input(defval=0.1, type=input.float, step=0.01)

// Determine where you've entered and in what direction
longStop = strategy.position_avg_price * (1 - stopPer)
longTake = strategy.position_avg_price * (1 + takePer)
shortStop = strategy.position_avg_price * (1 + stopPer)
shortTake = strategy.position_avg_price * (1 - takePer)

slope_limit = input(0.01, step=0.01)

profit_limit1 = input(0.3,step=0.01)

if window() and timeframe.isintraday
    // Long -----------------------------------------------
    //if crossover(ma1, ma2) and long_ma_slope > slope_limit and strategy.position_size == 0
    if mfi(close, 14) < 35 and strategy.position_size == 0
    //if trend == 1 and trend[1] == -1
        strategy.entry("Long", strategy.long, qty=1, comment='B01')
        last_long_price := close
        buy_once := true

        
    strategy.exit("SL/TP", "Long", limit=longTake, stop=longStop)
    
    // Short -----------------------------------------------

    if profit > profit_limit1
        strategy.close('Long', comment='cls0')
        //strategy.entry("Short", strategy.short, qty=contracts, comment='S01')
        last_short_price := close
        sell_once := true
 
//plot(last_buy_signal_cnt)

if not buy_once 
    last_buy_signal_cnt += 1
else
    last_buy_signal_cnt:=0


if not sell_once 
    last_sell_signal_cnt += 1
else
    last_sell_signal_cnt:=0



Upvotes: 0

Views: 514

Answers (1)

Starr Lucky
Starr Lucky

Reputation: 1951

The Money Flow Index built-in indicator uses hlc3 as a source series.

So if you want to reference with built-in you need to use

if mfi(hlc3, 14) < 35 and strategy.position_size == 0

Upvotes: 1

Related Questions