Still not having much luck with my strategy I'm building out. I think staring at the screen for hours on end isn't helping. I asked about this the other day but since then I've changed my code and added more conditions.
Basically, my conditions of entry are as follows. (long example) if 20 ema > 50 ema and we have a green candle and we have had 3 or more red candles, enter long.
The problem I'm having is when I assign them all to a variable like this:
buyCondition = cons_red_cond and maCrossover and bulC
I Don't get any trades on the chart.
So then I change it to:
buyCondition = cons_red_cond and maCrossover
It picks up trades but the problem is that it's only picking up trades with the specific lookback I specified and misses trades where there have been 5 red candles in a row followed by a green candle.
What is the best way to have my script enter when there have been 3 consecutive red and candles followed by a green candle but if we have let's say 6 consecutive green candles, how do I not miss those trades?
Do I need to use var keywords or loops or am I on the right track just need to reword some of the script?
Full code is below
Shoutout to @vitruvius. Part of my code is from a question he answered!
//@version=5
strategy("My script", overlay=true, initial_capital = 10000)
//Get MA lengths
var g_length = 'MA length'
maLength1 = input.int(title = 'MA Length 1', defval = 20, minval = 1, step = 10, group = g_length)
maLength2 = input.int(title = 'MA Length 2', defval = 50, minval = 0, step = 10, group = g_length) //Setting minval to 0 will turn off EMA
//Get MA types
var g_type = 'MA type'
maType1 = input.string(title = 'MA Type 1', defval = 'EMA', options = ['EMA', 'SMA', 'HMA', 'WMA', 'DEMA', 'VWMA', 'VWAP'], group = g_type)
maType2 = input.string(title = 'MA Type 2', defval = 'EMA', options = ['EMA', 'SMA', 'HMA', 'WMA', 'DEMA', 'VWMA', 'VWAP'], group = g_type)
allMatype = input.string(title = 'ALL MA TYPE', defval = 'Disabled', options = ['Disabled','EMA', 'SMA', 'HMA', 'WMA', 'DEMA', 'VWMA', 'VWAP'], group = g_type)
//Get Consecutive bars back settings
in_cnt_long = input.int(3, "Consecutive long event count")
in_cnt_short = input.int(3, "Consecutive short event count")
//Get LL/HH lookback settings
llhhLookback = input.int(title = 'Highest high/Lowest low lookback', defval = 3)
//Get selected Moving Average
getMA(_maType, _maLength) =>
if _maLength == 0 //Here we are saying, if the user selects 0, the script will return na, no MA will be selected
na
else //If the user doesn't select 0 and selects a value > 0, we have assigned a function to each of the arguments in the options menu of our inputs above
switch allMatype == 'Disabled' ? _maType : allMatype
'SMA' => ta.sma(close, _maLength)
'HMA' => ta.hma(close, _maLength)
'WMA' => ta.wma(close, _maLength)
'VWMA' => ta.vwma(close, _maLength)
'VWAP' => ta.vwap
'DEMA' => //Double EMA. This is a standard EMA as the first but the second is an EMA of the first EMA as seen in the code below
e1 = ta.ema(close, _maLength)
e2 = ta.ema(e1, _maLength)
2 * e1 - e2
=> ta.ema(close, _maLength)
//Get MA's
ma1 = getMA(maType1, maLength1)
ma2 = getMA(maType2, maLength2)
//Get MA crossover
maCrossover = ma1 > ma2
maCrossunder = ma1 < ma2
//Draw Ma's
plot(ma1, color = color.green, linewidth = 1, title = 'MA 1')
plot(ma2, color = color.red, linewidth = 1, title = 'MA 2')
//Get consecutive bars back filters
is_green_candle = (close > open)
is_red_candle = (not is_green_candle)
green_candle_cons_cnt = math.sum(is_green_candle ? 1 : 0, in_cnt_long) // Sum the number of candles where is_green_candle was true over the lookback period
cons_green_cond = (green_candle_cons_cnt >= in_cnt_long) // Check if the consecutive count is equal to your target
red_candle_cons_cnt = math.sum(is_red_candle ? 1 : 0, in_cnt_short) // Sum the number of candles where is_red_candle was true over the lookback period
cons_red_cond = (red_candle_cons_cnt >= in_cnt_short) // Check if the consecutive count is equal to your target
//Get Candle conditions
bulC = close > open
bearC = close < open
//Get lowest low/highest High filter
//lowestLow = low == ta.lowest(low, llhhLookback)
//highestHigh = high ==ta.highest(high, llhhLookback)
buyCondition = cons_red_cond and maCrossover //and bulC
sellCondition = cons_green_cond and maCrossunder //and bearC
if buyCondition
strategy.entry('Long', strategy.long)
if sellCondition
strategy.entry('Short', strategy.short)
Ignore the highest high and lowest low part.