TradingView
fikira
8 Feb 2022 pukul 18.27

Explaining VAR 

Bitcoin / United States DollarCoinbase

Deskripsi

Explaining VAR

Best to use on a Monthly Timeframe with a Ticker that exists longer then 2 years (example Bitcoin )
Then open the console so you can read and follow the instructions and explanation in the script

-> Perhaps technically some things could be better explained, but the main goal here is to explain 'var' in a rather simple way

Hope this helps, cheers!

Catatan Rilis

This is meant for Pine Script coders who have trouble with the distinction between
'var' declared variables and 'non var' declared variables

Catatan Rilis

This script is part of a collection of educational scripts
If you want to return to the open source INDEX page, click here
-> Education: INDEX
Komentar
Trendoscope
Good educational content for new coders. Well done mate :)
fikira
@HeWhoMustNotBeNamed, Thanks mate, appreciate it!
Trendoscope
Few hidden gems of var is when you use it in methods. Was just discussing this with @JohnBaron
increment()=> var i=0 i+=1 i result = 0 result2 = 0 for i=1 to 5 result := increment() result2 := increment() plot(result-result[1]) plot(result2-result)


Try this in your code and see what each will print. Conclusion is:

1. Each line of function call keeps its own copy of var.
2. Statements within loop use same var.

That means, result will increment 5 times each bar and so is result2. But, result and result2 keep different var to keep track of the value.
fikira
@HeWhoMustNotBeNamed, Yeah, in this case (if I see it correctly) a new var isn't declared on every bar since every bar references to the same variable (result & result2), so the functions will work the same way as:
var result = 0 result += 5

Nice to play with it! Thanks! :)
fikira
@HeWhoMustNotBeNamed, What I think is really freaky is the following:
step1 = input.bool(false, group='step 1', title='start') step2 = input.bool(false, group='step 2', title='start') // -------[ fresh start ]------- varip vp_Both_Off = 0 // both off varip vp_1_On_2_Off = 0 // 1 on 2 off varip vp_1_Off_2_On = 0 // 1 off 2 on varip vp_Both_On = 0 // both on f_reset(what, when) => varip then = 0 if what < when then +=1 else then := 0 [then] if barstate.islast if not step1 if not step2 (Rvp_Both_Off ) = f_reset(vp_Both_Off , 1000), vp_Both_Off := Rvp_Both_Off else (Rvp_1_Off_2_On) = f_reset(vp_1_Off_2_On, 1000), vp_1_Off_2_On := Rvp_1_Off_2_On else if not step2 (Rvp_1_On_2_Off)= f_reset(vp_1_On_2_Off, 1000), vp_1_On_2_Off := Rvp_1_On_2_Off else (Rvp_Both_On )= f_reset(vp_Both_On , 1000), vp_Both_On := Rvp_Both_On plot(vp_Both_Off) plot(vp_1_On_2_Off) plot(vp_1_Off_2_On) plot(vp_Both_On)


There are 4 possibilities (step1 on - step2 off, step1 on - step2 on, step1 off - step2 on, step1 off - step2 off),
in this care 4 different variables are declared, and they all remember their state
This you'll see when switching -> while plots are 0, counting goes further in the background,
example:
step1 on - step2 off -> count 1 -> 5 (5 ticks further)
when step2 is enabled -> previous varip will return 0, BUT when disabling step2 again after 10 ticks, it will show 15, NOT 0 or 5!

If you combine previous snippet together with this, you'll get something really freaky
step1 = input.bool(false, group='step 1', title='start') step2 = input.bool(false, group='step 2', title='start') varip _varip = 0 if barstate.islast _varip +=1 plot(_varip)


Even without linking step1 or step2 to varip, it gives 4 DIFFERENT VARIP's with the same values as previous snippet!
Trendoscope
@fikira, hmm. i am not sure if I am missing something. I can't seem to find where Rvp variables declared. They are just been used for assignment.
fikira
@HeWhoMustNotBeNamed, Yeah, [ if filtered out, replace ( -> [ ...
and in the function -> add (then) also with [
fikira
@HeWhoMustNotBeNamed, This should be easier I suppose :)
pastebin.com/FT2s9Juh
Trendoscope
@fikira, yes. It just keeps different varip for each line. Pretty handy if you want to keep multiple counters running!!
fikira
@HeWhoMustNotBeNamed,
I think it is mind blowing that while declaring just 1 varip, 4 different varip's are created
But the way as I see it, it all has to do with 'storylines'
Each time a different option is chosen, there is a new 'storyline' where a separate varip is created (declared)
-> 4 different outcomes (option) -> 4 different storylines

Returning to the same option gives you the varip of that storyline, the other 3 storylines are 'invisible' ('0'), but they keep on working on the background.

Took a while for me to grasp this :)
Lebih lanjut