Reading Assignment: Strategy Improvements

I used for this test a simple strategy, the crossover of 2 MA (short and long). I started with 50 days MA and 20 days MA, tested on 2017 interval. At first I tried to insert a stop-loss to improve the results, but it failed to do so. I changed the MA-s to 100 and 9, it improved the results for 2017 (selected segment) and also for other segments (2018-2020). The script:

//@version=3

strategy(title=“Trading Assignment: Strategy Improvements”, overlay = true, currency = currency.USD, initial_capital=2000, commission_type=strategy.commission.percent, commission_value=0.2, default_qty_type=strategy.cash, default_qty_value=2000)

//DATA AND TIME
fromMonth = input(defval=1, title =“From month”, minval=1)
fromDay = input(defval=1, title =“From day”, minval=1)
fromYear = input(defval=2017, title =“From year”, minval=1)

toMonth = input(defval=12, title =“To month”, minval=1)
toDay = input(defval=31, title =“To day”, minval=1)
toYear = input(defval=2017, title =“To year”, minval=2014)

// DEFINITIONS
LongMa = sma(close, 100)
ShortMa = sma(close, 9)

//PLOT
plot(series=LongMa, color=green)
plot(series=ShortMa, color=orange)

//LOGIC
timeInRange = (time > timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time < timestamp(toYear, toMonth, toDay, 23, 59))
longSignal = crossover(ShortMa, LongMa) and timeInRange
shortSignal = crossover(LongMa, ShortMa) and timeInRange

exitLong = crossover(LongMa, ShortMa)
exitShort = crossover(ShortMa, LongMa)

//POSITIONS
strategy.entry(id=“longposition”, long=true, when=longSignal)
strategy.entry(id=“shortposition”, long=false, when=shortSignal)

strategy.close(“longposition”, exitLong)
strategy.close(“shortposition”, exitShort)

1 Like

Does anyone have an idea how to add the Bollinger Band indicator to the script? Please let me know!

here i added the rsi indicator with some alterations. works really well in the current bullmarket of 2020-2021… wasn’t really able to test it for 2017-2019 as data was unfortunately not available(??)…

// Moving Average Crossover with RSI - Tradingbot exercise

strategy(title=“Moving Average Strategy”, overlay=true, initial_capital=500, commission_type=strategy.commission.percent, commission_value=0.2)

//Date & Time
fromMonth = input(defval=1, title=“from month”, minval=1)
fromDay = input(defval=15, title=“from day”, minval=1)
fromYear = input(defval=2021, title=“from year”, minval=2014)

toMonth = input(defval=3, title=“to month”, minval=1)
toDay = input(defval=15, title=“to day”, minval=1)
toYear = input(defval=2021, title=“to year”, minval=2014)

//Definitions
shortMa = sma(close, 43)
longMa = sma(close, 78)
rsi = rsi(close, 27)

//Logic
timeInRange = time > timestamp(fromYear, fromMonth, fromDay, 00, 00) and
time < timestamp(toYear, toMonth, toDay, 23, 59)
longSignal = crossover(shortMa, longMa) and timeInRange and rsi<70
shortSignal = crossover(longMa, shortMa) and timeInRange and rsi>45

//Positions
strategy.entry(id=“longPosition”, long=true, qty=0.3, when=longSignal)
strategy.entry(id=“shortPosition”, long=false, qty=0.3, when=shortSignal)

1 Like

Maybe this guide can help you set up the proper code for it :nerd_face:

https://mycoder.pro/apibridge/bollinger-band-pinescript-strategy/

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

Thank you Carlos, appreciate it!! Will get back to you if i need more help. Thanks a lot

1 Like

Screenshot 2021-03-22 180629

This is my code, but the thing is when I try to add it to the chart, a notification pops up saying that I can use a maximum of 3 indicators and no information on the strategy tester changes anymore.

1 Like

Probably you have added many indicators on your chart and reach the limit for the free account.

That case you just need to remove one of the indicators in order to add another one (because the free account is limited to 3 indicators max in the same chart)

