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:
- Establish a connection to the AlgoBulls Platform.
- Display all Strategies you have in your account.
- Select the strategy.
- Optionally, print the strategy once.
- Select instrument(s).
- Submit/Run a Backtest, Paper Trade or Real Trade job.
- Check Job Status.
- Fetch Logs (even while the job is running).
- 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.
Tip
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.
Tip
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
Note
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:
Select the strategy
Select the last entry of the strategyCode
column and display it.
strategy_code = all_strategies.iloc[-1]['strategyCode']
strategy_code
Print your 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>'
}
}
Code Snippets
- Backtesting
- Paper Trading
- Real Trading
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
)
Fetch Job Status
- Backtesting
- Paper Trading
- Real Trading
algobulls_connection.get_backtesting_job_status(strategy_code)
Stop a Job
- Backtesting
- Paper Trading
- Real Trading
algobulls_connection.stop_backtesting_job(strategy_code)
Fetch Logs
Tip
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 won’t update the latest logs unless called manually again).
- Progressive Logs (
- Backtesting
- Paper Trading
- Real Trading
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)
Fetch PnL Reports
Warning
Please Note Make sure that strategy's execution status is at STOPPED stage before generating PnL reports.
- Backtesting
- Paper Trading
- Real Trading
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
)
Fetch Report Statistics
- Backtesting
- Paper Trading
- Real Trading
algobulls_connection.get_backtesting_report_statistics(
strategy_code, # strategy code
report='full', # format of the report
html_dump=True # save report as html file
)
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.
In this article
- Previously...
- Now...
- Before you start...
- Let's Start...
- Create a new strategy file
- Import statements
- Establish a connection to the AlgoBulls Platform
- Select the strategy
- Print your Strategy code
- Search for instruments (based on a search query)
- Select an instrument
- Submit a Job
- Code Snippets
- Fetch Job Status
- Stop a Job
- Fetch Logs
- Fetch PnL Reports
- Fetch Report Statistics
- What's Next...