Simple Coding Help - No Promise.

pratapvb

Well-Known Member
yes sir manually drawn trendline
never done it....but I think you can label manually draw lines and access in AFL....so should be possible

will have to check
 

XRAY27

Well-Known Member
never done it....but I think you can label manually draw lines and access in AFL....so should be possible

will have to check
sir.. if possible do it ... or any setting is there in amibroker please tell me:)
 

josh1

Well-Known Member
This AFL exports entire price data into .csv file when I click on scan or explore in New Analysis. How can I make it export data for given Date range only? Can anybody help?
Code:
Filter=1;// By Barry Scarborough 7/14/2008, updated to handle large files 8/30/08

// #### READ THIS FIRST #### READ THIS FIRST #### READ THIS FIRST #### READ THIS FIRST #### READ THIS FIRST ####
// Export intraday and EOD data to .csv files 
// One file for each stock but the symbol name must be a valid Microsoft file Name OR you will have to modify it, see code below to change Name
// if the data exceeds 65500 lines it will be broken into separate files up to 327,500 lines OR about 227 days of 24 Hour, one Minute data
// This will create a directory H:\AmiBackup
// Select your symbols to export with the "Apply to" filter in AA window, to save data base use all symbols AND all quotes 
// Make sure the chart is on the period you want to save 
// Select the same timeframe period you want to save as using the AA "Settings"
// Press Scan button
// 

// created a directory on your C drive named AmiBroker data backup
dayhours = ParamToggle("Day hours only", "No|Yes");
fmkdir("H:\\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";
if (lname == " ") lname = "NSEI";
fh = fopen( "H:\\AmiBackup\\" + lname + ".csv", "w"); 
if( fh ) 
{ 
	if(Interval() == inDaily OR Interval() == inMonthly OR Interval() ==
inWeekly)
	{
		fputs( "Ticker,Date,Open,High,Low,Close,Volume \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,%.0f\n", O[ i ],H[ i ],L[ i ],C[ i
],V[ i ] ); 
      		fputs( qs, fh ); 
/*
			if(i == 65500 OR i == 130000 OR i == 196500 OR i == 262000)
			{
				fclose( fh ); 
				if(i == 65500  ) fh = fopen( "H:\\AmiBackup\\" + lname + " A.csv", "w"); 
				if(i == 130000 ) fh = fopen( "H:\\AmiBackup\\" + lname + " B.csv", "w"); 
				if(i == 196500 ) fh = fopen( "H:\\AmiBackup\\" + lname + " C.csv", "w"); 
				if(i == 262000 ) fh = fopen( "H:\\AmiBackup\\" + lname + " D.csv", "w"); 
			}
*/
		}
	}
	else // intraday so add time field
	{
		fputs( "Ticker,Date,Time,Open,High,Low,Close,Volume \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()) >= 91500 AND LastValue(TimeNum()) <=
153100)
			{ 
				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,%.0f\n", O[ i ],H[ i ],L[ i ],C[ i
],V[ 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,%.0f\n", O[ i ],H[ i ],L[ i ],C[ i
],V[ i ] ); 
		      	fputs( qs, fh ); 
			}
/*
			if(i == 65500 OR i == 130000 OR i == 196500 OR i == 262000)
			{
				fclose( fh ); 
				if(i == 65500  ) fh = fopen( "c:\\AmiBackup\\" + lname + " A.csv", "w"); 
				if(i == 130000 ) fh = fopen( "c:\\AmiBackup\\" + lname + " B.csv", "w"); 
				if(i == 196500 ) fh = fopen( "c:\\AmiBackup\\" + lname + " C.csv", "w"); 
				if(i == 262000 ) fh = fopen( "c:\\AmiBackup\\" + lname + " D.csv", "w"); 
			}
*/
 	  	} 
	}
   	fclose( fh ); 
} 

//Buy = 1;
Buy=((DateNum()>=1130426) AND (DateNum()<=1130429) );
//Buy = ( (TimeNum()>=190000 AND DateNum()==1050315) OR (TimeNum()<=030000 AND DateNum()==1050316) ) AND V>0;
 
Last edited:
I have a code from Rajendran sir SuperTrend.
The problem with the code is that following expression in the code does not work

if(LastValue(Buy))
{//Alert
}
if(LastValue(Sell))
{//Alert
}


The Code looks like this->
/* Done by Rajandran R */
/* Author of www.marketcalls.in */

