Illustration with Infantino’s PCA Strategy

Revision for “Illustration with Infantino’s PCA Strategy” created on May 8, 2015 @ 02:18:03

TitleContentExcerptRevision Note
Illustration with Infantino’s PCA Strategy
In this tutorial, we demonstrate some powerful features of <a href="http://numericalmethod.com/up/algoquant/" target="_blank">AlgoQuant</a> (which is backed by our <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> library) using <a class="wiki" href="http://numericalmethod.com/up/model/infantino2010/" target="_blank">Infantino’s PCA strategy</a> as an example. All the code referenced in this tutorial can be found in <a href="http://redmine.numericalmethod.com/projects/public/repository/svn-algoquant/show/core/src/main/java/com/numericalmethod/algoquant/model/infantino2010" target="_blank">algoquant/model/infantino2010 package</a> and <a href="http://redmine.numericalmethod.com/projects/public/repository/svn-algoquant/entry/core/src/main/java/com/numericalmethod/algoquant/model/infantino2010/strategy/Infantino2010ParamAnalysisDemo.java" target="_blank">its demo</a>.
<h1 id="PowerfulMathematicalSupport">Powerful Mathematical Support</h1>
<a href="http://numericalmethod.com/up/algoquant/" target="_blank">AlgoQuant</a> is backed by our numerical library (<a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a>), 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):
<pre class="toolbar-overlay:false show-lang:2 lang:java decode:true">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 &lt; = k; ++i) {
loadings.add(pca.loading(i));
}
Matrix PHI = cbind(loadings); // N by k
Matrix D = Y.multiply(PHI); // T by k
</pre>
<h1 id="QuickStrategyPrototyping">Quick Strategy Prototyping</h1>
With <a href="http://numericalmethod.com/up/algoquant/" target="_blank">AlgoQuant</a>, coding strategies for simulation can be done with minimal effort. A strategy class just needs to implement an interface, <a href="http://redmine.numericalmethod.com/projects/public/repository/svn-algoquant/entry/core/src/main/java/com/numericalmethod/algoquant/execution/strategy/Strategy.java" target="_blank">Strategy</a>. Simulation in <a href="http://numericalmethod.com/up/algoquant/" target="_blank">AlgoQuant</a> 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 <a href="http://redmine.numericalmethod.com/projects/public/repository/svn-algoquant/entry/core/src/main/java/com/numericalmethod/algoquant/model/infantino2010/strategy/Infantino2010Strategy.java" target="_blank">Infantino2010Strategy</a> which makes trade descisions upon price updates:
<pre class="toolbar-overlay:false show-lang:2 lang:java decode:true">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);
}
}
</pre>
<h1 id="ConvenientDataLoading">Convenient Data Loading</h1>
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.
<pre class="toolbar-overlay:false show-lang:2 lang:java decode:true">// 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);
</pre>
<h1 id="SimpleSimulationSetup">Simple Simulation Setup</h1>
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:
<pre class="toolbar-overlay:false show-lang:2 lang:java decode:true">new SimpleSimulatorBuilder().withDepthUpdates(caches).useExchangeRates(rates).synchronizeDepthUpdates().build();
</pre>
<h1 id="FlexibleExtendiblePerformanceMeasuresforAnalysis">Flexible &amp; Extendible Performance Measures for Analysis</h1>
There are plenty of performance measures available for you to analyze the performance of your strategy (see <a href="http://redmine.numericalmethod.com/projects/public/repository/svn-algoquant/show/core/src/main/java/com/numericalmethod/algoquant/execution/performance" target="_blank">performance package</a> for more details). You can also conveniently develop your own set of measures.
<pre class="toolbar-overlay:false show-lang:2 lang:java decode:true">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
</pre>
<h1 id="ParallelizedSensitivityAnalysisandMonteCarloSimulation">Parallelized Sensitivity Analysis and Monte Carlo Simulation</h1>
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 <a href="http://redmine.numericalmethod.com/projects/public/repository/svn-algoquant/entry/core/src/main/java/com/numericalmethod/algoquant/execution/simulation/batch/sensitivity/SensitivityAnalyzer.java" target="_blank">SensitivityAnalyzer</a> in <a href="http://numericalmethod.com/up/algoquant/" target="_blank">AlgoQuant</a>, you can find the answer in plots with a few lines of code (see <a href="http://redmine.numericalmethod.com/projects/public/repository/svn-algoquant/entry/core/src/main/java/com/numericalmethod/algoquant/model/infantino2010/strategy/Infantino2010ParamAnalysisDemo.java" target="_blank">Infantino2010ParamAnalysis</a>):
<pre class="toolbar-overlay:false show-lang:2 lang:java decode:true">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);
</pre>
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 <a href="http://redmine.numericalmethod.com/projects/public/repository/svn-algoquant/entry/core/src/main/java/com/numericalmethod/algoquant/execution/simulation/batch/montecarlo/MCSimulation.java" target="_blank">MCSimulation</a> with customizable price models.  

<a href="http://numericalmethod.com/wp-content/uploads/2015/04/CommmissionInfantino.png"><img class="alignnone wp-image-4684 size-full" src="http://numericalmethod.com/wp-content/uploads/2015/04/CommmissionInfantino.png" alt="CommmissionInfantino" width="680" height="420" /></a> 

<a href="http://numericalmethod.com/wp-content/uploads/2015/04/PnLAfterCommissionInfantino.png"><img class="alignnone wp-image-4685 size-full" src="http://numericalmethod.com/wp-content/uploads/2015/04/PnLAfterCommissionInfantino.png" alt="PnLAfterCommissionInfantino" width="680" height="420" /></a> 



Old New Date Created Author Actions
May 8, 2015 @ 02:18:03 henrymorco
May 8, 2015 @ 02:17:24 [Autosave] henrymorco
May 8, 2015 @ 01:54:09 henrymorco
May 7, 2015 @ 11:18:43 henrymorco
May 7, 2015 @ 11:18:03 henrymorco
May 7, 2015 @ 10:25:46 henrymorco
April 15, 2015 @ 15:29:19 webmaster
April 15, 2015 @ 15:29:18 [Autosave] webmaster
April 15, 2015 @ 15:24:53 webmaster
April 15, 2015 @ 15:23:05 webmaster
April 14, 2015 @ 06:27:20 sanjay
April 14, 2015 @ 06:26:29 [Autosave] sanjay
April 13, 2015 @ 13:24:49 sanjay
April 13, 2015 @ 12:00:53 sanjay
April 13, 2015 @ 11:59:21 sanjay
April 13, 2015 @ 08:00:20 sanjay