Amibroker - AFL Examples

#31
Hello Sanjay,

Thanks for your test report. Frankly I do not do any back test for any of my indicators. I just look for indicators that give me early entries and exists. The test process I follow is visual and laborious which many may not be willing to do or will not look like a sensible method to some. I am a person who looks for the easiest and simple methods or ways with reliability.

For an example I would show a few trades using this Ichimokku indicator. I believe this indicator was in use for more than a century in Japan. Now we have to make sure this works in our modern day electronic trades.

I am not trying to prove anything but just show the way how I will be using this indicator. Let us use daily data of Reliance as an example.

Let us first take 13th Sep 2006 when Ichimokku had given a buy signal. The close price is Rs.1116.15. Since I got a buy signal first thing I would like to check is where the price is in relation to MA(C,15). If you look at the charts the price has crossed MA(C,15) and closed just below it . Note also the rising slope of MA(C,15) and price climbing towards it. For additional confirmations RSI(15) is above 50 and climbing and also close well above EMA(C,50). I do not use RSI or other Oscillators much since their figures are always between +100 and -100 which are not very helpful in selecting stop loss or price target figures for your trades.

On the same date if you like to look at Nifty it can be seen there is a buy signal from Ichimokku and Nifty has closed above MA(C,15). Since Reliance is a Nifty component this information is useful

When Ichmokku gives a buy signal I look for certain characteristics in the SL , TL and Close lines in Ichimokku. I do not use DL line as it is future looking. For me when the buy signal was given I would like to see lines in the following order. SL at bottom and Red on top of SL preferably with some gap between these lines.

Now for Money management and position sizing. I will take only a maximum risk of 2% on my Account size of Rs 1 Lakh and commission I have to pay one way for delivery is .37%(Actual one way comission is .35 but I add .02 as STT and other charges)

Now my usual entry price is 2-3 Rs above close. That is Rs.1119. The Stop Loss I usually follow is a fixed price of 10-15 Rs per trade. Both entry price and Stop Loss vary according to the price of the scrips. For lower priced scrips it will be much tighter. So in this case my stop loss is Rs. 1104.

Using Ichimokku figures also stop loss can be set. In this case I can keep TL figure of Rs 1109.55 or if I want a wider stop I can use SL figure of Rs 1074.10 which works out to roughly nearly 4% from close price.


My Risk to reward ratio is 2:1.

Based on the above parameters my trade size quantity is 86 shares and money at risk is Rs.1197.36 for this trade if the trade goes against me and my stop is hit. Money in use for this trade is Rs.96,590. which is with in my Account Size.

The Break even price (BEP) is Rs.1127.30. My target price for the desired Risk reward ratio of 2:1 is Rs.1173.93 (4.91%).

Now you can hold on to the long position till Ichimokku or any of your other indicators give sell signal or your stop is hit or price target achieved. In this case stop is not hit but ichimokku gave a sell signal only on 3rd Oct 2006. The price target was achieved on 25th and 26th Sep 2006. giving you a net profit of Rs 4000. on this trade(4.91 %).

Let us assume if the price has gone up but has not touched the target and price starts reversing down. In such a situation what I usually do is if price on the way own has reached my BEP i will get out there without any loss or sell my position at my entry price with commission loss only. Since I can always re enter the position I would prefer to get out at entry price. I get out at stop loss only when price after entry straight away goes down.

Now another point is in this case we have a price target set by Money management technique based on a Risk reward ratio of 2:1. But suppose assume if we had SL or TL line on top of close then we can use these lines as target price or to make a decision to enter the trade by comparing the RR ratio with SL and TL figures. So in effect we can weed out trades which are not profitable. Or another more safer method is if SL is on top of TL and gap between these lines are narrow and close in between I would prefer to enter when close cuts or closes above SL line even though buy signal is already given earlier when close cut TL. Example is Reliance 11th Aug 2006 buy signal, 15th June 2006,


An example is buy signals on 31st Oct 2005. Here the close is between SL and TL and the lines are not in proper order. For a very strong buy usually TL will be above SL. But if a person is interested in quick profits he has a price target of Rs 774 or more. Here also if he enters at an entry price of Rs 765 (Rs 3 + previous close) with a stop loss of Rs. 755 or use the TL figure of Rs 758 as stop the trade size is 128 shares with a target price of Rs. 802 as per Money management which was reached 11th Nov 2005 on wards.

