User did not read the manual, was: Autoranking of AFL does not work

Please see code below. What i am trying to do is to find and buy the top 4 stocks (where the gain/day is the highest) in my watchlist of 10 stocks (mainly to test if the backtest does what i want).

The funny thing is the backtest gives me AXXJ, DBA, DBC and GLD as tickers to buy whereas explorae gives me ITA, XLF, JETS and DBC as seen in the pict.

Why does that happen? AXXJ, DBA and GLD are ranked actually ranked 6, 9, 8 from their values.


explore result

SetOption("InitialEquity", 100000);
SetOption("MaxOpenPositions", 4); //25% equity a trade
SetTradeDelays(1,1,1,1); // Buy/Sell at next bar after signal
SetPositionSize(25, spsPercentOfEquity ); //set 25% as max equity for each position. so total can have 4 concurrent positions

BuyPrice=Open; // buy at mkt open
SellPrice=Open; // sell at market open

//Buy criteria (Highest % gain)
Gain=100*((Close-Open)/Open);

Buy = Gain;

Sell = 0 ; // DO NOT exit normally

// APPLY STOP DURING BACKTESTING !
ApplyStop(stopTypeTrailing, stopModePercent, 15, True, True );

Filter = 1;
SetSortColumns( -5 );
AddTextColumn(fullName(), "Company");

AddColumn( Gain, "Gain", 1.4 );

First it is AFL (AmiBroker Formula Language), not ALF

Second, before making claims that something does not work, you should READ THE FINE MANUAL

https://www.amibroker.com/guide/h_ranking.html

and

https://www.amibroker.com/guide/h_portfolio.html

In short: YOU DID NOT define ranking at all (your code is missing PositionScore variable) and SetSortColumns function is for EXPLORATION, not backtest.

Third thing:

When posting the formula, please make sure that you use Code Tags (using </> code button) as explained here: How to use this site.

Using code button

Code tags are required so formulas can be properly displayed and copied without errors.

1 Like

Sorry that i may have unknowingly offended u. i tried using positionscore as well, it gave me the same 4 counters. can i humbly ask how amibroker chooses the 4 counters without the use of positionscore?

SetOption("InitialEquity", 100000);
SetOption("MaxOpenPositions", 4); //25% equity a trade
SetTradeDelays(1,1,1,1); // Buy/Sell at next bar after signal
SetPositionSize(25, spsPercentOfEquity ); //set 25% as max equity for each position. so total can have 4 concurrent positions

BuyPrice=Open; // buy at mkt open
SellPrice=Open; // sell at market open



Gain=100*((Close-Open)/Open);
PositionScore=Gain;

Buy = Gain;

Sell = 0 ; // DO NOT exit normally

// APPLY STOP DURING BACKTESTING !  
ApplyStop(stopTypeTrailing, stopModePercent, 15, True, True );

Please follow this advice: How to ask a good question

First: CLICK on the link I provided and READ.
Second: Once you read, you would know that ABSOLUTE value of PositionScore counts for ranking
Third: send a screenshot of DETAILED LOG again (after making code changes) read here: How do I debug my formula?

From the official knowledge base articles (IMHO it will help all users to read all of these articles)

1 Like

That one is not a condition returning zero or one!

Proper one would be like this

Buy = Gain>0;



It is not funny but it is logical consequence of your code.

In Backtest you have set trade delays and exploration is not backtest!
Settradedelays function is backtest function.

So you have to add delay to your exploration code as well.

AddColumn( Ref(Gain,-1), "Gain", 1.4 );

1 Like