Moving average concept

vsreeram77

Active Member
#1
Dear Friends,

I want simple but more effective afl for the following concept

indicators used

ema 50 (blue ) close

ema 100 (red) close

--------------

buy entry :

when a price candle open,high,low
and close above two moving averages
take a buy in next candle open
price

stoploss will be the ema50 line
or ema100 line which hits earlier

after hitting stoploss, wait for
the entry rule (open,high,low,close
above two moving averages) and
take a buy again with same
ema50 / ema 100 line stoploss


sell entry:

when a price candle open,high,low
and close below two moving averages
take a sell in next candle open
price

stoploss will be the ema50 /ema 100 line

after hitting stoploss, wait for
the entry rule (open,high,low,close
below two moving averages) and
take a sell again with same
ema50 / ema 100 line stoploss

-----------------------------------------

no trade zone :

if a price candle trades between two moving
average lines, those area treated as no trade
zone

if a price candle touches both moving averages
then it is treated as invalid

--------------------------------------------------
 

travi

Well-Known Member
#2
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();


_SECTION_BEGIN("Two EMA Crossover Rules");

P1 = ParamField("Price field",-1);
Periods1 = Param("Periods1", 50, 2, 300, 1, 10 );
Plot( EMA( P1, Periods1 ), _DEFAULT_NAME(), ParamColor( "Color1", colorCycle ), ParamStyle("Style1") );

P2 = ParamField("Price field",-1);
Periods2 = Param("Periods2", 100, 2, 300, 1, 10 );
Plot( EMA( P2, Periods2 ), _DEFAULT_NAME(), ParamColor( "Color2", colorCycle ), ParamStyle("Style2") );




Buy = EMA(P1, Periods1) > EMA(P2, Periods2);
Sell = EMA(P1, Periods1) < EMA(P2, Periods2);

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);


Filter=Buy OR Sell;
SetOption("NoDefaultColumns", True );
AddColumn( DateTime(), "Date", formatDateTime );
AddColumn( IIf( Buy, 66, 83 ), "Signal", formatChar );
addcolumn( Close, "Close price", 1.4 );


PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

_SECTION_END();
 

travi

Well-Known Member
#4
I got your point, sometimes skimming on the phone makes this happen.
Let Happy Singh come, I'm aware of logic but not much AFL Coding.

The middle part of logic will have to be changed.
Currently, with crossover data, you will now which line is above and therefore based on price above upper EMA and price below Lower EMA logic needs to be added before the ExRem buy and sell.
 

OneThatGotAway

Well-Known Member
#7
Time frame 60 min

try
Code:
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();


_SECTION_BEGIN("Two EMA Crossover Rules");

P1 = ParamField("Price field",-1);
Periods1 = Param("Periods1", 50, 2, 300, 1, 10 );
Plot( EMA( P1, Periods1 ), _DEFAULT_NAME(), ParamColor( "Color1", colorCycle ), ParamStyle("Style1") );

P2 = ParamField("Price field",-1);
Periods2 = Param("Periods2", 100, 2, 300, 1, 10 );
Plot( EMA( P2, Periods2 ), _DEFAULT_NAME(), ParamColor( "Color2", colorCycle ), ParamStyle("Style2") );


EMA1= EMA(C,50);

EMA2 = EMA(C,100);

Buy= O>EMA1 and H>EMA2 AND L>EMA1 and C>EMA2;

Sell = EMA2>O and EMA1>H AND EMA2>L and EMA1>C;


Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy); 


Filter=Buy OR Sell;
SetOption("NoDefaultColumns", True );
AddColumn( DateTime(), "Date", formatDateTime );
AddColumn( IIf( Buy, 66, 83 ), "Signal", formatChar );
addcolumn( Close, "Close price", 1.4 );


PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

_SECTION_END();
 

vsreeram77

Active Member
#8
i want amibroker format for the following mq4 code


#property copyright "xbox forex"
#property link "http://community.strangled.net"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Black
#property indicator_color2 Blue
#property indicator_color3 Red

extern int period = 27;
extern int offset = 0;
extern bool EnableAlerts = True;
extern bool EnableArrows = True;
extern color ArrowUP = Blue;
extern color ArrowDOWN = Magenta;

double g_ibuf_80[];
double g_ibuf_84[];
double g_ibuf_88[];
string gs_92 = "";
string gs_xb4_ind_100 = "XB4 ind";
int prevbar;

