Stocks To Keep A Close Eye On

Status
Not open for further replies.

SavantGarde

Well-Known Member
Anant's AFL Links To The Post Can Be Found On The First Post Of Chapter-II

http://www.traderji.com/equities/29292-stocks-keep-close-eye-chapter-ii-75.html#post315305

Another Link Where You Can Find Anant's Explanation & The AFL

http://www.traderji.com/equities/29292-stocks-keep-close-eye-chapter-ii-6.html#post398196


Happy & Safer Investing

SavantGarde

Hi all
Please anybody help me in which page anant's latest afl code in this thread. i am new for traderji. i cant get that properly. please guide me
 
thanks for ur quick reply ji. i really admired u three people's work in single thread. HATS OFF TO U ALL friends. i am new for amibroker and please clear my doubt sir. This afl code given by anant is for short term picks?. Please clarify ji
All the triggers given are trading picks with targets/stops. The target may get achieved in short term/may take take longer time also. Either targets/stops are hit. Pl dont carry the position if stops are hit.
 
Re: Stocks To Keep A Close Eye On - Chapter II


*********************************************************
Latest Update:

AFL modified

Last update on: 01 Jan 2010
*********************************************************
The modified portion is in MAGENTA Color




Hi Friends,

Now let us go into the details of system and the AFL. Here instead of just posting the AFL, I will describe the step-by-step development of the AFL based on the idea in our mind. This may be unnecessary for experts but will be useful for those who are new to AFL writing and want to learn to write their own AFL for their own trading ideas. It also helps in understanding the AFL in complete detail so that the experts can understand my logic and do their own tweaking and modifications. So, Let us start.

As I have already written in the earlier post, the strategy is based on Moving Average. The core of the strategy is, therefore, Buy when Price goes above the Moving Average and Sell when it goes below. Appears too simple but more refinement is required. We will represent the Moving Average by a variable A. The period of averaging is P. So, Buy = Close > A and Sell = Close < A, A = Moving Avg(Close, P).

Before Writing the AFL let us list what we want out of the AFL. My expectations from the AFL are:

1. It should give me Buy and Sell Signals and show these signals on chart
2. It should indicate on chart on which candle the Buy/Sell trigger is generated and print the message also indicating the trigger and the action to be taken next day.
3. It should show the present status whether the stock is in Buy-Hold or in Sell-Hold condition
4. In Buy-Hold condition it show on the chart the current profit/Loss at each candle
5. In Buy-Hold condition it should show the next Target expected and the stop loss
6. In Sell-Hold condition it should show the profit/loss made in the previous trade.
7. On the chart it should show the Target and Stop Loss as straight lines between which the price is moving
8. When target is reached, announce it on the chart and automatically calculate the next Target and Stop Loss and show on the chart.
9. For any selected day Generate reports giving the Buy-Sell triggers generated, the targets/Stop Loss reached and daily update of the stocks which have already given Buy Trigger.
10. Add Bollinger Bands to chart optionally, default no BB added. BB period and Width should be changeable without affecting Averaging period.

Quite a long list of expectations? Then the AFL should be very long and complicated. Is it so?

No, it is not that difficult to code these and the AFL will not be very long and complicated.

Now let us develop the AFL and incorporate our expectations into it.

You may later use this AFL as part of another strategy. For this purpose we will confine this AFL code in a separate Section. This can be done in AMiBroker AFL by enclosing the code between two statements:
*************************************
_SECTION_BEGIN(SingleMA);

(All of the AFL code here)

_SECTION_END();


**************************************

I have tested four types of Moving Averages: Simple, Exponential, Wilders and Linear Regression. Linear regression gives some very good triggers but it changes depending on future prices. Therefore I have eliminated it. We will have remaining three of them available in the same AFL and select whichever we want. I have put Wilders MA as default and the other two as options. I have found 20-day period of averaging good enough but we will have option of changing this also as required. We will also specify the type of report we would like to get: Triggers report, Update Report, Target / SL reached report. As we want charts also display the information, we define some options for charts also. The code for these is as follows:

