#!/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 = "71d4e9c405acc3f595ff62eaf96806127d921292" connection.set_access_token(API_TOKEN) # # Strategy Creation # ## Import Strategy from pyaglostrategypool # In[ ]: get_ipython().system(' wget -O bollinger_bands.py https://raw.githubusercontent.com/algobulls/pyalgostrategypool/master/pyalgostrategypool/bollinger_bands/_strategy.py') get_ipython().system(" sed -i '1s/^/from pyalgotrading.strategy import StrategyBase\\n/' bollinger_bands.py") # In[5]: from bollinger_bands import BollingerBands as strategy_cls # In[6]: response = connection.create_strategy(strategy_cls, overwrite=True) response # In[7]: strategy = response['strategyId'] # # Strategy Testing # ## Instruments Searching (optional) # In[8]: instrument = connection.search_instrument('TATAMOTORS', exchange='NSE') instrument # ## Setup Parameters # In[9]: parameters = { 'TIME_PERIOD': 12, 'STANDARD_DEVIATIONS': 2, } # In[10]: initial_virtual_funds = 7000 # in Rupees # ## Backtesting # ### Start # In[11]: connection.backtest( strategy=strategy, start='2021-08-01 09:15 +0530', end='2023-07-31 15:30 +0530', instrument='NSE:TATAMOTORS', lots=5, parameters=parameters, candle='1 hour', initial_funds_virtual=initial_virtual_funds ) # ### Status # In[12]: connection.get_backtesting_job_status(strategy) # ### Logs # In[13]: logs = connection.get_backtesting_logs(strategy) # In[14]: print(logs) # ### Stop # In[15]: connection.stop_backtesting_job(strategy) # ### Profit and Loss Reports # In[16]: pnl_reports = connection.get_backtesting_report_pnl_table(strategy) pnl_reports # ### Statistics Reports # #### Statistics # In[17]: connection.get_backtesting_report_statistics(strategy) # #### Quantstats Full Report # In[18]: connection.get_backtesting_report_statistics(strategy, report='full', html_dump=True ) # ### Order History # In[19]: order_history = connection.get_backtesting_report_order_history(strategy) print(order_history) # --- # ## Papertrading # ### Start # In[ ]: connection.papertrade( strategy=strategy, start='9:15 +0530', end='15:00 +0530', instruments='NSE:SBIN', 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 (Paper Trading) # In[ ]: order_history = connection.get_papertrading_report_order_history(strategy) print(order_history) # ---