# Strategic Portfolio Optimization Using Simulated, Digital, and Quantum Annealing
## 6 Portfolio Optimization Implementation
### Theoretical Remarks  
In this subsection we provide a theoretical analysis of the preprocessing steps as well as the QUBO model regarding scalability and complexity. First, we describe our preprocessing of the pairs trading strategy, which is the process of finding all stationary asset pairs of all available assets, see the pseudocode in Algorithm 1. ```
1:procedureFindStationaryPairs(assets)\(\triangleright\) list of assets containing price data
2:\(stationaryPairs=[]\)
3:for prices1 in assetsdo
4:for prices2 in assetsdo
5:if prices1 == prices2 then
6: continue
7:else
8:\(stationaryPair\), \(hedgeRatio\), \(hurstExponent=\) CadfTest(\(prices1\), \(prices2\))
9:if stationaryPair == True \(hurstExponent<0.4\)then
10: stationaryPairs.add((\(prices1\), \(prices2\), \(hedgeRatio\), \(hurstExponent\)))
11:endif
12:endif
13:endfor
14:endfor
15:return stationaryPairs
16:endprocedure
```  
**Algorithm 1** Pairs trading: find stationary pairs  
The procedure shown in Algorithm 1 has a time complexity of \(O(n^{2})\) since we need to iterate over each asset and then through each other asset. Next for preprocessing, we need to execute a backtest to see how well a pairs trading strategy performs on all stationary pairs. Therefore, the actual strategy illustrated in Algorithm 2 is executed on the same timeframe as the assets used for Algorithm 1. The time complexity for that procedure is \(O(n)\) since we only have to iterate over each timestep of the backtest period. Therefore, the overall time complexity of the pairs trading preprocessing is \(O(n^{2})\)```
1:procedurePairsTradingStrategy(stationaryPairs)
2:forpair in\(stationaryPair\)do
3:\(pairsTradingReturns=[]\)
4:\(capital=1\)
5:\(halfLife=\text{getHalHalLife($pair$.$prices1$,$ pair$.$prices2$)}\)
6:fori = 0; i \(<\text{length($prices1$)}-halfLife\); i++ do
7:\(spread=pair$.$prices1$-$(pair$.$.$hedgeRatio$\times pair$.$prices2$prices1$0i$prices1$1])\)
8:\(rollingWindow=pair$prices1$2prices1$prices1$3i$prices1$4+halfLife$prices1$5.$prices1$6[$prices1$7:(i$prices1$8)]\))
9:\(rollingMean=\text{mean}(rollingWindow)\)
10:\(rollingStd=\text{mean}(rollingWindow)\)
11:\(zScore=(spread-rollingMean)\) / \(rollingStd\)
12:if\(zScore>1.28\)then
13: Go LONG in \(pair$prices1$9asset1$ pair$0.$ pair$1\) and LONG in \(pair$ pair$2asset2\)
16:endif
17:if\(zScore<0.25\) LONG \(pair$ pair$3asset1$ pair$4.$ pair$5\)then
20: Close position and add to returns
21:endif
22:endfor
23:endfor
24:return\(pairsTradingReturns\)
25:endprocedure
```

**Algorithm 2** Pairs trading strategy

The trend-following strategy is executed on the same assets as the pairs trading strategy. The preprocessing procedure for the trend following strategy is described in Algorithm 3. It consists of two parts, first, for each asset we calculate the best lookback and holding period as well as their correlation coefficient as described in Section 6.2. Second, we execute the actual strategy based on the parameters found. The time complexity for that process is \(O(n)\) since we only have to iterate over each asset once. ```
1:procedureFindStationaryPairs(\(assets\))\(\triangleright\) list of assets containing price data
2:\(returns=[]\)
3:for\(prices\) in\(assets\)do
4:\(lookBackPeriod,holdingPeriod,corCoef=\text{GetLookbackHoldingPeriodsAndCor}\)Coef(\(prices\))
5:forprice in\(prices\)do
6:if\(corCoef>0\) (not LONG in asset \(|\) | not SHORT in asset) then
7: Go LONG in the current asset for the duration of the \(holdingPeriod\)
8:elseif\(corCoef<0\) (not LONG in asset \(|\) | not SHORT in asset) then
9: Go SHORT in the current asset for the duration of the \(holdingPeriod\)
10:endif
11:ifholdingPeriod is overthen
12: close position and add return to \(returns\)
13:endif
14:endfor
15:endfor
16:return\(returns\)
17:endprocedure
```

**Algorithm 3** Trend following: preprocessingNow we will give some theoretical remarks to our QUBO formulation, see Equation (24). The procedures will fill the matrix entirely because the covariance between each asset is needed.