It’s time we dive into how to request trading data from Sierra Charts into our Python project via SC-Py.
Here are the steps we will discuss in the next section:
- Subscribing to Sierra Chart Data via SC-Py
- Accessing Sierra Chart’s Subgraph Data
- Tuning into Live Data
Subscribing to Sierra Chart Data via SC-Py
The SC-Py API requests data from Sierra Chart. Here’s a snippet to get us started:
# SC-Py Import
from trade29.sc.bridge import SCBridge# Create an instance of the SCBridge class
sc = SCBridge()
# Request data from SCBridge
sc.graph_data_request(
key="xx", # ES key
base_data='1;2;3;4', # Open, High, Low, Close
historical_init_bars=10, # Historical data
on_bar_close=True, # Update on bar close
include_bar_index=True # Include bar index
)
# Get data from SCBridge
msg = sc.get_response_queue().get()
print(msg.df)
- SC-Py Import:
First, import the ‘SCBridge’ class from the ‘trade29.sc.bridge’ module. - Creating an Instance:
Next, create an instance of the ‘SCBridge’ class and name it ‘sc’. - Requesting Data:
Now, request data using ‘sc.graph_data_request()’ by passing along several pieces of information:
– key=‘xx’: place the key.
– base_data=’1;2;3;4′: requests the Open, High, Low, and Close values.
– historical_init_bars=10: request historical data.
– on_bar_close=True: updates when a bar closes.
– include_bar_index=True: include the bar index in the data. - Getting the Data:
Finally, we’re ready to collect the chart data. This is achieved by the ‘sc.get_response_queue().get()’ method. To see data in a neat dataframe format we can simply print the msg request: print(msg.df).
We will use a 50-bar Simple Moving Average (SMA) from the chart:
# Adding 50 bar SMA to our data request
sc.graph_data_request(
key="xx", # ES key
base_data='1;2;3;4', # Open, High, Low, Close
sg_data="ID2.[SG1]", # 50 bar SMA
historical_init_bars=10, # Historical data
on_bar_close=True, # Update on bar close
include_bar_index=True # Include bar index
)# Fetching the data
msg = sc.get_response_queue().get()
print(msg.df)
- Adding 50 bar SMA:
We’ll add a 50-bar SMA to our data request. - Updating the Data Request:
Now inside the ‘sc.graph_data_request()’ method include a new parameter: sg_data=”ID2.[SG1]”. This adds the 50-bar SMA to the data request. ‘sg_data’ is the parameter to specify the Subgraph data we want to subscribe to. ‘ID2.[SG1]’ is the identifier for the 50-bar SMA. ‘ID2’ refers to the study ID, and ‘[SG1]’ refers to the subgraph number. In Sierra Chart, every chart and subgraph has a unique identifier like this. - Fetching the Data:
As before, we’ll call the ‘sc.get_response_queue().get()’ method to fetch and print(msg.df) to display the chart data in a dataframe format.
By adding this code snippet, we’re able to request Subgraph data from Sierra Charts. For more detailed documentation and examples click here.
Now, let’s refactor our code to summon the live data:
# SC-Py Import
from trade29.sc.bridge import SCBridge# Create an instance of the SCBridge class
sc = SCBridge()
# Request data from SCBridge
sc.graph_data_request(
key="xx", # ES key
base_data='1;2;3;4', # Open, High, Low, Close
sg_data="ID2.[SG1]", # 50 bar SMA
historical_init_bars=50, # Historical data: Fetches 50 bars initially
realtime_update_bars=10, # Real-time updates: Rolling window of the last 10 bars
on_bar_close=True, # Update on bar close
include_bar_index=True # Include bar index
)
while True:
msg = sc.get_response_queue().get()
print(msg.df)
Now that we are working with live market data, the API works in two phases: the Historical init phase and the Real-time updates phase. Initially, when the API fetches 50 historical bars, we first receive 50 historical bars as an initialization and then receive a rolling window of the last 10 bars on each update.
- Requesting Live Data:
The ‘sc.graph_data_request()’ method is always our go-to for requesting both historical and live data. In this snippet, we’re setting it up to request live data. - Historical bars:
‘historical_init_bars=50’ is how we specify how many historical bars we want to initialize. - Realtime Update Bars:
‘realtime_update_bars=10’ is how we specify how many bars we want to update in real-time. - Continuous Fetching Realtime Data:
The ‘while True:’ block is a loop that will run indefinitely, fetching and printing the latest data from SCBridge as it arrives. The line ‘msg = sc.get_response_queue().get()’ fetches the latest data, and ‘print(msg.df)’ prints it out in a dataframe format.
We have created a live data feed that continuously updates with the latest market data.
That about covers it. We have now tapped into all that Sierra Charts data right from Python. Aren’t you excited? I know I am. We’ve hit a pretty cool milestone and are getting closer to diving into the realm of Machine Learning, Deep Learning, and Generative AI, and how to apply these applications in algorithmic trading using the robust features of Sierra Charts. Feel free to check out SC-Py documentation for more detailed examples of the graph_data_request API.
In the world of computer science, machine learning, and automation, we’re only limited by the limits we place on our imagination. As algorithmic traders, watching something we’ve created take trades is eye-opening, beautiful, and inspirational. Keep up the good work and until next time, happy trading and coding.