Trading different stocks in different indicator regimes

Suppose I had an indicator and I wanted to be long item 1 when the indicator was greater than 10, item 2 when the indicator was between 10 and -10 and item 3 when the indicator was below 10. I wrote the following, but it does not work as expected.

// symbols
uppreSYM = "IBM"
middleSYM = "MSFT"
lowerSYM = "NVDA"

// parameters -- levels
upper =     Param("upper level", 10);
lower =     Param("lower level", -10);

// calc the indicator
indicator = magicBoom();

// buy sell logic -- upper zone
SetForeign(upperSYM);
Buy =  Cross(indicator, upper);
Sell = Cross(upper, indicator);
RestorePriceArrays();

// buy sell logic -- middle zone
SetForeign(middleSYM);
// enter from above
Buy =  Cross(upper, indicator);
Sell = Cross(lower, indicator);
// enter from below
Buy =  Cross(indicator, lower);
Sell = Cross(indicator, upper);
RestorePriceArrays():

// buy sell logic -- lower zone
SetForeign(lowerSYM);
Buy =  Cross(lower, indicator);
Sell = Cross(indicator, lower);
RestorePriceArrays();

I suspect a case statement is going to be needed, but it is not clear to me how that should be implemented, or perhaps this is a task for some variation on rotational trading?

John -- who has yet to fully grok AB's object model

Cross is not above/below state signal but is impulse signal. So since you wrote above/below state(?) then you may use greater as > and smaller as < operators.

SetPositionSize(100, spsShares);
SetOption("MaxOpenPositions", 3);

// symbols
upperSYM = "IBM";
middleSYM = "MSFT";
lowerSYM = "NVDA";

nm = Name();

// parameters -- levels
upper = Param("upper level", 10);
lower = Param("lower level", -10);

// calc the indicator
indicator = magicBoom();// custom indicator not being part of AmiBroker

// buy sell logic -- upper zone

above_upper = indicator > upper;
below_lower = lower > indicator;
is_between  = indicator < upper AND indicator > lower;

cross_above = Cross(indicator, upper);
cross_below = Cross(lower, indicator);

Buy =  (above_upper * (nm == upperSYM)) OR 
		(is_between * (nm == middleSym)) OR
		(below_lower * (nm == lowerSYM));
		
Sell = (cross_below * (nm == upperSYM OR nm == middleSym)) OR
        (cross_above * (nm == lowerSYM OR nm == middleSym));

//PositionScore = RSI();

Then run Analysis portfolio backtest.

2 Likes

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