_SECTION_BEGIN("SuperTrend");
SetBarsRequired(100000,0);
GraphXSpace = 15;
SetChartOptions(0,chartShowArrows|chartShowDates);
SetChartBkColor(ParamColor("bkcolor",ColorRGB(0,0, 0)));
GfxSetBkMode(0);
GfxSetOverlayMode(1);
SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color", colorGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));
Plot(C,"\nPrice",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);

//SetTradeDelays(1,1,1,1);

SetPositionSize(2,spsShares);

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

Factor=Param("Factor",3,1,10,0.1);
Pd=Param("ATR Periods",7,1,100,1);
Up=(H+L)/2+(Factor*ATR(Pd));
Dn=(H+L)/2-(Factor*ATR(Pd));
iATR=ATR(Pd);
TrendUp=TrendDown=Null;
trend[0]=1;
changeOfTrend=0;
flag=flagh=0;

for (i = 1; i <BarCount-1; i++) {
TrendUp = Null;
TrendDown = Null;

trend=1;


if (Close>Up[i-1]) {
trend=1;
if (trend[i-1] == -1) changeOfTrend = 1;

}
else if (Close<Dn[i-1]) {
trend=-1;
if (trend[i-1] == 1) changeOfTrend = 1;
}
else if (trend[i-1]==1) {
trend=1;
changeOfTrend = 0;
}
else if (trend[i-1]==-1) {
trend=-1;
changeOfTrend = 0;
}

if (trend<0 && trend[i-1]>0) {
flag=1;
}
else {
flag=0;
}

if (trend>0 && trend[i-1]<0) {
flagh=1;
}
else {
flagh=0;
}

if (trend>0 && Dn<Dn[i-1]){
Dn=Dn[i-1];
}

if (trend<0 && Up>Up[i-1])
{ Up=Up[i-1];
}

if (flag==1)
{ Up=(H+L)/2+(Factor*iATR);;
}
if (flagh==1)
{ Dn=(H+L)/2-(Factor*iATR);;
}
if (trend==1) {
TrendUp=Dn;
if (changeOfTrend == 1) {
TrendUp[i-1] = TrendDown[i-1];
changeOfTrend = 0;
}
}
else if (trend==-1) {
TrendDown=Up;
if (changeOfTrend == 1) {
TrendDown[i-1] = TrendUp[i-1];
changeOfTrend = 0;
}
}
}

Plot(TrendUp,"Trend",colorGreen);
Plot(TrendDown,"Down",colorRed);

Buy = trend==1;
Sell=trend==-1;

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


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

if(LastValue(Buy))
{//Alert
}
if(LastValue(Sell))
{//Alert
}


