Messed this up and I'm sure the results shouldn't be this good! Please help!


I’m currently on the Trading Strategy : Strategy Improvement task and I’m pretty sure I’m messed something up. Starting from the code in the previous section my first though was to simply put some form of stop loss mechanism in before I went looking at the other indicators, and while I know what I put in was wrong and doesn’t do what I was intending its getting great results and I’ve been trying to figure out why. Also being of the option that if something is too good to be true, it probably is, I’m also sure this shouldn’t work. OK so here is my code

strategy(title=“MA Crossing Strategy”, overlay=true, initial_capital=2000, commission_type=strategy.commission.percent, commission_value=0.2)

//sma inputs
shortVal = input(defval=40, title=“Short SMA”, minval=1)
longVal = input(defval=80, title=“Long SMA”, minval=1)

//Date and Time
fromMonth = input(defval=2, title=“From month”, minval=1)
fromDay= input(defval=15, title=“From day”, minval=1)
fromYear= input(defval=2019, title=“From year”, minval=2014)

toMonth = input(defval=5, title=“To month”, minval=1)
toDay= input(defval=15, title=“To day”, minval=1)
toYear= input(defval=2019, title=“To year”, minval=2014)

short=sma(close, shortVal)
long=sma(close, longVal)

timeInRange=(time > timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time < timestamp(toYear, toMonth, toDay,23,59))

//buy=short >= long
longSignal = crossover(short, long) and timeInRange
// Sell on long
shortSignal = timeInRange and crossover(long, short)

//StopLoss level - I know this is wrong now as its looking at the current candle not the original entry price
long_stop_level = close - 250

strategy.entry(id=“longPosition”, long=true, qty=0.1,when=longSignal, comment=“buy”)
strategy.entry(id=“longPosition”, long=false, qty=0.1, when=shortSignal, comment=“sell”)

strategy.exit(“TP/SL”, “longPosition”, qty=0.1, stop=long_stop_level)

So barring the dodgy long_stop_level indicator which I know why its wrong all I added was the strategy exit and the results really good, but I can’t wrap my head around why!

I figure its something like when the short entry is hit when the original long position is closed its opening a short position (fair enough), but most of the short trades in the list of trades make a loss anyway (as the exit is then triggering).

I’m a real noob at this so please could someone with a bit more experience cast an eye over this and smack me round the head and point out why its significantly more profitable (in most data series I can check) and generally whats going on with it (as well as tell me if it won’t really work)

Please be gentle