Welcome to our Knowledge Base
Illustration with Infantino’s PCA Strategy
Illustration with Infantino’s PCA Strategy
In this tutorial, we demonstrate some powerful features of AlgoQuant (which is backed by our SuanShu library) using Infantino’s PCA strategy as an example. All the code referenced in this tutorial can be found in algoquant/model/infantino2010 package and its demo.
Powerful Mathematical Support
AlgoQuant is backed by our numerical library (SuanShu), so computing strategy signals becomes a very simple task, and the code usually takes only a few lines. For example, Infantino’s strategy computes its buy/sell signals based on principal component analysis (PCA). Here is the code snipplet for the computation of matrix D for the “dimensionally reduced returns” (mentioned on pages 25 and 26 in the paper):
PCA pca = new PCAbyEigen(X, false); Vector mean = pca.mean(); Matrix M_t = rbind(mean); Matrix M = rbind(nCopies(T, mean)); // each column is the average log returns for each stock Matrix Y = X.minus(M); // T by N List loadings = new ArrayList(k); for (int i = 1; i < = k; ++i) { loadings.add(pca.loading(i)); } Matrix PHI = cbind(loadings); // N by k Matrix D = Y.multiply(PHI); // T by k
Quick Strategy Prototyping
With AlgoQuant, coding strategies for simulation can be done with minimal effort. A strategy class just needs to implement an interface, Strategy. Simulation in AlgoQuant is event-driven, so a strategy just needs to subscribe to the events it is interested, such as price changes, order executions, etc. By implementing the corresponding event handlers, the strategy is called back when the subscribed events happened. Here is the skeleton for Infantino Strategy which makes trade descisions upon price updates:
public class Infantino2010Strategy implements Strategy, DepthHandler { // ... @Override public void onDepthUpdate(DateTime now, Depth depth, MarketCondition mc, TradeBlotter blotter, Broker broker) { // ... compute buy/sell signals from the updated prices (or depths) List orders = param.infantinoOrders.getOrders(signal, regime, blotter.positions(), mc.depths()); broker.sendOrder(orders); } }
Convenient Data Loading
Historical price data from various sources (e.g., Bloomberg, Yahoo! Finance, Gain Capital, Compustat, etc.) can be loaded for simulation. For example, we use end-of-day data from Yahoo! Finance in our simulation. The following code snippet loads the data files cached in the harddrive or automatically downloads them from Yahoo! Finance website if the cache is absent.
// load data from Yahoo! this.caches = new DepthCaches( new DepthCacheFactory(){ @Override public SequentialCache newInstance(Stock stock,Interval interval) throws Exception { return new YahooDepthCacheFactory("./log/Yahoo").newInstance(stock, interval); } }, stocks, interval);
Simple Simulation Setup
Simulator with various options can be configured easily. For example, we configure the simulator to update the strategy with synchronized views of product depths by this:
new SimpleSimulatorBuilder().withDepthUpdates(caches).useExchangeRates(rates).synchronizeDepthUpdates().build();
Flexible & Extendible Performance Measures for Analysis
There are plenty of performance measures available for you to analyze the performance of your strategy (see performance package for more details). You can also conveniently develop your own set of measures.
List measures = new ArrayList(); double commissionRate = 0.005; measures.add(new ProfitLossAfterCommission(commissionRate)); measures.add(new Commission(commissionRate)); measures.add(new InformationRatioForZeroInvestment(interval, Period.years(1), 0.)); measures.add(new MaxDrawdownPercentage(1.)); measures.add(new ExecutionCount()); // ... many other measures can be added
Parallelized Sensitivity Analysis and Monte Carlo Simulation
Usually, a strategy takes in a few parameters, and we want to find the optimal ones to trade with. In addition, we are also interested in how sensitive the strategy’s performance is to the change of these parameters. With parallelized SensitivityAnalyzer in AlgoQuant, you can find the answer in plots with a few lines of code (see Infantino2010ParamAnalysis):
SimSetting simSetting = new SimSetting(simulator, caches, rates); SensitivityAnalyzer analyzer = new SensitivityAnalyzer( simSetting, measures, new StrategyFactory() { @Override public Infantino2010Strategy newInstance(Param param) { return new Infantino2010Strategy(param); } }); SensitivityAnalysisReport report = analyzer.run(params, new NoOpProgressListener()); SensitivityPlotterUtils.plotAllMeasures(new GenericParamPlotter(true), report); printReport(report);
The simulations on various parameters run in parallel by utilizing all the available CPU cores, so that results can be obtained multiple times faster. Besides, you can also run MCSimulation with customizable price models.