Building a Strategy Discussion

Please provide the code in order that we can test it to help you solve the issue :face_with_monocle:

Carlos Z

1 Like
//@version=1
strategy(title="Moving Average Crossing", overlay=true);

//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 =  "Fromyear", minval=2017)

toMonth = input(defval=1, title = "To month", minval=1)
toDay = input(defval=1, title =  "To day", minval=1)
toYear = input(defval=2022, title = "To month", minval=2017)

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

You have a ; at the end of the declaration, delete it and it should work :nerd_face:

Carlos Z

1 Like

Great, now it works! Thank you Carlos :clap:
Seems like I was learning to much of Javascript before writing this pinescript, hahaha:)))

1 Like

Can’t figure out why I am getting this error message.
Screenshot from 2021-04-15 03-38-51

1 Like

Please provide the code so i can test it properly.

https://forum.ivanontech.com/t/faq-how-to-post-code-in-the-forum/35357/2

Carlos Z

Sorry for the delay.

//@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, 67)
longMa = sma(close, 87)
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 r < 50
shortSignal = crossover(longMa, shortMa) and timeInRange r > 50

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

1 Like

Problem is on the declaration for timeInRange variable whitin those 2 variables (long and short signals), that one should only return true or false based on the values you declare it.

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

When [time is greater than the specified time] and [time is less than specified time] it will return true or false.

To fix it, you just need to add an and between the timeInRange and r >50
it will look like this:

longSignal = crossover(shortMa, longMa) and timeInRange and r < 50
shortSignal = crossover(longMa, shortMa) and timeInRange and r > 50

Carlos Z

Ok, thanks Carlos.

Troy

image

Hi filip,

For some reason I get the message “no data” when I run test the strategy on 1 hour time interval.
Do you have any idea how this is possible?

I wrote my own code, but I also copied code from this forum with the same result.
So guess the problem is not in the code itself but maybe in the time interval or Symbol?

Hope you can help,

Thanks

PS if the image is not clear, here is the script:

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

1 Like

Hey @NiandraLaDes, hope you are ok.

Can you please provide your code in the following way please?

Then I can help you review your code to check the errors on it :face_with_monocle:

Carlos Z

He Carlos,

Sorry didn’t know about this. Works the same as on github I see :grinning:

strategy(title="MACStrategy", overlay=true)

//DATE AND 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)

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

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

// POSITIONS
strategy.entry("longPosition", true, when=longSignal)
strategy.entry("shortPosition", false, when=shortSignal)
1 Like

I dont see any issue with your code, it runs properly for me by just copy/paste.

image

Maybe you can describe how to replicate the issue :face_with_monocle:

Carlos Z

He Carlos I made a video so you can see my the problem, the code, settings and time interval + Symbol.

I uploaded it to dropbox since it is too big for uploading it to the forum here.
See Link

Thanks

1 Like

Its also working on different time frames, 1 hour, 4 hour, 1 day…

I think you have to zoom out or in the charts, I do it by the scroll wheel of my mouse, also you can zoom in/out the price by clicking it and holding the click while zooming.

Carlos Z

The strategy tester, as you can see in the video, says “No Data”. So it hasn’t have anything to do with zooming. I got it to work with different code

//DATE AND TIME
fromMonth = input(defval = 1,    title = "From Month",      type = input.integer, minval = 1, maxval = 12)
fromDay   = input(defval = 1,    title = "From Day",        type = input.integer, minval = 1, maxval = 31)
fromYear  = input(defval = 2020, title = "From Year",       type = input.integer, minval = 1970)
toMonth = input(defval = 1,    title = "Thru Month",      type = input.integer, minval = 1, maxval = 12)
toDay   = input(defval = 1,    title = "Thru Day",        type = input.integer, minval = 1, maxval = 31)
toYear  = input(defval = 2112, title = "Thru Year",       type = input.integer, minval = 1970)

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

// LOGIC
start     = timestamp(fromYear, fromMonth, fromDay, 00, 00)
finish    = timestamp(toYeay, toMonth, toDay, 23, 59) 
timeInRange()  => time >= start and time <= finish ? true : false      
longSignal = crossover(shortMa, longMa) and window()
shortSignal = crossover(longMa, shortMa) and window()
1 Like

when i add the code to chart , strategy tester reads no data. kindly asking what can be the issue?

//@version=3

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

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

Running into issues y’all!

So, I’m fairly certain I got the right code:

// @version=3
strategy(title = "MA Cross", 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)

// VARS
shortMA = sma(close, 20)
longMA = sma(close, 50)

// LOGIC
timeInRange = (time > timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time < timestamp(toYear, toMonth, toYear, 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)

When I “Add to Chart” I can see it, BUT, I only get 19 trades total and it runs until now-ish (2021).

Clearly something is not right, so what am I doing wrong?

1 Like

OMG, I’m THAT guy… replying to my own post… I think I figured out why it’s not matching… Filip’s chart is on 1 hour for 2018 and being in 2021 TW simply won’t go back that far for 1 hour, it will on 1 day, but then the data is not matching… but maybe I’m wrong :man_shrugging:

2 Likes