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)  
Clone Algorithm
13
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 53dcbfe42b77ea0734fe50dd
There was a runtime error.

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)  
Clone Algorithm
13
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 53dd2d6f0755800733d50806
There was a runtime error.

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.

Clone Algorithm
6
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 56a14ea085e0ba1191428c35
There was a runtime error.