I have following Classic Algorithm that hourly rebalances the portfolio with setHoldings. I would like to convert this into a Framework algorithm so that I can benefit from the more advanced execution models (such as stdev, wvap) that are already available there for plugging into other portfolio balancing samples (uniform, mean-variance, black-litterman) within the framework. I would be more than glad if someone from the community can help me in achieving that.
import io, requests
import numpy as np
import pandas as pd
from datetime import timedelta
assets = ['AUDUSD', 'EURUSD', 'GBPUSD', 'NZDUSD', 'USDCAD', 'USDCHF', 'USDJPY', 'USDNOK', 'USDSEK', 'USDSGD']
No_Channels = 10
Input_Size = 256
class BasicTemplateAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019,1,3) #Set Start Date
self.SetEndDate(2019,3,8) #Set End Date
self.SetCash(100000) #Set Strategy Cash
self.SetBrokerageModel(BrokerageName.OandaBrokerage)
for asset in assets: self.AddForex(asset, Resolution.Hour, Market.Oanda, True, 1.0)
self.History(512, Resolution.Hour)
def OnData(self, data):
cc = None
for asset in assets:
df = self.History([asset], timedelta(14), Resolution.Hour).loc[asset]
df = df['close'].resample('1H').interpolate(method='cubic')
if asset[-3:] != 'USD': df = 1.0 / df
df = np.log((df/df.shift(1)).tail(Input_Size))
cc = df if cc is None else: cc = pd.concat([cc, df], axis=1)
X = np.swapaxes(cc.values, 0, 1)
data = {'arr': X.reshape(-1).tolist()}
response = requests.post('https://api.server/model', json=data)
weights = np.array(response.json()['arr'])
self.Debug(weights)
for i, asset in enumerate(assets):
howmuch = weights[i] if asset[-3:] == 'USD' else (-1.0*weights[i])
self.SetHoldings(asset, howmuch)