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

[amibroker] Re: Problem using MA whe computing Composite Indicator - SCORE!



PureBytes Links

Trading Reference Links

Joe,

You were dead-on!!!!!!  Using "Nz()" solved it!    (Now why the MA 
version choked and the non-MA version did not choke is another 
issue.   I'm still clueless on that one.)

I very much appreciate your feedback and TJ's feedback.  Your 
solution nailed it and TJ's suggestions should help keep me out of 
trouble in the future.

Best Regards,

Dan.




--- In amibroker@xxxxxxxxxxxxxxx, "Joe Landry" <jelandry@xxxx> wrote:
> Hello Dan - Is it possible that you're dividing by zero in the 
statement for CloseLocValue, say for some of the tickers?
>   Possibly using the Nz function ?  like      NZ(( (C - TrueLow)-( 
TrueHigh - C) ) / (TrueHigh - TrueLow)) 
>  
>   CloseLocValue        =      ( (C - TrueLow)-( TrueHigh - C) ) / 
(TrueHigh - TrueLow)
> 
> Or as one of my friends does in a  moving average routine - he 
checks to see if the ticker is empty and puts in a NULL. 
> 
> 
> ticker    = Foreign(member, "Close");
>  gainfactor   = IIf(IsEmpty(Ref(ticker, -1)), Null, ROC(ticker, 
1) / 100);
> 
> and later as he's accumulating both the count and the gainfactor 
he checks again and makes sure it's a zero that's added
> in both the count of tickers and in the accumulated value used in 
the average calculation.  
> 
> gaintotal   = gaintotal + IIf(IsEmpty(gainfactor), 0, gainfactor);
>   count    = count + IIf(IsEmpty(gainfactor), 0, 1);
> 
> Just some suggestions where I've run aground before in 
accumulating composites. 
> 
> JOE
> 
>   ----- Original Message ----- 
>   From: Dan Clark 
>   To: amibroker@xxxxxxxxxxxxxxx 
>   Sent: Wednesday, August 31, 2005 11:14 PM
>   Subject: [amibroker] Problem using MA whe computing Composite 
Indicator
> 
> 
>   Hi,
> 
>    
> 
>   I'm trying to create a Composite Index and am having a major 
problem.   I'd appreciate any help you can give.   I hope it's just 
a simple user error.
> 
>    
> 
>   I'm trying to create a Money Flow-related composite indicator on 
Sectors using AddToComposite.  It is similar to the AD line, but 
also includes a money amount.   Other than being a little "twitchy", 
the basic code works GREAT!  That is, until I add a 3 period, simple 
MA on the base stock data to smooth it out a bit.
> 
>    
> 
>   You would expect an MA to smooth out the data.  However, when I 
added a simple MA on the stock-level data, the data went crazy.     
For example, the "Amount" histogram bars of the Materials Sector DMF 
(the "~DMF_USSEC_Materials") varies between positive 2 million 
dollars to negative 3 million dollars.   That's fine.  With a 3MA on 
the base data, we would expect smoothed data.  Instead the 3MA 
Sector data the ("~DMF3_USSEC_Materials") varies between about minus 
900 million dollars to minus 20 billion dollars!
> 
>    
> 
>   Below is a test Composite Scan that creates the Composite Sector 
DMF index.  I've colored the key code in red.  As you can see, the 
only difference between the "Good" symbols and the "Bad" symbols is 
a 3 period smoothing MA.   
> 
>    
> 
>   Just above the Scan code is Chart code that displays the values 
correctly.   
> 
>    
> 
>   Note that I'm using QuotesPlus data, but this should work with 
other data vendors as well.  Also, note that the scan stuffs the 
resulting symbols into WatchList 44.   I've assigned this to 
the "WLTest" variable. 
> 
>    
> 
>   To reproduce the error, 
> 
>    
> 
>   1)       Create a new chart showing price bars.
> 
>   2)       Add the Chart code to the Chart.
> 
>   3)       Paste the Composite Scan Code into an AFL window and 
save it to an AFL file.   
> 
>   4)       Run an exploration using "All Symbols" and Range = "1" 
last days.
> 
>   5)       Navigate to Watch List 44 in the Workspace and click on 
the "~DMF_USSEC_Materials".   The symbols should display a valid 
chart.
> 
>   6)       Now click on the 3MA symbol -
 "~DMF3_USSEC_Materials".   Data now looks bad.
