Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Determining price direction using exponential and log-normal distributions

I was hoping you guys would be so kind as to give me input on this algo.

I tried to using the exponential distribution when certain conditions/checks (located in batch transform) are met as an attempt at minimizing risk. It seems to work at times however trades infrequently (yet somewhat accurate). I use the log-normal when the prices vary past a certain threshold.

Let me know if there are any questions and I'll do my best to answer them.

Feel free to play with the code, leave comments, suggestions, tips, or anything you may see fit

12 responses

Hello David,

It would be helpful if you provided a summary outline of how your algorithm works. What are you trying to do?

Grant

Hi David,

I agree with Grant. I spent some good time looking at your algorithm but the underlying assumptions are opaque to me. Especially the way you employ the exponential and log normal distribution seems to model quite different things.

Or perhaps there is a paper this is based on?

Thomas

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

@Grant & Tom: Thanks for the responses. I apologize for the lack of expiation and late response.

Granted this might be a feeble attempt at finding breaks in the market and detecting specific patters. I will do my best to explain what's going on.

I retrieve probabilities based on the trailing 20 prices and append them to two different lists. If the prices over that time period are somewhat unstable ( (mu-var/2)/mu< some value ) I enter the trade at an attempt to find a future trend (i.e. assuming that the instability will cause a change in direction). I admit my explanation is very hand wavy but I will try to post (soon) my work as to how I found the seemingly arbitrary constantans I used as a threshold.

I used the lognormal distribution as I assume that stock prices follow this distribution over a longer period and given high instability I assume it will revert back to its stable state after the break ( (mu-var/2)/mu<=-63 ).

I also implemented a network that attempts to check, based on correlation of the stock in question relative to that of a security in my universe, if the probabilities are moving in the same direction. I also check their moving averages and determine whether the distribution would give me like results for both securities. If this is the case than the number of fires will surpass the misses. I do this for my entire universe and if the signal is great enough I have the go ahead to use the distribution to assess a profitable trade.

I hope this clears some questions you guys may have. Feel free to give suggestions or corrections that account for untrue assumptions or unaccounted for problems in the algo.

P.S. Thanks for all the views and responses. They are much appreciated

Hi David,

Thanks for explaining, that makes more sense now :). Although I still don't see why you assume an exponential, could you elaborate on that?

I think the probabilistic approach you are taking here is quite interesting and worthwhile. In fact, I'm starting to experiment with some probabilistic programming techniques that go into a similar direction.

Looking forward to your follow-up posts!

Thomas

Thanks Thomas,

Sorry for not responding sooner. I used the exponential distribution because of its memoryless property, along with the fact that it models the waiting time between occurrences (namely the time it takes for the price to go up or down). There are many papers published regarding these properties, along with other assumptions in this model, and whether it is correct to assume such things when assessing the market. I thought I'd skim through the papers and debates on the subject, as some are quite long and too complex for me to grasp at this point in my education, and just try to make a model with the tools I have to see if there is some promise in the use of the distribution in this context.

I hope this somewhat answers your question.

I have a few algos in the works but none yet ready to post. Hopefully I can get one or two more up by the end of the semester for you guys to check out

Thanks again,
David

David,

Nice one.. But couple of point:
1- what "var/mu" measures ? what are we aiming to capture with this ? it is not Z-score. Can you explain a bit pls?
2-
if (mu-np.var(prices[:,stock])/2/mu ≤ .5 and if (mu-var/2)/mu ≤ -63 As far as i see, these IF conditions are simply indicating 1 ≤ var/mu and 124 ≤ var/mu, respectively. That implies moving var/mu ratio must be so high to trade using log normal probability close to tails ?

3- Log normal distro estimates probability of last prices, but i am not able to follow what probability exactly you are trying to estimate in exponential probability? What is X and rate (\lambda) here ? I assume rate is here moving avg of up ticks and X is one uptick (X=1) and therefore you are trying to estimate probability of one uptick?

Overall nice one ... thanks for sharing...

Deniz

You can add a condition line like this to avoid selling not-yet-owned shares if I'm not mistaken:

                        **if position >= 100:**  
                            order(context.stocks[stock], -100)  

There are two places for it. Tried that. Still 8000%+.

Edit: I don't know why I was seeing such high numbers yesterday, now more normal.

You can also use the new order methods. E.g. if you want to eliminate a position, you can call order_target(context.stocks[stock], 0) and it will sell as many stocks to achieve your desired target.

Could you explain the exit. Just sell after 1 day? No metric?

FYI, please correct me if I'm wrong

This code is wrong.

I believe the intention is that shorter_s be the stock and shorter_c be the check. Instead of both being stock.

def mavg_check(rho,prices,stock,check,r1,r2):  
    shorter_s=np.average(prices[len(prices)-r1-1:len(prices)-1,stock])  
    longer_s=np.average(prices[len(prices)-r2-1:len(prices)-1,stock])  
    shorter_c=np.average(prices[len(prices)-r1-1:len(prices)-1,stock])  
    longer_c=np.average(prices[len(prices)-r2-1:len(prices)-1,stock])  

    if rho>.5:  
        if shorter_s>longer_s and shorter_c>longer_c:  
            return True  
        elif shorter_slonger_c:  
            return True  
        if shorter_s>longer_s and shorter_c  

I am a newbie and all but it seems that you are taking the average of the prices as mu and the variance. Shouldn't you be using the lognormal return, as in ln(price[0]/price[-1]), as your base data? Price is log normally distributed whereas the log-normal return is normally distributed and can be used to estimate mu over different time periods:

S = So * exp(mu+sigma^2/2)

Confidence intervals can also be estimated. I found a thesis online that did the stochastic calculus heavy lifting to derive the confidence intervals about the future expected value. Luckily, equations are not considered to be intellectual property.

anybody can convert to actual version?
thx!