Hi,
I'm trying to code a simple moving average cross over strategy. I've defined two pairs of moving averages and would like to take trades depending on the direction of the cross over of the pairs. I've noticed that when I run the below code under analysis then I get results where only either of the pair has crossed over. It would be fine if the other pair has not crossed over but there are cases that the other pair has crossed a few periods ago.
I would like to add a functionality wherein only those symbols are shown if both the pairs have crossed (irrespective of their direction) within a specific window of periods ( which can be defined via another parameter).
_section_begin("dual_ma_x_over");
minor_ema_1_p = param("Minor EMA Period - 1", 13, 1, 200, 1);
major_ema_1_p = param("Major EMA Period - 1", 34, 1, 200, 1);
minor_ema_2_p = param("Minor EMA Period - 2", 50, 1, 200, 1);
major_ema_2_p = param("Major EMA Period - 2", 200, 1, 200, 1);
min_c = param("Show stocks with close above", 10, 1, 100000, 1);
rsi_p = param("RSI Period", 14);
buy_fg_color = paramcolor("Buy Foreground Color", colorwhite);
buy_bg_color = paramcolor("Buy Background Color", colorgreen);
sell_fg_color = paramcolor("Sell Foreground Color", colorwhite);
sell_bg_color = paramcolor("Sell Background Color", colorred);
minor_ema_1_a = ema(c, minor_ema_1_p);
major_ema_1_a = ema(c, major_ema_1_p);
minor_ema_2_a = ema(c, minor_ema_2_p);
major_ema_2_a = ema(c, major_ema_2_p);
rsi_a = rsi(rsi_p);
buy_1 = cross(minor_ema_1_a, major_ema_1_a);
buy_2 = cross(minor_ema_2_a, major_ema_2_a);
buy = buy_1 or buy_2;
sell_1 = cross(major_ema_1_a, minor_ema_1_a);
sell_2 = cross(major_ema_2_a, minor_ema_2_a);
sell = sell_1 or sell_2;
filter = (buy or sell) and (c > min_c);
ema_1_fg_c = colordefault;
ema_1_bg_c = colordefault;
ema_1_action = "na";
ema_2_fg_c = colordefault;
ema_2_bg_c = colordefault;
ema_2_action = "na";
if (lastvalue(buy_1)) {
ema_1_fg_c = buy_fg_color;
ema_1_bg_c = buy_bg_color;
ema_1_action = "buy";
}
if (lastvalue(sell_1)) {
ema_1_fg_c = sell_fg_color;
ema_1_bg_c = sell_bg_color;
ema_1_action = "sell";
}
if (lastvalue(buy_2)) {
ema_2_fg_c = buy_fg_color;
ema_2_bg_c = buy_bg_color;
ema_2_action = "buy";
}
if (lastvalue(sell_2)) {
ema_2_fg_c = sell_fg_color;
ema_2_bg_c = sell_bg_color;
ema_2_action = "sell";
}
addcolumn(c, "Close" );
addtextcolumn(ema_1_action, "EMA-1 Action", 1.2, ema_1_fg_c, ema_1_bg_c);
addtextcolumn(ema_2_action, "EMA-2 Action", 1.2, ema_2_fg_c, ema_2_bg_c);
addcolumn(rsi_a, "RSI");
setsortcolumns(-6, 4, 5);
_section_end();
Thanks in advance.