image

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

1 Like

Just wondering what I’m doing wrong. In the video Filip gets a lot of trades. I am only getting on average 1 within the timeframe. I’m pretty sure this is the same code. Any help would be great! I can’t seem to see what I’m doing wrong.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © trueogls

//@version=4
strategy(title=“Moving Average Crossing”, overlay=true, initial_capital=2000, commission_type=strategy.commission.percent, commission_value=0.2)

//Date and Time
fromMonth = input(defval=5, title = “From month”, minval=1)
fromDay = input(defval=15, title = “From day”, minval=1)
fromYear = input(defval=2020, title = “From Year”, minval=2014)

toMonth = input (defval=8, title = “To month”, minval=1)
toDay = input(defval=15, title = “To day”, minval=1)
toYear = input(defval=2020, title = “To Year”, minval=2014)

//Definitions
shortMa = sma(close, 20)
longMa = sma(close, 50)

//Logic
timeInRange = (time > timestamp(fromYear, fromMonth, fromDay, 00,00)) and (time < timestamp(toYear, toMonth, toDay, 23, 59))
longSignal = crossover(shortMa, longMa) and timeInRange
shortSignal = crossover(longMa, shortMa) and timeInRange

//Positions
strategy.entry(id=“longPosition”, long=true, qty=0.1, when=longSignal)
strategy.entry(id=“shortPosition”, long=false, qty=0.1, when=shortSignal)

1 Like

Hey @LukeS, hope you are great.

Your codes looks and works well, the difference that you are facing on the trades are basically the different timelines on using the same strategy. Meaning, the price, volume and other factors where completely different when Filip records the video, the bitcoin price was completely different, so all trading factors are different.

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

@thecil Thanks for the reply! yes I realized that the dates were still off once I posted it ahah. but that was just testing different time periods. when I try it on the same time periods 2/2018-5/2018 I still only get 2 trades.

1 Like

Hi, it was my first time :rofl:
I tried a strategy with three EMA crossing, and I would like to use the overbought and underbough RSI level to close the orders, to improve the results. I wrote it, but after thousands try and changing I’m exhausted :grimacing:
it steel doesn’t work, is in the script but it doesn’t do nothings.
Any suggestions and help would be really appreciated;
thanks!
Schermata 2021-04-07 alle 15.43.31|690x391

Schermata 2021-04-07 alle 15.42.00

1 Like

Ok found it, but now I got another two issues:
1, the close works only on the shorts order…
2, after a stop, the strategy open again a new order, because the condition is steel valid… any suggestion?
Thanks

Schermata 2021-04-07 alle 16.12.43

Not trying to go too advance at the moment, as i was trying to add multiple indicators and it only cause things to get … weird. Utalizing this code and starting with my long at 70 and my short at 10, i found a profit factor of 0.46, and when switching to 40, 10, it increased to 2.96. This was for 2015.
However, When doing the same for 2016 and 2017 i found the opposite to be true. 70,10 was 2.57 and 17.46 profit rate and 40, 10 was 1.5 and 13.45 respectively.
It turns out 70, 10 might be the better option!

70,10 in 2017
image

40,10 in 2017
image

//@version=3

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

//Date and Time
fromMonth = input(defval=1, title=“From Month”, minval=1)
fromDay = input(defval=1, title=“From Day”, minval=1)
fromYear = input(defval=2017, title=“From Year”, minval=1)

toMonth = input(defval=12, title=“To Month”, minval=1)
toDay = input(defval=31, title=“To Day”, minval=1)
toYear = input(defval=2017, title=“To Year”, minval=2014)

//DEFINITIONS
longMa = sma(close, 70)
shortMa = sma(close, 10)

//PLOT
plot(series=shortMa, color=green)
plot(series=longMa, color=purple)

//LOGIC
timeInRange = (time > timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time < timestamp(toYear, toMonth, toDay, 23, 59))
longSignal = crossover(shortMa, longMa) and timeInRange
shortSignal = crossover(longMa, shortMa) and timeInRange

