Trigger buy when close less than 150 sma while 10 sma rising


Category:
0
0

Hi Pete,

The following  if then statement contains two conditions and it’s not working. It’s probably a beginner error :).

I want buy order if close is < SMA 150 with SMA 10  rising in the last period.

This is what i wrote:

def scan = (if close < SimpleMovingAvg (“length” = 150).SMA [0] and SimpleMovingAvg (“length” = 10).SMA [0] > SimpleMovingAvg (“length” = 10).SMA [1] then scan else 0) ;

Thank’s for your help !

Louis

 

 

RESOLVED
Marked as spam
Posted by (Questions: 5, Answers: 3)
Asked on January 30, 2020 1:23 pm
84 views
0
Private answer

I updated the title of your question so that it can be useful to the rest of our audience. We don't do one-off solutions here. Every post needs to contribute to the searchable knowledge-base of commonly asked questions.

You placed this in the Strategy Guide topic however your code does not plot a chart strategy. I see you used the variable name 'scan' so this probably should have been posted in the Stock Scanners topic. The Strategy Guide topic is reserved for code that includes buy and sell orders.

When working with if/then/else statements it's usually best to break things down into bit sized chunks. Writing three lines of code instead of 1 may seem more complex. However when you are writing a conditional statement it is so much easier to understand what is being said when you break out those individual elements. So the first thing I did was to break out your two moving averages into separate variable declarations.

def slowMA = SimpleMovingAvg(“length” = 150).SMA[0];
def fastMA = SimpleMovingAvg(“length” = 10).SMA;
def scan = close < slowMA and fastMA > fastMA[1];

Notice there is no longer an if/then/else statement. This is because none is required. Your original code was trying to create a recursive variable named 'scan'. I'm not sure why you tried to do that so I'll just say it was creating the following error: "Too early to access scan at..."

 

Marked as spam
Posted by (Questions: 37, Answers: 4086)
Answered on January 30, 2020 5:44 pm