Counting Positive Days

Hi,



I want to calculate number of updays in the past 1 year. I have coded as following using class. But i am not getting what I want. Can you please help me? 



After this I also want to calculate the same for last 1 year (252 days) excluding latest month (22 days).



class Positivedays(CustomFactor):

inputs = [EquityPricing.close]

def compute(self, today, assets, out, close):

for i in range(self.window_length-1):

diff = close[i+1]-close[i]

if (diff>0):

updays =+1

out[:] = updays



def make_pipeline(context):


Initialize the pipeline

pipe = Pipeline()

Construct Factors

plusdays = Positivedays(window_length=10)

pipe.add(plusdays, 'plus')

For all such cases you can use a generic pipeline factor that takes in a function. Like below

def technical_factor(lookback, indicator_fn, indicator_lookback):
    class TechnicalFactor(CustomFactor):
        inputs = [EquityPricing.close]
        def compute(self,today,assets,out,close_price):
            signals = np.apply_along_axis(
                indicator_fn, 0, close_price, indicator_lookback)
            out[:] = signals
    return TechnicalFactor(window_length = lookback)


Then define the factor you want. In this case for last n days up move count, you can define this as below

def up_days(px, lookback):
    return np.sum(np.where(np.diff(px) > 0, 1, 0))


Finally, call this in you pipeline constructions as below

def make_pipeline(context):
    # Initialize the pipeline
    pipe = Pipeline()
    plusdays = technical_factor(10+1, up_days, 0)
    pipe.add(plusdays, 'plus')
    return pipe

Note, we are incrementing 10 by 1 to take care of the np.diff call in the function. Also in the function up_days, we do not use the lookback.

For the case with 252 days with last 22 excluded, use 252+22 in place of 10 and subset the px in the function up_days to exclude last 22 values (px is a np.ndarray) before computing the count.

You can replace up_days with any function that takes in a px (np.ndarray) and a lookback (int) and returns a number using this generic pipeline function technical_factor.

Thank you for your reply. I got the result. The program looks elegant and can be used for many inicators. Thankyou so much.



But I would like to understand certain parts of the program. I have following questions.



1 -  def technical_factor(lookback, indicator_fn, indicator_lookback): // What is the indicator_lookback and why it is used?



2 - return TechnicalFactor(window_length = lookback) // I am not able to understand this line of logic



I would like to learn more of python programming for trading and investment. I know it will come throgh practice but I need good documentaion or help online. Where can I find it?

  1. the first argument lookback is (as you see in the line in #2 in your comment above) is the window_length of the custom factor, this is the amount of data the function (and the custome factor class) will get for each asset at a given run. This is not necessarily the amount of data your indicator_fn will use. The indicator_lookback is a parameter in the indicator_fn to control that - just in case they are different. Of course indicator_lookback must be equal to or less than lookback.


  2. In your original code you create a class Positivedays, and in the pipeline function your return an instance of this: Positivedays(window_length=10). The technical_factor function does both. Define the class TechnicalFactor and then the function itself returns an instance of the class TechnicalFactor(window_length=lookback)



    Best resource on Python are the user guide on the Python.org site (helps if you are familiar with at least another programming language). The other things you need for financial computing in Python is good familiarity with Numpy and Pandas. If you are familiar with any other programming language, the best place to start is with their official docs. Else I believe Quantra itself has some courses (check out Python for Trading). We recently published a free handbook on Python for financial use as well. There are lots of other materials - free or paid - on the web too. But you are bang right when you say it will only come with practice.

Can we use the following package in blueshift? Or if blueshift has similar things, please suggest.



https://kite.com/

This is not a python package.



We currently do not have auto-complete on the code editor (which is based on VS Code editor). Adding auto-complete is in our to-do list.

Thank You very much. Eagerly waiting for the auto-complete.