NEW UPDATER AFL
Hi All,
Hope you all did a good Muhurat Trading and have enjoyed the festival and holidays. Tomorrow all will be back to business in the markets. The Muhurat trading is inauguration of new trading year which will be in full swing tomorrow. All will start a new trading account book and record their trades and keep a 'Close Eye On' the Earnings. In order to keep the updates on day-to-day basis I am presenting an AFL today for all those who are using / intendinig to use AmiBroker.
This AFL is different from the earlier one which was made specifically to keep track of the triggers given by Savant Garde. The limitations of that AFL are:
1) All triggers of same day are to be grouped together by creating a separate list for each day.
2) The Buy Date is fixed for each run. Although it can be changed via 'Parameters' it has to be done each time for each group of triggers.
3) The Buying price and quantity bought can not be specified. Similarly the Target & Stop Loss also can not be specified. All these figures have to be entered manually in an Excel Sheet and Profit/Loss have to be calculated. This manual entry is a time consuming process and is causing delay for me in posting updates. I think Uma also is also facing this problem.
4) The Target Reached / StopLoss Hit are also to be checked in the Excel Sheet.
These limitations prompted me to look for an alternate AFL to update the triggers and to overcome the above limitations. In the process of making such an AFL it developed into a general one so that it can be used by anybody, not just for the Trigger's of Savant. You can update your own portfolio everyday with this AFL and keep track of the stocks you have purchased. This is not a Portfolio Management Software. It is just a small tool to update our holdings in a simple way.
This AFL does not take away all the manual work completely, but reduces it to a large extent. For first time use you have to type in all your stocks data in a text file (a CSV file). Whenever target is reached, the revised target/SL are to be entered manually. The Sell triggered or SL hit stocks have to be removed from the list manually. But these manual editings will not take long time as the number involved will be a few.
Before I explain the AFL these preliminary requirements have to be fulfilled:
1. Put all Symbols in your portfolio in a single Watchlist irrespective of date of purchase.
2. Make a list of all stocks you are holding. This you can make in Excel or any other Spreadsheet. The structure of the Spreadsheet is as follows:
No Headings for any column.
First Column: Symbol
Second Column: Buy Date
Third Column: Quantity Bought
Fourth Column: Bying Price
Fifth Column: Target Price
SixthColumn: Stop Loss Price
Make a Spread sheet like this and save it as a CSV file (Comma Separated Values) with the name "TRIGGERS.CSV"
3. The AFL uses three files:
CMP.CSV, TRIGGERS.CSV and
UPDATED.CSV
The
CMP.CSV and
UPDATED.CSV files are generated by the AFL and
TRIGGERS.CSV has to be provided by us in the format described above.
4. Before running the AFL make sure that CMP.CSV file DOES NOT EXIST. IF IT IS PRESENT, DELETE IT. This should be done every time you run the AFL.
The UPDATED.CSV file is generated by the AFL and if it is present already, it will be over-written every time you run the AFL. This file contains the updated data. If you need the older Updated files, copy and save the old file in a different location.
The Exact location (Drive and Folder) where each of these files are created has to be given in the AFL code. Therefore, note down the drive and folder in which you want these files to be created. I will explain where these location have to be entered in the AFL when I explain the AFL.
Once you make your Triggers.csv file, you are ready to run the AFL. As today is no trading day, you can spend the trading time in creating this file today, so that you can use the AFL right from tomorrow.
THE NEW UPDATER AFL:
The complete AFL is given below. Copy it and save in "AmiBroker-Formulas-Custom" folder.You can also download it from
here
In this AFL I am specifying the location of the three files as the D: drive, root directory (D:\).
You can use any other location you like and make the changes accordingly. The changes are to made in the following lines in the AFL:
The First Line just below the _SECTION_BEGIN("Updater") statement
The First Three Lines just below the _SECTION_BEGIN("Updated File") statement.
Please note the double reverse slashes (\\) in the file path. In Windows (or DOS and CommandPrompt) we use a single reverse slash like: C:\ProgramFiles\AmiBroker\Formulas". But in AFL each of these should be double, like this: C:\\ProgramFiles\\AmiBroker\\Formulas"
//***************************************************************************
_SECTION_BEGIN("Updater");
OutFile = "D:\\CMP.csv"; //Here give the Exact path of the file on your PC
fhOut = fopen(OutFile, "a");
Buy = 0;
Filter = 1;
SetOption("NoDefaultColumns", True);
AddTextColumn(Name(), "Symbol");
AddColumn(C, "CMP",6.2);
WriteLine = Name() + "," + C + "\n" ;
fputs(WriteLine, fhOut);
fclose(fhOut);
_SECTION_END();
_SECTION_BEGIN("Updated File");
InFile = "D:\\CMP.csv"; //Here give the Exact path of the file on your PC
OutFile = "D:\\Updated.csv"; //Here give the Exact path of the file on your PC
TriggersFile = "D:\\Triggers.csv"; //Here give the Exact path of the file on your PC
fhIn = fopen(InFile, "r");
fhOut = fopen(OutFile, "w");
fputs("Symbol,Buy Date,Quantity,Price,Target,Stop Loss,CMP,P / L per share,Total P/ L,P / L %,Target Reached?, SL Breached?\n", fhOut);
while(fhIn)
{
ReadCMP = fgets(fhIn);
if(feof(fhin)) break;
Symbol = StrToUpper(StrExtract(ReadCMP, 0));
CMP = StrToNum(StrExtract(ReadCMP, 1));
fhTrigs = fopen(TriggersFile, "r");
while(fhTrigs)
{
ReadTrigs = fgets(fhtrigs);
if(feof(fhtrigs)) break;
Ticker = StrToUpper(StrExtract(ReadTrigs, 0));
BuyDt = StrExtract(ReadTrigs, 1);
Q = StrExtract(ReadTrigs, 2);
Qty = StrToNum(Q);
BP = StrExtract(ReadTrigs, 3);
Price = StrToNum(BP);
Trgt = StrExtract(ReadTrigs, 4);
Tgt = StrToNum(Trgt);
Stp = StrExtract(ReadTrigs, 5);
SL = StrToNum(Stp);
if(Symbol == Ticker)
{
PL = Prec(CMP - Price, 2);
TotalPL = Prec(PL * Qty, 2);
PL100 = Prec(100 * PL / Price, 2);
WriteLine = Ticker + "," + BuyDt + "," + Q + "," + BP + "," + Trgt + "," + SL + "," + CMP + "," + PL + "," + TotalPL + "," + PL100 + "," + WriteIf(CMP >= Tgt, "YES,", " ,") + WriteIf(CMP < SL, "YES\n", "\n");
fputs(WriteLine, fhOut);
}
}
fclose(fhtrigs);
}
fclose(fhIn);
fclose(fhOut);
_SECTION_END();
//*************************************************************************************
Brief Explanation of AFL:
This AFL should be run on the watch list where you have put all your symbols. You can also run it on the whole market but unnecessarily a long time is taken by the AFL.
The AFL is run in Exploration mode and specify a single date for updating. (From and To Dates in Analysis Window should have same date).
After running the AFL the out put is saved in the "Updated.CSV" file. You can open it in Excel and read the updated data and manipulate it further as per your requirements.
First the AFL reads CMP data for each of the Symbols in the WatchList and saves the data in CMP.CSV file.
After completing the creation of CMP.CSV file it now opens the Triggers.CSV file and gathers the information we stored in it, such as Symbol, Buy Date, Buy Price, Quantity etc.
It then reads the CMP from the CMP file and if it finds the Symbol in Triggers.CSV then it writes the data in updated.csv file, calculates the P/L per share, Total P/L for the share, P/L%, checks for target hit, SL Hit and puts a "YES" in appropriate column. All this data is written to the UPDATED.CSV file.
Use the AFL and report any bugs, errors you encounter. Most Errors will result from incorrect TRIGGERS.CSV file or incorrect location of the three paths.. So take care to create this file as explained.
I Hope this Diwali present from me would be useful to you and like it.
Your comments, criticisms and suggestions are welcome.
Wishing you all a very successful trading year ahead,
-Anant