PureBytes Links
Trading Reference Links
|
Attached my code for Rel Strength. I just got home,
so I have not integrated your market cap yet. AFL file, but changed ending for
attaching
Ara
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
|
//File: Industry - Sector RS
//Select Issue with Positive Momentum and performance better than Industry average
//Select Industries with Positive Momentum and performance better than Sector average
//Version 0.9
//November 15, 2005
//
//Use Industry ATCs in order to have longer Industry histories for backtest.
//Intergrate Divergence code
// Initialize Variables
//Test Mode
Mkt_Timing = Param("1=RUT, 2=%Bull",0,0,2,1);
//Variables
Bars = 0;
Bars1 = 8;
Bars1 = Optimize("Bars1",8,2,20,2);
Bars2 = 60;
Bars2 = Optimize("Bars2",60,40,120,8);
Smooth = 5;
Sector = 0;
Issue_Delta = 0;
Industry_Delta = 0;
Issue_RS = 0;
Sector_RS = 0;
Industry_RS = 0;
Issue_RS2 = 0;
Sector_RS2 = 0;
Industry_RS2 = 0;
Issue_ROC = 0;
Industry_ROC = 0;
Sector_ROC = 0;
Issue_ROC2 = 0;
Industry_ROC2 = 0;
Sector_ROC2 = 0;
IssueRS = 0;
IndustryRS = 0;
SectorRS = 0;
IssueROC = 0;
IndustryROC = 0;
SectorROC = 0;
IssueRS2 = 0;
IndustryRS2 = 0;
SectorRS2 = 0;
IssueROC2 = 0;
IndustryROC2 = 0;
SectorROC2 = 0;
Buy_Trigger = 0;
Ind_issues = 0;
ATC_Date = "";
Ind_ATC = "";
Error = "";
#include <Mod - Osc-Price Divergence31.afl>
function ComputeRelativeStrength(Len)
{
//Len specified length1 od length2
if (Len==1) Bars=Bars1;
if (Len==2) Bars=Bars2;
//Compute Relative Strength wrt Industry
Issue_Ref = Ref(Close,-Bars);
Issue_Delta = Close - Issue_Ref;
Perc_IssDelta = Issue_Delta * 100 / Close;
//Create Industry symbol from Supplied Number
IndNum = IndustryID(0);
NumString = NumToStr(IndNum,1.0);
StrLength = StrLen(NumString);
if (StrLength==1) IDString = "00" +NumString;
if (StrLength==2) IDString = "0" +NumString;
if (StrLength==3) IDString = NumString;
//Use ATC Industry data
Ind_ATC = "~Ind_" + IDString;
Industry = Foreign(Ind_ATC,"O");
Ind_Issues = Foreign(Ind_ATC,"V"); // Number of stocks in industry
//Compute Industry Performance
Industry_Ref = Ref(Industry,-Bars);
Industry_Delta = Industry - Industry_Ref;
Perc_IndDelta = Industry_Delta * 100 / Industry;
//Create Sector symbol for ATC
Sector_Num = SectorID(0);
NumString = NumToStr(Sector_Num,1.0);
StrLength = StrLen(NumString);
if (StrLength==1) IDString = "0" + NumString;
if (StrLength==2) IDString = NumString;
Sector_ATC = "~Sec_" + IDString;
Sector = Foreign(Sector_ATC,"C");
Sector_Ref = Ref(Sector,-Bars);
Sector_Delta = Sector - Sector_Ref;
Perc_SecDelta = Sector_Delta * 100 / Sector;
//Compute Sector RS wrt SP500
Index = Foreign("!SPX","C");
Index_Ref = Ref(Index,-Bars);
Index_Delta = Index - Index_Ref;
Perc_IndxDelta = Index_Delta * 100 / Index;
if (Len==1)
{
Issue_RS = EMA((Perc_IssDelta - Perc_IndDelta),Smooth);
Industry_RS = EMA((Perc_IndDelta - Perc_SecDelta),Smooth);
Sector_RS = EMA((Perc_SecDelta - Perc_IndxDelta),Smooth);
Issue_ROC = Close - Ref(Close,-Bars);
Industry_ROC = Industry - Ref(Industry,-Bars);
Sector_ROC = Sector - Ref(Sector,-Bars);
}
if (Len==2)
{
Issue_RS2 = EMA((Perc_IssDelta - Perc_IndDelta),Smooth);
Industry_RS2 = EMA((Perc_IndDelta - Perc_SecDelta),Smooth);
Sector_RS2 = EMA((Perc_SecDelta - Perc_IndxDelta),Smooth);
Issue_ROC2 = Close - Ref(Close,-Bars);
Industry_ROC2 = Industry - Ref(Industry,-Bars);
Sector_ROC2 = Sector - Ref(Sector,-Bars);
}
//Plot(Industry,"Industry " + Ind_ATC,colorRed,styleLine|styleOwnScale);
_TRACE("Issue - Name " + Name() + " Issue " + WriteVal(Close,1.2) + " Iss Ref " + WriteVal(Issue_Ref,1.2) + " Issue Del " + WriteVal(Issue_Delta,1.2));
_TRACE("Indust - Name " + Name() + " Industry " + WriteVal(Industry,1.2) + " Ind Ref " + WriteVal(Industry_Ref,1.2) + " Ind Del " + WriteVal(Industry_Delta,1.2)
+ " Issue RS " + WriteVal(Issue_RS,1.2) + " Ind RS " + WriteVal(Industry_RS,1.2));
_TRACE("Sector - Name " + Name() + " Sec Del " + WriteVal(Sector_Delta,1.2) + " Sec Ref " + WriteVal(Sector_Ref,1.2)
+ " Ind RS " + WriteVal(Industry_RS,1.2) + " Sect RS " + WriteVal(Sector_RS,1.2));
}
//STAERT OF PROGRAM
//Loop through stock and correlate with Industries
//Verify last date of ATCs
//Verify date off ATCs
ATC_Date = Foreign("~Ind_000","H");
IndName = IndustryID(1);
SectorName = SectorID(1);
if (IndustryID(0)==0 OR SectorID(0) == 0)
{
Issue_RS = 0;
Industry_RS = 0;
Sector_RS = 0;
Error = "Index - No RS Value";
}
else
{
ComputeRelativeStrength(1);
IssueRS = IIf(Issue_RS >0,1,-1);
IndustryRS = IIf(Industry_RS >0,1,-1);
SectorRS = IIf(Sector_RS >0,1,-1);
IssueROC = IIf(Issue_ROC - Ref(Issue_ROC,-2) >= 0,1,-1);
IndustryROC = IIf(Industry_ROC - Ref(Industry_ROC,-2) >= 0,1,-1);
SectorROC = IIf(Sector_ROC - Ref(Sector_ROC,-2) >= 0,1,-1);
_TRACE("Assign1 - Bars " + WriteVal(Bars,1.0) + " Issue " + WriteVal(Issue_RS,1.2) + "/" + WriteVal(Issue_RS2,1.2)
+ " Ind " + WriteVal(Industry_RS,1.2) + "/" + WriteVal(Industry_RS2,1.2)
+ " Sec " + WriteVal(Sector_RS,1.2) + "/" + WriteVal(Sector_RS2,1.2));
ComputeRelativeStrength(2);
IssueRS2 = IIf(Issue_RS2 >0,1,-1);
IndustryRS2 = IIf(Industry_RS2 >0,1,-1);
SectorRS2 = IIf(Sector_RS2 >0,1,-1);
IssueROC2 = IIf(Issue_ROC2 - Ref(Issue_ROC2,-2) >= 0,1,-1);
IndustryROC2 = IIf(Industry_ROC2 - Ref(Industry_ROC2,-2) >= 0,1,-1);
SectorROC2 = IIf(Sector_ROC2 - Ref(Sector_ROC2,-2) >= 0,1,-1);
_TRACE("Assign2 - Bars " + WriteVal(Bars,1.0) + " Issue " + WriteVal(Issue_RS,1.2) + "/" + WriteVal(Issue_RS2,1.2)
+ " Ind " + WriteVal(Industry_RS,1.2) + "/" + WriteVal(Industry_RS2,1.2)
+ " Sec " + WriteVal(Sector_RS,1.2) + "/" + WriteVal(Sector_RS2,1.2));
}
_TRACE("Names - Issue " + Name() + " Ind " + IndName + " " + IndustryID(0) + " Sector " + SectorName + " " + SectorID(0));
Plot(0,"",colorBlack,styleLine);
//Plot(Issue_ROC,"Issue_ROC",colorYellow,styleThick|styleOwnScale);
Plot(Issue_RS,"Issue_RS",colorRed,styleLine|styleThick);
Plot(Issue_RS2,"Issue_RS2",colorRed,styleDots|styleThick);
//Plot(Industry_ROC,"Industry_ROC",colorYellow,styleLine|styleThick|styleOwnScale);
Plot(Industry_RS,"Industry_RS",colorBlue,styleLine|styleThick);
Plot(Industry_RS2,"Industry_RS2",colorBlue,styleDots|styleThick);
Plot(Sector_RS,"Sector_RS",colorYellow,styleLine|styleThick);
Plot(Sector_RS2,"Sector_RS",colorYellow,styleDots|styleThick);
//Criteria
//Use following parameters for trade criteria
//Issue_RS, Issue_ROC, Industry_RS, Industry_ROC, Sector_RS, Sector_ROC
//
//Call Divergence Module
_TRACE("Tracer-Diverge");
StocPeriod = 14;
//StocPeriod = Optimize("StocPeriod",14,8,36,4);
Osc_Issue = StochD(StocPeriod);
Middle = 50;
DivergeControl(Osc_Issue,Middle);
//Market Timing Variables
//EMA
Index = Foreign("!SPX","C");
Index_EMA200 = EMA(Index,200);
Index_EMABuy = Index > Index_EMA200;
Sector_EMA200 = EMA(Sector,200);
Sector_EMABuy = Sector > Sector_EMA200;
//%Bullish
//SP500 %Bullish
SP500_MA15WBull = Foreign("~SP500 MA15WK bull","C");
SP500_Count = Foreign("~SP500 MA15WK bull","I");
SP500_15WPercBull = SP500_MA15WBull / SP500_Count * 100;
SP500_MA10DBull = Foreign("~SP500 MA10DAY bull","C");
SP500_Count = Foreign("~SP500 MA10DAY bull","I");
SP500_10DPercBull = SP500_MA10DBull / SP500_Count * 100;
//ROC for both short and Long term are positive
ST_MktBuy = (SP500_10DPercBull - Ref(SP500_10DPercBull,-1)) > 0;
LT_MktBuy = (SP500_15WPercBull - Ref(SP500_15WPercBull,-1)) > 0;
ROC_MktBuy = LT_MktBuy AND ST_MktBuy;
//LT Valey greater than 50%
LT_Mkt = SP500_15WPercBull > 50;
Buy_Mkt = ROC_MktBuy;
LDive_Code = 11;
//LDive_Code = Optimize("LDiv_Code",11,11,18,1);
//Set Position Score Variable
Issue_Score = 100-Osc_Issue; // Profits cut in half
BuyOrder = BT_LDiv_Code >= LDive_Code AND IssueRS==1 AND IndustryRS==1;// AND IssueROC==1; // /*BT_LDiv_Code >= LDive_Code AND*/
ShortOrder = 0;
SellOrder = IssueRS2==-1 AND IndustryRS2==-1; // AND SectorROC==-1 AND IndustryRS==-1 AND IssueROC==-1 AND SectorROC==-1;
CoverOrder = 0;
ShortOrder = SellOrder;
StopOrder = Cross(SAR(0.01,0.2),Close);
ApplyStop(stopTypeTrailing, stopModePoint, StopOrder, True, True );
//Buy = BuyOrder;
//Sell = 0;
#include <Mod - Backtest.afl>
/*
HoldBars = 15;
Holdbars = Optimize("Holdbars",10,5,25,2);
ApplyStop( stopTypeNBar, stopModeBars, HoldBars );
*/
Sector_Name = SectorID(1);
Industry_Name = IndustryID(1);
_N(Title = Name() + " Relative Strength - Number of Stocks:" + WriteVal(Ind_Issues,1.0)
+ EncodeColor(colorRed) + " Stock " + WriteVal(Issue_RS,1.2) + "/" + WriteVal(Issue_RS2,1.2)
+ EncodeColor(colorBlue) + " - " + Industry_Name + " " + WriteVal(Industry_RS,1.2) + "/" + WriteVal(Industry_RS2,1.2)
+ EncodeColor(colorYellow) + " - " + Sector_Name + " " + WriteVal(Sector_RS,1.2) + "/" + WriteVal(Sector_RS2,1.2)
+ EncodeColor(colorRed) + "\n" + Error);
Filter = BuyOrder OR ShortOrder;
AddColumn(Close,"Close");
AddColumn(IIf(BuyOrder==1,1,-1),"Trade");
AddTextColumn(Ind_ATC,"Industry");
AddColumn(Ind_Issues,"# Issues",1.0);
AddColumn(Issue_RS,"Issue RS");
AddColumn(Issue_ROC,"Issue ROC");
AddColumn(Industry_RS,"Ind RS");
AddColumn(Industry_ROC,"Ind ROC");
AddColumn(Sector_RS,"Sec RS");
AddColumn(Sector_ROC,"Sec ROC");
|