Simple Coding Help - No Promise.

In the EMA AFL there were 2 conditions. 1st from Crossover on 15min and 2nd from Crossover in 5min.

In this AFL you want me to use 15min candle Close or 5min candle Close or both like as in EMA? And the signals need to be shown on 15min or 5min?
thank u so much for ur valuable time. yes i want same afl like ema . i want both 15 and 5 min timeframe lika as in ema. and signals should be on 5 min same like ema.. when there is buy signals candle close in 15 min then after that only buy signals on 5 min and if there is sell signal on 15 min candle close then after that only sell signals on 5 min chart ( same like ema crossover afl )
 
Last edited:
Hi,

The follg. code generates buy and sell signal perfectly in real time while exploring. But when back testing, it create 2 problems:

1. It misses the signal in which the SL hits in the same bar after the signal is generated.

2. It takes the high of the trigger bar as trigger price in case of buy instead of the low of the prv bar as per strategy.

Pls. help. Also tell me if I have written the Apply stop function correctly for sl and tgt as 1% of equity.

Here is the code:


_SECTION_BEGIN("simple reversal");

// to plot price

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


//buy and short conditions

Cond1 = Ref ( Low, -1 ) < Ref ( Low, -2 );
Cond2 = Ref ( Close, -1 ) > Ref ( Close, -2 );
Cond3 = Ref ( Close, -1 ) > Ref ( Open , -1);
Cond4 = Ref ( High , -1 ) < Ref ( High , -2 );
Cond5 = Ref (RSI (14), -2) < 30;
Cond6 = Ref (RSI (14) , -1 ) > 30 ;
Cond7 = Ref (Low, -1 ) < Ref (LLV (Low, 8) , -1 );
Cond8 = High > Ref ( High, -1 );


Cond9 = Ref ( High, -1) > Ref (High, -2 );
Cond10 = Ref ( Close, -1 ) < Ref ( Close, -2 );
Cond11 = Ref ( Close, -1 ) < Ref ( Open , -1);
Cond12 = Ref ( Low , -1 ) > Ref ( Low, -2);
Cond13 = Ref ( RSI (14) , -2 ) > 70 ;
Cond14 = Ref ( RSI (14) , -1 ) < 70 ;
Cond15 = Low < Ref (Low, -1) ;
Cond16 = Ref (High, -1 ) > Ref ( HHV (High ,8) , -1 );
Cond17 = TimeNum() < 143100 ;//signals after 2.30 r ignored
Cond18 = TimeNum() > 093100 ; // signals bfor 9.30 r ignored


Buy = Cond1 AND Cond2 AND Cond3 AND Cond4 AND Cond5 AND Cond6 AND Cond7 AND Cond8 AND Cond17 AND Cond18 ;
Short = Cond9 AND Cond10 AND Cond11 AND Cond12 AND Cond13 AND Cond14 AND Cond15 AND Cond16 AND Cond17 AND Cond18 ;

R = Ref (High ,-1 ) - Ref ( Low , -1);

if(Status("action") == actionExplore);

Filter = Buy | Short;

SetOption ("NoDefaultColumns", True );
AddTextColumn(Name(), "SYMBOL");
AddColumn(DateTime(), "DATE", formatDateTime);
AddColumn(IIf(Buy, 66, 83), "TRIGGER", formatChar, colorWhite, IIf(Buy, colorGreen, colorRed));
AddColumn(IIf(Buy, Ref ( High , -1), Ref ( Low, -1 )), "TRIGGER PRICE", 1.2);
AddColumn(IIf(Buy, Ref(Low ,-1), Ref(High , -1)) , "Stop Loss", 1.2);
AddColumn(IIf(Buy, Ref (High , -1) + R , Ref (Low , -1) - R ), "Target", 1.2);
AddColumn(IIf(Buy, 420/R ,420/R ),"shares",1.2 );

//ApplyStop(stopTypeLoss, stopModePercent, 1, 1);
//ApplyStop(stopTypeProfit , stopModePercent, 1, 1);


//to get alert via speakers and in alert output window with the text put in aphostrophies

AlertIf( Buy, "SOUND C:\\WINDOWS\\Media\\Raga.wav", "REVBUY", 1 );
AlertIf( Short, "SOUND C:\\Windows\\Media\\Raga.wav", "REVSELL", 3 );


//to plot up arrow and down arrow in chart for buy and short signal


PlotShapes(shapeUpArrow*Buy,colorBrightGreen,0,L);
PlotShapes (shapeDownArrow*Short, colorRed, 0, H);

_SECTION_END() ;

cond8 and cond15 is the trigger to buy and sell. i.e if it crosses prv bar high/low. rest of the conditions indicate buy/sell set up.

Pls. help.

Regards,
 
Check this one.

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("Trade lines");
P			= ParamField("Price field", 3);
Periods	= Param("Periods", 2, 2, 300, 1 );
Width		= Param("Width%", 0.15, 0, 10, 0.05 );

CenterLine	= MA( P, Periods );
BuyLine	= CenterLine * (1 + Width * 0.01);
SellLine	= CenterLine * (1 - Width * 0.01);

Plot( BuyLine, "5min BuyLine", colorGreen, ParamStyle("Style") );
Plot( SellLine, "5min SellLine", colorRed, ParamStyle("Style") );
_SECTION_END();