************************************************
_SECTION_BEGIN(SingleMA);

SetChartOptions(0, chartShowDates | chartWrapTitle);

Type = ParamList(Average Type, Wilders,SMA,EMA);
P = Param(Averaging Period, 20, 3, 100);
Report = ParamList(Trigs or Update or Tgt-SL, Triggers,Update,Tgt-SL);
Q = Param(%Change, 1, 0.1, 10, 0.1);
BP = Param("BB period", 20, 3, 100);
BW = Param("BB Width", 2, 0.5, 10, 0.5);
BBOption = ParamToggle("Plot BB?", "NO | YES");

If(Type == SMA) A = MA(C, P);
If(Type == EMA) A = EMA(C, P);

If(Type == Wilders) A = Wilders(C, P);

************************************************
Note the double = signs in the above if statements. Q is a number we use to define the percent change in price to determine Peaks and troghs in the prices. The default is 1.

Now define top and bottom Bollinger Bands. BB period is BP, BB width is BW and BB is based on Closing Price:

************************************************
BBTop = BBandTop(C, BP, BW);
BBBot = BBandBot(C, BP, BW);

************************************************


We define Stop Loss as the higher of lowest low in last 5 days or the previous trough and Target as two times the High on the day of Buy Trigger minus the Stop Loss. The code follows:

**************************************
SL = Max(Trough(Low, Q, 1), LLV(L, 5));
Tgt = 2 * High SL;
MeanPrice = (Open + Close) / 2;


****************************************
Mean price is average of Open and Close for the day. This is used to calculate buy and sell prices and profit/loss made. As we can not enter the actual buy/sell prices we use these figures. The calculated profit/loss will slightly differ from actual but it is sufficient to get an idea of our portfolio value.

A Buy or Sell condition when price (Closing price of the day) goes above or below the average A will lead to many false signals. Therefore, we take a Buy condition only if the whole candle is above the Moving Average and Sell when it is completely below. I found that the Sell condition is okay but if we wait till the whole candle goes above the average it will have some delay. By experimenting with different values I found that if about 70% of candle is above the Average is good enough and acceptable. Therefore, we should determine the part of the candle which is above the Average.

When the low of the candle is just touching the average (Low = Average) the candle is 100% above. When the High is just touching the Average (High = Average) the candle is 0% above Average. The average line cutting across the candle means a part of the candle is above. So (High Average) divided by (High Low) and then multiplied by 100 gives the part of candle above the average in percentage. We code this as follows:

**********************************
Part = 100 * (H A) / (H L);

Buy = (C > A) AND (Part > 70);
Sell = H < A;


************************************

This will give Buy for every candle which is 70% or more above A and Sell for every candle below A. We want only the first candle which is a Buy and only the first candle which is Sell. So, we remove all subsequent Buy/Sell signals till an opposite signal is found. So, the code is:

**********************************
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);


**********************************

In between the Buy and next Sell signal the stock is in BOUGHT condition or Buy-Hold (BH) as Savant calls it. Similarly between Sell and next Buy signal the stock is in SOLD condition (or Sell-Hold). We code this as:

********************************
Bought = Flip(Buy, Sell);
Sold = Flip(Sell, Buy);


*********************************

The Target Price calculation code is:

****************************************
NextTgt = ValueWhen(Buy, Tgt, 1);

*****************************************

When the Stock reaches its Target we should shift the Target price to the next value. This happens only when the stock is in Bought condition and not when the Buy trigger is generated. Similarly, the Stop Loss also is raised when a higher low is made after buying (Bought condition). This we code as follows:

************************************
For(i = 1; i < BarCount; i++)
{
If(Bought AND NOT Buy)
{
SL = Max(SL, SL[i 1]);
If(C[i 1] >= NextTgt[i 1]) NextTgt = Tgt[i 1];
NextTgt = Max(NextTgt, NextTgt[i 1]);
}
}

**************************************

For reporting the current status of the stock and profit/loss made we should know when the stock was bought, at what price it was bought and at what price it was sold. Similarly we should know whether the stock reached target or hit the stop loss. These are coded as follows:

