#!/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 = "82db759698c15daf45f70abd9fc6998b694cc049" connection.set_access_token(API_TOKEN) # # Strategy Creation # ## Import Strategy from pyaglostrategypool # # In[ ]: get_ipython().system(' wget -O stochastic_crossover.py https://raw.githubusercontent.com/algobulls/pyalgostrategypool/master/pyalgostrategypool/stochastic_crossover.py') get_ipython().system(" sed -i '1s/^/from pyalgotrading.strategy import StrategyBase\\n/' stochastic_crossover.py") # In[5]: from stochastic_crossover import StochasticCrossover 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('RELIANCE', exchange='NSE') instrument # ## Setup Parameters # In[9]: parameters = { 'FASTK_PERIOD': 7, 'SLOWK_PERIOD': 2, 'SLOWD_PERIOD': 2 } # In[10]: initial_virtual_funds = 70000 # 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:RELIANCE', lots=10, 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, initial_funds=initial_virtual_funds) # #### Quantstats Full Report # In[18]: connection.get_backtesting_report_statistics(strategy, mode='quantstats', report='full', html_dump=True, initial_funds=initial_virtual_funds) # ### 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:RELIANCE', 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) # ---