Building a Strategy Discussion

Hey @kudaform, hope you are great.

Please provide your code in text type so I can debug it myself. :face_with_monocle:

Here is the tutorial to post code properly in the forum.

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

Carlos Z.

//@version=3

strategy(title="Moving Average Crossing DKU", overlay=true, initial_capital=2000, commission_type=strategy.commission.percent,commision_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=2021, title = "From year", minval=2014)

toMonth = input(defval=1, title = "To month", minval=1)
toDay = input(defval=1, title = "To day", minval=1)
toYear = input(defval=2025, 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)
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)


Carlos here is the code I wrote. thank you again for taking the time!
Daniel
1 Like

HI all! Just a simple question. Maybe it will be covered sooner or later or i did not get the point at the moment.
My question is: Tltr: Is there an option in Pinescript to set the intervall?

Reason:
Using the data for backtesting i recognized, that it it makes a huge difference if i am using the 1h Intervall or the 1D Intervall. But the only option where i see at the moment is, to change this intervall in Tradingview. So far i have not seen a point or an variable where you can add / switch Change the Intervall.

Any ideas or did i miss something. All replies are welcome i am just a pinecript noob so i have no fear to learn it. Cheers Christian

1 Like

Hi, i think this should work for you.
Maybe you should use version4
as example:

//@version=4
// use the right commision type 
strategy(title="Moving Average Crossing", overlay=true, initial_capital=100, commission_type=strategy.commission.percent, commission_value=0.1)
1 Like

you can create intervals with something like this:

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

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

You must specify which parameters you want, minutes, days or hours.

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

Carlos Z.

I’ve done the same strategy as shown in the videos, but I’m not getting as many closed trades as in the videos.

Not many trades?

20210401_tradingview_problem

Why is this the case?

Trading Strategy Code
//@version=3
strategy(title="Moving Average Crossing", overlay=true, initial_capital=2000, commission_type="strategy.commission.percent", commission_value=0.2)

//plot moving averages
plot(sma(close, 20))
plot(sma(close, 50))

//date and time
fromMonth = input(defval=2, 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=5, 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, 30)
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)

EDIT: Solved. See my next post down below

1 Like

Thank you! Version4 worked! Awesome

Dear Carlos ! Thanks for the reply. But this is not what i was looking for. I am not looking for the input intervalls. My question is about the time frame in Tradingview. Look. This code works very well, when i use the candle intervall, or what is the terminus technicus for that i got different results. Which i understand, bur the question is how to add this in the script. Any ideas ?

This is the variable i am searching for ( see the screenshot in TV )
3hCandle

Looking forward to hear from you soon.
Cheers Christian

1 Like

Top-left corner --> keyboard shortcuts – search “interval”

You’ll find how to “change interval”. You can type “,” and it will allow you to change the interval. This is how I got the same trading strategy as the one in the video.

tradingview_intervals

1 Like

Dea Uyan! Thank you for your reply. But i never mentioned searching for a shortcut, As we can see when we use the strategy on different timeframes we will get different results. OK fine. I know how to change this intervalls (candlesticks) in TradingView (TV) .

My question is:
How to set the intervall In the code, that the strategy always works on this intervall .
I am searching for a code example. Could also be based on one of the lecures

Cheers and happy holidays

I was looking for that as well. It could be very useful.

I googled “pine script set interval” and the 2nd search result contained the following:

“No, you cannot change the current chart’s interval from the pinescript, only manually.
As you mentioned, you could use security function to access higher timeframe data, but trying to access lower resolution will return incorrect results, as it is not supported by the platform.”

hey @Uyan, @mchoeti hope you are ok.

Your code works very well, if you want to create an interval on your chart like 1 hour or 30 minutes, you have to define the time frame by variable, or just work with data[] of the variable, for example i suggest to look at the expresion to calculate previous candles, here you can research more about it. (also previous assignments we talk about this).

https://www.tradingview.com/pine-script-reference/#op_[]

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

Carlos Z.

I found confusion, and rightly so, with the different terminology used for what is the “interval”; I’ve heard “timeframe”, “period”, and today I heard “resolution”.

Here’s how to change the resolution in code. Check out the instructor’s TradingView page for their open source scripts, including the one demonstrated in the video.

“Resolution” is the word used in the documentation, so I will try to stick to that from now on.

2 Likes

@Uyan @thecil Hope you had a wonderful easter break. Well done, thanks for clearing that up. So as always resolution is the terminus technicus. Thanks for clearing that. See you soon. Cheers Ch

1 Like

Hi could anyone help. I’m inputting the following code and the following error messages show. Many thanks in advance

@version=3

strategy(title=“Moving Average Crossing”, overlay = true)

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

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

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

// Logic
longSignal = crossover(shortMa, longMa)
shortSignal = crossover (longMa, shortMa)

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

Add to Chart operation failed, reason: Cannot save empty source code

Script study added to the chart

Add to Chart operation failed, reason: line 6: syntax error at input ‘)’

Add to Chart operation failed, reason: line 6: syntax error at input ‘)’

Add to Chart operation failed, reason: line 6: syntax error at input ‘)’

Add to Chart operation failed, reason: line 6: syntax error at input ‘)’

Add to Chart operation failed, reason: line 6: syntax error at input ‘)’

Add to Chart operation failed, reason: Cannot save empty source code

Add to Chart operation failed, reason: Script could not be translated from: null

Add to Chart operation failed, reason: Script could not be translated from: null

Add to Chart operation failed, reason: Script could not be translated from: null

Add to Chart operation failed, reason: line 3: no viable alternative at character ‘“’

Add to Chart operation failed, reason: line 3: no viable alternative at character ‘“’

Add to Chart operation failed, reason: line 3: no viable alternative at character ‘“’

Add to Chart operation failed, reason: line 2: no viable alternative at character ‘“’

Add to Chart operation failed, reason: line 2: no viable alternative at character ‘“’

Add to Chart operation failed, reason: line 7: no viable alternative at character ‘“’

Add to Chart operation failed, reason: line 7: no viable alternative at character ‘“’

Add to Chart operation failed, reason: line 7: no viable alternative at character ‘“’

Did you save the script first?

Add to Chart operation failed, reason: line 3: no viable alternative at character ‘“’

Seems like the script doesn’t recognize the “ ” characters. Try " " instead.
(You’ll see both these quotation marks are different if you copy-paste them into Notepad)

1 Like

Here is my question , if i want to use crossunder insted of use crossover , i can right ? like is the same thing but what changes is the position of the previos functions
Here is an ex :

ShortMA = sma(close , 20 )
LongMA= sma(close, 50 )

LongSingnal= crossover(ShortMA, LongMA)
ShortSignal = crossunder(ShortMA, LongMA)

this is the same thing ?

LongSignal= crossover(ShortMA, LongMA)
ShortSignal= crossover(LongMA,ShorMA

1 Like

Add to Chart operation failed, reason: Script could not be translated from: null , the codeline @version=3 should be a comment, if not, is being take as a variable which syntax is incorrect.

// @version=3

Carlos Z

It is complete valid.

https://www.tradingview.com/pine-script-reference/#fun_crossunder

Carlos Z

Thanks guys all sorted now