Skip to content

Execute your Strategy

Previously...

You have uploaded your strategy to the AlgoBulls platform.


Now...

Using the uploaded strategy, you can now try:

  • Backtesting
  • Paper Trading
  • Real Trading

Before you start...

Open a Jupyter Notebook.

The steps you will follow are:

  1. Establish a connection to the AlgoBulls Platform.
  2. Display all Strategies you have in your account.
  3. Select the strategy.
  4. Optionally, print the strategy once.
  5. Select instrument(s).
  6. Submit/Run a Backtest, Paper Trade or Real Trade job.
  7. Check Job Status.
  8. Fetch Logs (even while the job is running).
  9. Fetch Reports. (PnL, Statistics, Order History)

Let's Start...

Run the following code snippets into the Jupyter Notebook one by one (or all together).

Create a new strategy file

eg: strategy_<unique_code_if_needed>_options_ema_crossover.py Make sure this strategy file is in the same folder as the jupyter notebook.

Coding Conventions

  • Keep a unique file name
  • Make sure that the file name is in lowercase and that each word is separated with an underscore '_' as shown above.

How to Code ?

To know more on how to code trading strategies and understand their format, click here. We have in detail explanation for regular strategies as well as options strategies

Import statements

from pyalgotrading.algobulls import AlgoBullsConnection
from datetime import datetime as dt
from pyalgotrading.constants import *

Establish a connection to the AlgoBulls Platform

algobulls_connection = AlgoBullsConnection()
algobulls_connection.get_authorization_url()
The output of the above step is:

Please login to this URL with your AlgoBulls credentials and get your developer access token: https://app.algobulls.com/user/login

Get Developer Key

You will need to log in to your AlgoBulls account and fetch the access token from: (See How)
Settings -> General -> Developer Options

Once you have the access token, set it in the code as shown here:

algobulls_connection.set_access_token('4365817b795770ea31040a21ad29c8e78b63ad88')

Replace the token you have copied with the token in the code above.


Display all strategies in your account


all_strategies = algobulls_connection.get_all_strategies()
all_strategies

An example of the output will be: Output

Select the strategy

Select the last entry of the strategyCode column and display it.

strategy_code = all_strategies.iloc[-1]['strategyCode']
strategy_code


You can print your strategy code once to verify if this is the correct code. This step is optional.

strategy_details1 = algobulls_connection.get_strategy_details(strategy_code)
print(strategy_details1)

Search for instruments (based on a search query)

Now display a few instruments with some keyword. The example below uses 'SBIN' as the keyword.

instruments = algobulls_connection.search_instrument('SBIN')
instruments

Select an instrument

From the output, select the instrument on which you wish to test your strategy. For this example, select the first one.

instrument = instruments[0]['value']
instrument


Submit a Job

Delete previous trades

algobulls_connection.delete_previous_trades(strategy=strategy)

Set the parameters for the strategy

parameters={
    'timeperiod1': 5,
    'timeperiod2': 12
}

vendor_details = {
    'brokerName': '<VENDOR_NAME>',
    'credentialParameters': {
        'api_key': '<API_KEY>',
        'secret_key': '<SECRET_KEY>'
    }
}
broking_details = {
    'brokerName': '<BROKER_NAME>',
    'credentialParameters': {
        'user_id': '<USER_ID>',
        'api_key': '<API_KEY>',
        'password': '<PASSWORD>'
    }
}

Click on each of the tabs to see the relevant code snippet.

algobulls_connection.backtest(
        strategy=strategy_code,         # strategy code
        start='2020-7-1 09:15 +0530',   # start date-time of strategy ('YYYY-MM-DD HH:MM z')
        end='2020-7-7 15:30 +0530',     # end date-time of strategy ('YYYY-MM-DD HH:MM z')
        instruments='NSE:SBIN',         # name of the instrument
        lots=1,                         # number of lots per trade
        parameters=parameters,          # parameters required for the strategy
        candle='15 minutes',            # candle size eg : '1 Day', '1 hour', '3 minutes'
        delete_previous_trades=True,    # delete the previous trades for papertrading (default is true),
        initial_funds_virtual=10000,    # virtual funds allotted before the paper trading starts (default is 1e9)
        vendor_details=vendor_details   # vendor's details for authentication and verification 
)
algobulls_connection.papertrade(
        strategy=strategy_code,         # strategy code
        start='09:15 +0530',            # start time of strategy (HH:MM z)     
        end='15:30 +0530',              # end time of strategy (HH:MM z)
        instruments='NSE:SBIN',         # name of the instrument
        lots=1,                         # number of lots per trade
        parameters=parameters,          # parameters required for the strategy
        candle='15 minutes',            # candle size eg : '1 Day', '1 hour', '3 minutes'
        delete_previous_trades=True,    # delete the previous trades for papertrading (default is true)
        initial_funds_virtual=10000,    # virtual funds allotted before the paper trading starts (default is 1e9)
        vendor_details=vendor_details   # vendor's details for authentication and verification 
)
algobulls_connection.realtrade(
        strategy=strategy_code,          # strategy code
        start='09:15 +0530',             # start time of strategy (HH:MM z)     
        end='15:30 +0530',               # end time of strategy (HH:MM z)
        instruments='NSE:SBIN',          # name of the instrument
        lots=1,                          # number of lots per trade
        parameters=parameters,           # parameters required for the strategy
        candle='15 minutes',             # candle size eg : '1 Day', '1 hour', '3 minutes'
        broking_details=broking_details  # broker's details for authentication and verification 
)

