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!