Back to Community
What is a trading event?

I started with the template Trading Algorithm.

# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
def initialize(context):  
    pass

# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    # Implement your algorithm logic here.

    # data[sid(X)] holds the trade event data for that security.  
    # data.portfolio holds the current portfolio state.

    # Place orders with the order(SID, amount) method.

    # TODO: implement your own logic here.  
    order(sid(24), 50)  

I understand that handle_data is being called on every trading event, but what is a trading event? No where in the code seem to define this.

Thanks,

7 responses

Hello Shenglan,

A trading event for a security is an OHLCV entry in the Quantopian database, with a datetime stamp. There are some subtleties with respect to thinly traded securities, but basically you should see handle_data called every minute/day the market is open.

For a more thorough explanation, you can read through https://www.quantopian.com/posts/thinly-traded-stocks-why-no-gaps, but I wouldn't get too hung up on the details at this point.

Grant

Hi Grant,
Thanks for your reply. So the code I present above essentially keeps buying 50 shares of AAPL on every tick? How do I set the frequency of the ticks? Thanks

Shenglan,

That's correct. See the attached backtest and its log output and other results.

Grant

Clone Algorithm
3
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 528fb52e0a3fc4076bea2b07
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

Quantopian supports minute-mode and daily-mode - there are no other natively supported rates. Obviously, with 1-minute mode you can make 2- or 5- or hour-modes yourself.

Find the mode change in the upper-right hand corner of your algorithm, right next to the date pickers.

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.

Shenglan,

Sorry...neglected to answer all of your questions (thanks Dan). See attached for a minute-level example, which you can simply clone and play with.

Grant

Clone Algorithm
3
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 528fb6e50a3fc4076bea3049
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

So, just to make sure, is an order execution considered a trade event?

Hello Tane,

As I mentioned above, a trading event for a security is an OHLCV entry in the Quantopian database, with a datetime stamp. There's a decent discussion (and code examples) of how missing data are handled on https://www.quantopian.com/posts/thinly-traded-stocks-why-no-gaps. You can also see https://www.quantopian.com/help#overview-checkingdata for how to handle the case of a security missing altogether from the database.

Grant