Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How do i get my program to check security signals periodically every 2 seconds?

My goal is to have 2 variables, where every 2 seconds they switch off to check the signal. So that if the
signal changes the program can calculate the difference between the value of the original signal versus
the new value of the a new signal. So my question is, what library does Quantopian offer us and what is the best way to write this program with Quantopian's API? Thanks in Advance

7 responses

Hi Isaac,

Quantopian backtesting and live trading effectively run on a minute-period clock, so you won't be able to run an algorithm at the second timescale. Would your algo work running on minute bars?

Grant

Yes, in a sense where i would compare the signal value from the previous minute with the current signal.
Do you know the best method/library that would allow me to do this

You might consider using the 'history' method (see the help page) and then applying .pct_change() to the resulting Pandas DataFrame (see http://pandas.pydata.org/pandas-docs/stable)./computation.html).

def initialize(context):  
    context.stocks = [sid(8554),sid(33652)]

def handle_data(context, data):  
    price_change = history(390,'1m','price').pct_change()  
    print price_change.tail(3)  

Okay a couple points:
1. Im only checking signals for one stock and i am just comparing the signal for that one stock for the current minute to the previous minute.
2. Also, I'm not sure how to check for change in percentage.
Essentially, I am checking to see if there is a sudden signal change in a small time period and then buy or sell to take advantage of that volatility.
I tried something like this pretty quickly:
if price_change >= .10 and notional < (context.max_notional - context.target):
order_value(context.stock, context.target)
But im not sure if i did the right thing for the percentage.
Isaac

Isaac,

Here's an example. Is this what your are looking to do?

Grant

def initialize(context):  
    context.stock = sid(8554)  
    # for development, turn off commissions & slippage  
    set_commission(commission.PerTrade(cost=0.0))  
    set_slippage(slippage.FixedSlippage(spread=0.00))

def handle_data(context, data):  
    price_change = history(2,'1m','price').pct_change().iloc[-1]  
    if price_change > 0.001:  
        order_target_percent(context.stock,1)  
    elif price_change < -0.001:  
        order_target_percent(context.stock,0)  

Hi Grant,

I know the above code is old and API change but I am trying to get my head around why the following line of code is failing:
if price_change > 0.001:

with the following exception message ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

The price_change is of type float when I debug it

Thanks

@JohnSam - The price_changeas defined in line 10 is a pandas.Series of length 1. Replace it with float(price_change) in lines 12 and 14.