exitLong = crossover(longMa, shortMa)
exitShort = crossover(shortMa, longMa)

//Positions
strategy.entry(id=“longPosition”, long=true, qty=0.1, when=longSignal)
strategy.entry(id=“shortPosition”, long=false, qty=0.1, when=shortSignal)

strategy.close(“longPosition”, exitLong)
strategy.close(“shortPosition”, exitShort)

2 Likes

i put
// definition
shortMa = sma(close, 65)
longMa = sma(close, 10)

and got 817 dollars. :smiley:

1 Like

@Hank all I can say is WOW great piece of work.

I’ve spent enough time with this, got the idea, it’s a nice feature. But I wouldn’t depend on this strategy. This is a simple moving average strategy, using 20,200 MA. Added a small delay on the long position, and then then sell as soon as the close crosses below the 20 MA.

image

strategy(title=“Moving Average Tester”, overlay=true, initial_capital=2000, commission_type=“strategy.commission.percent”, commission_value=0.2)

// Date and time
fromYear = input(defval=2021, title=“From year”, minval=2014)
fromMonth = input(defval=1, title=“From month”, minval=1)
fromDay = input(defval=1, title=“From day”, minval=1)
toYear = input(defval=2021, title=“To year”, minval=2014)
toMonth = input(defval=4, title=“To month”, minval=1)
toDay = input(defval=19, title=“To day”, minval=1)

// Definitions
shortMa = sma(close, 20)
longMa = sma(close, 200)

// Logic
timeInRange = (time > timestamp(fromYear, fromMonth, fromDay, 0, 0)) and (time < timestamp(toYear, toMonth, toDay, 23, 59))
longSignal = crossover(shortMa+100, longMa) and timeInRange
sellSignal = crossover(shortMa, close) and timeInRange

// Positions
strategy.entry(id=“longPosition”, long=true, qty=0.01, when=longSignal)
strategy.close(id=“longPosition”, when=sellSignal)

1 Like

//@version=4

strategy(title = “Stoch RSI + MACD + RSI”, overlay = true, initial_capital = 2000, commission_type = strategy.commission.percent, commission_value = 0.2)

//DATE AND TIME
fromMonth = input(defval = 10, title = “From month”, minval = 1)
fromDay = input(defval = 15, title = “From day”, minval = 1)
fromYear = input(defval = 2020, title = “From year”, minval = 2014)

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

//DEFINITIONS
//–Stochastic RSI–
smoothK = input(3, “K”, minval=1)
smoothD = input(3, “D”, minval=1)
lengthRSI = input(14, “RSI Length”, minval=1)
lengthStoch = input(14, “Stochastic Length”, minval=1)
src = input(close, title=“RSI Source”)
rsi1 = rsi(src, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(k, smoothD)
//–MACD–
// Getting inputs
fast_length = input(title=“Fast Length”, type=input.integer, defval=12)
slow_length = input(title=“Slow Length”, type=input.integer, defval=26)
sorc = input(title=“Source”, type=input.source, defval=close)
signal_length = input(title=“Signal Smoothing”, type=input.integer, minval = 1, maxval = 50, defval = 9)
sma_source = input(title=“Simple MA (Oscillator)”, type=input.bool, defval=false)
sma_signal = input(title=“Simple MA (Signal Line)”, type=input.bool, defval=false)
// Calculating
fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)
slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
hist = macd - signal
//–RSI–
r = rsi(close, 14)

//LOGIC
timeInRange = (time > timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time < timestamp(toYear, toMonth, toDay, 23, 59))
longSignal1 = crossover(k, d) and timeInRange
longSignal2 = crossover(macd, signal) and timeInRange
longSignal3 = r < 50 and timeInRange
shortSignal1 = crossover(d, k) and timeInRange
shortSignal2 = crossover(signal, macd) and timeInRange
shortSignal3 = r > 50 and timeInRange

