Page 1 of 1

Higher timeframes barstate not working

Posted: Fri Mar 04, 2022 10:15 am
by Francois
Can someone help me with the below code? I am having real trouble with it. I have thought myself to code through the youtube videos. My problem is as follows: I have taken the vumancho moneyflow code, and adjusted it to handle different time frames. After that I added a calculation where I calculate the average moneyflow while the moneyflow is green and when it is red. What I am having trouble with is, when I add the script to the 15min chart (while calculating the daily average moneyflow), the daily moneyflow and average moneyflow should stay constant(yesterdays) while today's daily candle is still unconfirmed. What is happening is when the script is added to the chart is shows past data correctly, but all newly data incorrectly(not yesterdays data but newly calculated), when I refresh/ reapply the script the data is then correct but all new data that is currently happening is incorrect again? I am using the barstate.isrealtime in the security function.
I would really really appreciate anyone's help. This has kept me stuck for the last 3 days.

//@version=4
study("MF testing", overlay = false)

plot(0,color=color.white,title="Zero Line")
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Money flow timeframe 5
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------

// RSI+MFI

usetf5 = input(true,title="Use TF5", group = "Use what TF",type=input.bool, inline='TF5')
resolutiontf5 = input("D",title="MFI Resolution for TF5", type = input.resolution,group="Use what TF", inline='TF5')
rsiMFIperiodtf5 = 60
rsiMFIMultipliertf5 = 150
rsiMFIPosYtf5 = 2.5

// RSI+MFI
f_rsimfitf5(_period, _multiplier, _tf) => security(syminfo.tickerid, _tf, sma(((close[barstate.isrealtime ? 1:0] - open[barstate.isrealtime ? 1:0]) / (high[barstate.isrealtime ? 1:0] - low[barstate.isrealtime ? 1:0])) * _multiplier, _period) - rsiMFIPosYtf5)


// RSI + MFI Area
rsiMFItf5 = f_rsimfitf5(rsiMFIperiodtf5, rsiMFIMultipliertf5, resolutiontf5)
mfcolortf5 = color.red
mfcolortf5:= rsiMFItf5 >0 ?color.green:color.red

//Green Moneyflow
gmfcounttf5 = 0.0
gmfsumtf5 = 0.0
gmfaveragetf5 = 0.0

gmfcounttf5 := rsiMFItf5 == rsiMFItf5[1] ? gmfcounttf5[1]: rsiMFItf5 > 0 ? gmfcounttf5[1] + 1 : gmfcounttf5[1]
gmfcounttf5 := rsiMFItf5 == rsiMFItf5[1] ? gmfcounttf5[1]: crossunder(rsiMFItf5,0) ? 0 : gmfcounttf5
gmfsumtf5 := rsiMFItf5 == rsiMFItf5[1] ? gmfsumtf5[1] : rsiMFItf5 > 0 ? gmfsumtf5[1] + rsiMFItf5 : gmfsumtf5[1]
gmfsumtf5 := rsiMFItf5 == rsiMFItf5[1] ? gmfsumtf5[1] : crossunder(rsiMFItf5,0) ? 0 : gmfsumtf5
gmfaveragetf5 := rsiMFItf5 == rsiMFItf5[1] ? gmfaveragetf5[1]: rsiMFItf5 > 0 ? gmfsumtf5/gmfcounttf5 : 0

plot(gmfaveragetf5>0 ? gmfaveragetf5:na,title = "Average calculated Green Moneyflow TF5",color = color.yellow, linewidth = 1, style = plot.style_circles)

// Red Moneyflow

rmfcounttf5 = 0.0
rmfsumtf5 = 0.0
rmfaveragetf5 = 0.0

rmfcounttf5 := rsiMFItf5 == rsiMFItf5[1] ? rmfcounttf5[1]: rsiMFItf5 < 0 ? rmfcounttf5[1] + 1 : rmfcounttf5[1]
rmfcounttf5 := rsiMFItf5 == rsiMFItf5[1] ? rmfcounttf5[1]: crossover(rsiMFItf5,0) ? 0 : rmfcounttf5
rmfsumtf5 := rsiMFItf5 == rsiMFItf5[1] ? rmfsumtf5[1] : rsiMFItf5 < 0 ? rmfsumtf5[1] + rsiMFItf5 : rmfsumtf5[1]
rmfsumtf5 := rsiMFItf5 == rsiMFItf5[1] ? rmfsumtf5[1] : crossover(rsiMFItf5,0) ? 0 : rmfsumtf5
rmfaveragetf5 := rsiMFItf5 == rsiMFItf5[1] ? rmfaveragetf5[1]:rsiMFItf5 < 0 ? rmfsumtf5/rmfcounttf5 : 0

belowzeroaboveaveragetf5 = rsiMFItf5 > rmfaveragetf5 and rsiMFItf5 < 0
abovezeroaboveaveragetf5 = rsiMFItf5 >= gmfaveragetf5 and rsiMFItf5 > 0
abovezerobelowaveragetf5 = rsiMFItf5 < gmfaveragetf5 and rsiMFItf5 > 0
belowzerobelowaveragetf5 = rsiMFItf5 <= rmfaveragetf5 and rsiMFItf5 < 0

colortf5 = color.black
colortf5 := belowzeroaboveaveragetf5 ? color.yellow: abovezeroaboveaveragetf5 ? color.green : abovezerobelowaveragetf5 ? color.orange : belowzerobelowaveragetf5 ? color.red : na

plot(rmfaveragetf5< 0?rmfaveragetf5: na,title = "Average Calculated Red Moneyflow TF5", color = color.orange, linewidth = 1, style = plot.style_circles)

plot(rsiMFItf5, style=plot.style_circles,color=colortf5,linewidth=2,title="Moneyflow")