Hello!
I'm new to using the Quantopian libraries, but I wanted to thank the development team and the users that has created such a rich experience. There are some great resources available, and I hope to be able to eventually contribute to the community as well. ^_^
I'm trying to make a simple strategy that looks at yesterday's price and compares it to the day before. If yesterday's close is 8% less than the day before it, place a market order to buy the security and sell it at either a 8% profit or 4% loss - whichever comes first.
So, if Tuesday the stock declines 8% from it's Monday close, place a market buy order to create a position. If the stock falls 4% afterwards, or rises 8%, close the position
I think that I've managed to get the buy condition correct, but I was wondering how I should create the exit strategy.
Thanks in advance for your help and understanding!
def initialize(context):
context.etf = symbol('TVIX')
def handle_data(context, data):
price_history = history(bar_count=2, frequency='1d', field='price')
for s in data:
yesterday_close = price_history[s][-2]
current_price = price_history[s][-1]
if current_price >= yesterday_close * 1.08:
order_target(s, 1)
if context.portfolio.pnl >= 1.08 or context.portfolio.pnl <= .92:
order_target(context.etf, 0)