Since his BEP is Rs.770 he can choose to get out at close price of Rs.777(2nd Nov 2005) for a small profit also. In this case also the price is well above his entry price. In fact if the position is held he could have sold it at the close of Rs 817 on 16th Nov 2005 which is well above the target price set by money management of Rs.802. Net profit being Rs. 5906 (6.80%).


Now the doubt arises this guy is showing all good trades especially when market is trending strongly. How about trades when markets are down or consolidating. Let us try it out.

I will show an example of a false buy signal. Reliance 23rd May 2006 , On22nd May 2006 according to the original Ichimokku sell condition the TL line had crossed SL line from above. Look at the other false buy signals on 26th May 2006, 2nd June 2006 and 9th June 2006 and price refusing to go below EMA(C,50) though later on it went below the EMA. In all these cases we could have taken a buy position if we had waited for the close to cross SL since SL was dropping down and TL holding steady.

But on 15th June 2006 buy signal close crossed SL on 19th June 2006 and on 21st June 2006 TL crossed SL from below as per original Ichimokku buy condition.

One other important characteristic I look for in an indicator is whether it will work under any market conditions.

For example look at Reliance from 10th may 2004 when Nifty also is falling down. On 13 th May 2004 there is buy signal in Reliance Ichimokku but in Nifty there is no buy signal.

Will you enter the trade just because there is a buy signal?

If you look at Reliance Ichimokku the TL is falling downwards and SL is on top of TL. Price well below MA(C, 15) and its slope is down wards. Price also below EMA(C,50). And the price is creating new lower lows.

Another important point is that on the Same day TL has crossed SL from above a clear sell indication as per Original Ichimokku guidelines. I will not take this long trade which is confirmed by next day sell signal. The only stop loss figure I can calculate is that of Money management or a fixed price stop in this case. I have no option to select any other figure as stop since price is already below SL, TL, MA(C, 15) and EMA(C, 50).

But i decide to enter a short Position. Here I have the option to select SL, TL, MA(C, 15) and EMA(C,50) as stop figures other than a fixed price stop. Moreover the price, Nifty and Indicators are supporting Short position.


So using the same money management and position sizing we can enter short position. Say at an entry price of Rs 512 on 14 th Ma 2004 with a stop of Rs 524 (Fixed price stop of Rs 12). On a trade size of 126 shares the very next days close of Rs.404 will have given you a net profit of Rs. 13,180(21%).

Now to go to 12th Aug 2002 Reliance Chart. Ichimokku has given a buy signal. But close in between SL and TL and good gap between SL and TL. Price below MA(C, 15) and its slope is down wards but price climbing upwards to MA(C, 15) . Price also well below EMA(C,50).

Look at Nifty on same date. Buy signal in Ichimokku . Nifty below MA(C, 15) and its slope is down wards but Nifty climbing upwards to MA(C, 15) . Nifty also well below EMA(C,50).

Will you take this long trade? It is possible to take this trade. Look at price. The close never crossed the TL on 14th Aug 2002 although it just rested on top of TL line without a sell signal cross. In my experience when after a buy cross or in a retracement after a continuous rise in price if close comes very close to TL line but fails to cut it and there are no sell signal arrows it is a very powerful upward move set up .

I can give more examples of the same set up in Reliance itself. 23 May 2003, 24 june 2003, 5th august 2003 etc. and in Reliance capital 11 th Sep 2006,

Nifty 11th Nov 2002, 28th july 2004, 8th March 2006, 23 Aug 2006,



In this case it is supported by the original Ichimokku buy condition of TL crossing SL from below on 26th Aug 2002.

Of course there will be false signals. But with experience and Money management, position sizing you can easily avoid such trades. There are many more ways to trade using Ichimokku especially short trades with small profits. That is another topic.

So now let us come to the point of your test results. In your back test can you incorporate the conditions like position of lines in Ichimokku when a buy cross is given , Price below or above MA Lines, Trade size, stop loss, price target etc as I have mentioned above under various scenarios. Your current test will have in all probability included all buy or sell signals generated by Ichimokku. In my case many of these buy or sell signals I would have avoided or the number of my successful trades will be more than failed ones there by resulting in net profit. If such conditions or criteria incorporated will the backtest results be the same. Please do try it out if it is possible and when you have time.


What I am trying to convey is an advice given to me a few years before by one of the top AFL experts when I e -mailed to him for help regarding a code. In is reply he had advised me to visually observe the price charts along with indicators. This means when an indicator gives a buy or sell signal what factors along with movement of price made the buy or sell signal a success or why a buy or sell signal failed or what are the other indicators saying with regard to such buy or sell signals.

