Fantastic me:( Thank you, I will try with that!!! Thank you @thecil
Testing the script as shown in the video. Chart does not load getting error messages. Help!
//@version=3
strategy(title=âMoving Average Crossingâ , overlay=true)
//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)
Hey @Giancarlo_M, hope you are well.
You have a syntax error on your code:
should be a comma ,
instead of /
If you have any more questions, please let us know so we can help you!
Carlos Z.
Hi Filip, amazing course! The pace was perfect and it was much easier to follow that I expected!
Iâve got a few question about a strategy I had in mind but have been unable to program properly.
Hereâs the strategy:
longSignal
- K% line crossing the D% while both lines are below the lower band of value 20
AND - BB close below the lower level (basis - 1dev)
shortSignal
- K% line crossing the D% while both lines are above the upper bank of value 80
AND - BB close above the upper level (basis + 1dev)
-
First question is does this look like a programmable strategy?
-
If so, I think one of the problems for me now is that these 2 indicators arenât coded properly on my end. Individually they work and I can back test them, but when I combine them the results in the strategy tester seem off as the number of trades stay the same regardless of the number of periods I set. Are there any additional rules to lookout for then programming 2 indicators to work together?
Sorry if this sounds confusing and Iâm definitely confused as well.
If anyone else is able to help too that would be amazing!
Thank you!
Cheers,
Chin
Iâm having some trouble compiling my code, can someone help me ?
//@version=3
strategy(title="Moving Average Crossing", 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 = 2025, title = "From year", minval = 2014)
tomMonth = input(defval = 1, title = "To month", minval = 1)
tomDay = input(defval = 1, title = "To day", minval = 1)
tomYear = input(defval = 2021, 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)
Should be to
, but you misspell it to tom
You got curvy quotes when there should be normal ones.
curvy quotes: â â
normal quotes: " "
If you have any more questions, please let us know so we can help you!
Carlos Z.
Did not see well when a copy the linesâŠ
Thanks a lot
For some reason now I keep getting a no data message when I add my code. What could possibly be causing that?
//@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(toYear, toMonth, toDay, 23, 59))
longSignal = crossover(shortMa, longMa) and timeInRange
shortSignal = crossover(longMa, shortMa) and timeInRange
//POSITIONS
strategy.entry(id=âlongPostionâ, long=true, qty=0.1, when=longSignal)
strategy.entry(id=âshortPositionâ, long=false, qty=0.1, when=shortSignal)
Hey @lacour1, hope you are ok.
I think you are just using the incorrect quotes, I just replace all curvy quotes ââ
with the normal ones ""
and the code was added to my chart in tradingview.
//@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(toYear, toMonth, toDay, 23, 59))
longSignal = crossover(shortMa, longMa) and timeInRange
shortSignal = crossover(longMa, shortMa) and timeInRange
//POSITIONS
strategy.entry(id="longPostion", long=true, qty=0.1, when=longSignal)
strategy.entry(id="shortPosition", long=false, qty=0.1, when=shortSignal)
Carlos Z
It is still not showing any data. Could it be that I am on the 30 free trial? It was working before then all of a sudden stopped.
In the âPine Editorâ tab, are you deploying correctly your code there? Right top corner of the Chart said âMAStrategyâ, delete it each time you have to deploy your code again.
Carlos Z
Yeah, Iâm still getting the same result of no data. I think this all started when I changed the data during the back testing tweaking video. Thatâs when it stopped showing data below. Maybe I should start from the beginning and see what happens.
Troy
You should code your script in the âPine Editorâ tab, then at the corner of its bar, you will see an âAdd to chartâ option, in case of error the console will show you, if not maybe yuo have not define properly the strategy.
If you have any more questions, please let us know so we can help you!
Carlos Z.
After making the changes suggested in âBacktesting Part 3: Tweakingâ I recieved error messages but canât find where I went wrong. I kept going back and forth from his code to mine.
Troy
//@version=3
strategy(title=âMoving Average Crossingâ, overlay=true, initial_capitol=2000, commission_type=âstrategy.commission.percentâ, comission_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(toYear, toMonth, toDay, 23, 59))
longSignal = crossover(shortMa, longMa) and timeInRange
shortSignal = crossover(longMa, shortMa) and timeInRange
//POSITIONS
strategy.entry(id=âlongPostionâ, long=true, qty= 0.1, when=longSignal)
strategy.entry(id=âshortPostionâ, long=false, qty= 0.1, when=shortSignal)
Hey @lacour1, hope you are well.
You have some few errors here:
-
initial_capitol=2000
should beinitial_capital
. -
commission_type=âstrategy.commission.percentâ
does not need the quotes, should becommission_type=strategy.commission.percent
. -
comission_value=0.2
you are missing an M for the wordcommision
.
So it should be this one at the end of those errors:
strategy(title="Moving Average Crossing", overlay=true, initial_capital=2000, commission_type=strategy.commission.percent, commission_value=0.2)
If you have any more questions, please let us know so we can help you!
Carlos Z.
Hey Carlos, in reference to:
commission_type=âstrategy.commission.percentâ
does not need the quotes, should be commission_type=strategy.commission.percent
.
In the video Filip made âstrategy.commission.percentâ a string. Either way I still get the same error message now whether I use quotes or not. I corrected the spelling too. What is my next step in order to figure this part out? Here is the code and screen shots
//@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(toYear, toMonth, toDay, 23, 59))
longSignal = crossover(shortMa, longMa) and timeInRange
shortSignal = crossover(longMa, shortMa) and timeInRange
//POSITIONS
strategy.entry(id=âlongPostionâ, long=true, qty= 0.1, when=longSignal)
strategy.entry(id=âshortPostionâ, long=false, qty= 0.1, when=shortSignal)
The code you are sharing is completely valid, I run it perfectly with my tradingview account.
The problem that i see in your images is on code line 24:
strategy.entry(id="longPostion", long=true, qty= 0.1 when=longSignal)
strategy.entry(id="shortPostion", long=false, qty= 0.1 when=shortSignal)
you are missing a comma ,
between arguments qty
and when
, so it should be:
strategy.entry(id="longPostion", long=true, qty= 0.1, when=longSignal)
strategy.entry(id="shortPostion", long=false, qty= 0.1, when=shortSignal)
If you have any more questions, please let us know so we can help you!
Carlos Z.
That fixed it, thanks Carlos.
Troy
Hi @filip, I modified your script to be able to switch between periods easily without having to reload or re-run anything.
It uses a baseMonth
, baseDay
, baseYear
, a currentOffset
, and a offsetLengthDays
.
Once the base parameters are set, the user can adjust the currentOffset
value to cycle through the various time ranges.
Hopefully it can be useful to someone else.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jpdupere
//@version=4
strategy("SMA Crossing", overlay=true, initial_capital=2000, commission_type=strategy.commission.percent, commission_value=0.2)
// DATE AND TIME
baseMonth = input(defval=1, title="Base month", minval=1)
baseDay = input(defval=15, title="Base day", minval=1)
baseYear = input(defval=2020, title="Base year", minval=2014)
currentOffset = input(defval=0, title="Current offset", minval=0)
offsetLengthDays = input(defval=90, title="Offset length in days", minval=1)
// DEFINITIONS
shortMa = sma(close, 30)
longMa = sma(close, 90)
offsetLengthMs = 1000*60*60*24*offsetLengthDays
currentOffsetFrom = timestamp(baseYear, baseMonth, baseDay, 00, 00) + currentOffset * offsetLengthMs
currentOffsetTo = currentOffsetFrom + offsetLengthMs
// LOGIC
timeInRange = (time > currentOffsetFrom) and (time < currentOffsetTo)
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)
Hi Carlos,
I hope you can help me with a similar issue I have following the Pine Programming in this chapter. I wrote the code according to Fil!'s instruction but got stuck on Line 3.