forex/models.py
def generate_dataframe(self, symbols=None, date_index=None, price_type="mid"):
        """
        Generate a dataframe consisting of the currency prices (specified by symbols)
        from the start to end date
        """

        # Set defaults if necessary
        if symbols is None:
            symbols = list(Currency.objects.all().values_list('symbol', flat=True))
        try:
            start_date = date_index[0]
            end_date = date_index[-1]
        except:
            start_date = DATEFRAME_START_DATE
            end_date = date.today()
        date_index = date_range(start_date, end_date)

        currency_price_data = CurrencyPrice.objects.filter(currency__symbol__in=symbols,
                                                           date__gte=date_index[0],
                                                           date__lte=date_index[-1]).values_list('date', 'currency__symbol', 'ask_price', 'bid_price')
        try:
            forex_data_array = np.core.records.fromrecords(currency_price_data, names=['date', 'symbol', 'ask_price', 'bid_price'])
        except IndexError:
            forex_data_array = np.core.records.fromrecords([(date(1900, 1, 1), "", 0, 0)], names=['date', 'symbol', 'ask_price', 'bid_price'])
        df = DataFrame.from_records(forex_data_array, index='date')
        df['date'] = df.index

        if price_type == "mid":
            df['price'] = (df['ask_price'] + df['bid_price']) / 2
        elif price_type == "ask":
            df['price'] = df['ask_price']
        elif price_type == "bid":
            df['price'] = df['bid_price']
        else:
            raise ValueError("Incorrect price_type (%s) must be on of 'ask', 'bid' or 'mid'" % str(price_type))

        df = df.pivot(index='date', columns='symbol', values='price')
        df = df.reindex(date_index)
        df = df.fillna(method="ffill")
        unlisted_symbols = list(set(symbols) - set(df.columns))
        for unlisted_symbol in unlisted_symbols:
            df[unlisted_symbol] = np.nan
        df = df[symbols]

        return df
def generate_dataframe(self, symbols=None, date_index=None, price_type="mid"):
        """
        Generate a dataframe consisting of the currency prices (specified by symbols)
        from the start to end date
        """

        # Set defaults if necessary
        if symbols is None:
            symbols = list(Currency.objects.all().values_list('symbol', flat=True))
        try:
            start_date = date_index[0]
            end_date = date_index[-1]
        except:
            start_date = DATEFRAME_START_DATE
            end_date = date.today()
        date_index = date_range(start_date, end_date)

        currency_price_data = CurrencyPrice.objects.filter(currency__symbol__in=symbols,
                                                           date__gte=date_index[0],
                                                           date__lte=date_index[-1]).values_list('date', 'currency__symbol', 'ask_price', 'bid_price')
        try:
            forex_data_array = np.core.records.fromrecords(currency_price_data, names=['date', 'symbol', 'ask_price', 'bid_price'])
        except IndexError:
            forex_data_array = np.core.records.fromrecords([(date(1900, 1, 1), "", 0, 0)], names=['date', 'symbol', 'ask_price', 'bid_price'])
        df = DataFrame.from_records(forex_data_array, index='date')
        df['date'] = df.index

        if price_type == "mid":
            df['price'] = (df['ask_price'] + df['bid_price']) / 2
        elif price_type == "ask":
            df['price'] = df['ask_price']
        elif price_type == "bid":
            df['price'] = df['bid_price']
        else:
            raise ValueError("Incorrect price_type (%s) must be on of 'ask', 'bid' or 'mid'" % str(price_type))

        df = df.pivot(index='date', columns='symbol', values='price')
        df = df.reindex(date_index)
        df = df.fillna(method="ffill")
        unlisted_symbols = list(set(symbols) - set(df.columns))
        for unlisted_symbol in unlisted_symbols:
            df[unlisted_symbol] = np.nan
        df = df[symbols]

        return df
def generate_dataframe ( self , symbols = None , date_index = None , price_type = "mid" ) : # Set defaults if necessary if symbols is None : symbols = list ( Currency . objects . all ( ) . values_list ( 'symbol' , flat = True ) ) try : start_date = date_index [ 0 ] end_date = date_index [ - 1 ] except : start_date = DATEFRAME_START_DATE end_date = date . today ( ) date_index = date_range ( start_date , end_date ) currency_price_data = CurrencyPrice . objects . filter ( currency__symbol__in = symbols , date__gte = date_index [ 0 ] , date__lte = date_index [ - 1 ] ) . values_list ( 'date' , 'currency__symbol' , 'ask_price' , 'bid_price' ) try : forex_data_array = np . core . records . fromrecords ( currency_price_data , names = [ 'date' , 'symbol' , 'ask_price' , 'bid_price' ] ) except IndexError : forex_data_array = np . core . records . fromrecords ( [ ( date ( 1900 , 1 , 1 ) , "" , 0 , 0 ) ] , names = [ 'date' , 'symbol' , 'ask_price' , 'bid_price' ] ) df = DataFrame . from_records ( forex_data_array , index = 'date' ) df [ 'date' ] = df . index if price_type == "mid" : df [ 'price' ] = ( df [ 'ask_price' ] + df [ 'bid_price' ] ) / 2 elif price_type == "ask" : df [ 'price' ] = df [ 'ask_price' ] elif price_type == "bid" : df [ 'price' ] = df [ 'bid_price' ] else : raise ValueError ( "Incorrect price_type (%s) must be on of 'ask', 'bid' or 'mid'" % str ( price_type ) ) df = df . pivot ( index = 'date' , columns = 'symbol' , values = 'price' ) df = df . reindex ( date_index ) df = df . fillna ( method = "ffill" ) unlisted_symbols = list ( set ( symbols ) - set ( df . columns ) ) for unlisted_symbol in unlisted_symbols : df [ unlisted_symbol ] = np . nan df = df [ symbols ] return df
Generate a dataframe consisting of the currency prices (specified by symbols)
        from the start to end date
Generate a dataframe consisting of the currency prices ( specified by symbols ) from the start to end date