Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How do you generate ideal buy/sell/hold signals which require looking at future data

I want to generate an 'idealsignal' for a stock and display it on a plot along with the stock price
And I am pretty new to writing quantopian algorithms

I am trying to do it using @batch_transform
All the examples of transforms I have seen use past data to calculate new transforms.
Such as a MovingAverage() uses mean() on the past N bars that is passed to it.

My idealsignal() should return 1, -1 and 0 based on the future N bars and a minimum profit percentage P
profit=((future_price-current_price)/current_price)*100
if profit > P: # price appreciates by more than P% within the next N bars
return 1
elif profit < -P: # price depreciates by more than -P% within the next N bars
return -1
else:
return 0 # hold otherwise

I want to be able to generate and see this signal on the graph.

From what I can tell, all of quantopian algorithm try to not show future data, which sort makes sense.
But when training model it I need to train on past data, but use ideal signals which essentially use future bars from within this historical/past price or other data.

OR

may be what I need to do is generate ideal signals in the past based on current bars which also I am not sure how to do.

Thx
Sarvi

5 responses

Hello Sarvi,

It is correct that by design, future data are not accessible (beyond the datetime of the current bar). However, a trailing window of data is accessible (either via the batch transform, or by accumulating it yourself, e.g. in a custom variable such as context.trailing_window). You can then apply your idealsigna() function over that window of data to generate a vector of signals (with element values of 1, -1, or 0).

For plotting, see the help documentation on the record function, which allows plotting of up to 5 variables versus time.

Grant

@Sarvi, the method you described is categorized as "supervised machine learning". Quantopian is a backtester and isn't really set up for that.

You can get daily data from Yahoo or 5 minute data from Stooq and do your supervised learning offline. Once you have a trained model you can use the indicator it generates to build a trading algorithm on Quantopian.

Using the backtester to train your model is not very efficient, in my opinion.

Be sure to pick a different historical time period for your offline learning and validation. Simlilarly you want to pick separate time periods to build and validate your trading algorithm.

I would really appreciate some sample code

The following is an example that calculates 20 day mean and I think I understand this. What it seems to return is a single value.
But that is accumulated into pandas datafram every R_P=0 meaning every bar. W_L=20 makes sure the last 20 values of the datapanel is available for this transform

@batch_transform(refresh_period=0, window_length=10) #set globals R_P & W_L above  
def get_avg(datapanel,sid):  
    prices = datapanel['price']  
    avg = prices[sid].mean()  
    return avg  

When I tried the following and did a record() to get it displayed along with the magnified price-change. The signal is a lagging indicator. Meaning
the signal goes +1, -1, 0 after has seen P% drop/gain, NOT before which is what I want to achieve.
The trading software I used to work on Trading Solutions used to work on had a way for me to design a ideal signal by looking ahead and deciding what my most ideal buy/sell/hold signals were. I could then use it to train machine learning algorithms

@batch_transform(refresh_period=0, window_length=20) #set globals R_P & W_L above  
def getsignal(datapanel,sid,profit):  
    prices = datapanel['price']  
    p = 100*(prices[sid][-1]-prices[sid][-20])/prices[sid][-20]  
    if p>profit:  
        sig=1  
    elif p<-profit:  
        sig=-1  
    else:  
        sig=0  
    return sig  

Thx,
Sarvi

Adding a backtest

Did you have code for ideal buy/sell/hold signals? I was just checking to see if you were able to generate signal data