AmiBroker formula Language

Dear KKSeal,
I have read your post of 26 December. Could understand how to set amibroker. Could not select market as "NSE". In Symbol menu, name does not appear. I am new to amibroker. Kindly help.
 
sir,

can anyone help me with a AFL to scan the stocks for the following conditions:

1. Price rises continuously for 3 days with increase in volume in each day .
2. Price decreases continuously for 3 days with increase in volume in each day.


Thanking you in advance
 

hitesh

Active Member
sir,

can anyone help me with a AFL to scan the stocks for the following conditions:

1. Price rises continuously for 3 days with increase in volume in each day .
2. Price decreases continuously for 3 days with increase in volume in each day.


Thanking you in advance
// 1. Conditions for Buy : Price rises continuously for 3 days with increase in
// Volume in each Day .
// 2. Conditions for Sell : Price decreases continuously for 3 days with
// increase in Volume in each Day.


/////////// Conditions /////////////////////////
Con1= C > Ref(C,-1) AND Ref(C,-1) > Ref(C,-2) AND Ref(C,-2) > Ref(C,-3);

Con2= C < Ref(C,-1) AND Ref(C,-1) < Ref(C,-2) AND Ref(C,-2) < Ref(C,-3);

VolCon = V > Ref(V,-1) AND Ref(V,-1) > Ref(V,-2) AND Ref(V,-2) > Ref(V,-3);


Buy=con1 AND VolCon;


Sell=Con2 AND VolCon;
//////////////// Exploration /////////////////

Filter=Buy OR Sell;

AddTextColumn( FullName( ), "Name" );
AddColumn(C,"Close");
AddTextColumn( MarketID( 1 ), "Market name" );

AddColumn(Buy,"Buy");
AddColumn(Sell,"Sell");

/////////////////////////////////////////////////////////////////////////

Hitesh
 
hi,

Can anybody help me out. I developed an afl for the following

I have just started using afl and am writing simple crossovers now. when i tried to code the following condition i get the signals incorrectly.

My condn is like this:

4sma>17sma>34ema>50sma ----to go long

4sma<17sma<34ema<50sma ----to go short

I used the following code. so please guide me in this where i went wrong.

ramash75

----------------------------
Code
----------------------------
Plot( MA( C, 4),"MA(Close,4)", colorGold, ParamStyle("Stylethick")); "\n";
Plot( MA( C, 17),"MA(Close,17)", colorGreen, ParamStyle("Stylethick"));"\n";
Plot( EMA( C ,34),"EMA(Close,34)", colorPink, ParamStyle("Stylethick") );
Plot( MA( C, 50),"MA(Close,50)", colorRed, ParamStyle("Stylethick") );

Buy1=Cross(MA(C,4),MA(C,17));
Buy2=Cross(MA(C,17),EMA(C,34));
Buy3=Cross(EMA(C,34),MA(C,50));
Sell1=Cross(MA(C,50),EMA(C,34));
Sell2=Cross(EMA(C,34),MA(C,17));
Sell3=Cross(MA(C,17),MA(C,4));

Mycolor = IIf(buy1 AND buy2,IIf(buy2 AND buy3 AND C>Peak(C,2,1),colorBrightGreen,colorRed),
IIf(sell3 AND Sell2 ,IIf(sell2 AND sell1,colorRed,colorBlue),colorGrey50));

PlotOHLC( Open, High, Low, Close,"", Mycolor, styleBar );

shape = Buy1 * shapeUpArrow + Sell3 * shapeDownArrow;


PlotShapes( shape, IIf( Buy1, colorBrightGreen, colorRed ),0, IIf( Buy1, Low, High ) );

GraphXSpace = 5;
dist = 1.5*ATR(8);

for( i = 0; i < BarCount; i++ )
{
if( Buy1 ) PlotText( "Buy\n@" + C[ i-1 ], i-1, L[ i-1 ]-dist, colorBrightGreen );
if( Sell3 ) PlotText( "Sell\n@" + H[ i-1], i-1, H[ i-1 ]+dist, colorRed);
}
 
Dear Praveen and my fellow friends,

I really need this afl. Its a very simple logic. Would be very thankful if you could code it for me.

What i want is :-

1. The script should filter out top loosers in percentage terms in last 2 days, 3 days, 4 days, 5 days etc. depending upon the input.(In descending order.)

