Rotational System with Market Filter and Modified Exit


I’m trying to make a rotational system with a market regime filter, that when the market filter turns off, the positions exit only when they drop below the worst rank. In the example below, the system buy four stocks from a watchlist. I can have the market filter work, with the Iif statement and the scoreNoRotate or scoreExitAll functions. With scoreNoRotate, when the market filter turns off, it will continue to hold the same four positions until the market filter turns back on. With scoreExitAll, it will exit all four positions when the market filter turns off. What I would like it to do, is when the market filter turns off, to only exit each of the four positions when they fall below the WorstRankHeld set at 6 in my example. It shouldn’t enter any new positions until the market filter turns back on.

Does anyone know of a way that I might be able to do this?

//Exits each position once it rises a number of positions

_SECTION_BEGIN("Rotational System with Filter"); 

Float = 100000;  
// Options  
SetOption("AllowPositionShrinking", True); 
SetOption("InitialEquity", Float);  

SetOption("MaxOpenPositions", 4); 
SetPositionSize( 100 / 4, spsPercentOfEquity );  
// trade on close 
SetTradeDelays( 0, 0, 0, 0 ); 
BuyPrice = Close; 
//Market regime filter
SetForeign( "SPY" );

SetBacktestMode( backtestRotational ); 
function MyIndicator() 
{return C/EMA(C,15); 


//PositionScore = IIf( MarketIsGood, Score ,scoreNoRotate);//if this is uncommented it holds the same stocks while market filter is off
PositionScore = IIf( MarketIsGood, Score ,scoreExitAll);//if this is uncommented it sells all the stocks while market filter is off
//is there a way to have the code, when the market filter turns off, to only sell the stocks when they have dropped below the worst rank


Did you get this to work ?


Hi @megamills, I'm still hoping to find out a way to exit my positions normally after a market filter turns off, but not to enter new positions until the market filter turns back on. I expect that you have to make a custom back test to do that. I'm taking some lessons with @mradtke, if we have time, I'm sure that he could help me. By the way, I would certainly recommend Matt Radtke or Alan from @HelixTrader for training. They both do excellent work. I'll post it here if I ever find an answer.


@marcel is correct: this would be straightforward if you used a CBT to control the rotation logic yourself. You would simply put your market filter check in the CBT to prevent you from taking new trades when the filter is "off", instead of putting it in your Phase 1 AFL and incorporating it in your PositionScore.


@fxshrat I understand that you're very good with Amibroker and that you help a lot of people with it, quite often expecting nothing in return. I find I can learn a lot from reading your posts.

I hope that I can get good enough with Amibroker so that at some point I can help other people here too. I believe the most important thing about this forum is that people are here to help each other.

I see a lot of posts from @HelixTrader too on this forum, where he has helped other people to learn about Amibroker and I'm sure that he will continue to do that. Thank you for your warning, but with Alan's training I was finally able to make money at trading. In the last year, using what he has taught me, I was easily able to make more than enough money to pay back what I spent for his lessons.


Honestly I don't believe that because you seem to be new in trading. In one of your posts you said you were able to make money in one week. This is simply naive because trading is not a short sprint run but it is a long run. A marathon. One week or one day or one hour or... are of zero relevance. Anyway I don't care for your trading (simply because of the fact that it is none of my business). I understand that you have to defend your spent money.

But, I am talking about vendor quality. I am talking about guys asking for money but not about general forum users! So it is a big difference I am talking about here. A guy who enters world of commercial services is watched differently and more carefully. Then you are not innocent anymore and less forgivable. And the guy I am talking about asks for money for weak coding (and without proven degree which would be less relevant to me if he would offer actual quality). And I am seriously questioning his quality (and his profession). Imagine you go to a French restaurant and you get a meal that looks like being made by a plumber. And on top of that it was really made by a plumber. Someone being used to going to McDonalds probably wouldn't care or realize a difference since he would be used to such "meal". But for someone who knows better (or in that case has much better taste) it would be like a slap to the face.

Back to programming, here is just one example by Helixtrader code writing


StockSector = StrLeft( GicsID( 0 ), 2 );

sec = "";

if( stocksector == "10" )
    sec = "$SPXE";

if( stocksector == "15" )
    sec = "$SPXM";

if( stocksector == "20" )
    sec = "$SPXI";

if( stocksector == "25" )
    sec = "$SPXD";

if( stocksector == "30" )
    sec = "$SPXS";

if( stocksector == "35" )
    sec = "$SPXA";

if( stocksector == "40" )
    sec = "$SPXF";

if( stocksector == "45" )
    sec = "$SPXT";

if( stocksector == "50" )
    sec = "$SPXL";

if( stocksector == "55" )
    sec = "$SPXU";

if( stocksector == "60" )
    sec = "$SPXR";

This upper single code example alone (out of several other ones) proves to me that he is not what he claims to be because it is code writing like beginners do. This is code you see a lot by people being new to programming. So basically he is just copying what beginners have posted in forums. Why asking for money if you can't write proper (advanced) code?

No professional/advanced programmer would write such code like above because in this specific case it is a one liner (maximum two lines) since it is a mathematical sequence. It is a repetition. A guy who writes such code and asks for money will certainly fail at (much) more comprehensive tasks! I have seen such weak code writing a lot in his public posts. Again such code writing is forgivable as far as basic users are concerned but it is not if you are commercial one. Then it is a no go, IMO.

Secondly he is just repeating things you can find online already. So we have a pattern here.

Third why is a guy who is asking for money asking me online for sharing code? It is still making no sense to me. I am still shaking my head in disbelief.

Apropos shaking my head... why is my above post being flagged? I am giving my opinion about vendors asking for money and it is a backed up valuable critic but it is not one been made out of the blue. Was it you or helixtrader himself? How far have we come that you can not post a critic? It is like prohibiting a book critic or any other critic. It is backed up critic resulting out of months of observation. Was it flagged by someone else who has to defend his money spent?


Everything about flagging:
If you find your post flagged it means that the other members found it inappropriate.

It is not the forum for "professional programmers" only, it is forum for every AmiBroker user regardless what his/her programming exposure is. Comments that discourage people from sending "non professional" code are not welcome. The only requirement about code posting here is using code tags for readability and proper formatting. There is no other requirement.

I am closing this thread now so it does not degenerate into flame war.