Hi guys,
This website has been very helpful for me in learning about trading algorithms. I am attempting to write code that buys SPY when the put call ratio of SPX is < 1 and sells SPY when the ratio is >= 1 but am having some difficulties. Can someone please help to guide me in the right direction?
Best,
Joe
##Testing algos referencing put/call ratios##
import numpy as np
import datetime
import pandas
def initialize(context):
# This is the search query we are using, this is tied to the csv file.
# User fetcher to get data. I uploaded this csv file manually, feel free to use.
context.query="SPX Put/Call Ratio"
url='https://dl.dropboxusercontent.com/s/4qaknmgfxbntz06/putcalltest.csv'
fetch_csv(url,
date_column = "Date",
date_format = "%m/%d/%Y",
symbol = context.query)
context.security = sid(8554) # S&P5000
# If the Put Call is below this value we buy. Otherwise we sell.
context.threshold = 1
def handle_data(context, data):
context.invested=False
#checks if put/call ratio is available for date
if context.query in data[context.query]:
indicator = (data[context.query][context.query])
if indicator < context.threshold:
order(context.security, 100)
context.invested = True
##this ensures that there is no shorting.
elif indicator >= context.threshold:
#and context.portfolio.positions[sid(8554)].amount >= 100:
order(context.security, -100)
context.invested = False
#record(data[context.query][context.query])