Building a Strategy Discussion

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

1 Like

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

everything is blank but i cant find any errors

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

2 Likes

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

1 Like

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

1 Like

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

1 Like

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:

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.

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)

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)

@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?

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 !

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

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

1 Like

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

1 Like

@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.

Sorry for the slow response @Big_Shadex. It’s quite complicated piece of code. You would need to draw inspiration from the crossover code from the tradingview part. But there is a little bit more coding involved when doing it in JS.

First of all, you need to define a crossover. The way I would define a daily crossover from the top is if yesterdays closing price is above the moving average of yesterday but todays closing price is below the moving average of today. So in pseudo code it would be something like this:

price[1] > ma[1] && price[0] < ma[0].

Where price[1] is yesterdays price and price[0] is todays price and same thing for ma.

Crossover from the bottom would be the opposite.

price[1] < ma[1] && price[0] > ma[0].

The same thing could of course be done for hourly. The most tedious part is for you is to get all of the data from the api. But this is how it would look on a conceptual level. Do you get my thinking?

All good mate, I know you guys are busy :hugs::hugs:

Okay yep, I think I get your thinking…

The part I’m struggling with is like you said… Getting the correct data from the API… because it’s not exactly set out in simple terms.

I’m pretty familiar with trading and technical terms… but the way they word things on there … just leaves me with a blank face :flushed::flushed::joy::joy: