Join our facebook group for Amibroker afl code discussion https://www.facebook.com/groups/603980776889348

Simple-Candlestick-Volume-Indicator

Simple-Candlestick-Volume-Indicator
Simple-Candlestick-Volume-Indicator



//www.aflcode.com
// Note: before using this file, you need to create the composite tickers by:
// 1. Scanning "composites-all" file against all stocks
// 2. Scanning "composites-universe" file against the watchlist containing stocks in your universe

// user-defined params
MApds = 3; // use 1 to plot actual value; > 1 for smoothing

// misc variables
_N( Industry = IndustryID(1) );
_N( Sector   = SectorID(1) );
_N( Market   = MarketID(1) );

// industry calculations
// my universe
uTtlInd     = Foreign( "~u" + Industry, "H" );
uAdvInd     = Foreign( "~u" + Industry, "V" );
uDecInd     = Foreign( "~u" + Industry, "I" );
uUnchInd    = uTtlInd - ( uAdvInd + uDecInd );
uModAdvInd  = uAdvInd + 0.5 * uUnchInd;
uModDecInd  = uDecInd + 0.5 * uUnchInd;
uAmDdApDInd = Nz( ( uModAdvInd - uModDecInd ) / ( uModAdvInd + uModDecInd ), 0.0000001 );
uEMA10Ind   = EMA( uAmDdApDInd, 19 );
uEMA5Ind    = EMA( uAmDdApDInd, 39 );
uMOInd      = uEMA10Ind - uEMA5Ind;
uMSIInd     = 1000 * ( uMOInd - ( 10 * uEMA10Ind ) + ( 20 * uEMA5Ind ) );

uPctAdvInd   = uAdvInd    / uTtlInd * 100;
uPctDecInd   = uDecInd    / uTtlInd * 100;
uPctUnchInd  = uUnchInd   / uTtlInd * 100;
uADURatioInd = uModAdvInd / uModDecInd;
uADURatioInd = IIf( IsFinite( uADURatioInd ), uADURatioInd, 999.99 );
uDAURatioInd = uModDecInd / uModAdvInd;
uDAURatioInd = IIf( IsFinite( uDAURatioInd ), uDAURatioInd, 999.99 );

// all stocks
aTtlInd     = Foreign( "~a" + Industry, "H" );
aAdvInd     = Foreign( "~a" + Industry, "V" );
aDecInd     = Foreign( "~a" + Industry, "I" );
aUnchInd    = aTtlInd - ( aAdvInd + aDecInd );
aModAdvInd  = aAdvInd + 0.5 * aUnchInd;
aModDecInd  = aDecInd + 0.5 * aUnchInd;
aAmDdApDInd = Nz( ( aModAdvInd - aModDecInd ) / ( aModAdvInd + aModDecInd ), 0.0000001 );
aEMA10Ind   = EMA( aAmDdApDInd, 19 );
aEMA5Ind    = EMA( aAmDdApDInd, 39 );
aMOInd      = aEMA10Ind - aEMA5Ind;
aMSIInd     = 1000 * ( aMOInd - ( 10 * aEMA10Ind ) + ( 20 * aEMA5Ind ) );

aPctAdvInd   = aAdvInd    / aTtlInd * 100;
aPctDecInd   = aDecInd    / aTtlInd * 100;
aPctUnchInd  = aUnchInd   / aTtlInd * 100;
aADURatioInd = aModAdvInd / aModDecInd;
aADURatioInd = IIf( IsFinite( aADURatioInd ), aADURatioInd, 999.99 );
aDAURatioInd = aModDecInd / aModAdvInd;
aDAURatioInd = IIf( IsFinite( aDAURatioInd ), aDAURatioInd, 999.99 );

// sector calculations
// my universe
uTtlSec     = Foreign( "~u" + Sector, "H" );
uAdvSec     = Foreign( "~u" + Sector, "V" );
uDecSec     = Foreign( "~u" + Sector, "I" );
uUnchSec    = uTtlSec - ( uAdvSec + uDecSec );
uModAdvSec  = uAdvSec + 0.5 * uUnchSec;
uModDecSec  = uDecSec + 0.5 * uUnchSec;
uAmDdApDSec = Nz( ( uModAdvSec - uModDecSec ) / ( uModAdvSec + uModDecSec ), 0.0000001 );
uEMA10Sec   = EMA( uAmDdApDSec, 19 );
uEMA5Sec    = EMA( uAmDdApDSec, 39 );
uMOSec      = uEMA10Sec - uEMA5Sec;
uMSISec     = 1000 * ( uMOSec - ( 10 * uEMA10Sec ) + ( 20 * uEMA5Sec ) );

