Package: lib-pybroker
Algorithmic Trading in Python with Machine LearningAre you looking to enhance your trading strategies with the power of Python and
machine learning? Then you need to check outPyBroker! This Python framework
is designed for developing algorithmic trading strategies, with a focus on
strategies that use machine learning. With PyBroker, you can easily create and
fine-tune trading rules, build powerful models, and gain valuable insights into
your strategy’s performance.Key FeaturesA super-fast backtesting engine built inNumPyand accelerated withNumba.The ability to create and execute trading rules and models across multiple instruments with ease.Access to historical data fromAlpaca,Yahoo Finance,AKShare, or fromyour own data provider.The option to train and backtest models usingWalkforward Analysis, which simulates how the strategy would perform during actual trading.More reliable trading metrics that use randomizedbootstrappingto provide more accurate results.Caching of downloaded data, indicators, and models to speed up your development process.Parallelized computations that enable faster performance.With PyBroker, you'll have all the tools you need to create winning trading
strategies backed by data and machine learning. Start using PyBroker today and
take your trading to the next level!InstallationPyBroker supports Python 3.9+ on Windows, Mac, and Linux. You can install
PyBroker usingpip:pipinstall-Ulib-pybrokerOr you can clone the Git repository with:gitclonehttps://github.com/edtechre/pybrokerA Quick ExampleGet a glimpse of what backtesting with PyBroker looks like with these code
snippets:Rule-based Strategy:frompybrokerimportStrategy,YFinance,highestdefexec_fn(ctx):# Get the rolling 10 day high.high_10d=ctx.indicator('high_10d')# Buy on a new 10 day high.ifnotctx.long_pos()andhigh_10d[-1]>high_10d[-2]:ctx.buy_shares=100# Hold the position for 5 days.ctx.hold_bars=5# Set a stop loss of 2%.ctx.stop_loss_pct=2strategy=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))# Run the backtest after 20 days have passed.result=strategy.backtest(warmup=20)Model-based Strategy:importpybrokerfrompybrokerimportAlpaca,Strategydeftrain_fn(train_data,test_data,ticker):# Train the model using indicators stored in train_data....returntrained_model# Register the model and its training function with PyBroker.my_model=pybroker.model('my_model',train_fn,indicators=[...])defexec_fn(ctx):preds=ctx.preds('my_model')# Open a long position given my_model's latest prediction.ifnotctx.long_pos()andpreds[-1]>buy_threshold:ctx.buy_shares=100# Close the long position given my_model's latest prediction.elifctx.long_pos()andpreds[-1]<sell_threshold:ctx.sell_all_shares()alpaca=Alpaca(api_key=...,api_secret=...)strategy=Strategy(alpaca,start_date='1/1/2022',end_date='7/1/2022')strategy.add_execution(exec_fn,['AAPL','MSFT'],models=my_model)# Run Walkforward Analysis on 1 minute data using 5 windows with 50/50 train/test data.result=strategy.walkforward(timeframe='1m',windows=5,train_size=0.5)User GuideGetting Started with Data SourcesBacktesting a StrategyEvaluating with Bootstrap MetricsRanking and Position SizingWriting IndicatorsTraining a ModelCreating a Custom Data SourceApplying StopsRebalancing PositionsRotational TradingFAQsOnline DocumentationThe full reference documentation is hosted atwww.pybroker.com.(For Chinese users:中文文档, courtesy ofAlbert King.)Contact