By Evgenii Grakov, Python Software Engineer at OneTick
onetick-py is a powerful library that, together with OneTick, simplifies market data processing by offering a Pandas-like API.
However, it is not always possible to deploy your own local Tick Server. In this case, onetick-py offers the ability to connect to the OneTick Cloud to execute queries using WebAPI.
This reduces configuration requirements, allowing you to make requests even from a minimal Python environment.
Today, we will go through the basic steps of setting up a connection to OneTick Cloud and make our first remote request.
Creating A OneTick Cloud Account
Let’s start by creating a free account with a 30-day trial on OneTick Cloud, if you don’t have one already. This will allow you to run test queries on demo data.
Visit OneTick Cloud website and go to sign up page.
Fill the short registration form.
After you create an account, verify your email address and sign in you will see OneTick Cloud Dashboard. For now, we only need the client credentials, which will be used a bit later: CLIENT_ID and CLIENT_SECRET.
Installing onetick-py
If you haven’t installed Python yet, you can do so by following this guide. The minimal Python version required by onetick-py is 3.9.
Now let’s install onetick-py. It’s available on PYPI, so it can be installed via pip, which is a part of Python distribution, without any additional setup steps.
Create a new project on your IDE or create and activate a new Python virtual environment. Using a virtual environment is recommended as it keeps your project dependencies isolated from other Python projects on your system, preventing potential conflicts between different packages.
Install onetick-py with webapi extra for WebAPI support:
pip install onetick-py[webapi]
Or add it to your requirements file and update packages:
onetick-py[webapi]==1.166.0
Configuring basic onetick-py parameters
Now, when we’ve installed onetick-py, let’s configure it to work with OneTick Cloud.
Note, if you have installed OneTick Server on your computer or machine:
By default, onetick-py (even installed with WebAPI support) tries to find OneTick installation on your computer, and if it's found, disables WebAPI mode. To enable it set environment variable OTP_WEBAPI=1
You can do this multiple ways: either at the system/current terminal session level or by adding this environment variable to os.environ from your script before importing onetick-py:
import os
os.environ['OTP_WEBAPI'] = '1'
We need to configure the connection settings and default parameters for onetick-py.
There are two ways to do this: through environment variables or by using otp.config.
We will use the first option — setting parameters through environment variables. This can be done in your OS before running the script, but for better clarity of the example we will set them in our code.
First, we need to set authentication parameters:
import os
# WebAPI server URL
os.environ['OTP_HTTP_ADDRESS'] = 'https://rest.cloud.onetick.com'
# Access token URL
os.environ['OTP_ACCESS_TOKEN_URL'] = 'https://cloud-auth.parent.onetick.com/realms/OMD/protocol/openid-connect/token'
# Client ID
os.environ['OTP_CLIENT_ID'] = 'ccf_demo_app_1'
# Your client secret
os.environ['OTP_CLIENT_SECRET'] = 'CLIENT_SECRET_HERE'
And default parameters, in case if you want to omit them while making queries:
os.environ['OTP_DEFAULT_TZ'] = 'UTC'
A full list of supported environment variables and corresponding onetick-py configuration parameters is provided in this article.
Exploring Available Data
For our test purposes, we will look at the databases available with the 30-day free trial that we will use for our test data. You can get a list of the databases, available dates, symbols, and tick types using database inspection module:
import os
os.environ['OTP_WEBAPI'] = '1'
os.environ['OTP_HTTP_ADDRESS'] = 'https://rest.cloud.onetick.com'
os.environ['OTP_ACCESS_TOKEN_URL'] = 'https://cloud-auth.parent.onetick.com/realms/OMD/protocol/openid-connect/token'
os.environ['OTP_CLIENT_ID'] = 'ccf_demo_app_1'
os.environ['OTP_CLIENT_SECRET'] = 'CLIENT_SECRET_HERE'
os.environ['OTP_DEFAULT_TZ'] = 'UTC'
import onetick.py as otp
databases = otp.databases()
print('\n'.join(databases))
Result:
CANADA_MKT_SHARE
EU_MKT_SHARE
NYSE_TAQ_MKT_SHARE
OTCLOUD_AVAILABILITY
OTC_CLIENT_CREDENTIALS
CA_COMP_SAMPLE
CA_COMP_SAMPLE_BARS
CA_COMP_SAMPLE_DAILY
CME_SAMPLE
CME_SAMPLE_BARS
CME_SAMPLE_DAILY
...
All SAMPLE databases have data for February and March of 2024.
Let’s check this for US_COMP_SAMPLE database:
us_comp_db = databases['US_COMP_SAMPLE']
print('\n'.join(map(str, us_comp_db.dates())))
Result:
2024-02-01
2024-02-02
2024-02-03
2024-02-04
2024-02-05
2024-02-06
2024-02-07
2024-02-08
...
Making Test Query
Now we are ready to create our first script. Let’s calculate the Volume Weighted Average Price, which is one of many available OneTick data aggregations, for the AAPL symbol in the US_COMP_SAMPLE database on February 1, 2024, between 10 a.m. and 5 p.m. UTC for each hour:
import os
os.environ['OTP_WEBAPI'] = '1'
os.environ['OTP_HTTP_ADDRESS'] = 'https://rest.cloud.onetick.com'
os.environ['OTP_ACCESS_TOKEN_URL'] = 'https://cloud-auth.parent.onetick.com/realms/OMD/protocol/openid-connect/token'
os.environ['OTP_CLIENT_ID'] = 'ccf_demo_app_1'
os.environ['OTP_CLIENT_SECRET'] = 'CLIENT_SECRET_HERE'
os.environ['OTP_DEFAULT_TZ'] = 'UTC'
import onetick.py as otp
src = otp.DataSource(
db='US_COMP_SAMPLE',
tick_type='TRD',
symbol='AAPL',
start=otp.dt(2024, 2, 1, 10),
end=otp.dt(2024, 2, 1, 17),
)
src = src.agg({'RESULT': otp.agg.vwap('PRICE', 'SIZE')}, bucket_interval=otp.Hour(1))
result = otp.run(src)
print(result)
Result:
Time RESULT
0 2024-02-01 11:00:00 185.444859
1 2024-02-01 12:00:00 185.478817
2 2024-02-01 13:00:00 185.613162
3 2024-02-01 14:00:00 184.704087
4 2024-02-01 15:00:00 184.721101
5 2024-02-01 16:00:00 185.819542
6 2024-02-01 17:00:00 185.703698
SQL Queries
If you are more experienced with SQL queries and prefer to use them, onetick-py provides otp.SqlQuery data source which could be also used with WebAPI:
import os
os.environ['OTP_WEBAPI'] = '1'
os.environ['OTP_HTTP_ADDRESS'] = 'https://rest.cloud.onetick.com'
os.environ['OTP_ACCESS_TOKEN_URL'] = 'https://cloud-auth.parent.onetick.com/realms/OMD/protocol/openid-connect/token'
os.environ['OTP_CLIENT_ID'] = 'ccf_demo_app_1'
os.environ['OTP_CLIENT_SECRET'] = 'CLIENT_SECRET_HERE'
os.environ['OTP_DEFAULT_TZ'] = 'UTC'
import onetick.py as otp
src = otp.SqlQuery(
"select "
" VWAP(PRICE, SIZE) as RESULT "
"from US_COMP_SAMPLE.TRD "
"where symbol_name = 'AAPL' "
"and start_time = '2024-02-01 10:00:00' and end_time = '2024-02-01 17:00:00' "
"group by time_bucket(INTERVAL '1' HOUR) "
)
result = otp.run(src)
print(result)
Result:
Time RESULT
0 2024-02-01 11:00:00 185.444859
1 2024-02-01 12:00:00 185.478817
2 2024-02-01 13:00:00 185.613162
3 2024-02-01 14:00:00 184.704087
4 2024-02-01 15:00:00 184.721101
5 2024-02-01 16:00:00 185.819542
6 2024-02-01 17:00:00 185.703698
You can learn more about SQL queries in OneTick and find out examples here.
What’s Next
Now you have successfully installed onetick-py, configured it to use OneTick Cloud, and run your first query. From here, you can explore more about use cases, like data filtering, aggregating or order books analytics. Or more complex cases, like Point-in-time benchmarks and calculating Participation weighted price. Check the documentation for more details and examples.
Compared to running onetick-py locally, WebAPI mode has some differences and constraints. You can check them out in the documentation.
And don’t forget about compute and resources quotas for demo accounts. You can check them on the Resource Limits tab in OneTick Cloud Dashboard.
Evgenii Grakov
Python Software Engineer at OneTick