title: Variables update when they shouldn't
body: <p>I'm trying to make a Pine Script for TradingView that will buy and sell based on MACD but only if the change is large enough. However my lastLong and lastShort variables keep updating even though no trade was made. How do i make sure lastLong and lastShort only update on an actual trade please?</p>
  <p><a href="https://i.stack.imgur.com/1NpG6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1NpG6.png" alt="enter image description here" /></a></p>
  <p>Edit: Unfortunately the answer suggested didn't fix the issue, the red line (lastShort) disappeared and the blue (lastLong) still updates more often than there are trades.
  <a href="https://i.stack.imgur.com/oLrPK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oLrPK.png" alt="enter image description here" /></a></p>
  <pre><code>//@version=4
  
  strategy(&quot;MACD Strategy&quot;, overlay=true)
  // Variables
  fastLength = input(12)
  slowlength = input(26)
  MACDLength = input(9)
  desiredGain = 1.05
  desiredLoss = 1.025
  lastLong = 1.0
  lastLong := na(lastLong[1]) ? na : lastLong[1]
  lastShort = 1.0
  lastShort := na(lastShort[1]) ? na : lastShort[1]
  
  inDateRange = (time &gt;= timestamp(syminfo.timezone, 2021, 04, 01, 0, 0))// and (time &lt; timestamp(syminfo.timezone, 2021, 05, 16, 0, 0))
  
  // Calculation
  fastMA = ema(close, fastLength)
  slowMA = ema(close, slowlength)
  macd = fastMA - slowMA
  signal = sma(macd, MACDLength)
  esignal = ema(macd, MACDLength)
  delta = macd - esignal
  
  didRiseEnough = close/lastLong &gt; desiredGain
  didFallEnough = lastShort/close &gt; desiredLoss
  
  if na(lastLong) // If lastLong is uninitialized, run the first entry without checking didFallEnough
      if (inDateRange and crossover(macd, esignal))
          strategy.entry(&quot;MacdLE&quot;, strategy.long, comment=&quot;MacdLE&quot;)
          lastLong := close
  else
      if (inDateRange and crossover(macd, esignal) and didFallEnough)
          strategy.entry(&quot;MacdLE&quot;, strategy.long, comment=&quot;MacdLE&quot;)
          lastLong := close
          
  if na(lastShort) // If lastShort is uninitialized, run the first entry without checking didRiseEnough
      if (inDateRange and crossunder(macd, esignal))
          strategy.entry(&quot;MacdSE&quot;, strategy.short, comment=&quot;MacdSE&quot;)
          lastShort := close
  else
      if (inDateRange and crossunder(macd, esignal) and didRiseEnough)
          strategy.entry(&quot;MacdSE&quot;, strategy.short, comment=&quot;MacdSE&quot;)
          lastShort := close
  
  // Plot
  plot(macd * 10 + close/2, color=color.blue)
  //plot(signal * 10 + close/2, color=color.orange)
  plot(esignal * 10 + close/2, color=color.orange)
  
  plot(lastLong, color=color.blue)
  plot(lastShort, color=color.red)
  
  plotshape(didRiseEnough, style=shape.arrowup, color=color.red)
  plotshape(didFallEnough, style=shape.arrowdown, color=color.blue)
  
  if (not inDateRange)
      strategy.close_all()
  </code></pre>