Other Script should filter out top gainers in percentage terms in last 2 days, 3 days, 4 days, 5 days etc. depending upon the input.(In descending order.)

Also I want only the stocks which have a market price of more than 10 and have an average daily volume greater than 5 Lakhs.


Please Note :- I dont want consecutive loosers or Consecutive Gainers. only top loosers and gainers.


Waiting for your reply..

Regards,
Himanshu Sharma
 
4sma>17sma>34ema>50sma ----to go long

4sma<17sma<34ema<50sma ----to go short

Buy1=Cross(MA(C,4),MA(C,17));
Buy2=Cross(MA(C,17),EMA(C,34));
Buy3=Cross(EMA(C,34),MA(C,50));
Sell1=Cross(MA(C,50),EMA(C,34));
Sell2=Cross(EMA(C,34),MA(C,17));
Sell3=Cross(MA(C,17),MA(C,4));
Don't user Cross in this case. Cross will return True only for the case when the actual cross happened.

In your case, Cross most probably will happen at different times for three times. Hence use '>' symbol directly.
 
hi praveen,

thanks for your reply.

I tried the same with the following condns.But here i don't get signals at right place when all the conds get satsified. One of our forum member helped with this.


RSI14 = RSI(14);
MACD513 = MACD(5,13);
SIGNAL1 = Signal(5,13,9);
d=MACD5131-SIGNAL1;

MA4 = MA(C, 4);
MA17 = MA(C, 17);
EMA34 = EMA(C, 34);
MA50 = MA(C, 50);

Buy = IIf(RSI14 < 40,IIf(d > 0,IIf(MA4 > MA17,IIf(MA17>EMA34,IIf(EMA34>MA50,1,0),0),0), 0),0);
Sell = IIf(RSI14 > 70,IIf(d < 0,IIf(MA4 < MA17,IIf(MA17<EMA34,IIf(EMA34<MA50,1,0),0),0), 0),0);

kINDLY help me in the conds as if all these are satisfied only i need to get buy or sell signal

thanks

ramash
 
hi praveen,

thanks for your reply.

I tried the same with the following condns.But here i don't get signals at right place when all the conds get satsified. One of our forum member helped with this.


RSI14 = RSI(14);
MACD513 = MACD(5,13);
SIGNAL1 = Signal(5,13,9);
d=MACD5131-SIGNAL1;

....
I don't know whats MACD5131-SIGNAL1 means. Your IIF's are actually doing AND conditions, which is correct, I guess.
 
Dear Praveen and my fellow friends,

I really need this afl. Its a very simple logic. Would be very thankful if you could code it for me.

What i want is :-

1. The script should filter out top loosers in percentage terms in last 2 days, 3 days, 4 days, 5 days etc. depending upon the input.(In descending order.)

Other Script should filter out top gainers in percentage terms in last 2 days, 3 days, 4 days, 5 days etc. depending upon the input.(In descending order.)

Also I want only the stocks which have a market price of more than 10 and have an average daily volume greater than 5 Lakhs.


Please Note :- I dont want consecutive loosers or Consecutive Gainers. only top loosers and gainers.


Waiting for your reply..

Regards,
Himanshu Sharma
Hi Praveen,

Please reply to my post.. I have been waiting since long for your reply..

Regards,
Himanshu
 
Dear sr members,
Few days back i have taken following AFL from some threads (i dont know right now) it works fine but when i tried it for auto analysis it gives following error message..similarly it gives error message for back test as under-----

****************
// -- Have we identified a pivot? If trend is down...