Title = EncodeColor(colorWhite)+ "Super Trend AFL code from www.marketcalls.in" + " - " + Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
" - " + Date() +" - "+"\n" +EncodeColor(colorRed) +"Op-"+O+" "+"Hi-"+H+" "+"Lo-"+L+" "+
"Cl-"+C+" "+ "Vol= "+ WriteVal(V)+"\n"+
EncodeColor(colorLime)+
WriteIf (Buy , " GO LONG / Reverse Signal at "+C+" ","")+
WriteIf (Sell , " EXIT LONG / Reverse Signal at "+C+" ","")+"\n"+EncodeColor(colorYellow)+
WriteIf(Sell , "Total Profit/Loss for the Last Trade Rs."+(C-BuyPrice)+"","")+
WriteIf(Buy , "Total Profit/Loss for the Last trade Rs."+(SellPrice-C)+"","");

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(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

TrendSL=IIf(trend==1,TrendUp,TrendDown);

for(i=BarCount-1;i>1;i--)
{
if(Buy == 1)
{
entry = C;
sig = "BUY";
sl = TrendSL;
tar1 = entry + (entry * .0050);
tar2 = entry + (entry * .0092);
tar3 = entry + (entry * .0179);

bars = i;
i = 0;
}
if(Sell == 1)
{
sig = "SELL";
entry = C;
sl = TrendSL;
tar1 = entry - (entry * .0050);
tar2 = entry - (entry * .0112);
tar3 = entry - (entry * .0212);


bars = i;
i = 0;
}
}
Offset = 20;
Clr = IIf(sig == "BUY", colorLime, colorRed);
ssl = IIf(bars == BarCount-1, TrendSL[BarCount-1], Ref(TrendSL, -1));
sl = ssl[BarCount-1];


Plot(LineArray(bars-Offset, tar1, BarCount, tar1,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
Plot(LineArray(bars-Offset, tar2, BarCount, tar2,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
Plot(LineArray(bars-Offset, tar3, BarCount, tar3,1), "", Clr, styleLine|styleDots, Null, Null, Offset);

//Plot(LineArray(bars-Offset, sl, BarCount, sl,1), "", colorDarkRed, styleLine|styleLine, Null, Null, Offset);
//Plot(LineArray(bars-Offset, entry, BarCount, entry,1), "", colorGreen, styleLine|styleLine, Null, Null, Offset);

/*
for (i=bars; i <BarCount;i++)
{
PlotText(""+sig+"@"+entry, BarCount-5,entry,Null,colorBlue);
PlotText("T1@"+tar1,BarCount-5,tar1,Null,Clr);PlotText("T2@"+tar2,BarCount-5,tar2,Null,Clr);PlotText ("T3@"+tar3,BarCount-5,tar3,Null,Clr);

}*/

messageboard = ParamToggle("Message Board","Show|Hide",1);
if (messageboard == 1 )
{
GfxSelectFont( "Tahoma", 13, 100 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );

if ( sig =="BUY")
{
GfxSelectSolidBrush( colorBlue ); // this is the box background color
}
else
{
GfxSelectSolidBrush( colorRed ); // this is the box background color
}
pxHeight = Status( "pxchartheight" ) ;
xx = Status( "pxchartwidth");
Left = 1100;
width = 310;
x = 5;
x2 = 290;

y = pxHeight;

GfxSelectPen( colorGreen, 1); // broader color
GfxRoundRect( x, y - 98, x2, y , 7, 7 ) ;
GfxTextOut( ( "Marketcalls - Supertrend"),13,y-100);
GfxTextOut( (" "),27,y-100);
GfxTextOut( ("Last " + sig + " Signal came " + (BarCount-bars-1) * Interval()/60 + " mins ago"), 13, y-80) ; // The text format location
GfxTextOut( ("" + WriteIf(sig =="BUY",sig + " @ ",sig + " @") + " : " + entry), 13, y-60);
GfxTextOut( ("Trailing SL : " + Ref(TrendSL,-1) + " (" + WriteVal(IIf(sig == "SELL",entry-sl,sl-entry), 2.2) + ")"), 13, y-40);
/*GfxTextOut( ("TGT:1 : " + tar1), 13, y -80);
GfxTextOut( ("TGT:2 : " + tar2), 13,y-60);
GfxTextOut( ("TGT:3 : " + tar3), 13,y-40);*/
GfxTextOut( ("Current P/L : " + WriteVal(IIf(sig == "BUY",(C-entry),(entry-C)),2.2)), 13, y-22);;




//Magfied Market Price
FS=Param("Font Size",30,11,100,1);
GfxSelectFont("Times New Roman", FS, 700, True );
GfxSetBkMode( colorWhite );
GfxSetTextColor( ParamColor("Color",colorGreen) );
Hor=Param("Horizontal Position",940,1,1200,1);
Ver=Param("Vertical Position",12,1,830,1);
GfxTextOut(""+C, Hor , Ver );
YC=TimeFrameGetPrice("C",inDaily,-1);
DD=Prec(C-YC,2);
xx=Prec((DD/YC)*100,2);
GfxSelectFont("Times New Roman", 11, 700, True );
GfxSetBkMode( colorBlack );
GfxSetTextColor(ParamColor("Color",colorYellow) );
GfxTextOut(""+DD+" ("+xx+"%)", Hor , Ver+45 );

}

_SECTION_END();

_SECTION_BEGIN("Time Left");
function GetSecondNum()
{
Time = Now( 4 );
Seconds = int( Time % 100 );
Minutes = int( Time / 100 % 100 );
Hours = int( Time / 10000 % 100 );
SecondNum = int( Hours * 60 * 60 + Minutes * 60 + Seconds );
return SecondNum;
}
RequestTimedRefresh( 1 );
TimeFrame = Interval();
SecNumber = GetSecondNum();
Newperiod = SecNumber % TimeFrame == 0;
SecsLeft = SecNumber - int( SecNumber / TimeFrame ) * TimeFrame;
SecsToGo = TimeFrame - SecsLeft;

x=Param("xposn",50,0,1000,1);
y=Param("yposn",380,0,1000,1);

GfxSelectSolidBrush( ColorRGB( 230, 230, 230 ) );
GfxSelectPen( ColorRGB( 230, 230, 230 ), 2 );
if ( NewPeriod )
{
GfxSelectSolidBrush( colorYellow );
GfxSelectPen( colorYellow, 2 );
Say( "New period" );
}
//GfxRoundRect( x+45, y+40, x-3, y-2, 0, 0 );
//GfxSetBkMode(1);
GfxSelectFont( "Arial", 14, 700, False );
GfxSetTextColor( colorRed );
GfxTextOut( "Time Left :"+SecsToGo+"", x, y );
_SECTION_END();
 

Nehal_s143

Well-Known Member
just change the last 2 to 2%

IIf(C>=200 AND C<500, 1,
2 ))))))))));

