How to compute the probabilities for all of the trades of the strategy?

Hi Everyone.

Is there a way to compute in a research notebook the statistics of the trades? Ex. Lets said that I have a cross over MA strategy and I want to check the probabilities everytime that the strategy is right or wrong and count all of the trades. Check the percentages of moves. and the average rise, or throwbacks. Why this is necesary? well it is in order to calculate Risk/Reward. We can see if the strategy has an edge or not.

Bull Market             Bear Market

Performance rank 13 out of 23 9 out of 19
Break-even failure rate 5% 7%
Average rise 34% 23%
Change after trend ends -30% –34%
Volume trend Downward Upward
Throwbacks 58% 42%

EG

57
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
# To run an algorithm in Quantopian, you need two functions: initialize and
# handle_data.
import pandas as pd
import talib

def initialize(context):
# This initialize function sets any data or variables that you'll use in
# your algorithm.  For instance, you'll want to define the security (or
# securities) you want to backtest.  You'll also want to define any
# parameters or values you're going to use.

# In our example, we're looking at Apple.  If you re-type this line
# yourself, you'll see the auto-complete that is available for the
# security ID.
context.stock = sid(39214)
#context.stock = sid(8554)

def handle_data(context, data):
# This handle_data function is where the real work is done.  Our data is
# minute-level tick data, and each minute is called a frame.  This function
# runs on each frame of the data.

# We will be working with Apple only
stock = context.stock

# And we will need Apple current and historic data
stock_data = data[stock]

# We will need average price data
mavg_short = stock_data.mavg(50)
mavg_long = stock_data.mavg(200)

current_price = stock_data.price

# In order to make market decisions we need to know how much cash we have
cash = context.portfolio.cash

# This is the meat of the algorithm, placed in this if statement.
#if mavg_short > mavg_long and cash > current_price:
if mavg_short > mavg_long and cash > current_price:

# When buying we need to know how many shares we can buy
number_of_shares = int(cash/current_price)
# int() helps us get a integer value, we can't buy half a share you know

# Finally, we place the buy order
order(stock,  number_of_shares)
elif mavg_short < mavg_long:
# Before we sell we need to know how many shares we have
number_of_shares = context.portfolio.positions[stock.sid].amount

# Now we can sell
order(stock, -number_of_shares) # When selling we need to pass a negative number of shares
There was a runtime error.
10 responses

The pyfolio code has a simplistic breakdown of transactions into round trip trades. I think it only works if a trade closes to 0 holdings; I've thought about making a better trade mapping using proper avg cost basis lots etc, but haven't felt the urgent need yet. But perhaps for a simple system their breakdown would help you?

@Eric, The concept I believe you're going for here is what's called MFE/MAE, and has to do with price excursions for specified durations after a trade. MFE/MAE analytics would be a cool addition I would gather to add to the tearsheet. I recall building edge calculations into various strategies some time ago, and it's not hard. Just maintain a collection of trades (signals actually) and specified durations at which to measure price. One would want to do this inside the strategy so that you can take advantage of the edge measurement as it unfolds and changes.

And when you get done with a back test you can easily print out a custom log like this:

Periods Wins  Win%   Ave chg% Edge ratio MFE avg MAE avg Signals
------- ----  ----  --------- ---------- ------- ------- -------
10   40  67.80     0.033      2.742    1.94    0.71      59
20   48  81.36     0.126      2.844    4.01    1.41      59
30   45  76.27     0.144      2.791    4.76    1.71      59
40   32  54.24     0.134      1.984    5.11    2.58      59

Translated we have:
• Number of periods after a signal (trade)
• How many times, at X periods, was current price above entry price (long trades only here)
• The percent of the wins
• What was the avg price change % after X periods
• The MFE divided by the MAE
• MFE, the maximum excursion of price, in the favorable direction, as a measure of % price
• MAE, the maximum excursion of price, in the adverse direction, as a measure of % price
• The number of signals (trades)

This is just a sample but you can see it's just a matter of record keeping and reporting.

What is then possible is to keep a queue of signals rather than a total collection, and expire the oldest after some number is reached. This would then allow you to keep a running, changing edge measurement which you can then use as a factor in your trades. If edge is > 2.0 keep trading, BUT, keep recording the signals regardless of whether you traded them or not. That's the important part. Signals are NOT trades.

Hi Market Tech,

Thanks for explaining. I agree that this would be a great addition to pyfolio / the tear sheet. Any interest in doing a pull request that adds the MFE/MAE analysis?

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.

Thomas, only if I could write the code in C#.

Market Tech,
You hit the nail, that is exactly what I was thinking.

Thomas,
Is there any plan to add this to pyfolio / the tear sheet? or do I need to develop my own?

Thanks
EG

Erick, and others,

I think we can add this to pyfolio. Certain seems feasible. It would definitely be easier if we can make the simplifying assumption that a trade in a ticker is either an opening or closing transaction, e.g. I buy X shares, then sell all X shares down to 0 for a complete round turn in all the shares (and that shares transacted would never follow some multi-"rebalancing" from say, X number of shares, then buying more up to Y number of shares, then selling down to some other number of shares, and then buying again, before closing the whole position to 0 shares, N number of transactions after the very first transaction.) Something like that which addresses various rebalance frequencies could be done (and we've experimented with some ways in-house as a way to approximate risk-reward in these not simple cases), but is certainly more convoluted to capture all cases, and I'm sure everyone would want it done differently (LIFO, FIFO, vs. most tax-efficient, etc).

Thoughts? Is the simplifying assumption reasonable -- that a single buy can be matched with single sell down to 0 shares, as well as a single short matched with a single cover of the short to 0 shares? If this is the case, then it would be easy to supply a flag to the tearsheet function in pyfolio to 'treat_all_trades_as_roundturns' (or something along those lines) such that those tables of figures would be computed. I only suggest the flag, to minimize/cutout calculation time, because I don't think the calculations make as much sense for an algo that simply rebalances a portfolio to different weights every month.

Thanks for the idea!
-Justin

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.

Justin,

Thanks for the quick response. In my opinion the simplifying assumption for the round trip sounds very good. I believe that this will help us to build stronger algos that have higher Risk:Reward. I don't think that in this community we have been exploring that concept enough. We are usually more focus on lower Draw Down or higher Alpha and low beta etc. At the end of the day for individual retail traders we want to be able to get higher rewards and knowing that we might have to get some draw downs here and there. I will let others to comment. However you have one vote here.

EG

Justin

I took a look to pyfolio and it looks like the tearsheet for round trip has been Implemeted.Do you know if it is already working on quantopian research? If so do you have an example? Thanks

EG

Hi Erick,
We're still working on pushing the latest pyfolio update to the Research environment. I will get a handle on the release date, and respond to this thread.

Great. Thanks Justin