> 
>    
> 
>   I hope this is just a dumb error on my part.  I've tried about 
20 different solutions and nothing changes the result.    I'd 
appreciate any help.
> 
>    
> 
>   Best regards,
> 
>    
> 
>   Dan.
> 
>    
> 
>    
> 
>   Chart Code:
> 
>   if (StrLeft(Name(), 4) == "~DMF")
> 
>          {
> 
>          DMFAmt               = C                  ;      
> 
>          DMFVolume            = V                  ;
> 
>          DMFCount             = OI                 ;
> 
>    
> 
>          Plot(DMFAmt, "DMF Amt", IIf(DMFAmt > 0, colorLime, 
colorRed), styleHistogram);
> 
>          Plot(DMFAmt, "DMF Amt", IIf(DMFAmt > 0, colorLime, 
colorRed), styleLine );
> 
>          Plot(DMFCount, "DMF Count", colorCustom1, styleLine | 
styleOwnScale);
> 
>          }
> 
>   else
> 
>          {
> 
>          Plot( C, "Close", IIf( C > O, ParamColor("Up Color", 
colorGreen ), ParamColor("Down Color", colorRed )    ); 
> 
>          }
> 
>    
> 
>   Composite Scan Code:
> 
>   // Created by Dan Clark
> 
>    
> 
>   //Parameters
> 
>   PriceMin = Param( "Minimum Price", 3, 1, 100, 1 );
> 
>   PriceMax = Param( "Maximum Price", 60, 1, 1000000, 1 );
> 
>   VolumeMin = Param( "Minimum Volume/1000", 100, 10, 100, 10 );
> 
>    
> 
>   //Watchlist Numbers
> 
>   WLTest               = 44   ;
> 
>    
> 
>    
> 
>   //AddToCompositeFlags
> 
>   atcCurrentFlags      = atcFlagResetValues + 
atcFlagCompositeGroup + atcFlagEnableInExplore;
> 
>    
> 
>    
> 
>    
> 
>   //Get group name
> 
>   GroupName =   GroupID(1);
> 
>    
> 
>   //Init Utility Variables
> 
>    
> 
>   SectorSymbol         =  NumToStr(SectorID(0), 1.0);  
> 
>   SectorName           =  SectorID(1);   
> 
>    
> 
>    
> 
>   //Filter the symbols
> 
>   USStock              =      C             >= PriceMin 
> 
>                          AND   V            >= (VolumeMin * 1000) 
> 
>                          AND   GroupName    == "Common Stocks"
> 
>                          AND   SectorName  != "Undefined"
> 
>                          ;
> 
>    
> 
>   //Set flags
> 
>   bUSStock             = USStock[BarCount-1];
> 
>    
> 
>   Buy           = 0;
> 
>   Sell          = 0;
> 
>   Filter        = USStock; //Buy OR Sell;  
> 
>    
> 
>   //Init
> 
>   DMFAmtTemp    = 0 ;  
> 
>   DMFVolumeTemp = 0 ;
> 
>   DMFCountTemp  = 0 ;
> 
>   DMFAmt        = 0 ;  
> 
>   DMFVolume     = 0 ;
> 
>   DMFCount      = 0 ;
> 
>   DMF3Amt              = 0 ;  
> 
>   DMF3Volume    = 0 ;
> 
>   DMF3Count     = 0 ;
> 
>   TrueHigh      = 0 ;
> 
>   TrueLow              = 0 ;
> 
>   PriorClose    = 0 ;
> 
>   TypicalPrice = 0 ;
> 
>    
> 
>   if (bUSStock)
> 
>          {
> 
>          //DMF
> 
>          // This weights the count and the Money Flow (not just 
volume) by the relative position of the close in the True Range
> 
>          PriorClose                 =  Ref(C, -
1)                                                                   
   ;
> 
>          TypicalPrice         =  C;         //(H + L + C) / 
3                                                    ;
> 
>          TrueLow                           =  L; //IIf( PriorClose 
< L, PriorClose, L )   ;
> 
>          TrueHigh                   =  H; //IIf( PriorClose > H, 
PriorClose, H )   ;
> 
>    
> 
>          CloseLocValue        =      ( (C - TrueLow)-( TrueHigh - 
C) ) / (TrueHigh - TrueLow)      ;       //Close Location Value 
range from +1 to 0 to -1
> 
>    
> 
>          DMFVolume                  =      CloseLocValue * 
(V/1000) ;    //Works fine.
> 
>          DMFAmt                     =      DMFVolume * 
TypicalPrice ;    //Works fine.     
> 
>          DMFCount                   =      CloseLocValue * 
1      ;    //Works fine.
> 
>    
> 
>          DMF3Volume                 =      MA(DMFVolume, 
3)       ;    //Bad Data!
> 
>          DMF3Amt                           =      MA(DMFAmt   , 
3)       ;    //Bad Data!
> 
>          DMF3Count                  =      MA(DMFCount , 
3)       ;    //Bad Data!
> 
>          } 
> 
>    
> 
>    
> 
>    
> 
>   //Add Symbols to USStock Watchlist and to composite indexes
> 
>   if (bUSStock)   //Buy[1])
> 
>          {
> 
>          // Init Symbols
> 
>          DMFUSMktSym                 
=  "~DMF_USMkt";                  
> 
>          DMFUSMktName                
=  "~DMF_USMkt";                  
> 
>    
> 
>          DMF3USMktSym                
=  "~DMF3_USMkt";                 
> 
>          DMF3USMktName               
=  "~DMF3_USMkt";                 
> 
>    
> 
>          /*
> 
>          ** US Master Indexes
> 
>          */     
> 
>          //creates DMF
> 
>          AddToComposite(DMF3Amt     ,      DMF3USMktSym, "C", 
atcCurrentFlags);
> 
>          AddToComposite(DMF3Volume, DMF3USMktSym, "V", 
atcCurrentFlags); 
> 
>          AddToComposite(DMF3Count,  DMF3USMktSym, "I", 
atcCurrentFlags); 
> 
>    
> 
>          AddToComposite(DMFAmt      ,      DMFUSMktSym,  "C", 
atcCurrentFlags);
> 
>          AddToComposite(DMFVolume,  DMFUSMktSym,  "V", 
atcCurrentFlags); 
> 
>          AddToComposite(DMFCount,   DMFUSMktSym,  "I", 
atcCurrentFlags); 
> 
>    
> 
>          /*
> 
>          ** US Sector Indexes
> 
>          */     
> 
>          DMFUSSecSym                 =  "~DMF_USSec_" + 
SectorSymbol;  //Advance/Decline line by sector
> 
>          DMFUSSecName               =  "~DMF_USSec_" + 
SectorName;           //Advance/Decline line by sector
> 
>    
> 
>          DMF3USSecSym               =  "~DMF3_USSec_"  + 
SectorSymbol; //Advance/Decline line by sector
> 
>          DMF3USSecName              =  "~DMF3_USSec_" + 
SectorName;          //Advance/Decline line by sector
> 
>    
> 
>    
> 
>          //creates DMF by Sector Composite
> 
>          AddToComposite(DMF3Amt     ,      DMF3USSecSym, "C", 
atcCurrentFlags);
> 
>          AddToComposite(DMF3Volume, DMF3USSecSym, "V", 
atcCurrentFlags); 
> 
>          AddToComposite(DMF3Count,  DMF3USSecSym, "I", 
atcCurrentFlags); 
> 
>    
> 
>          AddToComposite(DMFAmt      ,      DMFUSSecSym,  "C", 
atcCurrentFlags);
> 
>          AddToComposite(DMFVolume,  DMFUSSecSym,  "V", 
atcCurrentFlags); 
> 
>          AddToComposite(DMFCount,   DMFUSSecSym,  "I", 
atcCurrentFlags); 
> 
>    
> 
>          
> 
>          /*
> 
>          ** Modify Names
> 
>          */     
> 
>          //Modify the indices SymbolName 
> 
>          // Instantiate Broker.Application
> 
>          AB = CreateStaticObject("Broker.Application");
> 
>    
> 
>          cs = AB.Stocks(DMFUSMktSym);
> 
>          cs.FullName = DMFUSMktName;
> 
>    
> 
>          cs = AB.Stocks(DMFUSSecSym);
> 
>          cs.FullName = DMFUSSecName;
> 
>    
> 
>          cs = AB.Stocks(DMF3USMktSym);
> 
>          cs.FullName = DMF3USMktName;
> 
>    
> 
>          cs = AB.Stocks(DMF3USSecSym);
> 
>          cs.FullName = DMF3USSecName;
> 
>    
> 
>    
> 
>          /*
> 
>          ** Add to Watchlists
> 
>          */            
> 
>          //Add US Market Indexes
> 
>          CategoryAddSymbol( DMFUSMktSym    , categoryWatchlist, 
WLTest);   
> 
>    
> 
>          //Add Sector Indexes
> 
>          CategoryAddSymbol( DMFUSSecSym , categoryWatchlist, 
WLTest);   
> 
>    
> 
>          //Add US Market Indexes
> 
>          CategoryAddSymbol( DMF3USMktSym, categoryWatchlist, 
WLTest);   
> 
>    
> 
>          //Add Sector Indexes
> 
>          CategoryAddSymbol( DMF3USSecSym, categoryWatchlist, 
WLTest);   
> 
>          }
> 
>    
> 
>    
> 
>   SetOption("NoDefaultColumns", True );
> 
>   AddTextColumn(Name()        , "Symbol");
> 
>   AddTextColumn(FullName()   , "Company");
> 
>   AddColumn(C                 , "Close");
> 
>   AddColumn(V/1000            , "Vol/1000", format = 1.0);
> 
>   AddColumn(bUSStock          , "bUSStock");
> 
>   AddColumn( DMFVolume        , "DMFVolume" , 1.2);
> 
>   AddColumn( DMFCount               , "DMFCount"  , 1.2);
> 
>   AddColumn( DMFAmt           , "DMFAmt "  , 1.2);
> 
>    
> 
>   AddColumn( DMF3Volume             , "DMF3Volume" , 1.2);
> 
>   AddColumn( DMF3Count              , "DMF3Count"  , 1.2);
> 
>   AddColumn( DMF3Amt          , "DMF3Amt "  , 1.2);
> 
>    
> 
>   AddTextColumn(GroupName           , "GroupName");
> 
>   AddTextColumn(SectorSymbol , "SectorSymbol");
> 
>   AddTextColumn(SectorName   , "SectorName");
> 
>   AddTextColumn(IndustrySymbol      , "IndustrySymbol");
> 
>   AddTextColumn(IndustryName , "IndustryName");
> 
> 
> 
>   Please note that this group is for discussion between users only.
> 
>   To get support from AmiBroker please send an e-mail directly to 
>   SUPPORT {at} amibroker.com
> 
>   For other support material please check also:
>   http://www.amibroker.com/support.html
> 
> 
> 
> 
> 
> -------------------------------------------------------------------
-----------
>   YAHOO! GROUPS LINKS 
> 
>     a..  Visit your group "amibroker" on the web.
>       
>     b..  To unsubscribe from this group, send an email to:
>      amibroker-unsubscribe@xxxxxxxxxxxxxxx
>       
>     c..  Your use of Yahoo! Groups is subject to the Yahoo! Terms 
of Service. 
> 
> 
> -------------------------------------------------------------------
-----------




------------------------ Yahoo! Groups Sponsor --------------------~--> 
Help Sudanese refugees rebuild their lives through GlobalGiving.
http://us.click.yahoo.com/hjNroD/EbOLAA/cosFAA/GHeqlB/TM
--------------------------------------------------------------------~-> 

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

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