简体   繁体   English

Pine 脚本 - 尝试计算上市前低点的平均值

[英]Pine script - Trying to calculate the average of lows in premarket

I'm new to pine script and trying to calculate and plot the average low prices in premarket unfortunately my code does not seems to work properly and I cannot find the reason as to why我是 pine 脚本的新手并试图计算 plot 预售的平均低价不幸的是我的代码似乎无法正常工作,我找不到原因

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)

This code results in this mess: Graph这段代码导致了这个混乱:

Would be very thankful if someone could point me in the right direction!如果有人能指出我正确的方向,将不胜感激!

The issue seems to be either the handling of when values are na, or using barstate.问题似乎是处理值何时为 na,或使用 barstate。 Regarding na values the script allows for the possibility of calculating on na values, you must either use nz() to render the value 0 that or use na() to test for the condition and act accordingly.关于 na 值,脚本允许计算 na 值的可能性,您必须使用nz()来呈现值0或使用na()来测试条件并采取相应措施。 I'm not certain if barstate is causing the issues, but it shouldn't be necessary.我不确定 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)

It is important to be careful with na values, as you don't know when your script will begin execution.小心 na 值很重要,因为您不知道脚本何时开始执行。 It could start in the middle of pre-market, meaning that it's using averageLow to calculate averageLow when averageLow is na.它可以在上市前的中间开始,这意味着当averageLow 为na 时,它使用averageLow 来计算averageLow。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM