I'm trying to use the following code as a trading strategy. Ex: if x and y are met, trade stock ABC. I keep getting this error and I think it is because my "spread" variable is in the form: 01-01-2001 00:00:00 UT 1234.567. Does anyone know how to fix this? Thank you.
import numpy as np
###################################################
### INITIALIZE ###
def initialize(context):
#context.stock2 = sid(8554)
#context.stock1 = sid(2174)
#main and best so far QQQ and QID
context.stock1 = sid(49077)
context.stock2 = sid(49076)
context.assets=[context.stock1, context.stock2]
#counter to keep track of the current positions, i.e. short or long
# 0 means NO OPEN POSITION, 1 means SHORT, 2 means LONG
context.status = 0
#counters to keep track of the open positions of each stock
#context.qty1 = 0
#context.qty2 = 0
#set cost per trade
set_commission(commission.PerTrade(cost=0))
"""
#schedule function
schedule_function(func=daily_run,
date_rule=date_rules.every_day(),
time_rule=time_rules.market_open(minutes=1),
half_days=True
)
"""
###################################################
### CORE FUNCTIONS ###
#def daily_run(context,data):
def handle_data(context,data):
#call the functions to calculate z-score
spread = get_spread(context,data)
#call the function for trading strategy
trading_strategy(context,data,spread)
#call the exit strategy function
exit_strategy(context,data,spread)
###################################################
### SUB FUNCTIONS ###
def get_spread(context,data):
#shorten variables of stocks, for simplicity
s1=context.stock1
s2=context.stock2
assets=context.assets
## Step 1: Save closing prices of previous 1 day
close_data = data.history(assets, 'close', 2, '1d')[:-1]
#close_data=close.iloc[0:1]
print(close_data)
print(close_data[s1])
# Step 2: Find current percentage gain on the day
price1=data.current(s1,'price')
price2=data.current(s2,'price')
percent1=(price1-close_data[s1])/close_data[s1]
percent2=abs((price2-close_data[s2])/close_data[s2])
print('Percent 1: ', percent1)
print('Percent 2: ', percent2)
## Step 3: Find current spread
spread=np.log(percent1)-np.log(percent2)
print('Spread',spread)
# Step 5: Plot and return "z-score"
record('percent1', percent1)
record('percent2', percent2)
#print('%1: ',percent1,'--%2: ',percent2)
return (spread)
def trading_strategy(context,data,spread):
# Step 1: Shorten the names of the variables for quick referencing
s1 = context.stock1
s2 = context.stock2
#Get current position sizes
context.qty1=context.portfolio.positions[s1].amount
context.qty2=context.portfolio.positions[s2].amount
print(spread)
# Step 2: Check conditions for SHORT & place the order
if (spread > 0) & (context.status==0):
## PLACE ORDER ##
order_target_percent(s1, -0.5) # Place an order for selling stock1
order_target_percent(s2, 0.5) # Place an order for buying stock2
## UPDATE COUNTER ##
context.status = 1 # The current status is "short the spread"
# Step 3: Check conditions for LONG & place the order
if (spread < 0) & (context.status==0):
## PLACE ORDER ##
order_target_percent(s1, 0.5) # Place an order for buying stock1
order_target_percent(s2, -0.5) # Place an order for selling stock2
## UPDATE COUNTER ##
context.status = 2 # The current status is "long the spread"
record('counter',context.status)
def exit_strategy(context,data,spread):
#know when to get out
#Shorten the names of the variables for quick referencing
s1 = context.stock1
s2 = context.stock2
#Get current position sizes
context.qty1=context.portfolio.positions[s1].amount
context.qty2=context.portfolio.positions[s2].amount
if context.status==0:
return
if (context.status==1) and (spread<=0):
order_target_value(s1, 0) # Place an order for getting out
order_target_value(s2, 0) # Place an order for getting out
context.status=0
if (context.status==2) and (spread>=0):
order_target_value(s1, 0) # Place an order for getting out
order_target_value(s2, 0) # Place an order for getting out
context.status=0