All, I am getting started with Quantopian. Following is a code I wrote, but I have been unsuccessful to run it.
The starting idea is to screen for stocks which are in the range of $0.85 - $0.95 and purchase a quantity of 100 of each stock. The program needs to run every 60mins.
Also if the price is over $1.05 then sell it (need some guidance, how the algo will know about my holdings).
I would appreciate if you can review the code and point the errors. Many thanks in advance.
"""
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
def initialize(context):
"""
Called once at the start of the algorithm.
"""
#context.assets = USEquityPricing
# For every minute available (max is 6 hours and 30 minutes)
total_minutes = 6*60 + 30
# Create and attach an empty Pipeline.
pipe = Pipeline()
pipe = attach_pipeline(pipe, name='my_pipeline')
# Factor of current price (last traded price whether open hours or extended hours).
# >>> NOT WORKING : current_price = data.current(context.USEquityPricing, "last_traded")
current_price = USEquityPricing.close.latest
# Construct a Filter.
# Filter for stocks whose price is >=$0.85 but =<$0.95
goldilocks_temp = (current_price =< 0.95)
goldilocks = (goldilocks_temp >= 0.75)
# Register outputs.
pipe.add(goldilocks, 'goldilocks')
# Remove rows for which the Filter returns False.
pipe.set_screen(goldilocks)
return pipe
def handle_data(context,data):
"""
Called every 60minute.
"""
context.output = pipeline_output('my_pipeline')
for i in range(1, total_minutes):
Every 60 minutes run schedule
if i % 60 == 0:
# This will start at 9:31AM and will run every 60 minutes
schedule_function(
for s in context.output: order(s, 20),
date_rules.every_day(),
time_rules.market_open(minutes=i),
)
pass