Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops Pine脚本-试图计算售前低点的平均值_Loops_Average_Pine Script - Fatal编程技术网

Loops Pine脚本-试图计算售前低点的平均值

Loops Pine脚本-试图计算售前低点的平均值,loops,average,pine-script,Loops,Average,Pine Script,我不太熟悉pine脚本,试图计算和绘制上市前的平均低价 不幸的是,我的代码似乎不能正常工作,我找不到原因 var preMarketSession = "0400-0930" //setting the premarket time isPreMarket = not na(time(timeframe.period, "0400-0930")) //boolean for if the session is currently in premarket

我不太熟悉pine脚本,试图计算和绘制上市前的平均低价 不幸的是,我的代码似乎不能正常工作,我找不到原因

var preMarketSession = "0400-0930" //setting the premarket time
isPreMarket = not na(time(timeframe.period, "0400-0930")) //boolean for if the session is currently in premarket

var int barsInPreMarket = 0
var float averageLow = na


// Confirming first bar of premarket
if isPreMarket and not isPreMarket[1]
    averageLow := 0


//Counting the number of bars in premarket
if (isPreMarket and barstate.isnew)
    barsInPreMarket := barsInPreMarket + 1


//Calculating the avg of low prices on end of bar
if isPreMarket and barstate.isconfirmed
    for i=0 to barsInPreMarket - 1
        averageLow := (averageLow + low) / barsInPreMarket

// Resseting the car count and avarage upon exiting premarket
if not isPreMarket
    barsInPreMarket := 0
    averageLow := na


plot(averageLow)
这段代码导致了这种混乱:


如果有人能给我指出正确的方向,我会非常感激的

问题似乎要么是值为na时的处理,要么是使用barstate。关于na值,脚本允许对na值进行计算,您必须使用
nz()
来呈现该值
0
,或者使用
na()
来测试条件并相应地采取行动。我不确定barstate是否导致了这些问题,但这应该是没有必要的

//@version=4
study("My Script",overlay=true)
var preMarketSession = "0400-0930" //setting the premarket time
isPreMarket = not na(time(timeframe.period, "0400-0930")) //boolean for if the session is currently in premarket

var int barsInPreMarket = 0
var float lowSum = na
var float averageLow = na


if isPreMarket
    if na(lowSum)
        barsInPreMarket := 1
        lowSum := low
        averageLow := low
    else
        barsInPreMarket := barsInPreMarket + 1
        lowSum := lowSum + low
        averageLow := lowSum / barsInPreMarket
else if isPreMarket[1]
    lowSum := na
    barsInPreMarket := 0

plot(averageLow)
小心使用na值很重要,因为您不知道脚本何时开始执行。它可以在上市前中期开始,也就是说,当平均值为NA时,它使用平均数来计算平均值。