Sell after a given time interval

Dear all,

First I would like to say that I enjoy quantopian very much. I think it is a fascinating idea and the implementation of the IDE and backtester is very well done!

I am new to algorithmic trading and I have some basic questions about pitfalls, in particular in the light of global variables.

I would like to design an algorithm that buys stocks, holds them for a given fixed time and then sells them. This means, a function will decide at the point the item is bought also the time it is sold.

I have attached a template which illustrates how I imagine this could be done, but I would be interested in possible better ways of doing this. In particular, I have to introduce a global variable which keeps track of all the stock that is in the portfolio with a date when to sell it.

Are there any concerns using global variables for such long running jobs? Is there a built in global variable that I should use instead? The goal is live trading - is there any pitfalls with this approach?

best regards
Wolfgang

17
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
 Returns 1 Month 3 Month 6 Month 12 Month
 Alpha 1 Month 3 Month 6 Month 12 Month
 Beta 1 Month 3 Month 6 Month 12 Month
 Sharpe 1 Month 3 Month 6 Month 12 Month
 Sortino 1 Month 3 Month 6 Month 12 Month
 Volatility 1 Month 3 Month 6 Month 12 Month
 Max Drawdown 1 Month 3 Month 6 Month 12 Month
import datetime

# a global variable is defined to store the dates at which stocks should be sold.
globaldata = {}

def initialize(context):
context.security = symbol('AAPL')
# this dictionary uses sid as key and stores either: -1 which means we don't own any of it,
# or a date at which it should be sold.
globaldata[context.security.symbol] = -1

def handle_data(context, data):
# This is a possible template for an algorithm which buys at a given date
# and sells after a given time. These calculations will be done in the function
# analyse(sid) which gives back the number of days after which sid should be sold.

exchange_time = get_datetime()
if globaldata[context.security.symbol] == -1:
#analyse does all the work in deciding whether sid should be bought and when to sell it
order_percent(context.security, percentage)
globaldata[context.security.symbol] = exchange_time +  datetime.timedelta(days=sellafter)
else:
# if we do have sid lets see if its time to sell it.
if exchange_time > globaldata[context.security.symbol]:
order_target(context.security, 0)
globaldata[context.security.symbol] = -1

record(stock_price=data[context.security].price)

def analyse(sid):
# This function will contain the analysis of the current market situation.
# It returns three values:
# float percentage : how much should we buy
# int sellafter: sell it after a fixed number of days

#for illustration this is set to constants
return True,0.5,50

There was a runtime error.
2 responses

To my knowledge maintaining state in a global collection is the only way to manage conditional changes over time. I would point out that using a fixed number of periods would give you a calendar based offset which would not reflect actual trading periods transpired. 50 days calendar would end up being perhaps only 35 days trading. Incrementing or decrementing a state counter would give you a more accurate trading periods lapsed count.

If you are using a period count exit for testing of "edge" then you would want to use actual trading period counts and not calendar durations.

Hi Wolfgang,

Is there a reason that you're using a global variable versus 'context'? Here's an example with context

import datetime

def initialize(context):
context.security = symbol('AAPL')
# this dictionary uses sid as key and stores either: -1 which means we don't own any of it,
# or a date at which it should be sold.
context.global_data = {context.security.symbol : -1}
def handle_data(context, data):
# This is a possible template for an algorithm which buys at a given date
# and sells after a given time. These calculations will be done in the function
# analyse(sid) which gives back the number of days after which sid should be sold.
exchange_time = get_datetime()
if context.global_data[context.security.symbol] == -1:
#analyse does all the work in deciding whether sid should be bought and when to sell it
order_percent(context.security, percentage)
context.global_data[context.security.symbol] = exchange_time +  datetime.timedelta(days=sellafter)
else:
# if we do have sid lets see if its time to sell it.
if exchange_time > context.global_data[context.security.symbol]:
order_target(context.security, 0)
context.global_data[context.security.symbol] = -1

record(stock_price=data[context.security].price)

def analyse(sid):
# This function will contain the analysis of the current market situation.
# It returns three values: