Fxxtrader
Pine Script Rookie
Pine Script Rookie
Posts: 22
Joined: September 6th, 2020
Location: Toronto
Contact: Twitter TradingView Profile

Using var

Hello - how are you?

I need help with better understanding var and how to use it?
In a strategy script, I want to create a trade execution when two or more indicators give a trade signal, though the signal may come from different bars. Right now, my code is only executing trades when both indicators give the signal at the same time. Here is the script with the rules, which is no longer working that I added var:

Code: Select all

//  AROON INDICATOR

length      = input(14, title="Aroon Length", minval=1, step=2)
upper       = 100 * (highestbars(high, length+1) + length)/length
lower       = 100 * (lowestbars(low, length+1) + length)/length
plot(upper, "Aroon Up", color=#FF6A00)              //#FF6A00 is orange  BUY
plot(lower, "Aroon Down", color=#0094FF)            //#0094FF is blue    Sell

aroonBuy = crossover(upper, lower)
aroonSell = crossover(lower, upper)

var aroonBuySig = 0.0
var aroonSellSig = 0.0
if aroonBuy
    aroonBuySig := true

// VORTEX INDICATOR

period_ = input(14, title="Vortex Length", minval=3, step=2)
VMP = sum( abs( high - low[1]), period_ )
VMM = sum( abs( low - high[1]), period_ )
STR = sum( atr(1), period_ )
VIP = VMP / STR                             // blue buy
VIM = VMM / STR                             // red sell
// plot(VIP, title="VI +", color=color.blue) // #3BB3E4)
// plot(VIM, title="VI -", color=color.maroon) //

//  Vortex Trade Signals
vortexBuy = crossover(VIP, VIM)                                     // If vip > vip I want this to be saved true until not true anymore
vortexSell = crossover(VIM, VIP)                                    // if vim > vip I want this to be saved true until not true anymore

var vortexBuySig = 0.0
var vortexSellSig = 0.0
if vortexBuy
    vortexBuy := true                                               // save Buy signal until Sell signal is given. Need to save until Aroon is also buy signal

// Execute trade when 
avBuy = (aroonBuy and vortexBuy)                  // Buy trade when both Aroon and Vortex say buy, though they each may become true at different times
avSell = (aroonSell and vortexSell)              // opposite for sell trade

// before adding var trades were only executing when both aroon and vortex both gave signal on the same bar.  
//How do fix so that trade may execute when both are in agreement, which may happen on different bar, and only one trade at a time.  
Thank you.

Fxxtrader
Pine Script Rookie
Pine Script Rookie
Posts: 22
Joined: September 6th, 2020
Location: Toronto
Contact: Twitter TradingView Profile

Re: Using var

Hi, I think I found my error. Replaced 0.0 with 0, changed true to + 1 and now script compiles and strategy results is returned.

Code: Select all

aroonBuy = crossover(upper, lower)
aroonSell = crossover(lower, upper)

var aroonBuySig = 0
var aroonSellSig = 0
if aroonBuy
    aroonBuySig := + 1
if aroonSell
    aroonSellSig := + 1

However, I think my logic and coding still do not match. Though, I am having some fun. :cool:

kmarryat
Pine Script Rookie
Pine Script Rookie
Posts: 20
Joined: December 21st, 2020
Contact: TradingView Profile

Re: Using var

Hi,
Using var will initialize your variable once instead of reinitializing it on every new bar. It looks like you're using the var part correctly to maintain your variable values over multiple bars. I think you are trying to mix variable types with your if tests though. Pine Script sees a 0 and 1 as integers, but true and false as Boolean values. Unlike other scripting languages you can't mix true and false with 0 and 1. If you use an if statement with a variable containing any integer/number it will return true.

So this will return true and Do Something will execute

Code: Select all

MyVar = 2
if MyVar
    Do Something
Also, changing a variable value with := +1 won't add 1 to it's value, it will just change it's value to positive 1

If you want to add 1 to a variables value you need to do it like this.
MyVar := MyVar +1


The code below should get the result you were looking for. You will need to reset your BuySig and SellSig values back to false after you enter the trade.

Code: Select all

aroonBuy = crossover(upper, lower)
aroonSell = crossover(lower, upper)

var aroonBuySig = false
var aroonSellSig = false

if aroonBuy
    aroonBuySig := true
if aroonSell
    aroonSellSig := true

If you prefer to leave your original code which set's your BuySig and SellSig to 1, then you should change your trade execution to use a comparison operator == . Otherwise your trade will execute whenever BuySig and SellSig are anything but 0.

Code: Select all

// Execute trade when 
avBuy = (aroonBuy == 1 and vortexBuy ==1)             // Buy trade when both Aroon and Vortex say buy, though they each may become true at different times
avSell = (aroonSell ==1 and vortexSell ==1)              // opposite for sell trade 
Good Luck!

Return to “Pine Script Q&A”