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.
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¶
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:
Replace the token you have copied with the token in the code above.
Display all strategies in your account¶
An example of the output will be:
Select the strategy¶
Select the last entry of the strategyCode
column and display it.
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.
Select an instrument¶
From the output, select the instrument on which you wish to test your strategy. For this example, select the first one.
Submit a Job¶
Delete previous trades
Set the parameters for the strategy
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
You can stop a submitted job anytime.
Stop a Job¶
Click on each of the tabs to see the relevant code snippet.
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)
- Progressive Logs (
Click on each of the tabs to see the relevant code snippet.
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
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".
Fetch Order History¶
Click on each of the tabs to see the relevant code snippet.
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.