Help building an AFL

#1
:clap:Hi all
I'm in need of help creating an AFL with the below requites:

LTP above EMA 34 and cross ABOVE EMA 8 then Buy signal

LTP above EMA 34 and cross BELOW EMA 8 then CLOSE BUY POSITION

LTP BELOW EMA 34 and cross BELOW EMA 8 then Sell signal

LTP ELOW EMA 34 and cross above EMA 8 then CLOSE SELL POSITION

PARAM TO CHANGE BOTH EMA TIME FRAMES

Please help in building this AFL; Thanks in advance
 
Last edited:

amanfree

Active Member
#4
ps = Optimize("period Small",8,1,100,1);
pl = Optimize("period large",34,1,100,1);
A = EMA(Close,ps);
B = EMA(Close,pl);

Buy = C > A AND C>B;
Sell= C<A AND C<B;
Cover = Close > A;
Short = Close < A;

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

PlotShapes(Buy*shapeUpArrow, colorBlue, 0, High, 12) ;
PlotShapes(Short*shapeDownArrow, colorRed, 0, Low, 12) ;
PlotShapes(Cover*shapeUpArrow, colorBlack, 0, High, 12) ;
PlotShapes(Sell*shapeDownArrow, colorBlack, 0, Low, 12) ;


Filter = Buy OR Sell ;

AddColumn(Buy,"buy",1.2);
AddColumn(Sell,"sell",1.2);
 
#5
ps = Optimize("period Small",8,1,100,1);
pl = Optimize("period large",34,1,100,1);
A = EMA(Close,ps);
B = EMA(Close,pl);

Buy = C > A AND C>B;
Sell= C<A AND C<B;
Cover = Close > A;
Short = Close < A;

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

PlotShapes(Buy*shapeUpArrow, colorBlue, 0, High, 12) ;
PlotShapes(Short*shapeDownArrow, colorRed, 0, Low, 12) ;
PlotShapes(Cover*shapeUpArrow, colorBlack, 0, High, 12) ;
PlotShapes(Sell*shapeDownArrow, colorBlack, 0, Low, 12) ;


Filter = Buy OR Sell ;

AddColumn(Buy,"buy",1.2);
AddColumn(Sell,"sell",1.2);


Thanks Aman
but this code does not show anything on screen...?
Please help


and

Is there a way that we can create an excel / csv file when there is a buy / sell signal.....
I found a code to write an excel / csv file when the chart is activated.
however I'm looking for a code to create a excel / csv only if there is a buy or sell signal

{Excel Output}
Ticker Date Time Open High Low Close Open Interest Volume Signal
BANKNIFTY-I 12/12/2012 09:25:00 12382.6504 12399.5 12360.2002 12398.5 1322100 64675
BANKNIFTY-I 12/12/2012 09:35:00 12398 12400 12381 12395 1339825 47950
BANKNIFTY-I 12/12/2012 09:45:00 12396 12402 12370.5 12381 1337300 36550
{Excel output ends}

This method actualy backfills all data, and I'm looking to add all buy and sell signals in it once when a signal is generated then it should backfill data with all buy signals, please help create one for me.....; Thanks in advance

Above is the CSV genarated (Found this code in this forum), signal col is empty and is not genarating.... kindly help

