[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: Creating IM200 indices - JAYSON



PureBytes Links

Trading Reference Links

Here are two afl code fragments to create an index based on average 
day over day % change of each component in the index and display the 
same index in Indicator Builder. It is the only correct way to build 
an unweighted index (see Fosback's classic 'Stock Market Logic' for 
more info).

Enjoy!

downhillspeedster

AddtoComposite Code Fragement to Create an Index

// Here we do an arithmetic market average
CompFlag  = 3;
Temp28 = "~" + MarketSymbol + "_Indx" ;
CloseDelta =  (C-Ref(C,-1))/Ref(C,-1);
OpenDelta =  (O-C)/C;
LowDelta   =  (L-C)/C;
HighDelta  =  (H-C)/C;

AddToComposite(CloseDelta, Temp28 , "C" , flags = LastValue
(CompFlag)); 
AddToComposite(OpenDelta, Temp28 , "O" , flags = LastValue
(CompFlag)); 
AddToComposite(LowDelta, Temp28 , "L" , flags = LastValue(CompFlag)); 
AddToComposite(HighDelta, Temp28 , "H" , flags = LastValue
(CompFlag)); 
// add one to Open Interest field (we use this field as a totals 
counter)
AddToComposite( 1, Temp28 , "I" , flags = LastValue(CompFlag)); 

//   ****

Indicator Builder code fragment for reconstructing the index:


InitialIndexValue = 1000; // USER DEFINED INDEX INITIAL VALUE
// set the value of the index for the bar prior to the first bar

_N(Temp = "~" + MarketSymbol + "_Indx" );  

NoDataBit = IIf (IsEmpty(Foreign(Temp,"I")) OR IsEmpty(Foreign
(TempV,"C")) OR 
                           (Foreign(Temp,"I")) == 0 OR (Foreign
(TempV,"C"))  == 0 , 1, 0 );

IndxClose = IIf(NoDataBit, -1E10, (Foreign(Temp, "C")/ Foreign
(Temp,"I")) ); 
IndxLow =   IIf(NoDataBit, -1E10, (Foreign(Temp, "L")/ Foreign
(Temp,"I")) ); 
IndxHigh =  IIf(NoDataBit, -1E10, (Foreign(Temp, "H")/ Foreign
(Temp,"I"))  ); 
IndxOpen = IIf(NoDataBit, -1E10, (Foreign(Temp, "O")/ Foreign
(Temp,"I")) ); 

EnableScript("VBscript");

<%
' //VBscript code begins here

'            /********************************************************
************************************/ 

'// set the value of the index for the first bar

InitialIndexValue = AFL("InitialIndexValue")

'// the below error check fixes the scan if the current stock's first 
bar is > the date range selected  
'// by the user as the first bar. Without this error check, you get 
the run time error:
'//  'subscript out of range: k';
'// The real solution is to change the current stock to a stock with 
valid data for the date range selected 

vbStartBar = AFL("mystartbar")

If vbStartBar = 0 Then
   vbStartBar = 1
End If

vbClose = AFL("IndxClose")
vbOpen = AFL("IndxOpen")
vbLow = AFL("IndxLow")
vbHigh = AFL("IndxHigh")
vbVol = AFL("IndxVol")

ResultsClose = AFL("IndxClose")
ResultsOpen = AFL("IndxOpen")
ResultsLow = AFL("IndxLow")
ResultsHigh = AFL("IndxHigh")
ResultsVol = AFL("IndxVol")

Redim ResultsClose(Ubound(VbClose))
Redim ResultsOpen(Ubound(VbClose))
Redim ResultsLow(Ubound(VbClose))
Redim ResultsHigh(Ubound(VbClose))

For m = Lbound(VbClose) to (vbStartBar-2) Step 1

  ResultsClose(m) = InitialIndexValue 
  ResultsOpen(m) = InitialIndexValue 
  ResultsLow(m)   = InitialIndexValue 
  ResultsHigh(m)  = InitialIndexValue 

Next

k = vbStartBar-1  
ResultsClose(k) = (vbClose(k)+1)  *InitialIndexValue
ResultsOpen(k) = (vbOpen(k)+1)*InitialIndexValue
ResultsLow(k)   = (vbLow(k)+1)*InitialIndexValue
ResultsHigh(k)  = (vbHigh(k)+1)*InitialIndexValue

For i = vbStartBar to Ubound(VbClose) Step 1

   ResultsClose(i) = (vbClose(i)+1)*ResultsClose(i-1)
   ResultsOpen(i) = (vbOpen(i)+1)*ResultsClose(i)
   ResultsLow(i) =   (vbLow(i)+1)*ResultsClose(i)
   ResultsHigh(i) =  (vbHigh(i)+1)*ResultsClose(i)

Next 

AFL.Var("AvgClose") = ResultsClose
AFL.Var("AvgOpen") = ResultsOpen
AFL.Var("AvgLow") = ResultsLow
AFL.Var("AvgHigh") = ResultsHigh

