forex.models: 123 total statements, 100.0% covered

Generated: Fri 2015-05-29 12:29 AST

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

Stats: 112 executed, 0 missed, 11 excluded, 95 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):
  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. df['mid_price'] = (df['ask_price'] + df['bid_price']) / 2
  74. df = df.pivot(index='date', columns='symbol', values='mid_price')
  75. df = df.reindex(date_index)
  76. df = df.fillna(method="ffill")
  77. unlisted_symbols = list(set(symbols) - set(df.columns))
  78. for unlisted_symbol in unlisted_symbols:
  79. df[unlisted_symbol] = np.nan
  80. df = df[symbols]
  81. return df
  82. class CurrencyPrice(models.Model):
  83. """
  84. Represents a currency price to US
  85. """
  86. currency = models.ForeignKey(Currency)
  87. date = models.DateField()
  88. # Price Data per $1 of US
  89. ask_price = models.DecimalField(max_digits=20, decimal_places=PRICE_PRECISION,
  90. validators=[MinValueValidator(Decimal('0.00'))])
  91. bid_price = models.DecimalField(max_digits=20, decimal_places=PRICE_PRECISION,
  92. validators=[MinValueValidator(Decimal('0.00'))],
  93. blank=True, null=True)
  94. # Cached Data
  95. date_modified = models.DateTimeField(null=True, blank=True, editable=False, auto_now=True)
  96. date_created = models.DateTimeField(null=True, blank=True, editable=False, auto_now_add=True)
  97. # Add custom managers
  98. objects=CurrencyPriceManager()
  99. class Meta:
  100. verbose_name_plural = 'Currency Prices'
  101. verbose_name = 'Currency Price'
  102. ordering = ['date', ]
  103. unique_together=['date', 'currency']
  104. get_latest_by = "date"
  105. def __unicode__(self):
  106. return u'%s, %s' % (unicode(self.currency),
  107. unicode(self.date),)
  108. def save(self, *args, **kwargs):
  109. """
  110. Sanitation checks
  111. """
  112. if self.ask_price < 0:
  113. raise ValidationError("Ask price must be greater than zero")
  114. if self.bid_price < 0:
  115. raise ValidationError("Bid price must be greater than zero")
  116. if self.ask_price < self.bid_price:
  117. raise ValidationError("Ask price must be at least Bid price")
  118. super(CurrencyPrice, self).save(*args, **kwargs) # Call the "real" save() method.
  119. @property
  120. def mid_price(self):
  121. """
  122. Compute the mid point between the bid and ask prices
  123. """
  124. return (self.ask_price + self.bid_price) / Decimal('2.0')
  125. @property
  126. def spread(self):
  127. """
  128. Compute the difference between bid and ask prices
  129. """
  130. return (self.ask_price - self.bid_price)
  131. @property
  132. def ask_price_us(self):
  133. """
  134. Calculate the ask_price in USD. This is the inverse
  135. of the ask price.
  136. """
  137. if self.ask_price != 0:
  138. return 1 / Decimal(str(self.ask_price))
  139. else:
  140. raise ZeroDivisionError('Ask price is zero')
  141. @property
  142. def bid_price_us(self):
  143. """
  144. Calculate the bid_price in USD. This is the inverse
  145. of the bid price.
  146. """
  147. if self.bid_price != 0:
  148. return 1 / Decimal(str(self.bid_price))
  149. else:
  150. raise ZeroDivisionError('Bid price is zero')
  151. def convert_currency(from_symbol, to_symbol, value, date):
  152. """
  153. """
  154. if from_symbol == to_symbol:
  155. return value
  156. from_currency = Currency.objects.get(symbol=from_symbol)
  157. try:
  158. from_currency_price = CurrencyPrice.objects.get(currency=from_currency, date=date).mid_price
  159. except CurrencyPrice.DoesNotExist:
  160. print "Cannot fetch prices for %s on %s" % (str(from_currency), str(date))
  161. return None
  162. to_currency = Currency.objects.get(symbol=to_symbol)
  163. try:
  164. to_currency_price = CurrencyPrice.objects.get(currency=to_currency, date=date).mid_price
  165. except CurrencyPrice.DoesNotExist:
  166. print "Cannot fetch prices for %s on %s" % (str(to_currency), str(date))
  167. return None
  168. if type(value) == float:
  169. output = (value / float(from_currency_price)) * float(to_currency_price)
  170. elif type(value) == Decimal:
  171. output = Decimal(format((value / from_currency_price) * to_currency_price, '.%sf' % str(PRICE_PRECISION)))
  172. else:
  173. output = None
  174. return output