Code:
Code:
if(Buy[b])
	{
		PlotText("\n\n\n\n Buy\n "+NumToStr(BuyPrice[b],1.2),b,BuyPrice[b],colorBrightGreen);
		dayhours = ParamToggle("Day hours only", "No|Yes");
fmkdir("c:\\AmiBackup\\");
SetBarsRequired(100000,100000);
lname = Name(); // gets the name of the symbol
// note: if you have names with invalid characters like / you must :thumb:rename the file before you try to create a Name 
// add an IF line for each symbol you need to rename
if (lname == "ER2U8-GLOBEX-FUT") lname = "ER2U8";

fh = fopen( "c:\\AmiBackup\\" + lname + ".csv", "w"); 
if( fh ) 
{ 
	if(Interval() == inDaily OR Interval() == inMonthly OR Interval() ==
inWeekly)
	{
		fputs( "Ticker,Date,Open,High,Low,Close,Open Interest,Volume, sig \n", fh ); 
	   	for( i = 0; i < BarCount; i++ ) 
		{ 
 		  	y = Year(); 
 		  	m = Month(); 
		   	d = Day(); 
			fputs( Name() + "," , fh );
   		   	ds = StrFormat("%02.0f-%02.0f-%02.0f,", m[ i ], d[ i ], y[ i ] ); 
   	   		fputs( ds, fh ); 
      		qs = StrFormat("%.4f,%.4f,%.4f,%.4f,%.4f,%.0f\n", O[ i ],H[ i ],L[ i ],C[ i
],OI[ i ],V[ i ], Buy[b]); 
      		fputs( qs, fh ); 
			if(i == 1000000 OR i == 2000000 OR i == 3000000 OR i == 4000000)
			{
				fclose( fh ); 
				if(i == 1000000  ) fh = fopen( "c:\\AmiBackup\\" + lname + " A.csv", "w"); 
				if(i == 2000000 ) fh = fopen( "c:\\AmiBackup\\" + lname + " B.csv", "w"); 
				if(i == 3000000 ) fh = fopen( "c:\\AmiBackup\\" + lname + " C.csv", "w"); 
				if(i == 4000000 ) fh = fopen( "c:\\AmiBackup\\" + lname + " D.csv", "w"); 
			}
		}
	}
	else // intraday so add time field
	{
		fputs( "Ticker,Date,Time,Open,High,Low,Close,Open Interest,Volume, Signal \n", fh ); 
 	  	y = Year(); 
 	  	m = Month(); 
	   	d = Day(); 
	   	r = Hour();
	   	e = Minute();
	   	n = Second();
   
	   	for( i = 1; i < BarCount; i++ ) 
		{ 
			if (dayhours AND LastValue(TimeNum()) >= 92900 AND LastValue(TimeNum()) <=
161500)
			{ 
				fputs( Name() + "," , fh );
 		     	ds = StrFormat("%02.0f-%02.0f-%02.0f,", m[ i ], d[ i ], y[ i ] ); 
   			   	fputs( ds, fh ); 
 
  	    		ts = StrFormat("%02.0f:%02.0f:%02.0f,", r[ i ],e[ i ],n[ i ] ); 
  	    		fputs( ts, fh ); 

	 	     	qs = StrFormat("%.4f,%.4f,%.4f,%.4f,%.4f,%.0f\n", O[ i ],H[ i ],L[ i ],C[ i
],OI[ i ],V[ i ], Buy[b] ); 
		      	fputs( qs, fh ); 
			}
			else
			{ 
				fputs( Name() + "," , fh );
 		     	ds = StrFormat("%02.0f-%02.0f-%02.0f,", m[ i ], d[ i ], y[ i ] ); 
   			   	fputs( ds, fh ); 
 
  	    		ts = StrFormat("%02.0f:%02.0f:%02.0f,", r[ i ],e[ i ],n[ i ] ); 
  	    		fputs( ts, fh ); 

	 	     	qs = StrFormat("%.4f,%.4f,%.4f,%.4f,%.4f,%.0f\n", O[ i ],H[ i ],L[ i ],C[ i
],OI[ i ],V[ i ], Buy[b] ); 
		      	fputs( qs, fh ); 
			}
			if(i == 1000000 OR i == 2000000 OR i == 3000000 OR i == 4000000)
			{
				fclose( fh ); 
				if(i == 1000000  ) fh = fopen( "c:\\AmiBackup\\" + lname + " A.csv", "w"); 
				if(i == 2000000 ) fh = fopen( "c:\\AmiBackup\\" + lname + " B.csv", "w"); 
				if(i == 3000000 ) fh = fopen( "c:\\AmiBackup\\" + lname + " C.csv", "w"); 
				if(i == 4000000 ) fh = fopen( "c:\\AmiBackup\\" + lname + " D.csv", "w"); 
			}
 	  	} 
	}
   	fclose( fh ); 
}
}
else if ( Sell[b] )
{

PlotText("Sell  "+NumToStr(SellPrice[b], 1.2),b,SellPrice[b],colorDarkRed);

dayhours = ParamToggle("Day hours only", "No|Yes");
fmkdir("c:\\AmiBackup\\");
SetBarsRequired(100000,100000);
lname = Name(); // gets the name of the symbol
// note: if you have names with invalid characters like / you must rename the file before you try to create a Name 
// add an IF line for each symbol you need to rename
if (lname == "ER2U8-GLOBEX-FUT") lname = "ER2U8";

fh = fopen( "c:\\AmiBackup\\" + lname + ".csv", "w"); 
if( fh ) 
{ 
	if(Interval() == inDaily OR Interval() == inMonthly OR Interval() ==
inWeekly)
	{
		fputs( "Ticker,Date,Open,High,Low,Close,Open Interest,Volume,Signal \n", fh ); 
	   	for( i = 0; i < BarCount; i++ ) 
		{ 
 		  	y = Year(); 
 		  	m = Month(); 
		   	d = Day(); 
			fputs( Name() + "," , fh );
   		   	ds = StrFormat("%02.0f-%02.0f-%02.0f,", m[ i ], d[ i ], y[ i ] ); 
   	   		fputs( ds, fh ); 
      		qs = StrFormat("%.4f,%.4f,%.4f,%.4f,%.4f,%.0f\n", O[ i ],H[ i ],L[ i ],C[ i
],OI[ i ],V[ i ], Sell[i]); 
      		fputs( qs, fh ); 
			if(i == 1000000 OR i == 2000000 OR i == 3000000 OR i == 4000000)
			{
				fclose( fh ); 
				if(i == 1000000  ) fh = fopen( "c:\\AmiBackup\\" + lname + " A.csv", "w"); 
				if(i == 2000000 ) fh = fopen( "c:\\AmiBackup\\" + lname + " B.csv", "w"); 
				if(i == 3000000 ) fh = fopen( "c:\\AmiBackup\\" + lname + " C.csv", "w"); 
				if(i == 4000000 ) fh = fopen( "c:\\AmiBackup\\" + lname + " D.csv", "w"); 
			}
		}
	}
	else // intraday so add time field
	{
		fputs( "Ticker,Date,Time,Open,High,Low,Close,Open Interest,Volume, Signal \n", fh ); 
 	  	y = Year(); 
 	  	m = Month(); 
	   	d = Day(); 
	   	r = Hour();
	   	e = Minute();
	   	n = Second();
   
	   	for( i = 1; i < BarCount; i++ ) 
		{ 
			if (dayhours AND LastValue(TimeNum()) >= 92900 AND LastValue(TimeNum()) <=
161500)
			{ 
				fputs( Name() + "," , fh );
 		     	ds = StrFormat("%02.0f-%02.0f-%02.0f,", m[ i ], d[ i ], y[ i ] ); 
   			   	fputs( ds, fh ); 
 
  	    		ts = StrFormat("%02.0f:%02.0f:%02.0f,", r[ i ],e[ i ],n[ i ] ); 
  	    		fputs( ts, fh ); 

	 	     	qs = StrFormat("%.4f,%.4f,%.4f,%.4f,%.4f,%.0f\n", O[ i ],H[ i ],L[ i ],C[ i
],OI[ i ],V[ i ],Sell[i] ); 
		      	fputs( qs, fh ); 
			}
			else
			{ 
				fputs( Name() + "," , fh );
 		     	ds = StrFormat("%02.0f-%02.0f-%02.0f,", m[ i ], d[ i ], y[ i ] ); 
   			   	fputs( ds, fh ); 
 
  	    		ts = StrFormat("%02.0f:%02.0f:%02.0f,", r[ i ],e[ i ],n[ i ] ); 
  	    		fputs( ts, fh ); 

	 	     	qs = StrFormat("%.4f,%.4f,%.4f,%.4f,%.4f,%.0f\n", O[ i ],H[ i ],L[ i ],C[ i
],OI[ i ],V[ i ],Sell[i] ); 
		      	fputs( qs, fh ); 
			}
			if(i == 1000000 OR i == 2000000 OR i == 3000000 OR i == 4000000)
			{
				fclose( fh ); 
				if(i == 1000000  ) fh = fopen( "c:\\AmiBackup\\" + lname + " A.csv", "w"); 
				if(i == 2000000 ) fh = fopen( "c:\\AmiBackup\\" + lname + " B.csv", "w"); 
				if(i == 3000000 ) fh = fopen( "c:\\AmiBackup\\" + lname + " C.csv", "w"); 
				if(i == 4000000 ) fh = fopen( "c:\\AmiBackup\\" + lname + " D.csv", "w"); 
			}
 	  	} 
	}
   	fclose( fh ); 
}		
};
}
 
