Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
> Please help make it to work: Buy and Sell if certain Criteria is met.

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

1 response
goldilocks_temp = (current_price =< 0.95)  
    goldilocks = (goldilocks_temp >= 0.75)  

makes no sense,

goldilocks_temp evalues as a true/false
goldilocks then = (true/false >=0.75) which can never work

Try

goldilocks_high_limit = (current_price <= 0.95) #this is either true or false  
goldilocks_low_limit = (current_price >=  0.75) #this is either true or false

goldilocks = (goldilocks_high_limit & goldilocks_low_limit) #this is true when both are true and false otherwise

also, I think you have it right to say pipe.set_screen(goldilocks) but I don't think pipe.add(goldilocks, 'goldilocks') works because goldilocks is a true/false statement. I'm not that great at making pipelines though, just OK with boolean logic. See this example that someone else posted:

def initialize(context):  
    # Create, register and name a pipeline in initialize.  
    pipe = Pipeline()  
    attach_pipeline(pipe, 'dollar_volume_10m_pipeline')

    # Construct a 100-day average dollar volume factor and add it to the pipeline.  
    dollar_volume = AverageDollarVolume(window_length=100)  
    pipe.add(dollar_volume, 'dollar_volume')

    #Create high dollar-volume filter to be the top 2% of stocks by dollar volume.  
    high_dollar_volume = dollar_volume.percentile_between(99, 100)  
    # Set the screen on the pipelines to filter out securities.  
    pipe.set_screen(high_dollar_volume)

The screen is high_dollar_volume, but first you have to add a universe to the pipe to filter, dollar_volume in this case. otherwise it's just trying to filter your filter which doesn't make sense at all