Fetch Job Status

Click on each of the tabs to see the relevant code snippet. There are 4 stages of your strategy execution : STARTING, STARTED, STOPPING and STOPPED

algobulls_connection.get_backtesting_job_status(strategy_code)
algobulls_connection.get_papertrading_job_status(strategy_code)
algobulls_connection.get_realtrading_job_status(strategy_code)

You can stop a submitted job anytime.


Stop a Job

Click on each of the tabs to see the relevant code snippet.

algobulls_connection.stop_backtesting_job(strategy_code)
algobulls_connection.stop_papertrading_job(strategy_code)
algobulls_connection.stop_realtrading_job(strategy_code)

You can fetch the logs in the middle of a job to monitor the progress.


Fetch logs

Logging Tip

  • There are 2 variations when fetching logs:
    • Progressive Logs (print_live_logs = True): will show progress bar and update the latest logs as the strategy is executed
    • Complete Logs (print_live_logs = False): will fetch logs after strategy is executed.(It wont update the latest logs, unless called manually again)

Click on each of the tabs to see the relevant code snippet.

logs = algobulls_connection.get_backtesting_logs(
            strategy_code,                              # strategy code 
            display_progress_bar=True,                  # (default=True) to track the execution on progress bar as your strategy is executed
            print_live_logs: True                       # (default=False) to print the live logs as your strategy is executed
        )
print(logs)
logs = algobulls_connection.get_papertrading_logs(
            strategy_code,                              # strategy code 
            display_progress_bar=True,                  # (default=True) to track the execution on progress bar as your strategy is executed
            print_live_logs: True                       # (default=True) to print the live logs as your strategy is executed
        )
print(logs)
logs = algobulls_connection.get_realtrading_logs(
            strategy_code,                              # strategy code 
            display_progress_bar=True,                  # (default=True) to track the execution on progress bar as your strategy is executed
            print_live_logs: True                       # (default=True) to print the live logs as your strategy is executed
        )
print(logs)

You can fetch the PnL report, statistics and order history for a job.


Fetch PnL Reports

Click on each of the tabs to see the relevant code snippet.

Please Note

Make sure that strategy's execution status is at STOPPED stage before generating PnL reports

algobulls_connection.get_backtesting_report_pnl_table(
        strategy_code,                      # strategy code
        show_all_rows=True,                 # default=True         
        force_fetch=True,                   # pnl data is saved locally once fetched, to update the locally fetched data, make this parameter True
        country='USA',                      # country of the exchange that was used while starting the job ('India' or 'USA')
        broker_commission_percentage: 1     # Percentage of broker commission per trade
        broker_commission_price: 0.2        # Broker fee per trade
        slippage_percent: 3                 # Slippage percentage value
)
algobulls_connection.get_papertrading_report_pnl_table(
        strategy_code,                      # strategy code
        show_all_rows=True,                 # default=True         
        force_fetch=True,                   # pnl data is saved locally once fetched, to update the locally fetched data, make this parameter True
        country='USA',                      # country of the exchange that was used while starting the job ('India' or 'USA')
        broker_commission_percentage: 1     # Percentage of broker commission per trade
        broker_commission_price: 0.2        # Broker fee per trade
        slippage_percent: 3                 # Slippage percentage value
)
algobulls_connection.get_realtrading_report_pnl_table(
        strategy_code,              # strategy code
        show_all_rows=True,         # default=True         
        force_fetch=True,           # pnl data is saved locally once fetched, to update the locally fetched data, make this parameter True
        country='USA',              # country of the exchange that was used while starting the job ('India' or 'USA')
)

To know more about slippage and brokerage parameters click here

Fetch Report Statistics

Click on each of the tabs to see the relevant code snippet.

Analytics Tips

You can view your analytics on the Phoenix page as well, simply log inside your AlgoBulls Account, and look for your Strategy in Phoenix -> My Coded Strategies

algobulls_connection.get_backtesting_report_statistics(
    strategy_code,          # strategy code
    report='full',          # format of the report
    html_dump=True          # save report as html file
)
algobulls_connection.get_papertrading_report_statistics(
    strategy_code,          # strategy code
    report='full',          # format of the report
    html_dump=True          # save report as html file
)
algobulls_connection.get_realtrading_report_statistics(
    strategy_code,          # strategy code
    report='full',          # format of the report
    html_dump=True          # save report as html file
)

Generate Statistics from External CSV

Make sure your csv has the columns names as "net_pnl" and "entry_timestamp". The "net_pnl" column will contain the net profit and loss of every trade and its respective entry time will be stored in "entry_timestamp".

connection.get_report_statistics(file_path="testing_file.csv", initial_funds=3000)


Fetch Order History

Click on each of the tabs to see the relevant code snippet.

order_history = algobulls_connection.get_backtesting_report_order_history(strategy_code)
print(order_history)
order_history = algobulls_connection.get_papertrading_report_order_history(strategy_code)
print(order_history)
order_history = algobulls_connection.get_realtrading_report_order_history(strategy_code)
print(order_history)

Note

  • Order History for Real Trading is not supported by brokers.
  • Order History for Backtesting, Paper Trading and Real Trading is supported by the AlgoBulls Virtual Brokers.

What's Next...

You can now explore more by creating and uploading more complex strategies.

You can also check out the Analytics, to understand more about the returns and analyze your strategy based on the analytics report.


Last update: 2024-08-03