if (aLLVBars[curBar]
-------------------^

Error 10.
Subscript out of range.
You must not access array elements outside 0..(BarCount-1) range.

**********
// -- Have we identified a pivot? If trend is down...



if (aLLVBars[curBar]
-------------------^

Error 10.
Subscript out of range.
You must not access array elements outside 0..(BarCount-1) range.

I am simply pasting said AFL seniors are requested to plz see and rectify the error...


********
_SECTION_BEGIN("BUY-SELL");

_SECTION_BEGIN("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", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

_SECTION_BEGIN("Volume");
Plot( Volume, _DEFAULT_NAME(), ParamColor("Color", colorLavender ), styleNoTitle | ParamStyle( "Style", styleHistogram | styleOwnScale | styleThick | styleNoLabel, maskHistogram ), 2 );
_SECTION_END();

_SECTION_BEGIN("cord");
Cord = 0; // initialize
for( Counter = 52; Counter >= 26; Counter-- )
{
Cord = Max( Cord, LinearReg(Close,Counter) );
}

ProportionalATR = ATR(13)*2.5 / (Sum(C, 52) /13);
Factor = 2.618 * Cord * ProportionalATR;
LD = Cord - Factor;
LD = IIf(Cord < LD, Cord, LD);

// "The upper deviation (UD) is the Cord plus a factor of 2.618 times the 52 week proportional ATR

UD = Cord + Factor;

// Plots

GraphXSpace = 5;

Plot(Cord, "Cord", colorBlue);
Plot(LD, "LD", colorRed);
Plot(UD, "UD", colorGreen);

_SECTION_END();


_SECTION_BEGIN("pivots using arrows");
/* **********************************

Code to automatically identify pivots

********************************** */

// -- what will be our lookback range for the hh and ll?

farback=Param("How Far back to go",100,50,5000,10);
nBars = Param("Number of bars", 12, 5, 40);

// -- Title.



// -- Plot basic candle chart

PlotOHLC(Open, High, Low, Close,

"\n" + "O = " + O + "\n"+"H = "+ H + "\n"+"L = " + L

+ "\n"+"C ",

colorBlack, styleCandle);

GraphXSpace=7;

// -- Create 0-initialized arrays the size of barcount

aHPivs = H - H;

aLPivs = L - L;

// -- More for future use, not necessary for basic plotting

aHPivHighs = H - H;

aLPivLows = L - L;

aHPivIdxs = H - H;

aLPivIdxs = L - L;

nHPivs = 0;

nLPivs = 0;

lastHPIdx = 0;

lastLPIdx = 0;

lastHPH = 0;

lastLPL = 0;

curPivBarIdx = 0;

// -- looking back from the current bar, how many bars

// back were the hhv and llv values of the previous

// n bars, etc.?

aHHVBars = HHVBars(H, nBars);

aLLVBars = LLVBars(L, nBars);

aHHV = HHV(H, nBars);

aLLV = LLV(L, nBars);

// -- Would like to set this up so pivots are calculated back from

// last visible bar to make it easy to "go back" and see the pivots

// this code would find. However, the first instance of

// _Trace output will show a value of 0

aVisBars = Status("barvisible");

nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));

_TRACE("Last visible bar: " + nLastVisBar);

// -- Initialize value of curTrend

curBar = (BarCount-1);

curTrend = "";

if (aLLVBars[curBar] <

aHHVBars[curBar]) {

curTrend = "D";

}

else {

curTrend = "U";

}

// -- Loop through bars. Search for

// entirely array-based approach

// in future version

for (i=0; i<farback; i++) {

curBar = (BarCount - 1) - i;

// -- Have we identified a pivot? If trend is down...

if (aLLVBars[curBar] < aHHVBars[curBar]) {

// ... and had been up, this is a trend change

if (curTrend == "U") {

curTrend = "D";

// -- Capture pivot information

curPivBarIdx = curBar - aLLVBars[curBar];

aLPivs[curPivBarIdx] = 1;

aLPivLows[nLPivs] = L[curPivBarIdx];

aLPivIdxs[nLPivs] = curPivBarIdx;

nLPivs++;

}

// -- or current trend is up

} else {

if (curTrend == "D") {

curTrend = "U";

curPivBarIdx = curBar - aHHVBars[curBar];

aHPivs[curPivBarIdx] = 1;

aHPivHighs[nHPivs] = H[curPivBarIdx];

aHPivIdxs[nHPivs] = curPivBarIdx;

nHPivs++;

}

// -- If curTrend is up...else...

}

// -- loop through bars

}

// -- Basic attempt to add a pivot this logic may have missed

// -- OK, now I want to look at last two pivots. If the most

// recent low pivot is after the last high, I could

// still have a high pivot that I didn't catch

// -- Start at last bar

curBar = (BarCount-1);

candIdx = 0;

candPrc = 0;

lastLPIdx = aLPivIdxs[0];

lastLPL = aLPivLows[0];

lastHPIdx = aHPivIdxs[0];

lastHPH = aHPivHighs[0];

if (lastLPIdx > lastHPIdx) {

// -- Bar and price info for candidate pivot

candIdx = curBar - aHHVBars[curBar];

candPrc = aHHV[curBar];

if (

lastHPH < candPrc AND

candIdx > lastLPIdx AND

candIdx < curBar) {


// -- OK, we'll add this as a pivot...

aHPivs[candIdx] = 1;

// ...and then rearrange elements in the

// pivot information arrays

for (j=0; j<nHPivs; j++) {

aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs-

(j+1)];

aHPivIdxs[nHPivs-j] = aHPivIdxs[nHPivs-(j+1)];

}

aHPivHighs[0] = candPrc ;

aHPivIdxs[0] = candIdx;

nHPivs++;

}

} else {


// -- Bar and price info for candidate pivot

candIdx = curBar - aLLVBars[curBar];

candPrc = aLLV[curBar];

if (

lastLPL > candPrc AND

candIdx > lastHPIdx AND

candIdx < curBar) {


// -- OK, we'll add this as a pivot...

aLPivs[candIdx] = 1;

// ...and then rearrange elements in the

// pivot information arrays

for (j=0; j<nLPivs; j++) {

aLPivLows[nLPivs-j] = aLPivLows[nLPivs-(j+1)];

aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-(j+1)];

}

aLPivLows[0] = candPrc;

aLPivIdxs[0] = candIdx;

nLPivs++;

}

}

