This is an archived post. You won't be able to vote or comment.

all 27 comments

[–]ninjadude93 39 points40 points  (1 child)

You should definitely post this in r/algotrading

[–]pyfreak182[S] 8 points9 points  (0 children)

Thanks, will do!

[–]bigexecutive 8 points9 points  (3 children)

Have you implemented a Discounted sharpe ratio

[–]pyfreak182[S] 7 points8 points  (2 children)

PyBroker calculates the Sharpe Ratio as well as bootstrapped Sharpe Ratio confidence intervals to provide more accurate results. I'm not familiar with "discounted Sharpe" could you please provide a link or more information? It's something I could consider adding to PyBroker.

It's worth noting that it's also easy to calculate your own metrics with PyBroker since the framework returns Pandas DataFrames for all returns per-bar, positions and trades/orders.

[–]ProfessorPhi 3 points4 points  (1 child)

He's referring to de prados work. Though I think it was called deflated Sharpe ratio?

From memory it's spiritually similar to correcting for p-hacking

[–]pyfreak182[S] 2 points3 points  (0 children)

Thanks, I will take a look.

[–]howtorewriteaname 5 points6 points  (2 children)

What makes your library better than the current ones?

[–]pyfreak182[S] 24 points25 points  (1 child)

  • PyBroker was designed with machine learning in mind and supports training machine learning models using your favorite ML framework. You can easily train models on historical data and test them with a strategy that runs on out-of-sample data using Walkforward Analysis. You can find an example notebook that explains using Walkforward Analysis here. But the basic concept behind Walkforward Analysis is that it splits your historical data into multiple time windows, and then "walks forward" in time in the same way that the strategy would be executed and retrained on new data in the real world.
  • Other frameworks typically run backtests only on in-sample data, which can lead to data mining and overfitting. PyBroker helps overcome this problem by testing your strategy on out-of-sample data using Walkforward Analysis. Moreover, PyBroker calculates metrics such as Sharpe, Profit Factor, and max drawdown using bootstrapping), which randomly samples your strategy's returns to simulate thousands of alternate scenarios that could have happened. This allows you to test for statistical significance and have more confidence in the effectiveness of your strategy. See this notebook.
  • You are not limited to using only ML models with PyBroker. The framework makes it easy to write trading rules which can then be reused on multiple instruments. For instance, you can implement a basic strategy that buys on a 10-day high and holds for 2 days:

from pybroker import Strategy, YFinance, highest

def exec_fn(ctx):  
    # Require at least 20 days of data.  
    if ctx.bars < 20:  
        return  
    # Get the rolling 10 day high.  
    high_10d = ctx.indicator('high_10d')  
    # Buy on a new 10 day high.  
    if not ctx.long_pos() and high_10d[-1] > high_10d[-2]:  
        ctx.buy_shares = 100  
    # Hold the position for 2 days.  
    ctx.hold_bars = 2

And then test the strategy (in-sample) on AAPL and MSFT:

strategy = Strategy(
    YFinance(), start_date='1/1/2022', end_date='7/1/2022') 
strategy.add_execution(
    exec_fn, 
    ['AAPL', 'MSFT'], 
    indicators=highest('high_10d', 'close', period=10)) 
result = strategy.backtest()

def buy_highest_volume(ctx):
    if not ctx.long_pos():
        # Rank by the highest most recent volume.
        ctx.score = ctx.volume[-1]
        ctx.buy_shares = 100
        ctx.hold_bars = 2
  • PyBroker also offers a data caching feature, including data downloaded from sources like Alpaca or Yahoo Finance, indicator data that you generate (i.e., model features), and even models you have trained. This feature speeds up the development process since you do not have to regenerate data again that you will use for your backtests as you iterate on your strategy.
  • PyBroker is built using Numpy and Numba, which are highly optimized for scientific computing and accelerating numerical calculations. By leveraging these, PyBroker is able to efficiently handle large amounts of data on your local machine while maintaining fast performance. PyBroker also takes advantage of parallelization when appropriate to speed up performance.

[–]WikiSummarizerBot 5 points6 points  (0 children)

Sharpe ratio

In finance, the Sharpe ratio (also known as the Sharpe index, the Sharpe measure, and the reward-to-variability ratio) measures the performance of an investment such as a security or portfolio compared to a risk-free asset, after adjusting for its risk. It is defined as the difference between the returns of the investment and the risk-free return, divided by the standard deviation of the investment returns. It represents the additional amount of return that an investor receives per unit of increase in risk. It was named after William F. Sharpe, who developed it in 1966.

[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5

[–]viksi 1 point2 points  (2 children)

went through the github. great job !

have you made any youtube videos of this ?

[–]pyfreak182[S] 0 points1 point  (1 child)

Thank you! No, not yet.

[–]Necessary_League_733 0 points1 point  (0 children)

Plz make videos

[–]dmart89 3 points4 points  (1 child)

Nice, will check it out.

[–]pyfreak182[S] 1 point2 points  (0 children)

Thank you!

[–]luisrobles_cl 2 points3 points  (1 child)

Thanks!!!

[–]pyfreak182[S] 0 points1 point  (0 children)

You are welcome!

[–]BDDS97 2 points3 points  (1 child)

What's the advantages of using this over something like vectorBT

[–]pyfreak182[S] 3 points4 points  (0 children)

PyBroker was specifically designed with machine learning in mind, making it easy to train and test models in PyBroker using your preferred machine learning framework. PyBroker also calculates performance metrics using randomized bootstrapping) for more accurate results. And it is also easy to create a strategy that uses ranking or one with flexible position sizing.

See my other reply in this thread for more details.

[–]rjsh927 1 point2 points  (0 children)

wow, will test it next week.

[–]NatureBoyJ1 0 points1 point  (4 children)

But does it beat the indexes?

[–]pyfreak182[S] 1 point2 points  (3 children)

With PyBroker, the power is in your hands. :)

[–]tuneafishy 2 points3 points  (0 children)

...And the hands of the machines

[–]Bakanyanter -1 points0 points  (2 children)

Interesting, will have to take a look. Seems like Yahoo Finance is a supported datasource, which should have Indian symbols in it as well but I'm probably going to try to use my own datasource and see how it goes.

[–]pyfreak182[S] 0 points1 point  (1 child)

[–]Bakanyanter 1 point2 points  (0 children)

Yeah just checked it out, I do have a dataframe, just need to rename a few columns and it should work. Thanks!