TimeFrameSet(in5Minute*3);
_SECTION_BEGIN("15min Trade lines");
P2 = ParamField("Price field",3);
Periods2 = Param("Periods", 2, 2, 300, 1 );
Width2 = Param("Width%", 0.15, 0, 10, 0.05 );

CenterLine2 = MA( P2, Periods2 );
BuyLine2 = CenterLine2 * (1 + Width2 * 0.01);
SellLine2 = CenterLine2 * (1 - Width2 * 0.01);

BuyLine2e	= TimeFrameExpand(BuyLine2, in15Minute, expandLast);
SellLine2e	= TimeFrameExpand(SellLine2, in15Minute, expandLast);

Plot( BuyLine2e, "15min BuyLine", colorBlue, ParamStyle("Style") );
Plot( SellLine2e, "15min SellLine", colorPaleTurquoise, ParamStyle("Style") );
_SECTION_END();
TimeFrameRestore();

_SECTION_BEGIN("Trading Signals");
Buy1 = Cross(C, Buyline2e);
Sell1 = Cross(SellLine2e, C);
position	= IIf(BarsSince(Buy1) < BarsSince(Sell1), 1, 2);

Buy		= Cross(C, Buyline) AND position==1;
Short	= Cross(SellLine, C) AND position==2;
Cover	= Cross(C, Buyline);
Sell	= Cross(SellLine, C);

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

BuyPrice	= ValueWhen(Buy, Close);
SellPrice	= ValueWhen(Sell, Close);
ShortPrice	= ValueWhen(Short, Close);
CoverPrice	= ValueWhen(Cover, Close);

PlotShapes(Buy*shapeUpArrow, colorGreen, 0, Low, -28);
PlotShapes(Short*shapeDownArrow, colorRed, 0, High, -28);
PlotShapes(Cover*shapeHollowUpArrow, colorGreen, 0, Low, -45);
PlotShapes(Sell*shapeHollowDownArrow, colorRed, 0, High, -45);

dist	= 1.5*ATR(10);
for (i=0; i<BarCount; i++) {
	if (Cover[i]) {
		PlotText( "\nCover short: " + CoverPrice[i], i+1.5, L[ i ]-dist[i]-3, colorLime);
		PlotText( "\n\nProfit: " + (ShortPrice[i]-CoverPrice[i]), i+1.5, L[ i ]-dist[i]-3, colorLime);
	} else if (Sell[i]) {
		PlotText( "\nSell bought: " + SellPrice[i], i+1.5, H[ i ]+dist[i]+5, colorOrange);
		PlotText( "\n\nProfit: " + (SellPrice[i]-BuyPrice[i]), i+1.5, H[ i ]+dist[i]+5, colorOrange);
	}
	if(Buy[i]) {
		PlotText( "Buy: " + BuyPrice[i], i+1.5, L[ i ]-dist[i]-3, colorLime);
		sig		= "Buy";
	} else if( Short[i]) {
		PlotText( "Short: " + ShortPrice[i], i+1.5, H[ i ]+dist[i]+5, colorOrange);
		sig	= "Short";
 	}
}
_SECTION_END();
Buy = Cross(C, Buyline2) AND position==1;
Short = Cross(SellLine2, C) AND position==2;
Cover = Cross(C, Buyline2);
Sell = Cross(SellLine2, C);

thank u. above code was giving sytex error so i changed to below one. but almost all signals are repainting. can u fix it.

Buy = Cross(C, Buyline2) AND position==1;
Short = Cross(SellLine2, C) AND position==2;
Cover = Cross(C, Buyline2);
Sell = Cross(SellLine2, C);
 
RESPECTED ALL,

BELOW IS THE OPEN HIGH OPEN LOW,
is it possible to add in this afl STOPLOSS & TARGET, if yes PLEASE HELP


_SECTION_BEGIN("OPEN HIGH OPEN LOW ");
Buy = Open == Low;

Sell = Open == High;

Noise = High > Close;

AddColumn(Open,"Open");
AddColumn(Close,"Close");
AddColumn(High,"High");
AddColumn(Low,"Low");

Filter= (Buy OR Sell) AND Noise;

AddColumn(IIf(Buy,Open,Null)," open=low ", 6.2,1.2,colorGreen);
AddColumn(IIf(Sell,Open,Null)," open= high ",6.2,1.2,colorOrange);
_SECTION_END();

***above afl for scan & explore only

WARM REGARDS
CHINMAY
 
RESPCETED ALL
CAN WE USE BELOW CODE IN NEST TRADER TERMINAL ?
ANYBODY KNOW HOW TO ADD THIS CODE IN NEST TRADER TERMINAL ?

_SECTION_BEGIN("OPEN HIGH OPEN LOW ");
Buy = Open == Low;

Sell = Open == High;

Noise = High > Close;

AddColumn(Open,"Open");
AddColumn(Close,"Close");
AddColumn(High,"High");
AddColumn(Low,"Low");

Filter= (Buy OR Sell) AND Noise;

AddColumn(IIf(Buy,Open,Null)," open=low ", 6.2,1.2,colorGreen);
AddColumn(IIf(Sell,Open,Null)," open= high ",6.2,1.2,colorOrange);
_SECTION_END();

WARM REGARDS
CHINMAY
 

extremist

Well-Known Member
This is one thing I don't like with Amibroker coding, no way to initialize default values. What you are doing can be done by adding b+a for the last 10 candles using i=Barcount-10 to BarCount loop but at any instance we would have only the last value, if that is okay with you.
ya tht will do.
 

Similar threads