uPctAdvSec   = uAdvSec    / uTtlSec * 100;
uPctDecSec   = uDecSec    / uTtlSec * 100;
uPctUnchSec  = uUnchSec   / uTtlSec * 100;
uADURatioSec = uModAdvSec / uModDecSec;
uADURatioSec = IIf( IsFinite( uADURatioSec ), uADURatioSec, 999.99 );
uDAURatioSec = uModDecSec / uModAdvSec;
uDAURatioSec = IIf( IsFinite( uDAURatioSec ), uDAURatioSec, 999.99 );

// all stocks
aTtlSec     = Foreign( "~a" + Sector, "H" );
aAdvSec     = Foreign( "~a" + Sector, "V" );
aDecSec     = Foreign( "~a" + Sector, "I" );
aUnchSec    = aTtlSec - ( aAdvSec + aDecSec );
aModAdvSec  = aAdvSec + 0.5 * aUnchSec;
aModDecSec  = aDecSec + 0.5 * aUnchSec;
aAmDdApDSec = Nz( ( aModAdvSec - aModDecSec ) / ( aModAdvSec + aModDecSec ), 0.0000001 );
aEMA10Sec   = EMA( aAmDdApDSec, 19 );
aEMA5Sec    = EMA( aAmDdApDSec, 39 );
aMOSec      = aEMA10Sec - aEMA5Sec;
aMSISec     = 1000 * ( aMOSec - ( 10 * aEMA10Sec ) + ( 20 * aEMA5Sec ) );

aPctAdvSec   = aAdvSec    / aTtlSec * 100;
aPctDecSec   = aDecSec    / aTtlSec * 100;
aPctUnchSec  = aUnchSec   / aTtlSec * 100;
aADURatioSec = aModAdvSec / aModDecSec;
aADURatioSec = IIf( IsFinite( aADURatioSec ), aADURatioSec, 999.99 );
aDAURatioSec = aModDecSec / aModAdvSec;
aDAURatioSec = IIf( IsFinite( aDAURatioSec ), aDAURatioSec, 999.99 );

// market calculations
// my universe
uTtlMkt     = Foreign( "~u" + Market, "H" );
uAdvMkt     = Foreign( "~u" + Market, "V" );
uDecMkt     = Foreign( "~u" + Market, "I" );
uUnchMkt    = uTtlMkt - ( uAdvMkt + uDecMkt );
uModAdvMkt  = uAdvMkt + 0.5 * uUnchMkt;
uModDecMkt  = uDecMkt + 0.5 * uUnchMkt;
uAmDdApDMkt = Nz( ( uModAdvMkt - uModDecMkt ) / ( uModAdvMkt + uModDecMkt ), 0.0000001 );
uEMA10Mkt   = EMA( uAmDdApDMkt, 19 );
uEMA5Mkt    = EMA( uAmDdApDMkt, 39 );
uMOMkt      = uEMA10Mkt - uEMA5Mkt;
uMSIMkt     = 1000 * ( uMOMkt - ( 10 * uEMA10Mkt ) + ( 20 * uEMA5Mkt ) );

uPctAdvMkt   = uAdvMkt    / uTtlMkt * 100;
uPctDecMkt   = uDecMkt    / uTtlMkt * 100;
uPctUnchMkt  = uUnchMkt   / uTtlMkt * 100;
uADURatioMkt = uModAdvMkt / uModDecMkt;
uADURatioMkt = IIf( IsFinite( uADURatioMkt ), uADURatioMkt, 999.99 );
uDAURatioMkt = uModDecMkt / uModAdvMkt;
uDAURatioMkt = IIf( IsFinite( uDAURatioMkt ), uDAURatioMkt, 999.99 );

// all stocks
aTtlMkt     = Foreign( "~a" + Market, "H" );
aAdvMkt     = Foreign( "~a" + Market, "V" );
aDecMkt     = Foreign( "~a" + Market, "I" );
aUnchMkt    = aTtlMkt - ( aAdvMkt + aDecMkt );
aModAdvMkt  = aAdvMkt + 0.5 * aUnchMkt;
aModDecMkt  = aDecMkt + 0.5 * aUnchMkt;
aAmDdApDMkt = Nz( ( aModAdvMkt - aModDecMkt ) / ( aModAdvMkt + aModDecMkt ), 0.0000001 );
aEMA10Mkt   = EMA( aAmDdApDMkt, 19 );
aEMA5Mkt    = EMA( aAmDdApDMkt, 39 );
aMOMkt      = aEMA10Mkt - aEMA5Mkt;
aMSIMkt     = 1000 * ( aMOMkt - ( 10 * aEMA10Mkt ) + ( 20 * aEMA5Mkt ) );

