Home › Forums › Trading System Mentor Course Community › AmiBroker Coding and AFL › Backtest including or excluding specific industries
- This topic is empty.
-
AuthorPosts
-
March 23, 2016 at 10:04 am #101452StephaneFimaMember
Hi,
I am trying to code a piece of code that selects which industries to be included or excluded from the backtest.
I did the following:Code:SectTog = ParamToggle (“Sector Filter”,”Off|On”,0);Energy = ParamToggle(“Energy”,”Included|Excluded”,0);
Materials = ParamToggle(“Materials”,”Included|Excluded”,0);
Industrials = ParamToggle(“Industrials”,”Included|Excluded”,0);
ConsDisc = ParamToggle(“Consumer Discretionary”,”Included|Excluded”,0);
ConsStap = ParamToggle(“Consumer Staples”,”Included|Excluded”,0);
Healthcare = ParamToggle(“Healthcare”,”Included|Excluded”,0);
Financials = ParamToggle(“Financials”,”Included|Excluded”,0);
InfoTech = ParamToggle(“Information Technologies”,”Included|Excluded”,0);
Telco = ParamToggle(“Telecommunication Services”,”Included|Excluded”,0);
Utilities = ParamToggle(“Utilities”,”Included|Excluded”,0);EnerCond = IIf(NOT Energy, InGics(“10”), NOT InGics(“10”));
MatCond = IIf(NOT Materials, InGics(“15”), NOT InGics(“15”));
IndusCond = IIf(NOT Industrials, InGics(“20”), NOT InGics(“20”));
ConsDiscCond = IIf(NOT ConsDisc, InGics(“25”), NOT InGics(“25”));
ConsStpCond = IIf(NOT ConsStap, InGics(“30”), NOT InGics(“30”));
HealthCond = IIf(NOT Healthcare, InGics(“35”), NOT InGics(“35”));
Financials = IIf(NOT Healthcare, InGics(“40”), NOT InGics(“40”));
ITechCond = IIf(NOT InfoTech, InGics(“45”), NOT InGics(“45”));
TelcoCond = IIf(NOT Telco, InGics(“50”), NOT InGics(“50”));
UtiCond = IIf(NOT Utilities, InGics(“55”), NOT InGics(“55”));Sectors = EnerCond AND MatCond AND IndusCond AND ConsDiscCond AND ConsStpCond AND HealthCond AND Financials AND ITechCond AND TelcoCond AND UtiCond;
SectFilt = IIf(SectTog,Sectors,1);
However, it is not working, and I do not find why. Any ideas?
Thanks!
March 23, 2016 at 10:35 am #103421SaidBitarMemberCode:Sectors = EnerCond AND MatCond AND IndusCond AND ConsDiscCond AND ConsStpCond AND HealthCond AND Financials AND ITechCond AND TelcoCond AND UtiCond;.
may be you can start by changing the AND to OR
March 23, 2016 at 10:56 am #103423SaidBitarMemberMy bad it should be AND
here is the code
i checked it and it is OK
Code:_SECTION_BEGIN(“Universe Filter”);
//=================================================================================================SectTog = ParamToggle (“Sector Filter”,”Off|On”,0);
Energy = ParamToggle(“Energy”,”Included|Excluded”,0);
Materials = ParamToggle(“Materials”,”Included|Excluded”,0);
Industrials = ParamToggle(“Industrials”,”Included|Excluded”,0);
ConsDisc = ParamToggle(“Consumer Discretionary”,”Included|Excluded”,0);
ConsStap = ParamToggle(“Consumer Staples”,”Included|Excluded”,0);
Healthcare = ParamToggle(“Healthcare”,”Included|Excluded”,0);
Financials = ParamToggle(“Financials”,”Included|Excluded”,0);
InfoTech = ParamToggle(“Information Technologies”,”Included|Excluded”,0);
Telco = ParamToggle(“Telecommunication Services”,”Included|Excluded”,0);
Utilities = ParamToggle(“Utilities”,”Included|Excluded”,0);EnerCond = IIf( Energy == 0, InGics(“10”), InGics(“10”) == 0);
MatCond = IIf( Materials == 0, InGics(“15”), InGics(“15”) == 0);
IndusCond = IIf( Industrials == 0, InGics(“20”), InGics(“20”) == 0);
ConsDiscCond = IIf( ConsDisc == 0, InGics(“25”), InGics(“25”) == 0);
ConsStpCond = IIf( ConsStap == 0, InGics(“30”), InGics(“30”) == 0);
HealthCond = IIf( Healthcare == 0, InGics(“35”), InGics(“35”) == 0);
Financials = IIf( Healthcare == 0, InGics(“40”), InGics(“40”) == 0);
ITechCond = IIf( InfoTech == 0, InGics(“45”), InGics(“45”) == 0);
TelcoCond = IIf( Telco == 0, InGics(“50”), InGics(“50”) == 0);
UtiCond = IIf( Utilities == 0, InGics(“55”), InGics(“55”) == 0);
“EnerCond ” +
Sectors = EnerCond AND MatCond AND IndusCond AND ConsDiscCond AND ConsStpCond AND HealthCond AND Financials AND ITechCond AND TelcoCond AND UtiCond;SectFilt = IIf(SectTog,Sectors,1);
//=================================================================================================
_SECTION_END();Filter = SectFilt;
AddTextColumn(GicsID(0),”InGics”,1.2);March 23, 2016 at 11:58 am #103424StephaneFimaMemberThanks Said.
Well, I still have a problem.1) when I test my strategy with this filter off (i.e. all industries included), I have a given result
2) but when I switch the industry filter on and leave all the industries “included” (which should be equivalent to my previous point, I have no results from the backtester.This is strange.
March 23, 2016 at 12:14 pm #103425SaidBitarMemberCan you test first with explore to see where is the problem
March 23, 2016 at 12:19 pm #103426SaidBitarMemberyou know what I think the problem is here
if all are included then all are 0
EnerCond =InGics(“10”)
MatCond = InGics(“15”)
IndusCond =InGics(“20”)
and so onand you are asking in Sectors that all conditions should be satisfied this means the stock should be in energy , materials, industrials, .. at the same time and this is impossible.
this is why in the beginning I was saying AND is not correct
this is what I think I will know for sure when I go back home to check in Amibroker
March 23, 2016 at 12:38 pm #103427SaidBitarMemberOk I found the problem
in case only one is selected then all should be AND
in case all are selected then all should be OR
in case 2 are selected then there should be OR between these two and AND with the remainingwriting all the different combinations will not be good so you need to think of one way to generate the conditions for you
March 23, 2016 at 12:46 pm #103428SaidBitarMemberCode:_SECTION_BEGIN(“Universe Filter”);
//=================================================================================================SectTog = ParamToggle (“Sector Filter”,”Off|On”,0);
Energy = ParamToggle(“Energy”,”Included|Excluded”,0);
Materials = ParamToggle(“Materials”,”Included|Excluded”,0);
Industrials = ParamToggle(“Industrials”,”Included|Excluded”,0);
ConsDisc = ParamToggle(“Consumer Discretionary”,”Included|Excluded”,0);
ConsStap = ParamToggle(“Consumer Staples”,”Included|Excluded”,0);
Healthcare = ParamToggle(“Healthcare”,”Included|Excluded”,0);
Financials = ParamToggle(“Financials”,”Included|Excluded”,0);
InfoTech = ParamToggle(“Information Technologies”,”Included|Excluded”,0);
Telco = ParamToggle(“Telecommunication Services”,”Included|Excluded”,0);
Utilities = ParamToggle(“Utilities”,”Included|Excluded”,0);
sectors = 0;EnerCond = IIf( Energy == 0, InGics(“10”), InGics(“10”) == 0);
if EnerCond
Sectors = Sectors OR EnerCond;
MatCond = IIf( Materials == 0, InGics(“15”), InGics(“15”) == 0);
if MatCond
Sectors = Sectors OR MatCond;
IndusCond = IIf( Industrials == 0,InGics(“20”), InGics(“20”) == 0);
if IndusCond
Sectors = Sectors OR IndusCond;
ConsDiscCond = IIf( ConsDisc == 0, InGics(“25”), InGics(“25”) == 0);
if ConsDiscCond
Sectors = Sectors OR ConsDiscCond;
ConsStpCond = IIf( ConsStap == 0, InGics(“30”), InGics(“30”) == 0);
if ConsStpCond
Sectors = Sectors OR ConsStpCond;
HealthCond = IIf( Healthcare == 0, InGics(“35”), InGics(“35”) == 0);
if HealthCond
Sectors = Sectors OR HealthCond;
Financials = IIf( Healthcare == 0, InGics(“40”), InGics(“40”) == 0);
if Financials
Sectors = Sectors OR Financials;
ITechCond = IIf( InfoTech == 0, InGics(“45”), InGics(“45”) == 0);
if ITechCond
Sectors = Sectors OR ITechCond;
TelcoCond = IIf( Telco == 0, InGics(“50”), InGics(“50”) == 0);
if TelcoCond
Sectors = Sectors OR TelcoCond;
UtiCond = IIf( Utilities == 0, InGics(“55”), InGics(“55″) == 0);
if UtiCond
Sectors = Sectors OR UtiCond;SectFilt = IIf(SectTog,Sectors,1);
//=================================================================================================
_SECTION_END();Filter = SectFilt;
AddTextColumn(GicsID(0),”InGics”,1.2);this will do the trick but please check for the syntax I wrote it in notepad maybe I missed some ;
March 23, 2016 at 4:44 pm #103429SaidBitarMemberSorry for all the spam earlier
here is the code i tested it seems OK for me but please verify
Code:_SECTION_BEGIN(“Universe Filter”);
//=================================================================================================SectTog = ParamToggle (“Sector Filter”,”Off|On”,0);
Energy = ParamToggle(“Energy”,”Included|Excluded”,0);
Materials = ParamToggle(“Materials”,”Included|Excluded”,0);
Industrials = ParamToggle(“Industrials”,”Included|Excluded”,0);
ConsDisc = ParamToggle(“Consumer Discretionary”,”Included|Excluded”,0);
ConsStap = ParamToggle(“Consumer Staples”,”Included|Excluded”,0);
Healthcare = ParamToggle(“Healthcare”,”Included|Excluded”,0);
Financials = ParamToggle(“Financials”,”Included|Excluded”,0);
InfoTech = ParamToggle(“Information Technologies”,”Included|Excluded”,0);
Telco = ParamToggle(“Telecommunication Services”,”Included|Excluded”,0);
Utilities = ParamToggle(“Utilities”,”Included|Excluded”,0);
sectors = 0;EnerCond = IIf( Energy == 0, InGics(“10”), InGics(“10”) == 0);
if (EnerCond == 1 AND Energy == 0)
Sectors = Sectors + 1;MatCond = IIf( Materials == 0, InGics(“15”), InGics(“15”) == 0);
if (MatCond == 1 AND Materials == 0)
Sectors = Sectors + 1;IndusCond = IIf( Industrials == 0,InGics(“20”), InGics(“20”) == 0);
if (IndusCond == 1 AND Industrials == 0)
Sectors = Sectors + 1;ConsDiscCond = IIf( ConsDisc == 0, InGics(“25”), InGics(“25”) == 0);
if (ConsDiscCond == 1 AND ConsDisc == 0)
Sectors = Sectors + 1;ConsStpCond = IIf( ConsStap == 0, InGics(“30”), InGics(“30”) == 0);
if (ConsStpCond == 1 AND ConsStap == 0)
Sectors = Sectors + 1;HealthCond = IIf( Healthcare == 0, InGics(“35”), InGics(“35”) == 0);
if (HealthCond == 1 AND Healthcare == 0)
Sectors = Sectors + 1;Financials = IIf( Healthcare == 0, InGics(“40”), InGics(“40”) == 0);
if (Financials == 1 AND Healthcare == 0)
Sectors = Sectors + 1;ITechCond = IIf( InfoTech == 0, InGics(“45”), InGics(“45”) == 0);
if (ITechCond == 1 AND InfoTech == 0)
Sectors = Sectors + 1;TelcoCond = IIf( Telco == 0, InGics(“50”), InGics(“50”) == 0);
if (TelcoCond == 1 AND Telco == 0)
Sectors = Sectors + 1;UtiCond = IIf( Utilities == 0, InGics(“55”), InGics(“55″) == 0);
if (UtiCond == 1 AND Utilities == 0)
Sectors = Sectors + 1;SectFilt = IIf(SectTog,Sectors,1);
//=================================================================================================
_SECTION_END();Filter = SectFilt;
AddTextColumn(GicsID(0),”InGics”,1.2);March 23, 2016 at 6:02 pm #103431StephaneFimaMemberMany thanks for your messages Said.
In your code, if I choose to switch on the sector filter, “SectFilt = IIf(SectTog,Sectors,1);” will be equal to the variable “Sectors” which in turn will just be a number (successively incremented by 1). How Amibroker will know which sector to include or not?
I am going to think again about this tonight.
March 23, 2016 at 6:21 pm #103432SaidBitarMemberSFima wrote:In your code, if I choose to switch on the sector filter, “SectFilt = IIf(SectTog,Sectors,1);” will be equal to the variable “Sectors” which in turn will just be a number (successively incremented by 1). How Amibroker will know which sector to include or not?First Sectors will be incremented only when the conditions are true
Second Amibroker will read the code up to down so if the stock satisfy the required conditions the counter will be greater than 0 otherwise it will be 0.
amibroker does not need to know the stock is in which sector what is important is this stock satisfying the required conditions yes or no.
if no then the answer will be zero
if yes then the answer will be greater than or equal to one and the value is not important as long as it is greater than zero.so if the value is greater than zero this means it passed the filter then it will be used for the remaining code otherwise it will be over and AB will jump to the second stock and so on.
here are snapshots of the results
in the first snapshot i picked some sectors and in the second one i ran explore on the S&P500 showing the sector code
March 23, 2016 at 6:34 pm #103434SaidBitarMemberHere is another way to write may be this one is more clear.
Code:_SECTION_BEGIN(“Universe Filter”);
//=================================================================================================SectTog = ParamToggle (“Sector Filter”,”Off|On”,0);
Energy = ParamToggle(“Energy”,”Included|Excluded”,0);
Materials = ParamToggle(“Materials”,”Included|Excluded”,0);
Industrials = ParamToggle(“Industrials”,”Included|Excluded”,0);
ConsDisc = ParamToggle(“Consumer Discretionary”,”Included|Excluded”,0);
ConsStap = ParamToggle(“Consumer Staples”,”Included|Excluded”,0);
Healthcare = ParamToggle(“Healthcare”,”Included|Excluded”,0);
Financials = ParamToggle(“Financials”,”Included|Excluded”,0);
InfoTech = ParamToggle(“Information Technologies”,”Included|Excluded”,0);
Telco = ParamToggle(“Telecommunication Services”,”Included|Excluded”,0);
Utilities = ParamToggle(“Utilities”,”Included|Excluded”,0);EnerCond = IIf( Energy == 0, InGics(“10”), InGics(“10”) == 0);
MatCond = IIf( Materials == 0, InGics(“15”), InGics(“15”) == 0);
IndusCond = IIf( Industrials == 0,InGics(“20”), InGics(“20”) == 0);
ConsDiscCond= IIf( ConsDisc == 0, InGics(“25”), InGics(“25”) == 0);
ConsStpCond = IIf( ConsStap == 0, InGics(“30”), InGics(“30”) == 0);
HealthCond = IIf( Healthcare == 0, InGics(“35”), InGics(“35”) == 0);
FinancialsCond = IIf( Financials == 0, InGics(“40”), InGics(“40”) == 0);
ITechCond = IIf( InfoTech == 0, InGics(“45”), InGics(“45”) == 0);
TelcoCond = IIf( Telco == 0, InGics(“50”), InGics(“50”) == 0);
UtiCond = IIf( Utilities == 0, InGics(“55”), InGics(“55″) == 0);Sectors = (EnerCond AND Energy == 0) OR (MatCond AND Materials == 0) OR (IndusCond AND Industrials == 0) OR
(ConsDiscCond AND ConsDisc == 0) OR (ConsStpCond AND ConsStap == 0) OR (HealthCond AND Healthcare == 0) OR
(FinancialsCond AND Financials == 0) OR (ITechCond AND InfoTech == 0) OR (TelcoCond AND Telco == 0) OR
(UtiCond AND Utilities == 0);SectFilt = IIf(SectTog,Sectors,1);
//=================================================================================================
_SECTION_END();Filter = SectFilt;
AddTextColumn(GicsID(0),”InGics”,1.2);March 23, 2016 at 6:41 pm #103433StephaneFimaMemberI think I found something.
Maybe we have to think by exclusion, i.e. if no specifications, Amibroker will consider the entire universe.
So if we do not want to exclude one (or more) sector, it will be equal to 1, otherwise it will be equal to Not InGics(“…”).The code would then be the following:
Code:SectTog = ParamToggle (“Sector Filter”,”Off|On”,0);Energy = ParamToggle(“Energy”,”Included|Excluded”,0);
Materials = ParamToggle(“Materials”,”Included|Excluded”,0);
Industrials = ParamToggle(“Industrials”,”Included|Excluded”,0);
ConsDisc = ParamToggle(“Consumer Discretionary”,”Included|Excluded”,0);
ConsStap = ParamToggle(“Consumer Staples”,”Included|Excluded”,0);
Healthcare = ParamToggle(“Healthcare”,”Included|Excluded”,0);
Financials = ParamToggle(“Financials”,”Included|Excluded”,0);
InfoTech = ParamToggle(“Information Technologies”,”Included|Excluded”,0);
Telco = ParamToggle(“Telecommunication Services”,”Included|Excluded”,0);
Utilities = ParamToggle(“Utilities”,”Included|Excluded”,0);EnerCond = IIf(Energy ==0, 1, NOT InGics(“10”));
MatCond = IIf(Materials ==0, 1, NOT InGics(“15”));
IndusCond = IIf(Industrials ==0, 1, NOT InGics(“20”));
ConsDiscCond = IIf(ConsDisc ==0, 1, NOT InGics(“25”));
ConsStpCond = IIf(ConsStap ==0, 1, NOT InGics(“30”));
HealthCond = IIf(Healthcare ==0, 1, NOT InGics(“35”));
FiCond = IIf(Financials ==0, 1, NOT InGics(“40”));
ITechCond = IIf(InfoTech ==0, 1, NOT InGics(“45”));
TelcoCond = IIf(Telco ==0, 1, NOT InGics(“50”));
UtiCond = IIf(Utilities ==0, 1, NOT InGics(“55”));Sectors = EnerCond AND MatCond AND IndusCond AND ConsDiscCond
AND ConsStpCond AND HealthCond AND FiCond AND ITechCond
AND TelcoCond AND UtiCond;SectFilt = IIf(SectTog,Sectors,1);
Please verify but I think it is working.
There is also a “BUT”
I tried to exclude all of the sectors. I was then expecting no results in the baktester but in fact Amibroker returned signals over some delisted companies.
My guess is that Norgate do not assign GICS code to every stocks and in particular old delisted ones.March 23, 2016 at 6:42 pm #103435StephaneFimaMembersorry our messages crossed
March 23, 2016 at 6:42 pm #103436SaidBitarMemberno problem
i think the last one will work
test it please
-
AuthorPosts
- You must be logged in to reply to this topic.