1
votes

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 ?

3
Write a small, readible script, without all these unnecessary inputs, tricky conditions and million of variables. I think it'll help you to understand how it works and find the problem.Michel_T.
@Michel_T. thanks for the advice. I did simplify the script a lot to focus on the issue. It is persisting. This is not the behaviour I expect when i read the documentation.Fagui Curtain

3 Answers

1
votes

Both your questions/issues are described here: https://www.tradingview.com/wiki/Strategies#Broker_emulator the broker emulator topic covers your second issue and the Order placement commands covers first one.

In short (sorry for pun), you're putting two order with the same conditions, wich are filled in one bar. So that's emulates a situation, which sometimes happens with real broker. At first, long-position is filled, so you get BTC for 100k usd, then the second order says: 'I'd like to be in deficit of BTC in 100k', for that strategy tester sells your BTC for 100k and sells 100k more to make 'short' position.

0
votes

thanks Michel_T

If I change strategy.close into strategy.exit

the problem disappears

0
votes

This is huge and well-known bug in TradingView backtester. Whenever strategy.close condition aligns with strategy.entry condition (so one trade is closed at the same time as next one is opened), some very very ugly entry is added to List of Trades - a trade is entered with a signal "Close entry(s) order..." ??? It doesn't even make sense.

Entries like this are included in backtest calculation, therefore destroying it completely. Sometimes they are improving backtest results, sometimes they are making them worse - but all the time they are not correct.

Solution: export entire List of Trades, paste into Excel and remove those ugly trades manually. Export feature is provided by ProvitView or AutoView Chrome addons.