In a back test I am not sure I will be able to look at all buy or sell signals individually along with my entry, exit or other conditions. I too had earlier tried optimization, Monte carlo technique etc. Any way it was nice looking at 3D charts of this stuff. To be honest I still do not know what all these means. It is latin to me. I do envy people like you when you all talk of these stuff. May be I was never interested in learning the hard way.

I do not know how to code in AFL when close will just rest on TL as I mentioned above without a cross sell signal before the upward move. May be it is possible to code it. But i just do not have that capability.



Like I said earlier I like to keep it simple. During a trade all I wanted to remember is my BEP, entry price, Stop loss and price target nothing else. If trade gone wrong a free mind for a quick exit. If it is a buy I want it to go up and give me profits. If not I am out with minimum injury. Not for me DOW, Sensex, ADRs, Elliot ,Gann etc and a whole lot of stuff. It is beyond my capacity. I would prefer to trade just on price charts and few indicators nothing else.



Moreover in a visual search and study method the patterns and setups are clearly imprinted in the mind so any time it occurs again I know exactly what to do. All I have to do is run an AA analysis in Amibroker using Ichimokku every day. The results are then combed to weed out false signals by using other conditions or in combination with other indicators or price action. Then using Ichimokku you need not scan every day. If you get one or two scrips matching your set up it is more than enough to trade for a couple of weeks in the same scrips.

Our results differ because our trading styles also differ. My trade set ups are mostly based on visual confirmations which are very difficult to be coded. And kindly also note in some of the above trades I have earned between 5-20% return per trade and my risk % is only 2% per trade. So in all probability my net returns will be positive..

If you are using Ichimokku on weekly data then it is better to increase the stop loss figure as there could be bigger fluctuations in price.

So thanks Sanjay for your time and efforts. Perhaps all of us pool together our knowledge and trade better. Do post your suggestions and valuable observations when you have time.

Regards

Saji
 
#32
Dear Saji,

Thanks for the article. Really educative.

I took a printout... and checked all the trades mentioned... very well explained... Now i can say ... i can READ at least one chart (ie; Ichimokku)

Thanks & Regards
Siva
 
#33
Hello Siva,

Thanks for your reply. I just wanted to show that there are few indicators if used carefully will give you excellent returns which ever way market goes. You need not read too many charts . You require only one or two. Moreover you earn that profits without much tension and worries. After all trading is not profits alone you got to enjoy doing it. We often throw away many good indicators without properly studying it.

I was bit heistant to post that message because it was too long. Then i thought for other forum members who are looking to trade with reasonable safety i will post it. More over i needed to get my points across to sanjay as he has excellent knowledge in backtesting and AFL coding which will help us all to make better trades when he and other members come out with improvements and new ideas.

For a study purpose let us use a post by another member today in Technical Analysis , weekly stock calls. He has made some good calls.

Let us take NDTV , look at daily how in Ichimokku lines are well arranged and close and TL pointing upwards. RSI above 50 infact cut 60 level. Another secret when RSI(15) also cut 60 level from below usually you get a good upmove. Remember the word "Usually". And price above MA(C.15) and EMA(C, 50) and both MAs sloping upwards.

And NDTV is just below its HHV(H,50) ie 244.20( Highest value in last 50 days). So in my opinion for the next move it has to close or trade above 244.20. Again remember the word close above. Because some times it will open above this figure will stay at that level and then drop below.

Only negative point i can see is that it has moved a bit too much on friday.So-----?

Again go to the date 26 Oct 2006(daily). you have a buy signal there.And price above MA(c.15) and EMA(C, 50) and both MAs sloping upwards. But most important note the bounce of TL line by close without crossing it or without giving a sell signal. Infact if you look at the figures of TL and Close figures are same.After that it has really gone up.

Now the real beauty is go to weekly charts and look at buy signals. You got one on 11 Aug 2006 and 27th Oct 2006.So the 26 Oct 2006 daily buy is confirmed by weekly on 27 th. More over this signal was given by weekly after a bounce of close from TL . Although close crossed TL by 5 paisa.

So in short you could have been long in NDTV form 8th August 2006 which is confrmed by 11th Aug 2006 weekly buy signal. So you would have been long on NDTV well before others. From 8th Aug SL is solidly supporting TL and Close.

