I used the example "multiple SID" algorithim with minor modifications. I'm a bit green when it comes to the stock market, so I'm wondering why I'm getting a large amount of profit after starting with 100k capital, despite having negative returns (and stocks?) Backtests I run show at least positive returns at points, but my recent live trading shows nothing but a steady decline, yet my portfolio's funds grow. Is it a bug in Quantopian?
# This example runs the same momentum play as the first sample
# (https://www.quantopian.com/help#basic-algo), but this time it uses more
# securities during the backtest.
# Important note: All securities in an algorithm must be traded for the
# entire length of the backtest. For instance, if you try to backtest both
# Google and Facebook against 2011 data you will get an error; Facebook
# wasn't traded until 2012.
# First step is importing any needed libraries.
import datetime
import pytz
def initialize(context):
# Here we initialize each stock. Note that we're not storing integers; by
# calling sid(123) we're storing the Security object.
context.stocks = [sid(3951), sid(7027), sid(351), sid(1234), sid(24)]
context.vwap = {}
context.price = {}
# Setting our maximum position size, like previous example
context.max_notional = 1000000.1
context.min_notional = -1000000.0
# initializing the time variables we use for logging
utc = pytz.timezone('UTC')
context.d=datetime.datetime(2000, 1, 1, 0, 0, 0, tzinfo=utc)
def handle_data(context, data):
# Initializing the position as zero at the start of each frame
notional=0
# This runs through each stock. It computes
# our position at the start of each frame.
money = []
for stock in context.stocks:
price = data[stock].price
money.append(price*context.portfolio.positions[stock].amount) # price*amount of shares
notional = notional + context.portfolio.positions[stock].amount * price
tradeday = data[stock].datetime
# This runs through each stock again. It finds the price and calculates
# the volume-weighted average price. If the price is moving quickly, and
# we have not exceeded our position limits, it executes the order and
# updates our position.
for stock in context.stocks:
vwap = data[stock].vwap(3)
price = data[stock].price
if price < vwap * 0.995 and notional > context.min_notional:
order(stock,+100)
notional = notional - price*100
elif price > vwap * 1.005 and notional < context.max_notional:
order(stock,-100)
notional = notional + price*100
# If this is the first trade of the day, it logs the notional.
if (context.d + datetime.timedelta(days=1)) < tradeday:
log.debug(str(notional) + ' - notional start ' + tradeday.strftime('%m/%d/%y'))
context.d = tradeday
record(Intel_money=money[0], Sonic_money=money[1], AMD_money=money[2], Cheesecake_money=money[3], Apple_money=money[4])