Building a Strategy Discussion

#1

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

#2

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

everything is blank but i cant find any errors

#3

//@version=3
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)

//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, when=longSignal)
strategy.entry(id=“shortPosition”, long=false, when=shortSignal)

#4

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

#5

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.

#6

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…

#7

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:
//DEFINITIONS
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

//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

#8

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

or

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

#9

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: https://www.tradingview.com/script/fH6e5TuN-RSI-Divergence/

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:

#10

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.

#11

Hello Filip,
I’m having trouble getting specific data sets to appear. I have specifics selected but continue to get data outside of my date ranges. Have I made an error somewhere?

//@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=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)

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

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

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

#12

Yes, in the logic part you have entered the second timestamp values in the wrong order. It’s suppose to be timestamp(toYear, toMonth, toDay, 23, 59)

#13

@filip

Trying to workout how to code in the MA crossing in to node.js tradingbot.

Got it sorted on trading view, got a decent strat that id like to run on the sandbox now… But just cant workout the code i need to get my script in powershell to take trades when the MA crosses.

Are you able to help?

#14

Hi everybody, Hi @filip,

I have a question, maybe somebody has the answer :
Is there possibility to programmatically precise the time frame used to compute an indicator in Pinescript ?
For example, if I want to check an indicator in weekly chart and an other in daily chart for my strategy, how can I do ?

Thanks !

#15

You can use the security function to create a variable that holds data for a specific security with a specific timeframe. Then you can use that data for the specific indicators.

Here is info about the function I’m talking about. https://www.tradingview.com/study-script-reference/#fun_security. And a video if that helps: https://www.youtube.com/watch?v=K60Jc5WXsH4

1 Like
#16

I think @ivan is the best person to help you with this. He did the moving averages in nodejs in the course.

1 Like
#17

Check my sections in the course and you will learn how to do it in JavaScript

1 Like
#18

@ivan
Ive completed the whole course… You mention how to setup a moving average, i’ve done that…

Minutely, Hourly and Daily… However im stuck now with trying to configure it to take a trade when 2 MAs cross.

I’ve also completed your JavaScript section in Programming for smart contracts… But given im not a coder as such… So… im still a little lost when it comes to this.