简体   繁体   中英

Pine-Script function

I need some help to create a function as below, I want to get the sum of red volume in a defined period and the sum of green volume in this same period, to do the difference between both.

sumvolup(lenght) => 
    sumvolup = float(0)
    volwhengreen = valuewhen(close-open>=0, volume, 1)
    sumvolup := sum(volwhengreen, lenght)
    sumvolup
    
sumvoldown(lenght) => 
    sumvoldown = float(0)
    volwhenred = valuewhen(open-close>=0, volume, 1)
    sumvoldown := sum(volwhenred, lenght)
    sumvoldown

My issue is shown in the image below: when I try to check if my function works fine with a plotted char on my indicator, using sumvolup(1)>sumvoldown(1) , II get plotted of the red bar volume.

截图问题

It's best to include a compilable code snippet when asking questions, including the plotting code, if you are having problems with it.

This may help you get on your way:

//@version=4
study("")
i_length = input(20)

// This is the equivalent of your snippet, in shorter form.
volUp = close >= open ? volume : 0
volDn = close <= open ? volume : 0
sumvolup   = sum(volUp, i_length)
sumvoldown = sum(volDn, i_length)

// This plots the information.
volIsUp = sumvolup > sumvoldown
plot(sumvolup, "sumvolup", color.green)
plot(sumvoldown, "sumvoldown", color.red)
plotchar(volIsUp[1], "volIsUp[1]", "▲", location.top, size = size.tiny)

Note that in here, where the conditions are equivalent to yours, you are counting volume twice when close == open :

volUp = close >= open ? volume : 0
volDn = close <= open ? volume : 0

在此处输入图像描述

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.

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