/python/algo_local/test_allocation.py
https://bitbucket.org/vroomzel/semester2 · Python · 68 lines · 38 code · 16 blank · 14 comment · 3 complexity · 2f0c9d7b460bfb1d7b8fc92a4b0717b5 MD5 · raw file
- import matplotlib
- matplotlib.use("TkAgg")
- import datetime as dt
- import pandas
- import numpy as np
- import os
- import matplotlib.pyplot as plt
- import order_execution
- import time as time
- import risk_budget
- import utils
- import AllocationDaily
- #################################################### DATA LOAD #######################
- #set time zone, otherwise dates are screwed
- os.putenv("TZ","America/New_York")
- #LOCAL TEST
- path=""
- #load data
- cp=pandas.load("adj_close_px_all.bintest")
- '''
- ############################################## Symbol set up ######################################
- #market column (later on, can add 'Market' column and use it if SPY is not working)
- market='SPY'
- #exclude market column and those for which there is no sentiment data
- #excluded_tickers=[market,'EEM','EWJ','FAS','FAZ','GLD','IWM','NOK','QQQ','RIMM','SLV','TLT','UNG','USO','XLE','XLF','LVLT','SIRI']
- excluded_tickers=[market,'EEM','EWJ','FAS','FAZ','GLD','IWM','QQQ','SLV','TLT','UNG','USO','XLE','XLF','LVLT','SIRI']
- tickers=cp.columns
- for symbol in excluded_tickers:
- tickers=tickers[tickers!=symbol]
- '''
- tickers=cp.columns
- ############################################### Day count set up ###################################
- #get all the start of days for which you have data skip the first day as it used to generate initial parameters
- day_length=389 #389 minutes in a day
- days_range=[]
- for day in np.arange(389,len(cp.index),day_length):
- days_range.append(cp.index[day])
- trade_freq=10 #minutes
- risk=AllocationDaily.make(tickers, 20, 25, trade_freq, 1000)
- tickers=cp.columns
- #go day by day
- for day in days_range: #allow at least b_window days to calculate betas
- #go minute by minute
- 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):
- returns=cp.ix[trade_min,tickers]/cp.ix[trade_min-pandas.DateOffset(minutes=trade_freq),tickers]-1
-
- #returns[np.random.randint(0,4)]=0
- #returns[np.random.randint(0,4)]=0
- #returns['SPY']=0
- shares_to_trade=risk.get_allocation_sharpe(returns)