I am new to Pine-Script Currently I am playing with some existing scripts.
I am wondering if the Strategy Tester is bugged.
I have EDITED the question, simplifying the script a lot, just to focus on my problem, as suggested by the commenters.
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]
Thats why i have defined
strategy("Debug Qty and Returns ", default_qty_type=strategy.cash, default_qty_value=100000,overlay=true, precision=5)
I've tried the following script on the ticker BITMEX:XBTUSD interval DAILY
//@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() =>
(time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < 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)
With the chosen chart and time interval, there are only 3 signals.
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
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
This is not the behaviour that I expect.
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 ?