In my case i would with the other indicators i use i would have gone long on 25th july 2006 itself. I am not boasting or showing off. But just putting across a point there are some real good ones out there and the importance of visual observation. If used with money management and position sizing you can make quite a lot of money quietly. You do not need any trendlines or double top,triple bottom, Head and shoulders etc. All you need is discipline and use of your head in a calm manner. Most of these things i tried to learn but never could do it. Even now i do not know how to draw a trend line.More over i have this notorius habit of doing things in an easy and lazy manner.

See another example of his calls Rolta. In weekly Rolta has given a good buy signal on 3rd Nov 2006 and still in buy mode weekly which is confirmed by daily on friday.

But again Only negative point i can see is that it has moved a bit too much on friday.So-----?

So the game is try to spot out a position to enter before others even notice it. That is the game.Get out when all others are entering if you are not greedy. If necessary take some risk by way of early entry but never never compromise on money managment, stoplosss or position sizing.

If you had entered on 3rd Nov 2006 you will be laughing all the way to the bank when others are going to jump in on monday.

Like you i am also learning to use my indicators properly so that i can trade with ease after some time. Remember while tryingout the methods you will lose some money. I too had lost but mainly due to lack of discipline and hoping(biggest enemy and destroyer). Just do not hope. If wrong out. Easier said than done. I have excellent trade systems but discipline part is not upto the mark. I am working on it slowly. But that lossess has given me invaluable knowledge.That is cost of learning.

See another of his calls. Punj Loyd. How beautifully Ichimokku lines are positiond and check the buy signal on 1st Nov 2006(daily) supported by weekly on 3rd Nov 2006. Notice the bounce on 1st Nov from TL . Since the SL and TL are very close and SL on top for more safety you coud have bought after close crosses SL and waited for the riginal Ichimokku buy condition of TL crossing SL from below which happened on 3rd and 4thNov 2006. Moreover you could always keep SL, TL or MA figures as stoploss.

In this case Punj has already taken out its HHV(H,50) and on the way to more upward move provided market condition permits it. For that check Nifty if you require it.

The letter has gone too long. Oh my god , lot of pending work of my clients. They will kill me when they get penalty notices. I will be back after a few days . Till then experiment.

Here is another best advise i got . Cut your trade size to half of what is given by money management and position sizing.

Regards

Saji
 
#34
Hello,

how about this post by another member named SavantGarde

"You Don't Play Things As They Are On Your Blue Guitar" "Things As They Are, Are Changed Upon My Blue Guitar"


"Always Have A Dream"

Now the Explanation: Simply put it means, one can break all the rules of the game and be successful.

Regards

Saji
 

SGM

Active Member
#35
Hello Saji

Thanks for a detailed reply.

I agree with you that a system which uses discretion cannot be programmed / coded. The knowledge and wisdom a trader acquires with years of experience makes him select / reject trades in spite of various indicators.

The Money Management aspect is equally important but much neglected part of trading, thanks to give detailed description about your system of Money management.

From what you have described, you are using the afl just to screen/shortlist the stocks and take decisions based on many aspects (you had told about the MA in previous post also). I think I also understood your motivation behind modifying the original code. Your modifications will give signals faster, but as you will not be trading all of them it will reduce the wipsaws.

I will try to incorporate, the other aspects of your trading rules in the afl and see what comes out of it. I would be doing that during next week-end. I will definitely need your help for that. Meanwhile i will make a list of rules and post it here (trying to extract them from your previous posts). The objective would be to come up with an exploration if not a trading system based on ichimoku system.

I will also try to modify an afl called Distance to Cross Over and adapt it to ichimoku. It will cover some of the points you made in your second post.

You must have been spending lot of time and efforts researching and for writing these posts. Looking forward to more of such insights into afl codes, indicators and trading strategies.


Thanks and Regards
Sanjay
 

Attachments

Last edited:

karthikmarar

Well-Known Member
#36
Hey, This is another fanastic thread... Saji you are doing a wonderful work... missed it due to work overload... Now you can expect my active participation in this work..

warm regards

karthik
 
#37
Hello Karthik,

Your participation and wide experience in Amibroker and trading methods will be of immense contribution to all the forum members. you are welcome.

Regards

Saji
 

rpc

Active Member
#39
hi saji
thanks for the file .I have downloaded it and will ask you any queries regarding ichimoku within a day or two
pls let your AFL examples keep coming.
rpc
 
#40
Here I have attached my example of Ichimoku chart in Amibroker.
See the clear visibility of Clouds there appearing in different colors,up and down,I have plotted 4 different averages here for your info to get clear signals -->
 

Attachments

Similar threads