Afl for trading system....help in coding

#1
Dear members
I have developed a few customised indicators for Nifty Futures and am trying to develop an automated signal generator AFL from those.
I am stuck now......cannot get through the AFL language for the same.
Need the help of experienced fellow traders........please help.

Thanks and appreciation in advance.

This is what I am looking for:

A: plot green up arrow on price chart when all the following conditions are met:

1. Close crosses and closes above a customised Moving Average
2. Customised RSI crosses and closes above its customised signal line
3. Customised Stochastic crosses and closes above its customised signal line
4. Customised Stochastic RSI crosses and closes above its customised signal line


B: plot red down arrow on price chart when all the following conditions are met:

1. Close crosses and closes below a customised Moving Average
2. Customised RSI crosses and closes below its customised signal line
3. Customised Stochastic crosses and closes below its customised signal line
4. Customised Stochastic RSI crosses and closes below its customised signal line



C: create comments in the interpretation window for below:

If buy signal generated:

1. Mention Stop loss as last low minus 5
2. color of the background of the interpretation window to turn pale green
3. color of the background of the interpretation window to turn orange if close is equal to 30 plus the price at which buy signal generated


If sell signal generated:

1. Mention Stop loss as last high plus 5
2. color of the background of the interpretation window to turn pale pink
3. color of the background of the interpretation window to turn orange if close is equal to 30 minus the price at which sell signal generated
 
Last edited:
#2
Hi

RSI & STOCHASTICS -BOTH ARE POOR INDICATORS.
These 2 trap traders more than they help.
It is not worth the effort.
I personally know of traders who lost lot of money using RSI(2) and using stochastics.

When you base material for building foundation is itself so weak how can you expect profits?
OBSERVE THIS FIRST
Just take a price chart and keep stochastics chart below it and see.
when market is rising upwards stochastics gives more and more sell signals.result-you wont be in trending market.
stochastics is meant for sideways zones.
when market is falling,you get lots of buying signals from stochastics. if one buy he gets doomed.
 

manojborle

Well-Known Member
#3
ford7k is correct.
Stochastic is an oscillator and better to be used in a trading range...
For Trending market ADX could be helpful..
 
#5
Thanx a lot Ford, Manoj and Kelvin.....for your help.
I have the facts in mind.......and have decided to add one more condition to my set up....fast moving averages in relation to slower ones......that should take care of the trend to some extent.....and for the faulty trades I have the stop loss in place.
 

amitrandive

Well-Known Member
#6
From AmiBroker Library

_SECTION_BEGIN("AFL Example");
/*
This is an attempt to provide a basic trading system AFL. The system is purely
imaginary
AND NOT provided as one that would make money. This is just to provide a guide
to learners
on the common components of writing AFL.

Prepared by Graham Kavanagh 12 Aug 2005
AB Write http://e-wire.net.au/~eb_kavan/ab_write.htm

When you copy/paste ensure the existing continuous lines have not been wrapped.
This wrapping
can create error signals when you try to use the code. Click on the check afl
button in the
editor before trying to apply or scan.
I have used slash-asterisk /* */ /* for my comments to get around the problem
of wrapping,
which could happen if you used double slash //

I hope this helps the beginners in creating AFL code

*/

/*firstly some basics common*/
SetBarsRequired(10000,10000); /* this ensures that the charts include all bars
AND NOT just those on screen */
SetFormulaName("Sample System"); /*name it for backtest report identification
*/
SetTradeDelays( 1, 1, 1, 1 ); /* delay entry/exit by one bar */
SetOption( "initialequity", 100000 ); /* starting capital */
PositionSize = -10; /* trade size will be 10% of available equty */
SetOption( "MaxOpenPositions", 6 ); /* I don't want to comit more than 60% of
Equity at any one time */
SetOption( "PriceBoundChecking", 1 ); /* trade only within the chart bar's
price range */
SetOption( "CommissionMode", 2 ); /* set commissions AND costs as $ per trade
*/
SetOption( "CommissionAmount", 32.95 ); /* commissions AND cost */
SetOption( "UsePrevBarEquityForPosSizing", 1 ); /*set the use of last bars
equity for trade size*/
PositionScore = 100/C; /*Set the order for which stock trades when get mulitple
signals in one bar in backtesting */

//Trade system
/*
Buy when exp mov avg crosses and the high is highest for 50 bars
Sell when exp mov avg crosses back
Cross is first variable moves to above the second variable
*/

LongPer = Param("Long Period", 50, 30, 100, 5 ); /* select periods with
parameter window */
ShortPer = Param("Short Period", 5, 3, 10, 1 );

LongMA = EMA( C, LongPer );
ShortMA = EMA( C, ShortPer );
LastHigh = HHV( H, LongPer );