//POSITIONS
strategy.entry(id = “longPosition”, long = true, qty = 0.1, when = longSignal1 and longSignal2 and longSignal3)
strategy.entry(id = “shortPosition”, long = false, qty = 0.1, when = shortSignal1 and shortSignal2 and shortSignal3)

image

//@version=4

strategy(title = “Stoch RSI + MACD + RSI”, overlay = true, initial_capital = 2000, commission_type = strategy.commission.percent, commission_value = 0.2)

//DATE AND TIME
fromMonth = input(defval = 1, title = “From month”, minval = 1)
fromDay = input(defval = 15, title = “From day”, minval = 1)
fromYear = input(defval = 2021, title = “From year”, minval = 2014)

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

//DEFINITIONS
//–Stochastic RSI–
smoothK = input(3, “K”, minval=1)
smoothD = input(3, “D”, minval=1)
lengthRSI = input(14, “RSI Length”, minval=1)
lengthStoch = input(14, “Stochastic Length”, minval=1)
src = input(close, title=“RSI Source”)
rsi1 = rsi(src, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(k, smoothD)
//–MACD–
// Getting inputs
fast_length = input(title=“Fast Length”, type=input.integer, defval=12)
slow_length = input(title=“Slow Length”, type=input.integer, defval=26)
sorc = input(title=“Source”, type=input.source, defval=close)
signal_length = input(title=“Signal Smoothing”, type=input.integer, minval = 1, maxval = 50, defval = 9)
sma_source = input(title=“Simple MA (Oscillator)”, type=input.bool, defval=false)
sma_signal = input(title=“Simple MA (Signal Line)”, type=input.bool, defval=false)
// Calculating
fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)
slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
hist = macd - signal
//–RSI–
r = rsi(close, 14)

//LOGIC
timeInRange = (time > timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time < timestamp(toYear, toMonth, toDay, 23, 59))
longSignal1 = crossover(k, d) and timeInRange
longSignal2 = crossover(macd, signal) and timeInRange
longSignal3 = r < 50 and timeInRange
shortSignal1 = crossover(d, k) and timeInRange
shortSignal2 = crossover(signal, macd) and timeInRange
shortSignal3 = r > 50 and timeInRange

//POSITIONS
strategy.entry(id = “longPosition”, long = true, qty = 0.1, when = longSignal1 and longSignal2 and longSignal3)
strategy.entry(id = “shortPosition”, long = false, qty = 0.1, when = shortSignal1 and shortSignal2 and shortSignal3)

image

2 Likes
//@version=4
strategy(title = "My Strategy", overlay = true, initial_capital = 2000, commission_type = strategy.commission.percent, commission_value = 0.2)

//Date and Time
fromMonth = input(defval = 8, title = "From month", minval = 1)
fromDay = input(defval = 1, title = "From day", minval = 1)
fromYear = input(defval = 2020, title = "From year", minval = 2014)

toMonth = input(defval = 12, title = "To month", minval = 1)
toDay = input(defval = 1, title = "To day", minval = 1)
toYear = input(defval = 2020, title = "To year", minval = 2014)


//Definitions
shortMa = sma(close, 45)
longMa = sma(close, 60)
r = rsi(close, 21)


//Logic
timeInRange = (time > timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time < timestamp(toYear, toMonth, toDay, 23, 59))
longSignal = crossover(shortMa, longMa) and timeInRange and r > 50
shortSignal = crossover(longMa, shortMa) and timeInRange and r < 50

//Positions
strategy.entry(id = "longPosition", long = true, qty = 0.1, when = longSignal)
strategy.entry(id = "shortPosition", long = false, qty = 0.1, when = shortSignal)

1 Like

strategy(“MA”,overlay=true, initial_capital=1000, commission_type=strategy.commission.percent, commission_value=0.2)
fromMonth = input(1, ‘Mes’, minval=1, maxval=12)
fromDay = input(1, ‘Dia’, minval=1, maxval=30)
fromYear = input(2021, ‘Anho’, minval= 2017)