so change this to

IIf(C>=200 AND C<500, 1,
C*0.02 ))))))))));

you can round off appropriately

or you can have a complete dynamic boxsize by just saying

boxsize = c * 0.02 ;
sir this solution is not working... if used on 4 or more digit value stock/script p&f plots only for 2 rows and that to only few boxs

please provide other solution
 
sir this solution is not working... if used on 4 or more digit value stock/script p&f plots only for 2 rows and that to only few boxs

please provide other solution
Try

boxsize = LastValue(C) * 0.02

if you simply multiply C, it will end up creating array of name boxsize and that will cause havoc for renko charting. Sometime ago, I had posted renko code (working) on a thread created by ford7k. Pls find and use that.
 

josh1

Well-Known Member
I have a code from Rajendran sir SuperTrend.
The problem with the code is that following expression in the code does not work

if(LastValue(Buy))
{//Alert
}
if(LastValue(Sell))
{//Alert
}
You need something like this...
Code:
for( i = 0; i < BarCount; i++ ) 
{ 
if( Buy[i] ) PlotText( "Buy@" + C[ i ], i-3,low[i]-10, colorDarkBlue, colorSkyblue ); 
	AlertIf(Buy, [B]"SOUND H:\\AMIBROKER\\Sound\\ding.WAV"[/B], "Audio Alert", 1);
//if( Cover[i] ) PlotText( "Cover@" + O[ i ], i-3, Low[i]-10, colorDarkBlue, colorSkyblue ); 
if( Sell[i] ) PlotText( "Sell@" +C[ i ], i-4, High[i]+10, colorRed, colorRose ); 
	AlertIf(Sell, [B]"SOUND H:\\AMIBROKER\\Sound\\tada.WAV"[/B], "Sell", 3);
//if( Short[i] ) PlotText( "Short@" +H[ i ], i-4, High[i]+10, colorRed, colorRose );// H[ i ]+Trend[i]+50, colorRed, colorRose ); 
}
Note the .wav files. You have to replace the path with the path of your wav file. You will get different sound for buy and sell.
 
Hi
try this

HTML:
MA10=MA(C,10);
COLORMA =IIf(MA10>Ref(MA10,-1),colorGreen,IIf(MA10<Ref(MA10,-1),colorRed,colorGrey50));
Plot(MA10,"MA10",COLORMA,styleThick);
BARCOLOR =IIf(C>O,colorGreen,colorRed);
Plot(C,"C",BARCOLOR,styleCandle);
 
This AFL exports entire price data into .csv file when I click on scan or explore in New Analysis. How can I make it export data for

//Buy = 1;
Buy=((DateNum()>=1130426) AND (DateNum()<=1130429) );
//Buy = ( (TimeNum()>=190000 AND DateNum()==1050315) OR (TimeNum()<=030000 AND DateNum()==1050316) ) AND V>0;[/CODE]
hi josh1
may be this can help you
Code:
// You can choose ticker and time range by setting "Apply to" an "Range"
// Explore data by execute exploration in Automatic Analysis window
// Export the exploration result by clicking "File --> Export"


Filter =  C>0 ;

AddColumn(O,"Open",1.2); 
AddColumn(H,"High",1.2); 
AddColumn(L,"Low",1.2); 
AddColumn(C,"Close",1.2); 
AddColumn(V,"Volume",1.0,1.0);
 
How do I code this ?

Buy condition = (RSI(14)>50 AND (3SMA croses 10SMA)) OR ( RSI(14) crosses 50 AND 3SMA>10SMA)

Initial SL = lowest low of 5 periods

x= entry - stoploss

After trade is at 1x profit, now SL will be lowest low of 3 periods

Exit either at SL or if 3.15 ( no carrying trades overnight)

This might generate multiple trade conditions while a trade is running, hence if a position is running no more positions should be taken.

My major problem here is switching between two stops and exiting at 3.15 if stop is not triggered. Any help will be appreciated
 

Similar threads