def markowitz(self): if self.verbose: print('Optimizing portfolio...linear algebra is fun!!') mk = mojo.markowitz(self.portfolio) mk = mk.reshape(mk.shape[0], 1) #self.optimal = mk self.optimal = mojo.DataFrame(mk.T, columns=self.portfolio.columns.tolist()) if self.verbose: print( 'Master using it and you can have this... Portfolio optimized.' ) print(self.optimal.head())
def simulate(self, threshold=0.2, days=100, **options): on = options.pop('on', 'return') exclude = options.pop('exclude', []) iterations = options.pop('iterations', 10000) columns = list(self.portfolio.columns) for item in exclude: try: columns.remove(item) except: continue reduced_portfolio = self.portfolio[columns] probabilities = [] if self.verbose: print('Simulating...') print('Reticulatiing splines...') for column in reduced_portfolio.columns: out_dict = {'TKR': column} sim = mojo.simulate(self.portfolio[column], days, iterations) prob = mojo.probs_predicted(sim, threshold, on=on) breakeven = mojo.probs_predicted(sim, 0.0, on=on) roi = mojo.ROI(sim) EV = mojo.expected_value(sim) out_dict.update( {'Prob. ' + str(100 * threshold) + ' % Return': prob}) out_dict.update({'Breakeven': breakeven}) out_dict.update({'ROI': roi}) out_dict.update({'Expected Value': EV}) probabilities.append(out_dict) simulation = mojo.DataFrame(probabilities) simulation = simulation.set_index('TKR') if self.verbose: print('Simulation complete.') print(simulation) return simulation
def NCO(self, **options): mu = options.pop('mu', None) maxNumClusters = options.pop('max_clusters', 10) if maxNumClusters >= len(self.portfolio.columns): maxNumClusters = len(self.portfolio.columns) - 1 if self.verbose: print('Optimizing portfolio...linear algebra is fun!!') nco = mojo.optPort_nco(self.cov, mu=mu, maxNumClusters=maxNumClusters) self.nco = mojo.DataFrame(nco.T, columns=self.portfolio.columns.tolist()) if self.verbose: print( 'Master using it and you can have this... Portfolio optimized.' ) print(self.nco.head())
def trend(self, **options): span = options.pop('span', [3, 10, 1]) exclude = options.pop('exclude', []) output = mojo.DataFrame() columns = list(self.portfolio.columns) for item in exclude: try: columns.remove(item) except: continue reduced_portfolio = self.portfolio[columns] if len(self.portfolio.columns) > 0: if self.verbose: print('Reticulating splines...') if not isinstance(span, list): print('Error: Span must be a list') print('Setting span to default: [3, 10, 1]') span = [3, 10, 1] df_list = [] for column in reduced_portfolio.columns: out_trend = mojo.GetBinsFromTrend(self.portfolio[column], column, span=span) df_list.append(out_trend) output = mojo.merge_stocks(df_list) else: print('Grumble, Grumble') print('Portfolio requires at least one item.') return output
def hurst(self, **options): if self.verbose: print('Calculating Hurst Exponents...') lag1 = options.pop('lag1', 2) lag2 = options.pop('lag2', 20) H = [] for column in self.portfolio.columns: h0 = mojo.hurst(self.portfolio[column], lag1=lag1, lag2=lag2) H.append(h0) H = mojo.np.array(H) H = H.reshape(H.shape[0], 1) self.H = mojo.DataFrame(H.T, columns=self.portfolio.columns.tolist()) if self.verbose: print('Hurst Exponents.') print(self.H.head())