toMonth = input(4, ‘Mes’, minval=1, maxval=12)
toDay = input(1, ‘Dia’, minval=1, maxval=30)
toYear = input(2021, ‘Anho’, minval= 2017)

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

longitud_mav = input(8, title=‘longitud de mav’)
longitud_maa = input(21, title=‘longitud de maa’)
longitud_mar = input(30, title=‘longitud de mar’)

mav = sma(close, longitud_mav)
maa = sma(close, longitud_maa)
mar = sma(close, longitud_mar)

alcista = crossover(mav,maa)
bajista = crossunder(mav,maa)

strategy.entry(‘comprar’, true, when=alcista and timeinRange)
strategy.entry(‘vender’, false, when=bajista and timeinRange)

plot(mav, color=color.green )
plot(maa, color=color.yellow )
//plot(mar, color=color.red )

1 Like

Defined Ivan on Tech (IoT) Strategy as: enter long when short (20d) moving average crosses over long (50d) moving average and inside the specified time range, exit long on the inverse.

As I’m just starting out, I wanted to keep this simple. I defined my personal strategy as: get slope between yesterday and today’s 10d moving average, if positive enter long, if negative exit long.

I honestly didn’t tweak it much, but my personal strategy came out ahead of a basic IoT 4 out of 6 times.

Screen Shot 2021-05-18 at 4.32.13 PM

Pine
strategy(title=“MA Crossover”, overlay=true, initial_capital=50, commission_type=“strategy.comission.percent”, commission_value=0.2)

//date time
fromMonth=input(defval=1, title= “From Month”, minval=1)
fromDay=input(defval=1,title = “From Day”, minval=1)
fromYear=input(defval=2020,title = “From Year”, minval=2014)

toMonth=input(defval=4, title= “To Month”, minval=1)
toDay=input(defval=1,title = “To Day”, minval=1)
toYear=input(defval=2020,title = “To Year”, minval=2014)

//definitions
shortMA=sma(close, 10) //modify this before switching strategies
longMA=sma(close, 50)
slopePeriod=1
slope=(shortMA - shortMA[slopePeriod]) / slopePeriod
plot(slope, color=color.red, linewidth=2)

//logic
timeInRange=(time > timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time < timestamp(toYear, toMonth, toDay, 23, 59))
longSignal=crossover(shortMA, longMA) and timeInRange
shortSignal=crossover(longMA, shortMA) and timeInRange
posSlope=(slope>0) and timeInRange
negSlope=(slope<0) and timeInRange

//positions
//strategy.entry(id=“longPosition”,long=true, qty=0.1, when=longSignal)
//strategy.entry(id=“longPosition”,long=false, qty=0.1, when=shortSignal)
strategy.entry(id=“longPosition”,long=true, qty=0.1, when=posSlope)
strategy.entry(id=“longPosition”,long=false, qty=0.1, when=negSlope)

Screen Shot 2021-05-18 at 4.32.50 PM

1 Like

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=4
strategy(title=“Moving Average BTC”, overlay=true, initial_capital=2000, commission_type=strategy.commission.percent,commission_value=0.2)
//DATE AND TIME
fromMonth= input(defval=2, title= “From month”, minval=1)
fromDay= input(defval=1, title= “From Day”, minval=1)
fromYear=input(defval=2021, title=“From year”, minval=2017)
toMonth= input(defval=2, title= “To month”, minval=1)
toDay= input(defval=1, title= “To Day”, minval=1)
toYear=input(defval=2028, title=“To year”, minval=2017)

shortMa=sma(close,88)
longMa=sma(close,90)
r = rsi(close, 14)
//LOGIC
timeInRange= (time > timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time <timestamp(toYear, toMonth, toDay, 23, 59))
longSignal=crossover(shortMa,longMa) and timeInRange and r < 50
shortSignal=crossover(longMa,shortMa) and timeInRange and r > 50
//POSITIONS
strategy.entry(“longPosition”, long=true, qty=0.1, when=longSignal)
strategy.entry(“shortPosition”, long=false, qty=0.1, when=shortSignal)

1 Like