forex.models: 129 total statements, 95.8% covered

Generated: Mon 2015-06-08 11:27 AST

Source file: /home/kevin/Development/custom apps/valuehorizon-forex/forex/models.py

Stats: 113 executed, 5 missed, 11 excluded, 98 ignored

  1. from django.db import models
  2. from django.db.models import Manager
  3. from django.core.validators import MinValueValidator, ValidationError
  4. # Import misc packages
  5. import numpy as np
  6. from datetime import date, timedelta
  7. from decimal import Decimal
  8. from pandas import DataFrame, date_range
  9. PRICE_PRECISION = 4
  10. DATEFRAME_START_DATE = date(2005,1,1)
  11. class Currency(models.Model):
  12. """
  13. Represents a currency according to ISO 4217 standards.
  14. """
  15. name = models.CharField(max_length=255)
  16. symbol = models.CharField(max_length=10, unique=True)
  17. ascii_symbol = models.CharField(max_length=20, null=True, blank=True)
  18. num_code = models.IntegerField(null=True, blank=True)
  19. digits = models.IntegerField(null=True, blank=True) # Digits after decimal (minor unit)
  20. description = models.TextField(blank=True)
  21. # Cached Data
  22. date_modified = models.DateTimeField(null=True, blank=True, editable=False, auto_now=True)
  23. date_created = models.DateTimeField(null=True, blank=True, editable=False, auto_now_add=True)
  24. class Meta:
  25. verbose_name_plural = 'Currencies'
  26. verbose_name = 'Currency'
  27. ordering = ['name', 'symbol']
  28. def __unicode__(self):
  29. return u'%s, %s' % (unicode(self.name), unicode(self.symbol))
  30. def generate_dataframe(self, start_date=None, end_date=None):
  31. """
  32. """
  33. first_series_point = CurrencyPrice.objects.filter(currency=self)[0]
  34. last_series_point = CurrencyPrice.objects.filter(currency=self).reverse()[0]
  35. start_date = first_series_point.date if start_date == None else max(first_series_point.date, start_date)
  36. temp_start_date = start_date - timedelta(days=3) # Add lag
  37. end_date = last_series_point.date if end_date == None else min(last_series_point.date, end_date)
  38. currency_date = CurrencyPrice.objects.filter(currency=self, date__gte=temp_start_date, date__lte=end_date).values_list('date', 'ask_price', 'bid_price')
  39. currency_data_array = np.core.records.fromrecords(currency_date, names=['DATE', "ASK", "BID"])
  40. df = DataFrame.from_records(currency_data_array, index='DATE').astype(float)
  41. df['MID'] = (df['ASK'] + df['BID']) / 2.0
  42. df['CHANGE'] = df['MID'].pct_change()
  43. required_dates = date_range(start_date,end_date)
  44. df = df.reindex(required_dates)
  45. df = df.fillna(method='ffill')
  46. return df
  47. class CurrencyPriceManager(Manager):
  48. """ Adds some added functionality """
  49. def generate_dataframe(self, symbols=None, date_index=None, price_type="mid"):
  50. """
  51. Generate a dataframe consisting of the currency prices (specified by symbols)
  52. from the start to end date
  53. """
  54. # Set defaults if necessary
  55. if symbols == None:
  56. symbols = list(Currency.objects.all().values_list('symbol', flat=True))
  57. try:
  58. start_date = date_index[0]
  59. end_date = date_index[-1]
  60. except:
  61. start_date = DATEFRAME_START_DATE
  62. end_date = date.today()
  63. date_index = date_range(start_date, end_date)
  64. currency_price_data = CurrencyPrice.objects.filter(currency__symbol__in=symbols,
  65. date__gte=date_index[0],
  66. date__lte=date_index[-1]).values_list('date', 'currency__symbol', 'ask_price', 'bid_price')
  67. try:
  68. forex_data_array = np.core.records.fromrecords(currency_price_data, names=['date', 'symbol', 'ask_price', 'bid_price'])
  69. except IndexError:
  70. forex_data_array = np.core.records.fromrecords([(date(1900,1,1) , "", 0, 0)], names=['date', 'symbol', 'ask_price', 'bid_price'])
  71. df = DataFrame.from_records(forex_data_array, index='date')
  72. df['date'] = df.index
  73. if price_type == "mid":
  74. df['price'] = (df['ask_price'] + df['bid_price']) / 2
  75. elif price_type == "ask_price":
  76. df['price'] = df['ask_price']
  77. elif price_type == "bid_price":
  78. df['price'] = df['bid_price']
  79. else:
  80. raise ValueError("price_type must be on of 'ask', 'bid' or 'mid'")
  81. df = df.pivot(index='date', columns='symbol', values='price')
  82. df = df.reindex(date_index)
  83. df = df.fillna(method="ffill")
  84. unlisted_symbols = list(set(symbols) - set(df.columns))
  85. for unlisted_symbol in unlisted_symbols:
  86. df[unlisted_symbol] = np.nan
  87. df = df[symbols]
  88. return df
  89. class CurrencyPrice(models.Model):
  90. """
  91. Represents a currency price to US
  92. """
  93. currency = models.ForeignKey(Currency)
  94. date = models.DateField()
  95. # Price Data per $1 of US
  96. ask_price = models.DecimalField(max_digits=20, decimal_places=PRICE_PRECISION,
  97. validators=[MinValueValidator(Decimal('0.00'))])
  98. bid_price = models.DecimalField(max_digits=20, decimal_places=PRICE_PRECISION,
  99. validators=[MinValueValidator(Decimal('0.00'))],
  100. blank=True, null=True)
  101. # Cached Data
  102. date_modified = models.DateTimeField(null=True, blank=True, editable=False, auto_now=True)
  103. date_created = models.DateTimeField(null=True, blank=True, editable=False, auto_now_add=True)
  104. # Add custom managers
  105. objects=CurrencyPriceManager()
  106. class Meta:
  107. verbose_name_plural = 'Currency Prices'
  108. verbose_name = 'Currency Price'
  109. ordering = ['date', ]
  110. unique_together=['date', 'currency']
  111. get_latest_by = "date"
  112. def __unicode__(self):
  113. return u'%s, %s' % (unicode(self.currency),
  114. unicode(self.date),)
  115. def save(self, *args, **kwargs):
  116. """
  117. Sanitation checks
  118. """
  119. if self.ask_price < 0:
  120. raise ValidationError("Ask price must be greater than zero")
  121. if self.bid_price < 0:
  122. raise ValidationError("Bid price must be greater than zero")
  123. if self.ask_price < self.bid_price:
  124. raise ValidationError("Ask price must be at least Bid price")
  125. super(CurrencyPrice, self).save(*args, **kwargs) # Call the "real" save() method.
  126. @property
  127. def mid_price(self):
  128. """
  129. Compute the mid point between the bid and ask prices
  130. """
  131. return (self.ask_price + self.bid_price) / Decimal('2.0')
  132. @property
  133. def spread(self):
  134. """
  135. Compute the difference between bid and ask prices
  136. """
  137. return (self.ask_price - self.bid_price)
  138. @property
  139. def ask_price_us(self):
  140. """
  141. Calculate the ask_price in USD. This is the inverse
  142. of the ask price.
  143. """
  144. if self.ask_price != 0:
  145. return 1 / Decimal(str(self.ask_price))
  146. else:
  147. raise ZeroDivisionError('Ask price is zero')
  148. @property
  149. def bid_price_us(self):
  150. """
  151. Calculate the bid_price in USD. This is the inverse
  152. of the bid price.
  153. """
  154. if self.bid_price != 0:
  155. return 1 / Decimal(str(self.bid_price))
  156. else:
  157. raise ZeroDivisionError('Bid price is zero')
  158. def convert_currency(from_symbol, to_symbol, value, date):
  159. """
  160. """
  161. if from_symbol == to_symbol:
  162. return value
  163. from_currency = Currency.objects.get(symbol=from_symbol)
  164. try:
  165. from_currency_price = CurrencyPrice.objects.get(currency=from_currency, date=date).mid_price
  166. except CurrencyPrice.DoesNotExist:
  167. print "Cannot fetch prices for %s on %s" % (str(from_currency), str(date))
  168. return None
  169. to_currency = Currency.objects.get(symbol=to_symbol)
  170. try:
  171. to_currency_price = CurrencyPrice.objects.get(currency=to_currency, date=date).mid_price
  172. except CurrencyPrice.DoesNotExist:
  173. print "Cannot fetch prices for %s on %s" % (str(to_currency), str(date))
  174. return None
  175. if type(value) == float:
  176. output = (value / float(from_currency_price)) * float(to_currency_price)
  177. elif type(value) == Decimal:
  178. output = Decimal(format((value / from_currency_price) * to_currency_price, '.%sf' % str(PRICE_PRECISION)))
  179. else:
  180. output = None
  181. return output