Skip to content

Commit

Permalink
ironing out the kinks in the sqlite caching functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
chinchalinchin committed Sep 13, 2021
1 parent ea5e07c commit 7cbab99
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 30 deletions.
7 changes: 0 additions & 7 deletions src/scrilla/analysis/markets.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,12 @@ def sharpe_ratio(ticker, start_date=None, end_date=None, risk_free_rate=None, ti
except errors.InputValidationError as ive:
raise ive

print('inside markets.sharpe_ratio')
result = profile_cache.filter_profile_cache(ticker=ticker, start_date=start_date, end_date=end_date)

print('result', result)
if result is not None and result[static.keys['STATISTICS']['SHARPE']] is not None:
return result[static.keys['STATISTICS']['SHARPE']]

if ticker_profile is None:
print('calcuatling rr from markets.sharpe_ratio')
ticker_profile = statistics.calculate_risk_return(ticker=ticker, start_date=start_date,
end_date=end_date)

Expand All @@ -59,7 +56,6 @@ def sharpe_ratio(ticker, start_date=None, end_date=None, risk_free_rate=None, ti

sharpe_ratio = (ticker_profile['annual_return'] - risk_free_rate)/ticker_profile['annual_volatility']

print('RRRRRRIGGGHHHHTTT FUCKING HERE!!!')
profile_cache.save_or_update_row(ticker=ticker, start_date=start_date,
end_date=end_date,sharpe_ratio=sharpe_ratio)

Expand Down Expand Up @@ -218,9 +214,6 @@ def cost_of_equity(ticker, start_date=None, end_date=None, market_profile=None,
except errors.APIResponseError as api:
raise api

print('prem', premium)
print('beta', beta)
print('risk_free', services.get_risk_free_rate())
equity_cost = (premium*beta + services.get_risk_free_rate())

profile_cache.save_or_update_row(ticker=ticker, start_date=start_date, end_date=end_date, equity_cost=equity_cost)
Expand Down
14 changes: 5 additions & 9 deletions src/scrilla/analysis/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,8 @@ def calculate_risk_return(ticker, start_date=None, end_date=None, sample_prices=
except errors.InputValidationError as ive:
raise ive

print('inside statistics.calculate_risk_return')
results = profile_cache.filter_profile_cache(ticker=ticker, start_date=start_date, end_date=end_date)

print('results', results)
if results is not None \
and results[static.keys['STATISTICS']['RETURN']] is not None \
and results[static.keys['STATISTICS']['VOLATILITY']] is not None:
Expand Down Expand Up @@ -689,16 +687,14 @@ def calculate_ito_correlation(ticker_1, ticker_2, asset_type_1=None, asset_type_
except errors.APIResponseError as api:
raise api

if (len(sample_prices[ticker_1]) == 0) \
or (len(sample_prices[ticker_2]) == 0) \
or (len(sample_prices[ticker_1]) != len(sample_prices[ticker_2])) \
or (len(helper.intersect_dict_keys(sample_prices[ticker_1], sample_prices[ticker_2])) == 0):
raise errors.PriceError("Prices cannot be retrieved for correlation calculation")

if asset_type_1 != asset_type_2:
# remove weekends and holidays from crypto prices so samples can be compared
sample_prices[ticker_1], sample_prices[ticker_2] = helper.intersect_dict_keys(sample_prices[ticker_1], sample_prices[ticker_2])


if (len(sample_prices[ticker_1]) == 0) \
or (len(sample_prices[ticker_2]) == 0):
raise errors.PriceError("Prices cannot be retrieved for correlation calculation")

if asset_type_1 == asset_type_2 and asset_type_1 == static.keys['ASSETS']['CRYPTO']:
trading_period = static.constants['ONE_TRADING_DAY']['CRYPTO']
else:
Expand Down
6 changes: 1 addition & 5 deletions src/scrilla/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def __init__(self):

@staticmethod
def to_dict(query_results):
return { static.keys['STATISTICS']['CORRELATION']: query_results[0] }
return { static.keys['STATISTICS']['CORRELATION']: query_results[0][0] }

def save_row(self, ticker_1, ticker_2, start_date, end_date, correlation):
logger.verbose(f'Saving ({ticker_1}, {ticker_2}) correlation from {start_date} to {end_date} to the cacche')
Expand Down Expand Up @@ -245,10 +245,6 @@ def filter_profile_cache(self, ticker, start_date, end_date):
formatter = { 'ticker': ticker, 'start_date': start_date, 'end_date': end_date}
result = self.execute_query(query=ProfileCache.profile_query, formatter=formatter)

print(result)
for r in result:
for p in r:
print(p)
if len(result)>0:
return self.to_dict(result)
else:
Expand Down
2 changes: 1 addition & 1 deletion src/scrilla/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def get_asset_type(symbol):
#return None
return static.keys['ASSETS']['EQUITY']
# default to equity for overlap until a better method is determined.
return None
return static.keys['ASSETS']['EQUITY']

def get_watchlist():
"""
Expand Down
12 changes: 6 additions & 6 deletions src/scrilla/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sys, os
import sys, os, traceback

# Note: need to import from package when running from wheel.
# if running locally through main.py file, these imports should be replaced
Expand Down Expand Up @@ -51,15 +51,15 @@ def validate_function_usage(selection, args, wrapper_function, required_length=1

# TODO: CLI APPLICATION ERROR HANDLING GOES HERE
except errors.PriceError as pe:
logger.comment(str(pe))
traceback.print_exc()
except errors.SampleSizeError as se:
logger.comment(str(se))
traceback.print_exc()
except errors.APIResponseError as api:
logger.comment(str(api))
traceback.print_exc()
except errors.InputValidationError as ive:
logger.comment(str(ive))
traceback.print_exc()
except errors.ConfigurationError as ce:
logger.comment(str(ce))
traceback.print_exc()

def do_program():
if len(sys.argv)>0:
Expand Down
4 changes: 2 additions & 2 deletions src/scrilla/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def slice_prices(self, start_date, end_date, asset_type, prices):
elif asset_type == static.keys['ASSETS']['CRYPTO']:
start_index = list(prices[settings.AV_RES_CRYPTO_FIRST_LAYER].keys()).index(start_string)
end_index = list(prices[settings.AV_RES_CRYPTO_FIRST_LAYER].keys()).index(end_string)
prices = dict(itertools.islice(prices[settings.AV_RES_CRYPTO_FIRST_LAYER].items(), end_index, start_index))
prices = dict(itertools.islice(prices[settings.AV_RES_CRYPTO_FIRST_LAYER].items(), end_index, start_index+1))
return prices

except KeyError as ke:
Expand Down Expand Up @@ -309,7 +309,7 @@ def get_daily_price_history(ticker, start_date=None, end_date=None, asset_type=N
(asset_type == static.keys['ASSETS']['EQUITY']
and (helper.business_days_between(start_date, end_date) + 1) == len(prices))
or
(asset_type == static.keys['ASSET']['CRYPTO']
(asset_type == static.keys['ASSETS']['CRYPTO']
and (helper.days_between(start_date, end_date) + 1) == len(prices))
):
return prices
Expand Down

0 comments on commit 7cbab99

Please sign in to comment.