int init() {
SetIndexStyle(0, DRAW_HISTOGRAM);
SetIndexStyle(1, DRAW_HISTOGRAM);
SetIndexStyle(2, DRAW_HISTOGRAM);
IndicatorDigits(Digits + 1);
SetIndexBuffer(0, g_ibuf_80);
SetIndexBuffer(1, g_ibuf_84);
SetIndexBuffer(2, g_ibuf_88);
IndicatorShortName(gs_xb4_ind_100);
SetIndexLabel(1, NULL);
SetIndexLabel(2, NULL);
prevbar = Bars;
return (0);
}

int start() {

double ld_0;
double ld_8;
double ld_16;
int li_24 = IndicatorCounted();
double ld_28 = 0;
double ld_36 = 0;
double ld_60 = 0;
double l_low_76 = 0;
double l_high_84 = 0;
creataalltext();
int li_92 = 16777215;
if (li_24 > 0) li_24--;
int li_96 = Bars - li_24;

for (int li_100 = offset; li_100 < li_96; li_100++) { //First bar 0(+offset) to Last bar
l_high_84 = High[iHighest(NULL, 0, MODE_HIGH, period, li_100)];
l_low_76 = Low[iLowest(NULL, 0, MODE_LOW, period, li_100)];
ld_16 = (High[li_100] + Low[li_100]) / 2.0;
ld_28 = 0.66 * ((ld_16 - l_low_76) / (l_high_84 - l_low_76) - 0.5) + 0.67 * ld_36;
ld_28 = MathMin(MathMax(ld_28, -0.999), 0.999);
g_ibuf_80[li_100] = MathLog((ld_28 + 1.0) / (1 - ld_28)) / 2.0 + ld_60 / 2.0;
ld_36 = ld_28;
ld_60 = g_ibuf_80[li_100];
}
bool li_104 = TRUE;
for (li_100 = li_96 - (2 + offset); li_100 >= 0; li_100--) { //Last bar-2(+offset) to First bar
ld_8 = g_ibuf_80[li_100];
ld_0 = g_ibuf_80[li_100 + 1];
if ((ld_8 < 0.0 && ld_0 > 0.0) || ld_8 < 0.0) li_104 = FALSE;
if ((ld_8 > 0.0 && ld_0 < 0.0) || ld_8 > 0.0) li_104 = TRUE;
if (!li_104) {
g_ibuf_88[li_100] = ld_8;
g_ibuf_84[li_100] = 0.0;
gs_92 = "SHORT";
li_92 = 65535;
} else {
g_ibuf_84[li_100] = ld_8;
g_ibuf_88[li_100] = 0.0;
gs_92 = "LONG";
li_92 = 65280;
}
}

bool Newbar = False;
if (prevbar != Bars) Newbar = True; prevbar = Bars;

if (Newbar)
{
if (g_ibuf_80[3] < 0.0 && g_ibuf_80[1] > 0.0)
{
if (EnableAlerts) Alert(Symbol() + " xb4d BUY");
if (EnableArrows)
{
ObjectCreate("xb4d_Buy_" + Time[0], OBJ_ARROW, 0, TimeCurrent(), Bid);
ObjectSet("xb4d_Buy_" + Time[0], OBJPROP_ARROWCODE, 228);
ObjectSet("xb4d_Buy_" + Time[0], OBJPROP_COLOR, ArrowUP);
}
}
if (g_ibuf_80[3] > 0.0 && g_ibuf_80[1] < 0.0)
{
if (EnableAlerts) Alert(Symbol() + " xb4d SELL");
if (EnableArrows)
{
ObjectCreate("xb4d_Sell_" + Time[0], OBJ_ARROW, 0, TimeCurrent(), Bid);
ObjectSet("xb4d_Sell_" + Time[0], OBJPROP_ARROWCODE, 230);
ObjectSet("xb4d_Sell_" + Time[0], OBJPROP_COLOR, ArrowDOWN);
}
}
}


settext("SIGNAL STRENGTH", gs_92, 12, li_92, 10, 15);
return (0);
}

void creataalltext() {
createtext("xboxforex_ind");
settext("xboxforex_ind", "", 12, White, 10, 15);
}

void createtext(string a_name_0) {
ObjectCreate(a_name_0, OBJ_LABEL, WindowFind(gs_xb4_ind_100), 0, 0);
}

void settext(string a_name_0, string a_text_8, int a_fontsize_16, color a_color_20, int a_x_24, int a_y_28) {
ObjectSet(a_name_0, OBJPROP_XDISTANCE, a_x_24);
ObjectSet(a_name_0, OBJPROP_YDISTANCE, a_y_28);
ObjectSetText(a_name_0, a_text_8, a_fontsize_16, "Arial", a_color_20);
}
 

Similar threads