Buy = Cross( ShortMA, LongMA ) AND H > Ref( LastHigh, -1 );
/* ref,-1 is used for the high to have todays high greater than the previous 50
bar high.
To just use H==LastHigh couold mean a previous high was equal to current
high */
Sell = Cross( LongMA, ShortMA );
/* exrem is one method to remove surplus strade signals*/
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);


/* Now for exploration results.
Will restrict results of exploration to when the Buy AND Sell signals occur

You can use Filter=1; to display every bar result */

Filter = Buy OR Sell;
AddTextColumn( FullName(), "Company Name" );
AddColumn( Buy, "Buy", 1 );
AddColumn( Sell, "Sell", 1 );
AddColumn( C, "Close", 1.3 );
AddColumn( H, "High", 1.3 );
AddColumn( LastHigh, "HHV", 1.3 );
AddColumn( LongMA, "Long MA", 1,3 );
AddColumn( ShortMA, "Short MA", 1,3 );


/* Now to show this on a chart */
/* I use WriteVal to limit the values to the wanted number of decimal places,
seeing a value of 5 decimal places can be frustrating.
I have included additional information in the plot title sections to add
some
information to the title block */

GraphXSpace = 10; /* create empty space of 10% top and bottom of chart */

Plot( C, " Close Price", colorGrey50, styleBar );
Plot( LongMA, " EMA(C,"+WriteVal(LongPer,1)+")", colorRed,
styleLine|styleNoRescale );
Plot( ShortMA, " EMA(C,"+WriteVal(ShortPer,1)+")", colorGreen,
styleLine|styleNoRescale );
Plot( Ref(Lasthigh,-1), " HHV(H,"+WriteVal(LongPer,1)+")", colorBlue,
styleNoLine|styleDots|styleNoRescale );

/* styleNoRescale in the plots stops the added plots from compressing the
original bar chart to the middle of the pane */

PlotShapes( shapeUpArrow*Buy, colorGreen, 0, L, -10 );
PlotShapes( shapeDownArrow*Sell, colorRed, 0, H, -10 );

Title = " {{NAME}} {{DATE}} {{INTERVAL}} "+_DEFAULT_NAME()+" Chart values :
{{VALUES}} ";
/* _DEFAULT_NAME() shows the section name or, if not present, the file name
the items in {{}} are short cuts for the title block. It can be done long hand

Title = Name() +" "+ Date() +" "+ "{{INTERVAL}}"+_DEFAULT_NAME()+" Chart values
: " +
" Close Price = " + C +
" EMA(C,"+WriteVal(LongPer,1)+") = "+WriteVal(LongMA,1.3) +
" EMA(C,"+WriteVal(ShortPer,1)+") = "+WriteVal(ShortMA,1.3) +
" HHV(H,"+WriteVal(LongPer,1)+") = "+WriteVal(Ref(LastHigh,-1),1.3) ;

*/

_SECTION_END();
 
#8
From AmiBroker Library

_SECTION_BEGIN("AFL Example");
/*
This is an attempt to provide a basic trading system AFL. The system is purely
imaginary
AND NOT provided as one that would make money. This is just to provide a guide
to learners
on the common components of writing AFL.

Prepared by Graham Kavanagh 12 Aug 2005
AB Write http://e-wire.net.au/~eb_kavan/ab_write.htm

When you copy/paste ensure the existing continuous lines have not been wrapped.
This wrapping
can create error signals when you try to use the code. Click on the check afl
button in the
editor before trying to apply or scan.
I have used slash-asterisk /* */ /* for my comments to get around the problem
of wrapping,
which could happen if you used double slash //

I hope this helps the beginners in creating AFL code

*/

/*firstly some basics common*/
SetBarsRequired(10000,10000); /* this ensures that the charts include all bars
AND NOT just those on screen */
SetFormulaName("Sample System"); /*name it for backtest report identification
*/
SetTradeDelays( 1, 1, 1, 1 ); /* delay entry/exit by one bar */
SetOption( "initialequity", 100000 ); /* starting capital */
PositionSize = -10; /* trade size will be 10% of available equty */
SetOption( "MaxOpenPositions", 6 ); /* I don't want to comit more than 60% of
Equity at any one time */
SetOption( "PriceBoundChecking", 1 ); /* trade only within the chart bar's
price range */
SetOption( "CommissionMode", 2 ); /* set commissions AND costs as $ per trade
*/
SetOption( "CommissionAmount", 32.95 ); /* commissions AND cost */
SetOption( "UsePrevBarEquityForPosSizing", 1 ); /*set the use of last bars
equity for trade size*/
PositionScore = 100/C; /*Set the order for which stock trades when get mulitple
signals in one bar in backtesting */

