Hi Dan,
I tried to code it with a random strategy. But I get this error:
TypeError: 'int' object is not callable
USER ALGORITHM:59, in handle_data
order(context.aapl,-quantity)
Can you please help me?
Thanks
Davide
from pytz import timezone
from datetime import datetime, timedelta
from zipline.utils.tradingcalendar import get_early_closes
from random import randint
def initialize(context):
context.aapl = sid(24)
# Initializing your start and end dates to check for early closes
start_date = context.aapl.security_start_date
end_date = context.aapl.security_end_date
context.early_closes = get_early_closes(start_date,end_date).date
# Minutes before close that you want to execute your order, so this will execute at 3:55 PM only
context.minutes_closeposition = 30
context.minutes_openposition = 0
def handle_data(context, data):
loc_dt = get_datetime().astimezone(timezone('US/Eastern'))
date = get_datetime().date()
# Checks for an early close on special dates such as holidays and the day after thanksgiving
# The market closes at 1:00PM EST on those days
if date in context.early_closes:
# Returns true if it's 1:00PM - minutes so in this case 12:54PM
if loc_dt.hour == 12 and loc_dt.minute == (59-context.minutes_closeposition):
log.debug(get_datetime())
order = 1
elif loc_dt.hour == 12 and loc_dt.minute == (59-context.minutes_openposition):
log.debug(get_datetime())
order = 2
else:
order = 0
# Returns true if it's 4:00PM EST - minutes so in this case at 3:54PM
# Daylight savings time are accounted for, so it will automatically adjust to DST
elif loc_dt.hour == 15 and loc_dt.minute == (59-context.minutes_closeposition):
log.debug(get_datetime())
order = 1
elif loc_dt.hour == 15 and loc_dt.minute == (59-context.minutes_openposition):
log.debug(get_datetime())
order = 2
else:
order = 0
side = randint(0,1)
price = data[context.aapl].price
position = context.portfolio.positions[context.aapl].amount
cost = context.portfolio.positions[context.aapl].cost_basis
quantity = int(100000/price)
if side == 0 and position == 0 and order == 2:
order(context.aapl,-quantity)
elif side == 1 and position == 0 and order == 2:
order(context.aapl,quantity)
# Take profits
profit = 0.015
if position 0 and price > cost * (1+profit):
order(context.aapl,-position)
# Stop loss
stop = 0.005
if position cost * (1+stop):
order(context.aapl,-position)
elif position > 0 and price 0 and order == 1:
order(context.aapl,-quantity)
elif position