LIVE DATA | PLOT SIMPLE CALENDAR SPREAD | INTERACTIVE BROKERS (IB) | PYTHON | MATPLOTLIB
LIVE DATA | PLOT SIMPLE CALENDAR SPREAD | INTERACTIVE BROKERS (IB) |
v154
2023115
? Subscribe to this channel:: ? https://bit.ly/3ysz8kc
?Join our FREE Discord:: ? https://bit.ly/3QBCSsj
--------------------
Code here: ? https://bit.ly/46hYRKD
# Real-Time Price and Spread Plotting with IB-InSync
This script provides an example of how to use the `ib_insync` library to request real-time tick-by-tick data from Interactive Brokers (IB) and plot the prices of two contracts as well as the spread between them. The script connects to the TWS (Trader Workstation) or Gateway application via the IB API and continuously updates the price plots based on the received tick data.
## Prerequisites
Before running the script, make sure you have the following:
- Python 3.x installed
- `ib_insync` library installed (`pip install ib_insync`)
- Access to the TWS or Gateway application provided by Interactive Brokers
## Getting Started
1. Import the necessary modules:
```python
import asyncio
import matplotlib.pyplot as plt
from ib_insync import IB, Contract, TickByTickAllLast
```
2. Define an `async` function, `market_data()`, to handle the real-time data retrieval and plotting:
```python
async def market_data():
# Create an IB object and connect to the TWS or Gateway
ib = IB()
await ib.connectAsync('localhost', 4002, clientId=1)
# Define the first contract
contract1 = Contract(conId=604258925, symbol='FDAX', secType='FUT', exchange='EUREX', currency='EUR')
await ib.qualifyContractsAsync(contract1)
# Define the second contract
contract2 = Contract(conId=540729504, symbol='FDAX', secType='FUT', exchange='EUREX', currency='EUR')
await ib.qualifyContractsAsync(contract2)
# Request tick-by-tick data for both contracts
ticker1 = ib.reqTickByTickData(contract1, 'AllLast')
ticker2 = ib.reqTickByTickData(contract2, 'AllLast')
# Create lists to store timestamps and prices for each contract
timestamps1 = []
prices1 = []
timestamps2 = []
prices2 = []
spreads = []
# Create subplots for each contract's price plot and the spread plot
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True)
# Set labels and titles for the first subplot
ax1.set_xlabel('Timestamp')
ax1.set_ylabel('Price')
ax1.set_title('Price Plot - Contract 1')
ax1.grid(True)
# Set labels and titles for the second subplot
ax2.set_xlabel('Timestamp')
ax2.set_ylabel('Price')
ax2.set_title('Price Plot - Contract 2')
ax2.grid(True)
# Set labels and titles for the third subplot (spread plot)
ax3.set_xlabel('Timestamp')
ax3.set_ylabel('Spread')
ax3.set_title('Spread Plot')
ax3.grid(True)
while True:
# Pause execution for 2 seconds
await asyncio.sleep(2)
# Get the latest timestamp and price for contract 1
timestamp1 = ticker1.time
price1 = ticker1.last
print('Contract 1:', timestamp1, price1)
# Get the latest timestamp and price for contract 2
timestamp2 = ticker2.time
price2 = ticker2.last
print('Contract 2:', timestamp2, price2)
# Skip calculation if any price is NaN
if price1 is None or price2 is None:
continue
# Add the timestamp and price to the respective lists for contract 1
timestamps1.append(timestamp1)
prices1.append(price1)
# Add the timestamp and price to the respective lists for contract 2
timestamps2.append(timestamp2)
prices2.append(price2)
# Calculate the spread between the two contracts
spread = price1 - price2
spreads.append(spread)
# Plot the prices for contract 1
ax1.plot(timestamps1, prices1, color='blue')
ax1.figure.canvas.draw()
# Plot the prices for contract 2
ax2.plot(timestamps2, prices2, color='red')
ax2.figure.canvas.draw()
# Plot the spread
ax3.plot(timestamps2, spreads, color='green')
ax3.figure.canvas.draw()
# Pause briefly to update the plots
plt.pause(0.01)
```
3. Create an `async` function, `main()`, to run the `market_data()` function within the default event loop:
```python
async def main():
# Run the market_data function within the default event loop
await market_data()
```
4. Run the `main()` function within the `__main__` block to start the script:
```python
if __name__ == '__main__':
# Run the main function
asyncio.run(main())
```
## Customization
- Modify the connection settings (`localhost`, `4002`, `clientId`) in the `ib.connectAsync()` line to match your TWS or Gateway configuration.
- Adjust the contract details (`conId`, `symbol`, `secType`, `exchange`, `currency`) for `contract1` and `contract2` to match your desired contracts.
- Customize the plot labels, titles, colors, and other styling options to suit your preferences.
#python #interactivebrokers #matplotlib
Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «LIVE DATA | PLOT SIMPLE CALENDAR SPREAD | INTERACTIVE BROKERS (IB) | PYTHON | MATPLOTLIB», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.
Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.
Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!
Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.