Using K-Ratio in PositionScore?

Is it possible to use K-Ratio for PositionScore in a rotation AFL?

Thanks

Short SMS-style questions leave a lot of room for guessing. That makes is pretty hard to deliver the "right" answers (that answer what you actually had in mind). It is better to follow How to ask a good question

From the other duplicate threads that you have created it can be guessed that you want to calculate K-Ratio for each symbol separately and then using it in position score.

The problem with such an idea is that you can't calculate K-ratio unless you know when you are trading, and you don't know when you are trading because you need K-ratio to calculate your positions. So you have classic chicken and egg problem.

So to make it doable you would need to calculate individual symbols' K-ratios first (in first backtest phase) using some fixed set of trading rules (that don't take into account ranking and other portfolio effects), store them in static variables and then use such individual-K-ratio as any other indicator in second phase

http://www.amibroker.com/kb/2014/11/20/how-to-show-indicator-values-in-backtest-trade-list/

If you have access to old AmiBroker mailing list there were formulas calculating K-ratio for single symbol (using Equity() to evaluate performance of single stock). Please note that there are at least 3 different "versions" of Kestners work as he dramatically changed the definition of K-ratio over time in his consecutive books. Also some formulas use log( Equity) to account for compounding as Kestner's books did not.

1 Like

Thank you for this post. I did not see it before sending you a PM.
I'm going to look into using K-Ratio as the optimization target in a walk-forward test.
Yes, I've done reading on Kestner's work. I believe AB uses his 2nd iteration of K-Ratio, correct?

Please see the code below. I understand it is not correct. There is an error trying to use MyHistStat1 in the trading system code because it is undefined there. The goal is to have a rotational strategy based on K-Ratio. You mentioned calculating K-ratio manually. Is it possible to obtain from PerformanceStats as in this example? Can it then be passed to the trading system? Maybe what I'm trying to do isn't possible.

// Source: http://www.amibroker.com/kb/index.php?s=metric

SetOption("UseCustomBacktestProc", True ); 

if( Status("action") == actionPortfolio )
{
  bo = GetBacktesterObject();

  bo.PreProcess(); // Initialize backtester

  // initialize with null
  // you can have as many historical metrics as you want
  // (just duplicate line below for many metrics you want)
  MyHistStat1 = Null;
  
  for(bar=0; bar < BarCount; bar++)
  {
   bo.ProcessTradeSignals( bar );
  
   // recalculate built-in stats on EACH BAR
   stats = bo.GetPerformanceStats( 0 ); 
 
   // the line below reads the metric and stores it as array element
   // you can add many lines for each metric of your choice
   MyHistStat1[ bar ] = stats.GetValue("K-Ratio"); // get K-Ratio value calculated this bar
  
  }

  bo.PostProcess(); // Finalize backtester
  
  }

  // now STORE the historical data series representing the metric of your choice
  // duplicate the line below for as many metrics as you want
  AddToComposite( MyHistStat1, "~~~UI_HISTORICAL", "X", atcFlagEnableInPortfolio | atcFlagDefaults );
  
EnableRotationalTrading();
PositionScore = MyHistStat1;
PositionSize = -25;
SetOption("WorstRankHeld", 7 );
SetOption("MaxOpenPositions", 4 );

Let's back up a step. Apologies if you already know this, but AmiBroker takes a 2-phased approach to running a backtest. In the first phase, each symbol in your watchlist is processed individually, primarily for the purpose of generating signals and pricing (at least in the context of a backtest). In the second phase, all of those signals are processed on a bar-by-bar basis to determine actual trades.

The code inside the block

if( Status("action") == actionPortfolio )

is your Phase 2 code. Everything else is Phase 1. That means that the call to bo.GetPerformanceStats() doesn't occur until AFTER all the symbols have been processed, including the setting of PositionScore. In reality, you wouldn't have the final K-Ratio available until the backtest was complete. This is why @Tomasz said you have a "chicken and egg problem".

2 Likes

Also, the concept of running individual backtest to collect K-ratio for individual symbols would work perfectly if you run regular backtest. But in rotational backtest you don't have buy/sell signals. You just have basket of competing symbols and top-N highest ranked symbols win. If you used individual backtest, you would get just one symbol per backtest and no competition (this one symbol would always "win"). So you would have equivalent of just buy-and-hold K-ratio. B&H K-ratio can be calculated without backtest.

1 Like

"B&H K-ratio can be calculated without backtest."
Ok, so can this be done in AB: using a watchlist of the 100 symbols in the Nasdaq 100, have each symbol enter on the open and exit on the close each day for a month (~22 trades), compute the K-Ratio on each of the 100 symbols for this month, then take the symbols with the 3 highest K-Ratios and do a buy on the open, sell on the close for the next one week period. Then, shift forward a week and repeat for 30 weeks. I realize this is a combination of rotation and walk-forward and may not be possible. If not, could this test be partially automated using the new batch features?

Custom programming on user request is not in the scope of free support. It costs $120 per hour.

I already wrote you that ready-to-use formulas for K-ratio that can be used for individual symbols are on the YahooMailingList, all it takes is Google search. Here is the pointer to relevant material, you can figure out the rest yourself https://www.mail-archive.com/amibroker@yahoogroups.com/msg36807.html and https://www.mail-archive.com/amibroker@yahoogroups.com/msg17656.html