aPctAdvMkt   = aAdvMkt    / aTtlMkt * 100;
aPctDecMkt   = aDecMkt    / aTtlMkt * 100;
aPctUnchMkt  = aUnchMkt   / aTtlMkt * 100;
aADURatioMkt = aModAdvMkt / aModDecMkt;
aADURatioMkt = IIf( IsFinite( aADURatioMkt ), aADURatioMkt, 999.99 );
aDAURatioMkt = aModDecMkt / aModAdvMkt;
aDAURatioMkt = IIf( IsFinite( aDAURatioMkt ), aDAURatioMkt, 999.99 );

// code for explorer
Filter = 1;

AddTextColumn( Industry, "          Industry / My Universe          " );
AddColumn( uTtlInd,      "Total",     1.0 );
AddColumn( uAdvInd,      "Adv",       1.0 );
AddColumn( uDecInd,      "Dec",       1.0 );
AddColumn( uUnchInd,     "Unch",      1.0 );
AddColumn( uPctAdvInd,   "Pct/Adv",   1.0 );
AddColumn( uPctDecInd,   "Pct/Dec",   1.0 );
AddColumn( uPctUnchInd,  "Pct/Unchg", 1.0 );
AddColumn( uADURatioInd, "Ratio A/D", 1.2 );
AddColumn( uDAURatioInd, "Ratio D/A", 1.2 );
AddColumn( uMSIInd,      "   MSI   ", 1.2 );

AddTextColumn( Industry, "          Industry / All Stocks          " );
AddColumn( aTtlInd,      "Total",     1.0 );
AddColumn( aAdvInd,      "Adv",       1.0 );
AddColumn( aDecInd,      "Dec",       1.0 );
AddColumn( aUnchInd,     "Unch",      1.0 );
AddColumn( aPctAdvInd,   "Pct/Adv",   1.0 );
AddColumn( aPctDecInd,   "Pct/Dec",   1.0 );
AddColumn( aPctUnchInd,  "Pct/Unchg", 1.0 );
AddColumn( aADURatioInd, "Ratio A/D", 1.2 );
AddColumn( aDAURatioInd, "Ratio D/A", 1.2 );
AddColumn( aMSIInd,      "   MSI   ", 1.2 );

AddTextColumn( Sector, "          Sector / My Universe          " );
AddColumn( uTtlSec,      "Total",     1.0 );
AddColumn( uAdvSec,      "Adv",       1.0 );
AddColumn( uDecSec,      "Dec",       1.0 );
AddColumn( uUnchSec,     "Unch",      1.0 );
AddColumn( uPctAdvSec,   "Pct/Adv",   1.0 );
AddColumn( uPctDecSec,   "Pct/Dec",   1.0 );
AddColumn( uPctUnchSec,  "Pct/Unchg", 1.0 );
AddColumn( uADURatioSec, "Ratio A/D", 1.2 );
AddColumn( uDAURatioSec, "Ratio D/A", 1.2 );
AddColumn( uMSISec,      "   MSI   ", 1.2 );

AddTextColumn( Sector, "          Sector / All Stocks          " );
AddColumn( aTtlSec,      "Total",     1.0 );
AddColumn( aAdvSec,      "Adv",       1.0 );
AddColumn( aDecSec,      "Dec",       1.0 );
AddColumn( aUnchSec,     "Unch",      1.0 );
AddColumn( aPctAdvSec,   "Pct/Adv",   1.0 );
AddColumn( aPctDecSec,   "Pct/Dec",   1.0 );
AddColumn( aPctUnchSec,  "Pct/Unchg", 1.0 );
AddColumn( aADURatioSec, "Ratio A/D", 1.2 );
AddColumn( aDAURatioSec, "Ratio D/A", 1.2 );
AddColumn( aMSISec,      "   MSI   ", 1.2 );

