I have two problems with this algo that I will get to shortly.
My goal with this algo is to short two leveraged ETFs, the bull and bear on the same index. So basically the opposites of each other. It will short on the market open and buy back on the market close.
The code was working and it no longer works.
When it was working it showed a very negative return for shorting both ETFs, as a result I changed the program to go long on both ETFs. It still showed a very negative return. Did I write something wrong? Both results should not be negative.
import pytz
# Initialize the timezone:
EST = pytz.timezone('US/Eastern')
# Put any initialization logic here. The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
context.morning_ordered = False
context.afternoon_ordered = False
context.current_day = None
# Will be called on every trade event for the securities you specify.
def handle_data(context, data):
# timestamp of current event
UTCdate = get_datetime()
ESTdate = get_datetime().astimezone(EST)
log.info("Time is %s" % str(ESTdate))
today = ESTdate.date()
if today != context.current_day:
context.morning_ordered = False
context.afternoon_ordered = False
context.current_day = today
if ESTdate.hour == 9 and ESTdate.minute <= 59 and context.morning_ordered == False:
log.info("Executing morning orders.")
order(sid(32270), -100)
order(sid(32382), -100)
context.morning_ordered = True
elif ESTdate.hour == 15 and ESTdate.minute >= 0 and context.afternoon_ordered == False:
log.info("Executing afternoon orders.")
order(sid(32270), 100)
order(sid(32382), 100)
context.afternoon_ordered = True