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");