Hi all,
I am trying to implement a simple algorithm that buys/sells a random security based on a coin flip (aka Cramer's algorithm). How can I select a stock at random? The following code does not work, and errors out saying sid takes only one parameter.
import random
# Put any initialization logic here. The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
pass
# Will be called on every trade event for the securities you specify.
def handle_data(context, data):
# Implement your algorithm logic here.
# data[sid(X)] holds the trade event data for that security.
# data.portfolio holds the current portfolio state.
# Place orders with the order(SID, amount) method.
thestock = int(random.randint(1,200))
stock = sid(thestock)
coin = random.random()
if (coin < 0.5):
order(stock, 1)
else:
order(stock, -1)