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

The help page states:

The use of random() isn't supported because of technical limitations with our start-of-day initialization.

It used to be that importing random was blocked, but it is now possible. Does this mean that the restriction on backtests needing to be repeatable has been relaxed?

Refs:

https://www.quantopian.com/posts/use-of-random-values-in-an-algo
https://www.quantopian.com/posts/random-selection-with-ratcheting-price

7 responses

Hi Grant,

While random may not be blocked in the IDE, the use of random is not supported in either backtesting or live trading.

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.

Jamie,

My understanding is that the actual restriction is that backtests need to be repeatable when live trading. So random can be used, but only if the seed is specified, correct?

Grant

Grant,
Are you looking for a (pseudo)random number generator?
If so, there are good ones, even better than the system function random().
alan

Grant,

A great number generator that I use is numpy.random.rand() if that's what you're looking for

I was just curious about the use of a random number generator in an algo. Other than random now being importable as a module, I don't think anything has changed. It's not that random and similar modules are not supported. The only restriction is that live-trading algos need to be repeatable, although I don't think this is spelled out in the help docs.

Ok, I'll bite...why would live-trading algos have such a restriction? That is usually only useful for deterministic debugging.

Here is a scenario I'm facing lately...
I have an algo( in fact a modification of one of yours Grant!), which when I make the start-date different, and fix the end-date to today, gives a fairly wide range of TotalReturns = ~(-10% to +30%), using backtest windows of from ~(1.5-10) years...I like the algo, as it's pretty solid for a good proportion of
the start times I've run it in the IDE backtester....so what do I do? ... as at any time, should I start or not?

Well...comments welcome, but the path I'm currently taking is:

  1. Use the Research environment to sweep the start-date as a parameter, to analyze the ending point distributions of this
    particular type of "random walk".
    (This is easier said than done, but I'm making progress, and may find out I don't need this infrastructure)
    So let's assume I complete that task, and move on to what I do with that information.
  2. Develop a restart-signal, with a controlled modeled-random-jump to a new start-time that gets me into a statistically good starting
    region, in simulation.
  3. Deploy this algo, with the restart-signal+modeled-random-jump, into the live-trading environment.
    [Note: This is where the live random number generator is actually needed...not a fixed-seed deterministic one,
    as I'll have a stochastic model that is predicting good jumps to start-times.]

As Alexander pointed out, if some entity bans the use of random(), I'd probably just use the numpy version, or even my own version if need be.
alan

Alan,

I suspect that the restriction is for quality control testing. If you use a fixed seed for whatever random number generator you chose, it'll probably work fine.

Grant