PageRenderTime 22ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/python/algo_local/test_allocation.py

https://bitbucket.org/vroomzel/semester2
Python | 68 lines | 62 code | 3 blank | 3 comment | 0 complexity | 2f0c9d7b460bfb1d7b8fc92a4b0717b5 MD5 | raw file
  1. import matplotlib
  2. matplotlib.use("TkAgg")
  3. import datetime as dt
  4. import pandas
  5. import numpy as np
  6. import os
  7. import matplotlib.pyplot as plt
  8. import order_execution
  9. import time as time
  10. import risk_budget
  11. import utils
  12. import AllocationDaily
  13. #################################################### DATA LOAD #######################
  14. #set time zone, otherwise dates are screwed
  15. os.putenv("TZ","America/New_York")
  16. #LOCAL TEST
  17. path=""
  18. #load data
  19. cp=pandas.load("adj_close_px_all.bintest")
  20. '''
  21. ############################################## Symbol set up ######################################
  22. #market column (later on, can add 'Market' column and use it if SPY is not working)
  23. market='SPY'
  24. #exclude market column and those for which there is no sentiment data
  25. #excluded_tickers=[market,'EEM','EWJ','FAS','FAZ','GLD','IWM','NOK','QQQ','RIMM','SLV','TLT','UNG','USO','XLE','XLF','LVLT','SIRI']
  26. excluded_tickers=[market,'EEM','EWJ','FAS','FAZ','GLD','IWM','QQQ','SLV','TLT','UNG','USO','XLE','XLF','LVLT','SIRI']
  27. tickers=cp.columns
  28. for symbol in excluded_tickers:
  29. tickers=tickers[tickers!=symbol]
  30. '''
  31. tickers=cp.columns
  32. ############################################### Day count set up ###################################
  33. #get all the start of days for which you have data skip the first day as it used to generate initial parameters
  34. day_length=389 #389 minutes in a day
  35. days_range=[]
  36. for day in np.arange(389,len(cp.index),day_length):
  37. days_range.append(cp.index[day])
  38. trade_freq=10 #minutes
  39. risk=AllocationDaily.make(tickers, 20, 25, trade_freq, 1000)
  40. tickers=cp.columns
  41. #go day by day
  42. for day in days_range: #allow at least b_window days to calculate betas
  43. #go minute by minute
  44. for trade_min in pandas.DateRange(dt.datetime.combine(day.date(),dt.time(10,0)),dt.datetime.combine(day.date(),dt.time(15,58)),offset=pandas.datetools.Minute()*trade_freq):
  45. returns=cp.ix[trade_min,tickers]/cp.ix[trade_min-pandas.DateOffset(minutes=trade_freq),tickers]-1
  46. #returns[np.random.randint(0,4)]=0
  47. #returns[np.random.randint(0,4)]=0
  48. #returns['SPY']=0
  49. shares_to_trade=risk.get_allocation_sharpe(returns)