#!/usr/bin/env python # coding: utf-8 # # Imports # In[ ]: get_ipython().system('pip install --upgrade pyalgotrading') # In[1]: from pyalgotrading.algobulls import AlgoBullsConnection # # Connection # In[2]: connection = AlgoBullsConnection() # In[3]: connection.get_token_url() # In[4]: API_TOKEN = "0a3f547761154cd89b5616aa3f34603e9902d002" connection.set_access_token(API_TOKEN) # #### NOTE: Before creating or executing the strategy, ensure your Alpaca account is binded with your AlgoBulls account (as shown below). To know more click [here](https://algobulls.github.io/pyalgotrading/brokers/connecting_alpaca/).![Reference](https://algobulls.github.io/pyalgotrading/python_build/imgs_v2/python_build_Oauth_successful.png) # # Strategy Creation # ## Import Strategy from pyalgostrategypool # In[5]: get_ipython().system(' wget -O aroon_crossover_us.py https://raw.githubusercontent.com/algobulls/pyalgostrategypool/master/pyalgostrategypool/aroon_crossover/_strategy.py') get_ipython().system(" sed -i '1s/^/from pyalgotrading.strategy import StrategyBase\\n/' aroon_crossover_us.py") # In[6]: from aroon_crossover_us import AroonCrossover as strategy_cls # In[7]: response = connection.create_strategy(strategy_cls, overwrite=True) response # In[8]: strategy = response['strategyId'] # # Strategy Testing # ## Instruments Searching (optional) # In[9]: instrument = connection.search_instrument('AAPL', exchange='NASDAQ')[0]['value'] instrument # ## Setup Parameters # In[10]: parameters = { 'TIME_PERIOD': 12 } # In[11]: initial_virtual_funds = 4000 # in dollars # ## Backtesting # #### NOTE: Ensure your Alpaca account is binded with your AlgoBulls account (as shown below). To know more click [here](https://algobulls.github.io/pyalgotrading/brokers/connecting_alpaca/). # ### Start # In[12]: connection.backtest( strategy=strategy, start='2021-08-01 09:15 -0400', end='2023-07-31 15:30 -0400', instrument='NASDAQ:AAPL', lots=5, parameters=parameters, candle='1 hour', initial_funds_virtual=initial_virtual_funds ) # ### Status # In[13]: connection.get_backtesting_job_status(strategy) # ### Logs # In[14]: logs = connection.get_backtesting_logs(strategy) # In[15]: print(logs) # ### Stop # In[16]: connection.stop_backtesting_job(strategy) # ## Profit and Loss Reports # In[17]: pnl_reports = connection.get_backtesting_report_pnl_table(strategy) pnl_reports # ## Statistics Reports # ### Statistics # In[18]: connection.get_backtesting_report_statistics(strategy) # ### Quantstats Full Report # In[19]: connection.get_backtesting_report_statistics(strategy, report='full', html_dump=True) # ## Order History # In[20]: order_history = connection.get_backtesting_report_order_history(strategy) print(order_history) # ## Papertrading # #### NOTE: Ensure your Alpaca account is binded with your AlgoBulls account (as shown below). To know more click [here](https://algobulls.github.io/pyalgotrading/brokers/connecting_alpaca/). # ### Start # In[ ]: connection.papertrade( strategy=strategy, start='9:15 -0400', end='15:00 -0400', instruments='NASDAQ:AAPL', lots=5, parameters=parameters, candle='1 minute' ) # ### Status # In[ ]: connection.get_papertrading_job_status(strategy) # ### Logs # In[ ]: logs = connection.get_papertrading_logs(strategy) print(logs) # ### Stop # In[ ]: connection.stop_papertrading_job(strategy) # ### Profit and Loss Reports (Paper Trading) # In[ ]: pnl_reports = connection.get_papertrading_report_pnl_table(strategy) pnl_reports # ### Statistics Reports (Paper Trading) # #### Statistics # In[ ]: connection.get_papertrading_report_statistics(strategy) # #### Quantstats Full Report # In[ ]: connection.get_papertrading_report_statistics(strategy, mode='quantstats', report='full', html_dump=True) # ### Order History (Papertrading) # In[ ]: order_history = connection.get_papertrading_report_order_history(strategy) print(order_history) # ## Live Trading (Real Trading) # #### NOTE: Ensure your Alpaca account is binded with your AlgoBulls account (as shown below). To know more click [here](https://algobulls.github.io/pyalgotrading/brokers/connecting_alpaca/). # ### Start # In[ ]: broking_details = { 'brokerName': 'ALPACA LIVE', 'credentialParameters': {} } # In[ ]: connection.realtrade( strategy=strategy, start='9:00 -0400', end='15:00 -0400', instruments='NASDAQ:AAPL', lots=5, parameters=parameters, candle='1 minute', broking_details=broking_details ) # ### Status # In[ ]: connection.get_realtrading_job_status(strategy) # ### Logs # In[ ]: logs = connection.get_realtrading_logs(strategy, display_logs_in_auto_update_mode=False) print(logs) # ### Stop # In[ ]: connection.stop_realtrading_job(strategy) # ### Profit and Loss Reports (Live Trading) # In[ ]: pnl_reports = connection.get_realtrading_report_pnl_table(strategy) pnl_reports # ### Statistics Reports (Live Trading) # #### Statistics # In[ ]: connection.get_realtrading_report_statistics(strategy) # #### Quantstats Full Report # In[ ]: connection.get_realtrading_report_statistics(strategy, mode='quantstats', report='full', html_dump=True)