Green candle crosses EMA20 or EMA50 or EMA200


Category:
0
0

Hello Pete,

I would like to add a study to a scan. The code of the study should reflect when a green candle crosses the 20 or the 50 or the 200 daily EMAs. The crossing will be valid for any price along the candle (it can cross at high, or low, or close…or the body). The attached picture shows that the scan should give a signal at number 1 (check the picture), because a green candle crossed EMA20, then another signal should be given the day after because a green candle crossed the EMA20 again. A 3rd, 4th, and 5th signals should be given in the following three days because a green candle crossed the EMA20 and 50, the EMA50, and the EMA200 in the following days respectively. I would like to mention that this is only part of my scan. My actual scan has other conditions.

Thank you,

Attachments:
Marked as spam
Posted by (Questions: 6, Answers: 8)
Asked on February 19, 2018 9:17 pm
333 views
0
Private answer

In addition to the 5 candles you marked in your screenshot, I counted 15 additional candles that qualified as valid signals. Just thought I would mention that in case there is something missing from your specification.

I would avoid using the term “crossing” when describing your specification. On a chart, a cross occurs when price begins on one side of a level, then moves to the other side of a level. In your screenshot, candles 1, 3 and 5 are crossing. The others are not.

In case you are questioning this, be sure to read all the details here. This is the authoritative source on the matter: http://toslc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/crosses.html

In it’s purest form, what I see from your specification is this:

The high and low of any green candle spans any or all of the three exponential moving averages (20, 50 and 200).

Stated from the perspective of the exponential moving averages:

Any one of the 20, 50 or 200 exponential moving averages touches any part of a green candle, inclusive of the high and low.

What we do when we set out writing code is to ensure we understand the specifications from the perspective of the computer, rather than the human. I wanted to take the time to explain all of this because it will help you in the future when you are requesting assistance on some other matter.

The code:

def ema20 = ExpAverage(close, 20);
def ema50 = ExpAverage(close, 50);
def ema200 = ExpAverage(close, 200);
def candleSpansEma20 = high > ema20 and low < ema20;
def candleSpansEma50 = high > ema50 and low < ema50;
def candleSpansEma200 = high > ema200 and low < ema200;
plot scan = candleSpansEma20 or candleSpansEma50 or candleSpansEma200;

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on February 20, 2018 8:30 am