Back to Community
Volume, Leverage

Volume : I´m new to Quantopian, but I´m experienced in Mql4 and 5. Can please somebody help me to understand in the example of "Pipeline Tutorial - Lesson 12 2" is the volume of the securities ? I used to work with lots like 1, 0.10 or 0.01.
This example for long :

context.longs = []  
    for sec in pipe_results[pipe_results['longs']].index.tolist():  
        if data.can_trade(sec):  

Where is the volume ?

Leverage : I used to work with 1, 100, even 500, why is it so low in quantopian algorithms.. like 1.1.

2 responses

First off, Welcome to Quantopian.

The code above simply makes a list of all the securities we want to hold long positions for. context.longs will be a python list of security objects. At this point there is no calculation for 'how much'. It's also not actually ordering anything at this point.

In that same lecture (Pipeline Tutorial - Lesson 12 "Moving to the IDE") there is a function called compute_target_weights. That is where the algo takes the context.longs list and now assigns a weight to each position. The result is a python dic object. Most times the ordering on Quantopian is thought of as a 'target weight'. In other words, we'd like to place an order so our final position will represent X percent of our portfolio. That isn't required though. One can place an order for a specific number of shares, or a specific dollar value, or several other ways (see all the order types in the docs here)

The specific order type where one specifies 'volume', or quantity of shares, is the basic order type (see

my_stock = symbol('AAPL')  
qty_of_shares = 120  
order(my_stock, qty_of_shares)

The second question "why is [the leverage] so low in Quantopian algorithms". The Quantopian definition of leverage is the "sum of the absolute value of long and short positions divided by the total portfolio value". If a portfolio is all cash, the leverage will be 0. If one has no cash but also hasn't dipped into their margin, the leverage will be 1. If one has bought shares worth 120% of the portfolio value (ie bought an additional 20% using margin), the leverage will be 1.2.

Realistic margin levels are between 0-2. Brokers do let you go higher for intra-day trading though. However, when building an algo it's best to assume a leverage of 1. When complete, scale up (or down) the leverage to see the performance impact. Typically, measures like return, volatility, and drawdown will scale linearly with leverage but there are always exceptions. One thing to note. Currently, margin interest is not calculated by the backtester. This will make highly leveraged algos look better than they really are.

Good questions! Hope that helps.


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.

Thank you very much!!