pybroker
v1.2.5


您是否希望通过Python和机器学习的力量来增强交易策略?然后,您需要查看Pybroker !该Python框架旨在制定算法交易策略,重点是使用机器学习的策略。借助Pybroker,您可以轻松地制定和微调交易规则,建立强大的模型,并对策略的绩效获得宝贵的见解。
借助Pybroker,您将拥有创建由数据和机器学习支持的获胜交易策略所需的所有工具。立即开始使用Pybroker,并将您的交易提升到一个新的水平!
Pybroker在Windows,Mac和Linux上支持Python 3.9+。您可以使用pip安装Pybroker:
pip install -U lib-pybroker或者,您可以用以下方式克隆git存储库。
git clone https://github.com/edtechre/pybroker瞥见使用这些代码段的Pybroker进行回测的样子:
基于规则的策略:
from pybroker import Strategy , YFinance , highest
def exec_fn ( ctx ):
# 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 5 days.
ctx . hold_bars = 5
# Set a stop loss of 2%.
ctx . stop_loss_pct = 2
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 ))
# Run the backtest after 20 days have passed.
result = strategy . backtest ( warmup = 20 )基于模型的策略:
import pybroker
from pybroker import Alpaca , Strategy
def train_fn ( train_data , test_data , ticker ):
# Train the model using indicators stored in train_data.
...
return trained_model
# Register the model and its training function with PyBroker.
my_model = pybroker . model ( 'my_model' , train_fn , indicators = [...])
def exec_fn ( ctx ):
preds = ctx . preds ( 'my_model' )
# Open a long position given my_model's latest prediction.
if not ctx . long_pos () and preds [ - 1 ] > buy_threshold :
ctx . buy_shares = 100
# Close the long position given my_model's latest prediction.
elif ctx . long_pos () and preds [ - 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 )完整的参考文档托管在www.pybroker.com上。
(适用于中国用户:中文文档,由阿尔伯特·金(Albert King)提供。)

请了解最高趋势股票的AI驱动新闻。有关最新更新,请参见www.trendninja.ai!