AddTextColumn( Market, "          Market / My Universe          " );
AddColumn( uTtlMkt,      "Total",     1.0 );
AddColumn( uAdvMkt,      "Adv",       1.0 );
AddColumn( uDecMkt,      "Dec",       1.0 );
AddColumn( uUnchMkt,     "Unch",      1.0 );
AddColumn( uPctAdvMkt,   "Pct/Adv",   1.0 );
AddColumn( uPctDecMkt,   "Pct/Dec",   1.0 );
AddColumn( uPctUnchMkt,  "Pct/Unchg", 1.0 );
AddColumn( uADURatioMkt, "Ratio A/D", 1.2 );
AddColumn( uDAURatioMkt, "Ratio D/A", 1.2 );
AddColumn( uMSIMkt,      "   MSI   ", 1.2 );

AddTextColumn( Market, "          Market / All Stocks          " );
AddColumn( aTtlMkt,      "Total",     1.0 );
AddColumn( aAdvMkt,      "Adv",       1.0 );
AddColumn( aDecMkt,      "Dec",       1.0 );
AddColumn( aUnchMkt,     "Unch",      1.0 );
AddColumn( aPctAdvMkt,   "Pct/Adv",   1.0 );
AddColumn( aPctDecMkt,   "Pct/Dec",   1.0 );
AddColumn( aPctUnchMkt,  "Pct/Unchg", 1.0 );
AddColumn( aADURatioMkt, "Ratio A/D", 1.2 );
AddColumn( aDAURatioMkt, "Ratio D/A", 1.2 );
AddColumn( aMSIMkt,      "   MSI   ", 1.2 );

// code for IB
MAuMSIInd = MA( uMSIInd, MApds );
MAaMSIInd = MA( aMSIInd, MApds );
MAuMSISec = MA( uMSISec, MApds );
MAaMSISec = MA( aMSISec, MApds );
MAuMSIMkt = MA( uMSIMkt, MApds );
MAaMSIMkt = MA( aMSIMkt, MApds );

Plot( MAuMSIInd, "U/Ind", colorBrightGreen, styleLine );
Plot( MAaMSIInd, "A/Ind", colorDarkGreen,   styleLine );
Plot( MAuMSISec, "U/Sec", colorLightYellow, styleLine );
Plot( MAaMSISec, "A/Sec", colorGold,        styleLine );
Plot( MAuMSIMkt, "U/Mkt", colorRed,         styleLine );
Plot( MAaMSIMkt, "A/Mkt", colorDarkRed,     styleLine );
Plot( Close,     "Close", colorBlue,        styleCandle|styleOwnScale );
Plot( Volume/1000, "Volume", colorGrey40, styleHistogram|styleOwnScale, 
      0, LastValue(HHV( Volume/1000, Cum(Status("barvisible")))));
_N( Title = EncodeColor( colorBlue ) + "Summation  Indexes"
          + EncodeColor( colorBlack ) + "        " + IndustryID(1) + ": "
           + EncodeColor( colorBrightGreen ) + WriteVal( MAuMSIInd, 1.2 )
            + EncodeColor( colorBlack ) + " / "
             + EncodeColor( colorDarkOliveGreen ) + WriteVal( MAaMSIInd, 1.2 )
          + EncodeColor( colorBlack ) + "        " + SectorID(1) + ": "
           + EncodeColor( colorLightYellow ) + WriteVal( MAuMSISec, 1.2 )
            + EncodeColor( colorBlack ) + " / "
             + EncodeColor( colorGold ) + WriteVal( MAaMSISec, 1.2 )
          + EncodeColor( colorBlack ) + "        " + MarketID(1) + ": "
           + EncodeColor( colorRed ) + WriteVal( MAuMSIMkt, 1.2 )
            + EncodeColor( colorBlack ) + " / "
             + EncodeColor( colorDarkRed ) + WriteVal( MAaMSIMkt, 1.2 ) );

// write stats to interpretation window
//--Indicator-End--
"Close:     " + Close;
"Volume:  " + WriteVal( Volume, 1.0 );
"";
"Stock Count  My Universe  All Stocks";
"--------------  ---------------  -----------";
"Industry               "  + WriteVal(uTtlInd, 3.0)  + "               " + WriteVal(aTtlInd, 3.0);
"Sector                  " + WriteVal( uTtlSec, 3.0 )  + "              " + WriteVal(aTtlSec, 3.0);
"Market                 "  + WriteVal( uTtlMkt, 3.0 )  + "             " + WriteVal(aTtlMkt, 3.0);
Previous
Next Post »