//Trade system
/*
Buy when exp mov avg crosses and the high is highest for 50 bars
Sell when exp mov avg crosses back
Cross is first variable moves to above the second variable
*/

LongPer = Param("Long Period", 50, 30, 100, 5 ); /* select periods with
parameter window */
ShortPer = Param("Short Period", 5, 3, 10, 1 );

LongMA = EMA( C, LongPer );
ShortMA = EMA( C, ShortPer );
LastHigh = HHV( H, LongPer );

Buy = Cross( ShortMA, LongMA ) AND H > Ref( LastHigh, -1 );
/* ref,-1 is used for the high to have todays high greater than the previous 50
bar high.
To just use H==LastHigh couold mean a previous high was equal to current
high */
Sell = Cross( LongMA, ShortMA );
/* exrem is one method to remove surplus strade signals*/
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);


/* Now for exploration results.
Will restrict results of exploration to when the Buy AND Sell signals occur

You can use Filter=1; to display every bar result */

Filter = Buy OR Sell;
AddTextColumn( FullName(), "Company Name" );
AddColumn( Buy, "Buy", 1 );
AddColumn( Sell, "Sell", 1 );
AddColumn( C, "Close", 1.3 );
AddColumn( H, "High", 1.3 );
AddColumn( LastHigh, "HHV", 1.3 );
AddColumn( LongMA, "Long MA", 1,3 );
AddColumn( ShortMA, "Short MA", 1,3 );


/* Now to show this on a chart */
/* I use WriteVal to limit the values to the wanted number of decimal places,
seeing a value of 5 decimal places can be frustrating.
I have included additional information in the plot title sections to add
some
information to the title block */

GraphXSpace = 10; /* create empty space of 10% top and bottom of chart */

Plot( C, " Close Price", colorGrey50, styleBar );
Plot( LongMA, " EMA(C,"+WriteVal(LongPer,1)+")", colorRed,
styleLine|styleNoRescale );
Plot( ShortMA, " EMA(C,"+WriteVal(ShortPer,1)+")", colorGreen,
styleLine|styleNoRescale );
Plot( Ref(Lasthigh,-1), " HHV(H,"+WriteVal(LongPer,1)+")", colorBlue,
styleNoLine|styleDots|styleNoRescale );

/* styleNoRescale in the plots stops the added plots from compressing the
original bar chart to the middle of the pane */

PlotShapes( shapeUpArrow*Buy, colorGreen, 0, L, -10 );
PlotShapes( shapeDownArrow*Sell, colorRed, 0, H, -10 );

Title = " {{NAME}} {{DATE}} {{INTERVAL}} "+_DEFAULT_NAME()+" Chart values :
{{VALUES}} ";
/* _DEFAULT_NAME() shows the section name or, if not present, the file name
the items in {{}} are short cuts for the title block. It can be done long hand

Title = Name() +" "+ Date() +" "+ "{{INTERVAL}}"+_DEFAULT_NAME()+" Chart values
: " +
" Close Price = " + C +
" EMA(C,"+WriteVal(LongPer,1)+") = "+WriteVal(LongMA,1.3) +
" EMA(C,"+WriteVal(ShortPer,1)+") = "+WriteVal(ShortMA,1.3) +
" HHV(H,"+WriteVal(LongPer,1)+") = "+WriteVal(Ref(LastHigh,-1),1.3) ;

*/

_SECTION_END();
How can I set conditions e.g. plot up arrow if conditions a,b,c and d are satisfied
 
#9
hi friends

Thanks for KelvinHand for showing new outlook with divergence.

My question to traders

Can you locate divergence?
If no, if it is too difficull for you,then
stay away from RSI & STOCHASTICS.

Read my post on TSTW.
There it is shown that you trade rsi with price trendline as filter.
you can buy at price when rsi is crossing oversold or overbought zones.

Only one question

Can you draw a reasonably decent trendline on price chart?
It is price trend that decides if you buy or not,not RSI
iN THE RSI OB ZONE ,THERE IS A FALSE SELL SIGNAL AS RSI CROSS BELOW 70 LEVEL.
IF SOLD THERE,YOU are in problem.
can you see it?













Thanx a lot Ford, Manoj and Kelvin.....for your help.
I have the facts in mind.......and have decided to add one more condition to my set up....fast moving averages in relation to slower ones......that should take care of the trend to some extent.....and for the faulty trades I have the stop loss in place.
 
#10
hi experts!

I am trying my hand at afl writing. I am not able to capture today's date and time and so the afl starts from where the charts started, about 2 months back. please tell me how to capture current date and time.
 

Similar threads