Building a Strategy Discussion

Fantastic me:( Thank you, I will try with that!!! :+1: Thank you @thecil

1 Like

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)

1 Like

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! :slight_smile:

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)
  1. First question is does this look like a programmable strategy?

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

1 Like

:upside_down_face: :upside_down_face: :upside_down_face:
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 :nerd_face:

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! :slight_smile:

Carlos Z.

1 Like

Did not see well when a copy the lines


Thanks a lot :+1: :+1: :+1:

1 Like

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)

1 Like

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.

1 Like

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

1 Like

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! :slight_smile:

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)

1 Like

Hey @lacour1, hope you are well.
You have some few errors here:

  • initial_capitol=2000 should be initial_capital.
  • commission_type=“strategy.commission.percent” does not need the quotes, should be commission_type=strategy.commission.percent.
  • comission_value=0.2 you are missing an M for the word commision.

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! :slight_smile:

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)

1 Like

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! :slight_smile:

Carlos Z.

That fixed it, thanks Carlos.

Troy

1 Like

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)
1 Like

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.
Screen Shot 2021-03-29 at 7.37.20 PM

1 Like