title: Strategy Tester in TradeView : problem with Qty and Returns
body: <p>I am new to Pine-Script
  Currently I am playing with some existing scripts.</p>
  
  <p>I am wondering if the Strategy Tester is bugged.</p>
  
  <p>I have <strong>EDITED</strong> the question, simplifying the script a lot, just to focus on my problem, as suggested by the commenters.</p>
  
  <p>1) I have problems with quantities. I wrote this script which takes a simple signal (cross of 2 MA) and tries to flip a long/short position of always the same size [100,000 USD]</p>
  
  <p>Thats why i have defined </p>
  
  <pre><code>strategy("Debug Qty and Returns ", default_qty_type=strategy.cash, default_qty_value=100000,overlay=true, precision=5)
  </code></pre>
  
  <p>I've tried the following script on the ticker BITMEX:XBTUSD interval DAILY</p>
  
  <pre><code>//@version=3
  strategy("Debug Qty and Returns ", default_qty_type=strategy.cash, default_qty_value=100000,overlay=true, precision=5)
  
  // Upon execution, why are some short trades with a notional of 200,000 USD equivalent ?
  
  tradeType   = input("BOTH", title="Trade Type ", options=["LONG", "SHORT", "BOTH"])
  
  // === BACKTEST RANGE ===
  FromMonth = input(defval = 1, title = "From Month", minval = 1)
  FromDay   = input(defval = 1, title = "From Day", minval = 1)
  FromYear  = input(defval = 2019, title = "From Year", minval = 2014)
  ToMonth   = input(defval = 1, title = "To Month", minval = 1)
  ToDay     = input(defval = 1, title = "To Day", minval = 1)
  ToYear    = input(defval = 9999, title = "To Year", minval = 2014) 
  
  testPeriod() =&gt;
      (time &gt; timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time &lt; timestamp(ToYear, ToMonth, ToDay, 23, 59))
  
  
  //////////////////////////////
  
  isLongOpen = false
  isShortOpen = false
  
  //Order open on previous ticker?
  isLongOpen := nz(isLongOpen[1])
  isShortOpen := nz(isShortOpen[1])
  
  ////////////
  //Somes EMAs to trigger trades
  ema7Avg = ema(ohlc4, 7)
  ema30Avg = ema(ohlc4, 30)
  
  plot(ema7Avg)
  plot(ema30Avg)
  
  
  //Entry Conditions
  shortEntry= (tradeType=="SHORT" or tradeType=="BOTH") and crossunder(ema7Avg,ema30Avg )
  longEntry = (tradeType=="LONG" or tradeType=="BOTH") and crossover(ema7Avg,ema30Avg )
  
  
  
  ///////////////////
  
  shortExit = isShortOpen[1] and longEntry
  longExit = isLongOpen[1] and shortEntry
  
  
  if(testPeriod() and (tradeType == "LONG" or tradeType == "BOTH" ))
      strategy.entry("long", strategy.long, when=longEntry)
      strategy.close("long", when = longExit)
  
  if(testPeriod() and (tradeType == "SHORT" or tradeType == "BOTH" ))
      strategy.entry("short", strategy.short, when=shortEntry)
      strategy.close("short", when = shortExit)
  
  
  //If the value changed to invoke a buy, lets set it before we leave
  isLongOpen := longEntry  ? true : (longExit == true ? false : isLongOpen)
  isShortOpen := shortEntry ? true : (shortExit == true ? false : isShortOpen)
  
  plotshape(isShortOpen,  title= "Short Open", color=red, style=shape.circle, location=location.bottom)
  plotshape(isLongOpen,  title= "Long Open", color=green, style=shape.circle, location=location.bottom)
  </code></pre>
  
  <p>With the chosen chart and time interval, there are only 3 signals.</p>
  
  <p>it opens a long on Jan8th for 24.96 contracts (BTC) at a price of 4,005 which have a value of 24.96 so that the total value spent is 4,005*24.96 = 100,000 usd as desired</p>
  
  <p>Then, the strategy tester shows that when the strategy flips on Jan12th, it does close (at a loss) the long position at 3,631.5 and goes short at the same price, HOWEVER the quantity of the short position is 52.5055 contracts (BTC) which corresponds to a total value of (3,631.5*52.5055) = 200,000 USD</p>
  
  <p>This is not the behaviour that I expect.</p>
  
  <p>2) i notice that the strategy.entry command opens the trade at the price equal of the OPEN of the NEXT CANDLE after which the entry condition is fulfilled. Is it the normal behaviour ? </p>