Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
ASDASD

ASDASDASD

5 responses

Derek, this is a great script and thanks for sharing! This addresses a lot of common requests and questions about algorithm management.

These are very clear and elegant functions!

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.

Hi,

I have taken the liberty to clean up and factor this code a little but not completely (as Alisa offered it as a good example for something she thought I needed to do this morning). I applied techniques from the 'functional programming' style. The resulting code is lighter on syntax/noise/moving parts, more robust and less brittle/more flexible (i.e, higher-order). Please apply the same techniques throughout the rest of the code and it will be higher-quality/more robust, clearer, more readable, more reusable and more maintainable.

If you cannot see easily which changes were made or have difficulty understanding the changes or why they were made, please feel free to ask for clarification.

Thank you,
Jonathan

Glad to see that people apreciate functional programming. You might be want to take a look at the itertools package. While python is strict, we can use generators to achieve lazily evaluated sequences. For example, in your function:

def get_all_open_orders():  
    return reduce(operator.add, map(snd, get_open_orders().iteritems()), [])  

you might be suprised to know that this function will traverse the length of the open orders twice, once for the map, and once for the fold (iteritems returns an iterator so this will be lazy). This is because python is eagerly evaluating the list returned by map so that it can pass it to reduce. If you are coming from python 3, you should know that map in python 2 does not return a generator, but instead returns a list. you can replace this call to map with its counterpart from the itertools package like:

def get_all_open_orders():  
    return reduce(operator.add, imap(snd, get_open_orders().iteritems()), [])  

There are counterparts for other functions too, for example: ifilter, izip, dropwhile, takewhile and other goodies you might want from your functional programming toolkit.

Just a note, there is already a name for foldl (+) in the standard libraries, you just need to coerce it a little ^_^

def get_all_open_orders():  
    return sum(imap(snd, get_open_orders().iteritems()), [])  

keep up the λ's

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.

Hi Joe,

Yea, I'm aware of itertools and such. Maybe I'm weird but I always go for the core functions before importing additional modules unless performance is critical (e.g., range v. xrange). I didn't figure performance was critical in this particular case so didn't need to import anything extra.

Thanks for the tips though!

Thank you. This has been very helpful to read through. One comment brought up a question for me...

The comment: "Cancle any open orders ourselves(In live trading this would be done for us, soon in backtest too)"

Does this mean that open orders do not persist in Live trading, and are only good for the day?