从 Docker 容器内的交互式代理检索数据

问题描述 投票:0回答:1

我有一个 docker 容器和一个包含以下代码的脚本:

    import backtrader as bt
    import pandas as pd
    
    class MyStrategy(bt.Strategy):
    
        def __init__(self):
            print('initializing strategy')
    
        def next(self):
            print('next')
    
    def start():
        print('starting backtrader')
        cerebro = bt.Cerebro()
    
        store = bt.stores.IBStore(host = '127.0.0.1', port=7497, _debug = True)
        data = store.getdata(dataname='EUR.USD', sectype = 'CASH', exchange = 'IDEALPRO', timeframe = bt.TimeFrame.Seconds)
        print(data.LIVE)
        cerebro.resampledata(data, timeframe = bt.TimeFrame.Seconds, compression = 15)
        


        # spy_prices = pd.read_csv("SPY.csv", index_col='Date',parse_dates=True)
        # feed_spy  = bt.feeds.PandasData(dataname=spy_prices)
        # cerebro.adddata(feed_spy)
    
    
        cerebro.addstrategy(MyStrategy)
        cerebro.run()
    
    start()

我正在使用运行在7497端口上的模拟交易账户和配置:

上面脚本的输出如下:

starting backtrader
4
<error id=-1, errorCode=502, errorMsg=Couldn't connect to TWS.  Confirm that "Enable ActiveX and Socket Clients" is enabled on the TWS "Configure->API" menu.>
<error id=-1, errorCode=502, errorMsg=Couldn't connect to TWS.  Confirm that "Enable ActiveX and Socket Clients" is enabled on the TWS "Configure->API" menu.>
<error id=-1, errorCode=502, errorMsg=Couldn't connect to TWS.  Confirm that "Enable ActiveX and Socket Clients" is enabled on the TWS "Configure->API" menu.>
<error id=-1, errorCode=502, errorMsg=Couldn't connect to TWS.  Confirm that "Enable ActiveX and Socket Clients" is enabled on the TWS "Configure->API" menu.>
initializing strategy

由于与TWS的连接失败,因此def next(self):不会运行。每当加载数据(例如使用间谍价格)时都会运行 def next(self)

我认为由于我在 docker 容器内工作,因此这些 store = bt.stores.IBStore(host = '127.0.0.1', port=7497, _debug = True) 设置的连接不成功。楼主应该不一样吧?因为这是默认主机?或者我应该在 docker 容器内映射一个端口?

dockerfile如下:

# Arguments
ARG IMAGE_VARIANT=slim-buster
ARG PYTHON_VERSION=3.9.5

# Base image
FROM python:${PYTHON_VERSION}-${IMAGE_VARIANT} as py3

# Working directory
WORKDIR data

# Copy requirements.txt
COPY requirements.txt .

# Pip and apt-get install packages
RUN python -m pip install --upgrade pip
RUN apt-get update && \
    apt-get install -y libpq-dev gcc
# Pip install dependencies
RUN pip install pandas
RUN pip install backtrader
RUN pip install IbPy2

# Remove requirements file, no longer needed
RUN rm requirements.txt

也许由于 TWS(交互式经纪商的交易者工作站)在端口 7947 上运行,我必须从 docker 连接到该端口。通过公开随机端口(可能是 7400)然后使用 docker run -it -p 7400:7497 -v ${PWD}:/data image_name bash 然后使用 python main.py 来实现此目的。

   Add EXPOSE 7400 to docker file
 Changing to: store = bt.stores.IBStore(host = 'host.docker.internal', port=7400, _debug = True)

    Run in terminal: docker run -it -p 7400:7497 -v ${PWD}:/data image_name bash
    Run in terminal/container: python main.py

-p 7400:7497 映射端口。最后我想通过使用 docker compose 来做一些事情。因为我不想从终端工作,而是从 VScode 工作。(顺便说一下,建议的方法不起作用)

python docker networking port
1个回答
0
投票

尝试使用代码:

store = bt.stores.IBStore(host='host.docker.internal', port=7497, _debug=True)

在码头工人:

EXPOSE 7497

运行全部:

 docker run -it -p 7497:7497 -v ${PWD}:/data your_image_name bash

如果您仍然遇到问题。请确保:

  • 主机上的防火墙允许 TWS 端口上的传入连接。

  • Docker 容器与运行在同一网络上 TWS应用程序。

© www.soinside.com 2019 - 2024. All rights reserved.