title: TradingView's PineScript - how are dynamic data "series" variables evaluated?
body: <p><strong>Introduction</strong></p>
  <p>Pinescript is a propriety programming language for backtesting trading algorithms. The language has always been interesting to me because it is orientated around series which as a result certain features works in a way that differs from normal languages.</p>
  <p>The four main series variables, <code>open</code>, <code>high</code>, <code>low</code> and <code>close</code> point to arrays that form bar data, so if you were to write the code</p>
  <pre><code>plot(close)
  </code></pre>
  <p>It would plot the close prices on the graph. That's easy enough to understand as under the hood the variable <code>close</code> is just pointing to a array of float price data and the <code>plot</code> method is plotting out that array.</p>
  <p>Similarly the code</p>
  <pre><code>plotchar(close &gt; open, 'A')
  </code></pre>
  <p>Will plot the character 'A' where the close value is greater than the open value in the arrays.</p>
  <p>If statements work in unique ways when a comparison of two series take place:</p>
  <p>1.</p>
  <pre><code>if (close &gt; open)
       strategy.entry(id=&quot;EL&quot;, long=true, qty=5)
  </code></pre>
  <p>Which is equivalent to</p>
  <p>2.</p>
  <pre><code>strategy.entry(id=&quot;EL&quot;, when = close &gt; open, long=true, qty=5)
  </code></pre>
  <p>In example 1, the if statement results are applied to all the arguments of the <code>strategy.entry</code> method ( wherever any series is referenced as long as it's in that if statement scope). They key parameter in this case is the <code>when</code> parameter. When not defined the <code>when</code> parameter points to an array full of true values. However since its inside a scope where series values can only be evaluated where <code>close &gt; open</code>, the result is that it works in exactly the same way as example 2.</p>
  <p>The example above is not directly relevant to my question, but I kept it in to emphasise how Pinescript works in different ways to what might be expected in a traditional programming language.</p>
  <p><strong>Dynamic data</strong></p>
  <p>The part that I do not understand is when certain special variables get mixed into normal expressions. These variable's data do not exist until certain functions are called, however they appear to be evaluated as one would intuitively expect regardless of the fact that their data cannot exist at the time of the parser reading the input code.
  For example the series variable <code>strategy.position_size</code> is a series variable (of floats) that contains the overall position size as the orders are evaluated. E.g.</p>
  <pre><code>strategy.entry(id=&quot;EL&quot;, when = close &gt; open, long=true, qty=5)
  plot(strategy.position_size)
  </code></pre>
  <p>This will plot out the position size on the chart. This is simple enough, except that the below code will also yield the same results:</p>
  <pre><code>plot(strategy.position_size)
  strategy.entry(id=&quot;EL&quot;, when = close &gt; open, long=true, qty=5)
  </code></pre>
  <p>This suggests that the <code>positon_size</code> variable is treated as some kind of special variable that will not be evaluated till any of the order functions (<code>strategy.entry</code>) are executed. Maybe the compiler will detect that the line of code <code>plot(strategy.position_size)</code> cannot be evaluated at that point so is saved till after any code that affects this variable (<code>strategy.entry</code>) is ran.</p>
  <p>However the bit where it gets confusing is the fact that you can use these special variables within expressions that would directly effect the result of that variable. For example:</p>
  <pre><code>strategy.entry(id=&quot;EL&quot;, when = strategy.position_size==0, long=true, qty=5)
  </code></pre>
  <p>When the compiler reaches the <code>strategy.entry</code> method, it see's that it should only enter a position if <code>position_size == 0</code>. However the <code>position_size</code> variable data is directly dependent on when <code>strategy.entry</code> is called. This seems like a catch 22 scenario and I am unsure as to how the compiler would handle this code.</p>
  <p>Do you have any idea how the Pinescript compiler handles these special &quot;dynamic&quot; variables?
  Are there any documented examples of a scenario like this occurring in different programming languages?</p>
  <p>Apologies if my terminology is not on point or confusing. I've been getting into interpreter design for only a year now and I still have a lot to learn.</p>