// -- Dump inventory of high pivots for debugging

/*

for (k=0; k<nHPivs; k++) {

_TRACE("High pivot no. " + k

+ " at barindex: " + aHPivIdxs[k] + ", "

+ WriteVal(ValueWhen(BarIndex()==aHPivIdxs[k],

DateTime(), 1), formatDateTime)

+ ", " + aHPivHighs[k]);

}

*/

// -- OK, let's plot the pivots using arrows

PlotShapes(

IIf(aHPivs==1, shapeDownArrow, shapeNone), colorRed, 0,

High, Offset=-8);

PlotShapes(

IIf(aLPivs==1, shapeUpArrow , shapeNone), colorGreen, 0,

Low, Offset=-8);

_SECTION_END();

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
THIS SECTION DRAWS TD TREND LINES */

_SECTION_BEGIN("Trend Line");
percent = 0.01 * 1; /* Adjust this percent as necessary, I use .01 because FOREX is a 0.0000 number */
firstpointL = 2;
firstpointH = 2;

y0=LastValue(Trough(L,percent,firstpointL));
y1=LastValue(Trough(Ref(L,-1),percent,1));

for( i = 1; i < BarCount AND y0 >= y1; i++ )
{

firstpointL++;
y0=LastValue(Trough(L,percent,firstpointL));
}

x0=BarCount - 1 - LastValue(TroughBars(L,percent,firstpointL));
x1=BarCount - 1 - LastValue(TroughBars(Ref(L,-1),percent,1));
LineL = LineArray( x0, y0, x1, y1, 1 );
/*
Plot(C, "C", colorBlack, styleCandle);
*/
Plot( LineL, " Support Trend line", colorBrown,4 +8 );


yt0=LastValue(Peak(H,percent,firstpointH));
yt1=LastValue(Peak(Ref(H,-1),percent,1));

for(i = 1; i < BarCount AND yt0 <= yt1; i++ )
{

firstpointH++;
yt0=LastValue(Peak(H,percent,firstpointH));
}
xt0=BarCount - 1 - LastValue(PeakBars(H,percent,firstpointH));
xt1=BarCount - 1 - LastValue(PeakBars(Ref(H,-1),percent,1));
LineH = LineArray( xt0, yt0, xt1, yt1, 1 );

Plot( LineH, "Resistance Trend line", colorBrown,4 + 8 );

_SECTION_END();
/*-----------------------------------------------------------------------*/

_SECTION_BEGIN("Giving price");
GraphXSpace = 20;
dist = 1.1*ATR(20);
T1 = 0; T2 = 0; B1 = 0; B2 = 0;
for(m=0;m<=BarCount-1;m++){
if(aHPivs[m] == 1){
PlotText("" + (BarCount - m - 1)+ "C" + C[m], m, H[m]+dist[m],colorRed);
if (T1 != 0) T2 = T1;
T1 = H[m];
}
if(aLPivs[m] == 1){
PlotText("" + (BarCount - m - 1)+ "C" + C[m], m, L[m]-dist[m], colorGreen);
if(B1!=0) B2 = B1;
B1 = L[m];
}
}

Buy = C > T1 AND T1 > T2 AND B1 > B2 ;
Sell = C < B1 ;

_SECTION_END();
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/


_SECTION_END();
 

Similar threads