Skip to content

Commit

Permalink
Converted field caching to properties
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinali1 committed May 24, 2015
1 parent 28e6c26 commit c4766f5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 54 deletions.
File renamed without changes.
22 changes: 22 additions & 0 deletions forex/migrations/0006_auto_20150524_1013.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('forex', '0005_auto_20150522_1402'),
]

operations = [
migrations.RemoveField(
model_name='currencyprices',
name='ask_price_us',
),
migrations.RemoveField(
model_name='currencyprices',
name='bid_price_us',
),
]
80 changes: 26 additions & 54 deletions forex/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
from django.db.models import Manager

# Import misc packages
import calendar as cal
import numpy as np
from datetime import date, datetime, timedelta
from datetime import date, timedelta
from decimal import Decimal
from pandas import DataFrame, date_range

Expand Down Expand Up @@ -34,24 +33,13 @@ def generate_dataframe(self, start_date=None, end_date=None):
"""
first_series_point = CurrencyPrices.objects.filter(currency=self)[0]
last_series_point = CurrencyPrices.objects.filter(currency=self).reverse()[0]
if start_date == None:
start_date = first_series_point.date
else:
start_date = max(first_series_point.date, start_date)
# Get a one day lag so the change wont be null
temp_start_date = start_date - timedelta(days=3)

if end_date == None:
end_date = last_series_point.date
else:
end_date = min(last_series_point.date, end_date)
start_date = first_series_point.date if start_date == None else max(first_series_point.date, start_date)
temp_start_date = start_date - timedelta(days=3) # Add lag
end_date = last_series_point.date if end_date == None else min(last_series_point.date, end_date)

currency_date = CurrencyPrices.objects.filter(currency=self,
date__gte=temp_start_date,
date__lte=end_date).values_list('date', 'ask_price', 'bid_price')
currency_date = CurrencyPrices.objects.filter(currency=self, date__gte=temp_start_date, date__lte=end_date).values_list('date', 'ask_price', 'bid_price')
currency_data_array = np.core.records.fromrecords(currency_date, names=['DATE', "ASK", "BID"])
df = DataFrame.from_records(currency_data_array, index='DATE')
df = df.astype(float)
df = DataFrame.from_records(currency_data_array, index='DATE').astype(float)
df['MID'] = (df['ASK'] + df['BID']) / 2.0
df['CHANGE'] = df['MID'].pct_change()

Expand All @@ -60,12 +48,6 @@ def generate_dataframe(self, start_date=None, end_date=None):

return df

def save(self, *args, **kwargs):
"""
Generates name and cached data
"""

super(Currency, self).save(*args, **kwargs) # Call the "real" save() method.


class CurrencyPricesManager(Manager):
Expand All @@ -81,23 +63,19 @@ def generate_dataframe(self, symbols=None, date_index = None):
if symbols == None:
symbols = Currency.objects.all().values_list('symbol')
try:
assert(date_index != None)
assert(len(date_index > 0))
assert date_index != None and len(date_index) > 0
except:
start_date = date(2005,1,1)
end_date = date.today()
date_index = date_range(start_date, end_date)

currency_price_data = CurrencyPrices.objects.filter(currency__symbol__in=symbols, date__in=date_index.tolist()).values_list('date', 'currency__symbol', 'ask_price')
try:
# Generate numpy array from queryset data
forex_data_array = np.core.records.fromrecords(currency_price_data, names=['date', 'symbol', 'ask_price'])
except IndexError:
# If there is no data, generate an empty array
forex_data_array = np.core.records.fromrecords([(date(1900,1,1) ,"",0)], names=['date', 'symbol', 'ask_price'])
df = DataFrame.from_records(forex_data_array, index='date')

# Create pivot table
df['date'] = df.index
df = df.pivot(index='date', columns='symbol', values='ask_price')

Expand All @@ -116,10 +94,6 @@ class CurrencyPrices(models.Model):
ask_price = models.DecimalField(max_digits=20, decimal_places=4,)
bid_price = models.DecimalField(max_digits=20, decimal_places=4, blank=True, null=True)

# Price Data US per 1 Unit of Currency
ask_price_us = models.DecimalField(max_digits=20, decimal_places=4, editable=False)
bid_price_us = models.DecimalField(max_digits=20, decimal_places=4, editable=False)

# Add custom managers
objects=CurrencyPricesManager()

Expand All @@ -138,33 +112,31 @@ def mid_price(self):
"""
Compute the mid point between bid and ask
"""
if self.ask_price != None and self.bid_price != None:
return (self.ask_price + self.bid_price) / Decimal('2.0')
else:
return None
return (self.ask_price + self.bid_price) / Decimal('2.0')

def save(self, *args, **kwargs):
@property
def ask_price_us(self):
"""
Generates name
Calculate the ask_price in USD. This is the inverse
of the ask price.
"""

if self.ask_price != None:
if self.ask_price != 0:
self.ask_price_us = 1 / Decimal(str(self.ask_price))
else:
raise ZeroDivisionError('Ask price is zero')
if self.ask_price != 0:
return 1 / Decimal(str(self.ask_price))
else:
raise ValueError('Ask price must be specified')
raise ZeroDivisionError('Ask price is zero')

if self.bid_price != None:
if self.bid_price != 0:
self.bid_price_us = 1 / Decimal(str(self.bid_price))
else:
raise ZeroDivisionError('Bid price is zero')
@property
def bid_price_us(self):
"""
Calculate the bid_price in USD. This is the inverse
of the bid price.
"""
if self.ask_price != 0:
return 1 / Decimal(str(self.bid_price))
else:
raise ValueError('Bid price must be specified')
super(CurrencyPrices, self).save(*args, **kwargs) # Call the "real" save() method.
raise ZeroDivisionError('Bid price is zero')




def convert_currency(from_symbol, to_symbol, value, date):
Expand Down

0 comments on commit c4766f5

Please sign in to comment.