***********************************************
BuyDate = ValueWhen(Buy, Ref(DateTime(), 1), 1);
BuyPrice = ValueWhen(Buy, Ref(MeanPrice, 1), 1);
SellPrice = ValueWhen(Sell, Ref(MeanPrice, 1), 1);
TgtReached = IIf(Bought AND NOT Buy AND C>= NextTgt, True, False);
SLHit = IIF(Bought AND NOT BUY AND C < SL, True, False);
SLHit = ExRem(SLHit, Buy);

************************************************

Now we have all the information computed by the AFL and now we want this to be displayed on the chart or shown in a report. First we take the case of displaying in the chart. The information can be displayed by including it in the chart title. This we do as follows:

********************************************
Ttl = EncodeColor(colorTurquoise) + Single MA System, AFL by ANANT NAVALE \n
+ WriteIf(Buy, EncodeColor(colorGreen) + Buy Triggered Today. Buy this stock tomorrow., )
+ WriteIf(Sell, EncodeColor(coloRed) + Sell Triggered Today. Sell this Stock Tomorrow., )
+ EncodeColor(colorTan) + WriteIf(Bought AND NOT Buy, Bought @ + BuyPrice + .
+ Target Price = + NextTgt + , Stop Loss = + SL + .\n
+ WriteIf(TgtReached, Target Reached. Next Target = + Ref(NextTgt, 1) + .\n, )
+ EncodeColor(colorGold) + Profit / Loss so far =
+ Prec(100 * (C BuyPrice) / (BuyPrice, 2) + %, )
+ WriteIf(Sold AND NOT Sell, Sold @ + SellPrice + \nProfit / Loss in previous trade =
+ Prec(100 * (SellPrice BuyPrice) / BuyPrice, 2) + %, );


*********************************************

In the above code you can replace my name with yours if you want.

Finally the title to display is:

*********************************************
_N(Title = StrFormat({{NAME}} ({{INTERVAL}}), {{DATE}} ; O=%g, H=%g, L=%g, L=%g, C=%g, {{VALUES}}\n\n, O, H, L, C) + Ttl);

**********************************************

We will show Arrow marks on the chart at the candles where the Buy/Sell signals are generated.

Now plot the chart with Buy/Sell Arrows

***********************************************
If(Status(action) == actionIndicator)
{
PlotOHLC(O, H, L, C, , colorLightGrey, styleBar);
Plot(A, Type + ( + P +), colorYellow, styleLine | styleThick);
Plot(IIf(Bought, NextTgt, Null), Target, colorBlueGrey, styleLine);
Plot(SL, Trail SL, colorTeal, styleLine);
Plotshapes(Marker, MarkerColor, 0, MarkerDist);
if(BBOption) Plot(BBTop, "BB-Top", colorPink, styleLine);
if(BBOption) Plot(BBBot, "BB-Bottom", colorPink, styleLine);

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

}

*******************************************

To get different reports:

Triggers report

*******************************************
if((Status(action) == actionExplore) AND Report == Triggers)
{
Filter = Buy OR Sell;
SetOption(NoDefaultColumns, True);
AddTextColumn(Name(), Symbol, 77, colorDefault, colorDefault, 120);
AddColumn(DateTime(), Trigger Date, formatDateTime);
AddColumn(IIf(Buy, 66, 83), Signal, formatChar, colorYellow, IIf(Buy, colorGreen, colorRed);
AddColumn(C, C. M. P., 6.2);
AddColumn(IIf(Buy OR bought, NextTgt, Null), Target, 6.2);
AddColumn(IIf(Buy OR Bought, SL, Null), Stop Loss, 6.2);
}

***********************************

For updating:

****************************************
If((Status(action) == actioExplore) AND Report == Update)
{
Filter = True;
SetOption(NoDefaultColumns, True);
ADDColumn(DateTime(), Updated on, formatDateTime);
AddTextColumn(Name(), Symbol, 77, colorDefault, colorDefault, 120);
AddColumn(BuyDate, Buy Date, formatDateTime);
AddColumn(BuyPrice, Buy Price, 6.2);
AddColumn(NextTgt, Target, 6.2);
AddColumn(SL, StopLoss, 6.2);
AddColumn(C, C. M. P., 6.2);
}

*******************************************

For Target/Stoploss Hit Report:

******************************************
if(Status(action) == actionExplore AND Report == Tgt-SL

{
Filter = TgtReached OR SLHit;
SetOption(NoDefaultColumns, True);
AddColumn(DateTime(), Updated on, formatDateTime);
AddTextColumn(Name(), Symbol, 77, colorDefault, colorDefault, 120);
AddColumn(BuyDate, Buy Date, formatDateTime);
AddColumn(BuyPrice, Buy Price, 6.2);
AddColumn(NextTgt, Target, 6.2);
AddColumn(SL, StopLoss, 6.2);
AddColumn(C, C. M. P., 6.2);
AddColumn(IIf(TgtReached, 89, 32), Tgt Hit?, formatChar, colorYellow, IIf(TgtReached, colorGreen, colorDefault));
AddColumn(IIf(SLHit, 89, 32), SL Hit?, formatChar, colorYellow, IIF(SLHit, colorRed, colorDefault));
}

******************************************

The complete modified AFL file is available here. You can download it and save in your Formulas/Custom folder. To plot the chart double click on it and to get reports run it in Analysis.

You may post all your queries, comments and suggestions and I will try to answer them as much as possible.

I am using a list if about 300 stocks to track trading signals using the AFL. The list can be obtained from the following link.

Stocks List


Regards

-Anant


With this AFL i am getting this error how can I rectify the error

WriteIf(Sold AND NOT Sell, "Sold @ " + SellPrice + "\nProfit / Loss in Previous Trade = " + Prec(100 * (SellPrice - BuyPrice) / BuyPrice, 2) + "%", "");

_N(Title = StrFormat("{{NAME}} ({{INTERVAL}}), {{DATE}}
-----------------------------------------------------------
{{OHLCX}}, V=%1.0f\n {{VALUES}}\n\n", V + Ttl));
----------^
Error1
operation not allowed operator/operand typemismatch.
 

saivenkat

Well-Known Member
Re: Stocks To Keep A Close Eye On - Chapter II

Hi Friends,

Updates based on EOD of 11-01-2010 are:

TARGETS HIT

The following have reached TARGETS:

Code:
Symbol     | Buy Date | Buy Price |  Target | Stop Loss | C.M.P.  | P / L %
-----------|----------|-----------|---------|-----------|---------|-----------
AHMEDFORGE | 16-07-09 |    46.00  |   75.85 |    45.00  |   83.10 |  80.65%
DCW        | 07-10-09 |    19.40  |   22.25 |    16.60  |   24.50 |  26.29%
ENGINERSIN | 09-11-09 |  1318.80  | 1580.65 |  1062.95  | 1635.95 |  24.05%
GUJFLUORO  | 29-12-09 |   131.50  |  140.55 |   122.45  |  144.55 |   9.92%
OMAXAUTO   | 30-11-09 |    56.00  |   67.75 |    54.25  |   68.10 |  21.61%
SCI        | 07-12-09 |   148.70  |  164.25 |   139.15  |  169.10 |  13.72%
SUNTV      | 20-07-09 |   246.45  |  366.30 |   301.10  |  370.75 |  50.44%
------------------------------------------------------------------------------

Next Target and SL for the above will be set by Savantji.

STOP LOSS HIT TODAY

Code:
Symbol     | Buy Date | Buy Price |  Target | Stop Loss | C.M.P.  | P / L %
-----------|----------|-----------|---------|-----------|---------|-----------
TWL        | 11-01-10 |   424.90  |  459.25 |   414.95  |  414.50 |  -2.45%
------------------------------------------------------------------------------

The updated Excel File is available here.


Regards

*** Uma ***
In the Excel sheet under the month "November 2009" the 34th entry NIIT Technologies is showing a wrong buy price of 1699/- and their is a huge loss indicated there. Kindly check it sir.

Also, at entry 143 IFCI BUY PRICE IS INDICATED AS 271, and at entry number 258 RENUKA SUGAR buy price is 927.65, and at 283 entry STC INDIA is indicated with buy price of 1037.55, and at entry 317, TV-18 BUY PRICE IS GIVEN AS 269.45

Please check if they are wrong or those are values before split.

One thing i must say to savantji and Anant sir, I missed Traderji forum for last 3 years, as i browsed various other forums, but not this one.. and even after joining traderji forum, i missed this entire thread for all these days. I am very much worried. Only yesterday i accidentally browsed across this one..

OMG! i have not even taken a single call from Savant and Anant sir.. What a costly miss. Sir, one question, what approximately the amount one would have required if one is to trade all calls if one to buy the specified shares as said.
( all those 340 calls given the trigger list)

Regards
Saivenkat.:)
 
Last edited:

uma_k

Well-Known Member
Re: Stocks To Keep A Close Eye On - Chapter II

In the Excel sheet under the month "November 2009" the 34th entry NIIT Technologies is showing a wrong buy price of 1699/- and the a huge loss is indicated there. Kindly check it sir.
Regards
Saivenkat.:)
Hi Sai,

It is not only NIITTECH but all the triggers of 10-11-09 have error. You may remember that on 10-11-09 Savantji had hurriedly posted the triggers without giving Target/SL. He gave them two days later. However, during copying those Target/SL I had made some mistake and the same is continuing. I also saw the error yesterday and already corrected them in the latest update. As there is some problem with uploading to Google docs, I have not yet uploaded the correct Excel sheet. Thanks for keenly observing and pointing out the error.

*** Uma ***
 
Re: Stocks To Keep A Close Eye On - Chapter II

Hi Friends,

Updates based on EOD of 15-01-2010 are:

TARGETS HIT

The following have reached TARGETS:

Code:
-----------|----------|-----------|---------|-----------|---------|-----------
Symbol     | Buy Date | Buy Price |  Target | Stop Loss | C.M.P.  | P / L %
-----------|----------|-----------|---------|-----------|---------|-----------
BEML       | 28-05-09 |   768.00  | 1183.00 |   737.00  | 1207.05 |  57.17%
DISHMAN    | 29-12-09 |   239.80  |  259.30 |   226.50  |  262.50 |   9.47%
ENGINERSIN | 09-11-09 |  1318.80  | 1802.55 |  1537.15  | 2082.80 |  57.93%
INDIACEM   | 23-11-09 |   102.70  |  129.85 |    97.95  |  131.35 |  27.90%
NFL        | 09-11-09 |   64.800  |   79.65 |    47.95  |   79.85 |  23.23%
NMDC       | 10-11-09 |   370.00  |  457.55 |   282.45  |  479.30 |  29.54%
SCI        | 07-12-09 |   148.70  |  172.55 |   159.05  |  172.70 |  16.14%
STCINDIA   | 10-11-09 |   358.70  |  426.45 |   280.95  |  459.50 |  28.10%
TECHM      | 29.12-09 |  1013.65  | 1117.55 |   923.95  | 1137.15 |  12.18%
TV-18      | 10-11-09 |    81.20  |   93.30 |    69.10  |   94.00 |  15.76%
------------------------------------------------------------------------------

Next Target and SL for the above will be set by Savantji.

STOP LOSS HIT TODAY

Code:
-----------|----------|-----------|---------|-----------|---------|-----------
Symbol     | Buy Date | Buy Price |  Target | Stop Loss | C.M.P.  | P / L %
-----------|----------|-----------|---------|-----------|---------|-----------
           |          |           |         |           |         |           
------------------------------------------------------------------------------

The updated Excel File will be available later. (Facing problem in uploading to Google Docs).


Regards

*** Uma ***
please let me know in which AFL you are exploring and which parameters.I am explored with anant's single MA system it is showing different values with your value. (my parameters avg type SMA,avg perd 20,%change 1,BB perd 20,BB width 2,trigs Triggers)
siva2005
 
Status
Not open for further replies.

Similar threads