#6
ps = Optimize("period Small",8,1,100,1);
pl = Optimize("period large",34,1,100,1);
A = EMA(Close,ps);
B = EMA(Close,pl);

Buy = C > A AND C>B;
Sell= C<A AND C<B;
Cover = Close > A;
Short = Close < A;

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

PlotShapes(Buy*shapeUpArrow, colorBlue, 0, High, 12) ;
PlotShapes(Short*shapeDownArrow, colorRed, 0, Low, 12) ;
PlotShapes(Cover*shapeUpArrow, colorBlack, 0, High, 12) ;
PlotShapes(Sell*shapeDownArrow, colorBlack, 0, Low, 12) ;


Filter = Buy OR Sell ;

AddColumn(Buy,"buy",1.2);
AddColumn(Sell,"sell",1.2);
I had tried embedding your code into mine still its not working properly please help....

here is my code
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", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
//Plot(array, Name, Color, style=styleLine, Min=Null, Max=Null, xshift=0, zorder=0);
Plot(EMA(Close, 8),"EMA8", ParamColor("8EMAColor", colorBlue), styleLine);
Plot(EMA(Close, 34),"EMA34", ParamColor("34EMAColor", colorGreen), styleLine);



/*
Buy = Close > EMA(  Close , 34 ) 	AND Close > EMA(  Close , 8);

Sell =	Close > EMA(  Close , 34 ) 
	AND Close < EMA(  Close , 8 );

Short = Close < EMA(  Close , 34 ) 
	AND Close < EMA(  Close , 8 );

Cover = 	Close < EMA(  Close , 34 ) 
	AND Close > EMA(  Close , 8 );
*/

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

BuyPrice = ValueWhen(Buy,C);
ShortPrice = ValueWhen(Short,C);
*/



Buy= (Close > EMA(  Close , 34 ) 	AND Cross(C, Close > EMA(Close , 8)));


PlotShapes(IIf(Buy, shapeSquare, shapeNone), colorDarkBlue, 0, L, Offset=-40);
//PlotShapes(IIf(ShortPrice, shapeCircle, shapeNone), colorDarkBlue, 0, L, Offset=-50);


_SECTION_END();
:confused:
 
#10
Please help me do it, Please provide step by step instruction in Analysing....

I'm getting all zeros
ok will do it through team viewer

pm me ur skype or yahoo or google talk id

dont post ur TV vredential here in open forum
 

Similar threads