Automated Top Down Analysis

Currently I am experimenting with a top down approach that starts with checking if the SPY is above a 10 month moving average (which can be handled easily using "foreign"). If it is then I look at the sectors and select the top x number based on their relative performance and if their performance is also greater than the SPY. A portion of equity is placed on trades in the sector ETFs. Then industry group ETFs within the winning sectors are checked for relative performance and an additional portion of equity is traded on these. An optional final step would be to trade the remaining equity on individual stocks selected in a similar way.

Currently looking at re-balancing between sectors and industry groups on a monthly (maybe quarterly) basis. If individual trades get stopped out between re-balance periods, new trades would be placed after code reevaluates selections.

It would be really great to be able to automate the whole process. Then it would possible to test changes and see the resulting complete equity curve including gains, losses, and draw downs.

The steps involved would be.

  1. Check for up trending SPY (SPY > MA(10 months)
  2. Get the top X sectors based on relative strength to the SPY and trade 20% of equity divided equally.
    --- This far can be handled by selecting a watch list of sector ETFs and running code against it.
  3. Using the top X sectors selected above, select the top Y number of industry groups for each selected sector and trade 30% of total equity split equally between them using industry group ETFs.
  4. Repeat step 3 for the final 50% of equity replacing sectors with industry groups and industry groups with individual stocks.
    --- This is where I don't know how to automate the process.

I think the amount of equity allocated to sector ETFs, then industry group ETFs, and finally individual stocks could be handled using static variables.

But don't see a way to select different symbol filters to handle branching based on sector and industry groups. Thought batch processing might be helpful, but did not see a way to select different symbol filters there either.

After a periodic analysis and re-balancing the results might something like this.
SPY is greater than MA(10 months)
Top sectors: XLK, XLF, XLC, XLI but limited to XLK and XLF and XLC because max is 3.
Top industries in XLK: CIBR, SOXX, IGN, FDN but limied to CIBR and SOXX because max is 2.
Top industries in XLF: KBE, KIE, PGF but limited to KBE and KIE because max is 2.
Top industries in XLC: none because no industry ETFs with adequate volume

Sector Trades 20% of equity: XLK 6.7%, XLF, 6.7%, XLC 6.6%
Industry Trades 30% of equity: CIBR 7.5%, SOXX 7.5%, KBE 7.5%, KIE 7.5%
Company Trades 50% of equity: (similar allocations as above but at individual stocks level)

To complicate things further, I would ultimately would like to explore trading other assets via ETFs such as bonds using a similar process if the SPY failed the MA(10 months) test.

Does anyone have a suggestion how to implement the whole process in an automated way? I can do it manually by looking at the results of each step and branching accordingly, but evaluating the overall results manually is cumbersome and time consuming. Looking for a better way.

Yeah, we don't see it, either. Post your code (using code tags).

I recommend starting your research with Rotational Trading.

Don't have any code for the parts I'm having trouble with because I don't know how to make it happen.

Know how to do rotational trading for a single set of symbols specified by filter settings, but don't know how to transition to different symbol sets within a single code block and execute additional trades on the new symbol set.

Easy to have separate strategies: one strategy for each sector selected, and one strategy each for sector industry group of ETFs, and other strategies one for each group of individual companies in each industry group.

Can accomplish the goal if I manage transitioning from top level strategy to lower level strategies by manually setting up a new symbol filter between each step. That's not hard. Just tedious and hence prone to errors.

But how do you automate moving between each strategy level with different symbol sets all within one code block?

Here is an idea of how the code might look for the first level of the top down approach.

// Top Down Analysis Level 1
// Step 1 - Select Stock sectors or Bond types based on SPY status
// Step 2 - Trade top 3 symbols based on ROC if > IndexROC on 20% of account equity

procedure SetWatchList(IndexSym) { // Index symbol name is also the watch list name
	global Index;
	
	// Set Apply to filter to IndexSym watch list
	// *** Haven't found any way to do this
	
	Index = Foreign("IndexSym", "C");
}

// Step 1 - Select Stock sectors or Bond types based on SPY status
if (typeof(Level) == "undefined") {
	Level = 1;
	SPY = Foreign("SPY", "C");
	// Select which type of ETFs to trade - stocks or bonds
	if (LastValue(SPY > MA(SPY, 200))) SetWatchList("SPY"); // Setup to trade stock ETFs
	else SetWatchList("AGG"); // Setup to trade bond ETFs
	
	SetOption("MaxOpenPositions", 3); // 3 Max open positions at this level
	SetPositionSize(20 / 3, spsPercentOfEquity); // Only trading 20% of total equity at this level
}

// Step 2 - Trade Level 1 ETFs
if (Level == 1) {
	// The symbols traded here would also determine which watch lists to load next
	PositionScore = ROC(C, 200) - ROC(Index, 200); // Positive means better ROC than index
	PositionScore = IIf(PositionScore > 0, PositionScore, 0); // Only trade positive position scores
}

As you can see to automate the process for live trading and to back test a top down approach like this appears to require the ability to set the "Apply to" filter from within code and I don't know how. Is there a way to do this or another approach that accomplishes the same thing.

Also to proceed to the next level would require using the ETF names selected in this first level. Could possibly work around this with a static variable containing a comma separated string of the ETF names.

Was hoping maybe the batch processing would help but it seems too limited because you can't conditionally run an analysis project. Can set the current symbol, but not the current watch list.

Has anyone done something like this? If so, how did you go about it?

Reaching stock symbols should be acheivable but a it cumbersome.

You will need to have a couple of things

  1. An ETF equivalent to a sector / industry group / industry
  2. Have stocks allocated to a group (sector etc) and have them accessible via a table that you create ahead of time.

It might be easier to start from bottom up, evalute stocks then confirm the quality of group.

Also if you want to start with top down, you might consider trading group ETFs only and forgo the stocks. Perhaps this should be your first step and if you need to add the stocks, you can do that as an adder

Using GICS IDs might be useful for this type of task.

My problem is not how to find or assign symbols to groups, but rather how to automate the process of using a top down analysis to get to the symbols to trade. This seems to boil down to the question of how to automate the process of moving from a top level set of symbols to the next level down by changing the "Apply to" symbol filters automatically that Amibroker uses when doing a scan, exploration, or back test.

For example the above code might select stocks (SPY) over bonds (AGG). Then evaluating the sector ETFs (XLB, XLE, XLF, XLI, XLK, XLP, XLU, XLV, XLY, XLC) by comparing momentum might select XLK, XLF, and XLC on which to trade 20% of account equity.

The next step being to evaluate (by comparing the momentum) of each industry ETF within those sectors to place another 30% of account equity on the chosen industry ETFs as additional trades.

The question is how to automate (or make possible) the transition from choosing between stocks and bonds to selecting and trading XLK, XLF, and XLC with 20% of equity without manually evaluating and selecting the next group of symbols (sector ETFs in this case) by manually changing the "Apply to" symbol filters.

And then to go from sector ETFs to industry group ETFs. Once again the problem is not in finding or assigning symbols to a group that represent each sector, but rather in automating the process of working the way down from a top level to lower levels where actual trades are made.

The question is how to use code to automate the transition from one table (watch list or group) to the next when processing from higher levels to lower levels.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.