Building a Strategy Discussion


Welcome to this discussion thread. Feel free to ask questions or discuss things related to this section.


after adding the code to the chart i cant see anymore the positions

everything is blank but i cant find any errors


strategy(title =“moving average crossing”, overlay =true)

//Date and Time
fromMonth = input(defval =5, title =“from month”,minval=1)
fromDay = input(defval =15, title =“from day”,minval=1)
fromYear = input(defval =2018, 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 =2018, title =“to year”,minval=2014)

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

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

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


it updates in the “stratgey tester” but i cant see anything on the chart


I tried your code and it works. You just need to have the correct date range set so that it matches the data that is currectly being viewed on the screen. So if you for example selects 5 min timeframe and position yourself to view the latest data (March), and you select for example march 1st to april 1st, then you will get positions.


i did the same code with you but i got 277.79 result, but you got 280,77 result. I think tradingview does not calculate very well.sometimes it makes mistakes…


Hi Filip,

First of all, great course with some high value content!

I have a question regarding my trading idea with multiple MA’s crossing.

My idea is to take a trade when the 8,13,21 MA crosses the 50 MA.
How would i add mulitple MA’s crossing the 50 MA in pine editor?

Here is a part of my pine script as is:
shortMa = sma(close, 8)
13Ma = sma(close,13)
21Ma = sma(close, 21)
longMa = sma(close , 50)
max_risk = strategy.equity * 0.01
stoploss = 100
size = max_risk/stoploss

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


Well, first of all. All of those moving averages probably will never cross at the same time. But the way you combine them would be to do something like this in your logic part.

longSignal = crossover(shortMa, longMa) and crossover(13Ma, longMa) and crossover(21Ma, longMa) and timeInRange


longSignal = crossover(shortMa, longMa) or crossover(13Ma, longMa) or crossover(21Ma, longMa) and timeInRange


Thanks for your reply Filip.
the ‘or’ did the job.

I would like to create a strategy based on RSI divergence, like this study i found:

I’d like to know if it is possible to convert this study into a strategy and where to start.
Would appreciate a helping hand :slight_smile:


Absolutely, you should be able to use the rsi divergence indicator created in that script and add buy/sell logic to that when it for example crosses over and under 0.