<commit_before># Standard library imports
import pandas as pd
import logging

import helper


##
# Algo base, for working with signals and other items
class Algo(object):

    def optimise(self):
        return None


##
# Client Algo, which uses a robinhood.Client object
class ClientAlgo(Algo):

    ##
    # Initialise with a client object
    # @client A robinhood.Client object
    def __init__(self, client):
        self.client = client


# A defined algo uses pre-determined symbols set to a pre-determined holding.
# Useful for holding symbols for long-term
class UniverseAlgo(Algo):

    def __init__(self, symbols=[], holding=1.0):
        self.symbols = symbols
        self.holding = holding / len(self.symbols)

    def optimise(self):
        portfolio = {symbol: self.holding for symbol in self.symbols}
        return pd.Series(portfolio)


##



##
# SharpeAlgo, which calculates the optimal Sharpe portfolio given a set of assets
class SharpeAlgo(Algo):

    ##
    # Initialise with a client object
    # @client A robinhood.Client object
    def __init__(self, client, lookback=21, min_lookback=7):
        self.client = client
        self.lookback = lookback
        self.min_lookback = min_lookback

    ##
    # Get all prices for the given universe
    # @returns A pandas dataframe of prices; vertical axis are dates and horizontal axis are symbols
    def prices(self, universe):
        prices = self.client.historical_prices(*universe).iloc[-self.lookback:]
        logging.info('Found prices %s - %s for %s', prices.index[0], prices.index[-1], ", ".join(list(prices.columns)))
        logging.debug(prices)
        return prices

    def weights(self, prices):
        target_weights = self._calculate_target_weights(prices)
        logging.info('Target weights: %s',
                     ', '.join(['{}: {:0.1f}%'.format(s, w * 100.0) for s, w in target_weights.iteritems()]))
        logging.debug(target_weights.round(2))
        return target_weights

    ##
    # Calculate the optimal Sharpe portfolio
    def _calculate_target_weights(self, prices):
        prices = prices.astype(float)
        best_weights, best_sharpe, best_days = None, 0.0, 0

        collected_weights = []

        while len(prices.index) >= self.min_lookback:
            weights, sharpe = self._calculate_target_weights_inner(prices)
            weights['(SHARPE)'] = sharpe<commit_msg>Make SharpeAlgo use ClientAlgo<commit_after># Standard library imports
import pandas as pd
import logging

import helper


##
# Algo base, for working with signals and other items
class Algo(object):

    def optimise(self):
        return None


##
# Client Algo, which uses a robinhood.Client object
class ClientAlgo(Algo):

    ##
    # Initialise with a client object
    # @client A robinhood.Client object
    def __init__(self, client):
        self.client = client


##
# A defined algo uses pre-determined symbols set to a pre-determined holding.
# Useful for holding symbols for long-term
class UniverseAlgo(Algo):

    def __init__(self, symbols=[], holding=1.0):
        self.symbols = symbols
        self.holding = holding / len(self.symbols)

    def optimise(self):
        portfolio = {symbol: self.holding for symbol in self.symbols}
        return pd.Series(portfolio)


##



##
# SharpeAlgo, which calculates the optimal Sharpe portfolio given a set of assets
class SharpeAlgo(ClientAlgo):

    ##
    # Initialise with a client object
    # @client A robinhood.Client object
    def __init__(self, client, lookback=21, min_lookback=7):
        super().__init__(client)
        self.lookback = lookback
        self.min_lookback = min_lookback

    ##
    # Get all prices for the given universe
    # @returns A pandas dataframe of prices; vertical axis are dates and horizontal axis are symbols
    def prices(self, universe):
        prices = self.client.historical_prices(*universe).iloc[-self.lookback:]
        logging.info('Found prices %s - %s for %s', prices.index[0], prices.index[-1], ", ".join(list(prices.columns)))
        logging.debug(prices)
        return prices

    def weights(self, prices):
        target_weights = self._calculate_target_weights(prices)
        logging.info('Target weights: %s',
                     ', '.join(['{}: {:0.1f}%'.format(s, w * 100.0) for s, w in target_weights.iteritems()]))
        logging.debug(target_weights.round(2))
        return target_weights

    ##
    # Calculate the optimal Sharpe portfolio
    def _calculate_target_weights(self, prices):
        prices = prices.astype(float)
        best_weights, best_sharpe, best_days = None, 0.0, 0

        collected_weights = []

        while len(prices.index) >= self.min_lookback:
            weights, sharpe = self._calculate_target_weights_inner(prices)
            weights['(SHARPE)'] = sharpe
{'fraction_non_alphanumeric': 0.1020703933747412, 'fraction_numerical': 0.007039337474120083, 'mean_word_length': 2.9892650701899255, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1898243', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1401, 'n_words': 415}