'// 
**********************************************************************
**********************/ 

%>

PlotOHLC( AvgOpen, AvgHigh, AvgLow, AvgClose, " ", color=BarColors, 
style = styleCandle ) ; 

--- In amibroker@xxxxxxxxxxxxxxx, "Jayson" <jcasavant@xxxx> wrote:
> 
> Markus,
> 
> see answers below...
> 
> Hello Jayson,
> 
> don´t ya worry!
> 
> You´ve already helped me a great deal in understanding the "basix" 
of
> Addtocomposite.
> 
> 1./ somehow my indices have been stored in -->markets-->market253 
AND group
> 253. While I understand the latter, I don´t understand the former. 
Is this a
> MUST?
> 
> I think markets 253 is equivalent to All your stocks and tickers. I 
am not
> sure what you are doing but I tend to break that group into watch 
lists for
> testing and explorations. For instance you could create a watch 
list of Nas
> 100 stocks. This watchlist would contain stocks that reside in the 
new list
> AND market 253........
> 
> 2./ I used the "x" option for the field code, figuring that would 
enable me
> to make a bar chart from it (since OHLC are updated). But this is 
NOT the
> case. I get only. No way to produce a bar chart as in HGS/QP2 from 
it?? I
> thought of generating 4 ATC´s (one for Open, one for High etc.) but 
how to
> bring those together in one index bar for each trading day
> ((( 
> 
> Yes you may plot candles but think about what you were trying to 
do....
> 
>  AddToComposite(C,sym,"X");  would not this populate closing value 
through out all the fields? try......
> 
> 
> AddToComposite(C,sym,"C");
> AddToComposite(O,sym,"O");
> AddToComposite(H,sym,"H");
> AddToComposite(L,sym,"L");
> AddToComposite(1,sym,"V"); 
> 
> Plotting your ticker now will show the appropriate OHLC data.  
Unfortunately if you have any holes in your data they may cause 
misleading candles. By dividing the values by the number of 
calculations in each composite you can smooth these errors. To plot 
this create a custom indicator
> 
> 
> C= c /V;
> O= o /V;
> H= h /V;
> L= L /V;
> 
> Plot(C,"Sector Index",colorWhite,styleCandle);
> 
> or better yet..If you want to plot your index in the same chart as 
a component stock try........
> 
> sym="~"+SectorID(1);
> C=Foreign(sym,"C")/V;
> O=Foreign(sym,"o")/V;
> H=Foreign(sym,"h")/V;
> L=Foreign(sym,"l")/V;
> 
> Plot(C,"Sector Index",colorWhite,styleCandle);    For separate 
window or add |styleownscale for the same window 
> 
> AB will determine what sector (or industry in your case) the stock 
belongs to then automatically plot the index for that stock. I use 
this approach daily to compare the stocks RSI and the sectors or the 
stocks momentum and the sectors or to measure the stocks correlation 
to the sectors etc
> 
> 
>   
>  
>  
> 3./ It makes me wonder that the newly created indices (in our case 
the IM200´s) have to be updated EVERY day MANULY(running the scan). 
Is there no workaround for this (i.e. store the calculated day 
somewhere and onl update the LAST session as with regular stock 
data??). 
> 
> The calculations needed require a look at your whole universe. Just 
save your scan and run it... think of it as part 2 of your daily 
update. It take but a moment. IMO this feature alone is worth the 
price of admission to AB. I used to do all this work as a QP scan 
then export to excel, sort, calculate and import to metastock. If I 
missed a day I had to rewrite the scan to get yesterdays data etc, 
etc. With AB the composite is re-created each day so if I miss a day, 
no problem. If QP makes an adjustment, no problem. 
> 
> I have one scan that creates all my sector composites, all my 
industry composites, and several market breadth composites. It takes 
my old P600 system about 2 minutes to do the work then AB even sends 
them to the appropriate watch list for further analysis. Super 
feature........
> 
> Regards,
> 
> jayson 
>  
> Highly appreciate your help!!!!
>  
> Thanx
>  
> Markus
>   ----- Original Message ----- 
>   From: Jayson 
>   To: amibroker@xxxxxxxxxxxxxxx 
>   Sent: Thursday, April 24, 2003 6:38 AM
>   Subject: RE: [amibroker] Creating IM200 indices - JAYSON
> 
> 
>   JMarkus,
>    
>   I have been gone most of the afternoon. Sorry to leave you 
hanging. Answers below......
> 
>   Jayson 
>   -----Original Message-----
>   From: funnybiz@xxxx [mailto:funnybiz@x...]
>   Sent: Wednesday, April 23, 2003 5:26 PM
>   To: amibroker@xxxxxxxxxxxxxxx
>   Subject: Re: [amibroker] Creating IM200 indices - JAYSON
> 
> 
>   Anthony,
> 
>   thanks for s
> tepping in here.
> 
>   If I may:
> 
>   1./ does Jayson´s SYM variable refer to the array part of the
> addtocomposite
>   function. If so, why is it not put in parenthesis (though this 
results in
> an
>   error).
> 
>   The line
> 
>   sym="~"+sectorid(1)
> 
>   looks at each stock you scan and places "~" + the sector ID for 
that stock
> in its place. If, for instance the stock resides in the Utilities 
Sector
> then AB notes this and returns sym= "~Utilities". Addtocomposite 
grabs this
> shorthand and Therefore the next line
>   AddToComposite(C,sym ,"C");   becomes addtocomposite
(c,"~utilities","C");
> 
>   This is repeated for each stock scanned. addtocomposite then 
simply looks
> to the stocks sectorid and places the data in the appropriate ticker
> 
> 
> 
> 
>   2./ sym="~"+SectorID(1);// this does sectors for industry groups 
use
>   industryid(0)
>   I don´t understand Jayson´s remark here: WHICH stocks does that 
include? I
>   want to include all stocks belonging to the same of the 200 
industry
> groups.
> 
>   Sectorid(1) returns the 12 sectors (Capital goods, utilities, 
financials
> etc) If you want industry groups then replace that line with
> 
>   sym="~"+industryID(1);
> 
>   Ab will look at all the stocks in your scan. All the stocks with 
with the
> same industryID will be counted in the appropriate composite Ticker.
> 
>   3./ if I wanted to create an index for all the 200 used industry 
groups
>   (Quotes Plus Two), would I have to write this code 200 times??
> 
>   No... See above... AB does all the grunt work for you...
> 
>   4./ would I have to run the scan EVEREY day to bring my 
Addtocomposites up
>   to date?
> 
>   Yes. the scan will create/update all 200 industry groups and 
store them in
> group 253 (the default location for your Composites. You may also 
create
> watch lists of these tickers to separate them. For instance I have a
> watchlist with just the 12 sectors, a second for the industry group 
tickers
> etc....
> 
>   5./ I added "flag=16" which gave me an error. How do I have to 
specify if
> I
>   want to use addtocomposite in exploration mode?
> 
>   flag=16 is a description.... just add comma 16
>   AddToComposite(C,sym ,"C" ,16 );
> 
> 
> 
>   Many thanks for your help!
> 
>   Markus
> 
>   ----- Original Message -----
>   From: "Anthony Faragasso" <ajf1111@xxxx>
>   To: <amibroker@xxxxxxxxxxxxxxx>
>   Sent: Wednesday, April 23, 2003 8:37 PM
>   Subject: Re: [amibroker] Creating IM200 indices - JAYSON
> 
> 
>   > Also...do not forget to add this dummy line...which is needed 
for
>   scanning.
>   >
>   > Buy=0;
>   >
>   >
>   >
>   > Send BUG REPORTS to bugs@xxxx
>   > Send SUGGESTIONS to suggest@xxxx
>   > -----------------------------------------
>   > Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx
>   > (Web page: http://groups.yahoo.com/group/amiquote/messages/)
>   > --------------------------------------------
>   > Check group FAQ at:
>   http://groups.yahoo.com/group/amibroker/files/groupfaq.html
>   >
>   > Your use of Yahoo! Groups is subject to
> http://docs.yahoo.com/info/terms/
>   >
>   >
> 
> 
> 
> 
>   Send BUG REPORTS to bugs@xxxx
>   Send SUGGESTIONS to suggest@xxxx
>   -----------------------------------------
>   Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx
>   (Web page: http://groups.yahoo.com/group/amiquote/messages/)
>   --------------------------------------------
>   Check group FAQ at:
> http://groups.yahoo.com/group/amibroker/files/groupfaq.html
> 
>   Your use of Yahoo! Groups is subject to the Yahoo! Terms of 
Service.
> 
> 
>   Send BUG REPORTS to bugs@xxxx
>   Send SUGGESTIONS to suggest@xxxx
>   -----------------------------------------
>   Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx
>   (Web page: http://groups.yahoo.com/group/amiquote/messages/)
>   --------------------------------------------
>   Check group FAQ at:
> http://groups.yahoo.com/group/amibroker/files/groupfaq.html
> 
>   Your use of Yahoo! Groups is subject to the Yahoo! Terms of 
Service.
> 
> 
>       Yahoo! Groups Sponsor
> 
> 
> 
> 
> 
> Send BUG REPORTS to bugs@xxxx
> Send SUGGESTIONS to suggest@xxxx
> -----------------------------------------
> Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx
> (Web page: http://groups.yahoo.com/group/amiquote/messages/)
> --------------------------------------------
> Check group FAQ at:
> http://groups.yahoo.com/group/amibroker/files/groupfaq.html
> 
> Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get A Free Psychic Reading!
Your Online Answer To Life's Important Questions.
http://us.click.yahoo.com/pDi3hB/OC5FAA/AG3JAA/GHeqlB/TM
---------------------------------------------------------------------~->

Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx 
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/