그래서 저는 파이썬 대화 형 브로커 API를 사용하고 있으며 대화 형 브로커로 제한 주문을하려고합니다. 내가 가진 코드는 다음과 같습니다.
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import Order
import threading
import time
class TradingApp(EWrapper, EClient, Contract, Order):
def __init__(self):
EClient.__init__(self, self)
def nextValidOrderId(self, orderId):
super().nextValidId(orderId)
self.nextValidOrderId = orderId
print("NextValidId:", orderId)
def websocket_con():
app.run()
app = TradingApp()
app.connect("127.0.0.1", 7497, clientId=1)
con_thread = threading.Thread(target=websocket_con, daemon=True)
con_thread.start()
time.sleep(1)
contract=Contract()
contract.symbol = 'AMD'
contract.secType = 'STK'
contract.currency = 'USD'
contract.exchange = 'SMART'
order=Order()
order.action = 'BUY'
order.orderType = 'LMT'
order.totalQuantity = 1
order.lmtPrice = 10
app.placeOrder(app.nextValidOrderId,contract,order)
time.sleep(5)
이 코드는 이미 만들어진 종이 거래 계좌에 연결할 수 있어야합니다. 오류를 받고 있지만 :
--- Logging error ---
Traceback (most recent call last):
File "C:\Users\B1880\Anaconda3\lib\logging\__init__.py", line 1025, in emit
msg = self.format(record)
File "C:\Users\B1880\Anaconda3\lib\logging\__init__.py", line 869, in format
return fmt.format(record)
File "C:\Users\B1880\Anaconda3\lib\logging\__init__.py", line 608, in format
record.message = record.getMessage()
File "C:\Users\B1880\Anaconda3\lib\logging\__init__.py", line 369, in getMessage
msg = msg % self.args
File "C:\Users\B1880\AppData\Roaming\Python\Python37\site-packages\ibapi-9.76.1-py3.7.egg\ibapi\object_implem.py", line 12, in __repr__
return str(id(self)) + ": " + self.__str__()
File "C:\Users\B1880\AppData\Roaming\Python\Python37\site-packages\ibapi-9.76.1-py3.7.egg\ibapi\contract.py", line 85, in __str__
str(self.conId),
AttributeError: 'TradingApp' object has no attribute 'conId'
Call stack:
File "C:\Users\B1880\Anaconda3\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\Users\B1880\Anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\B1880\Anaconda3\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "C:\Users\B1880\Anaconda3\lib\site-packages\traitlets\config\application.py", line 664, in launch_instance
app.start()
File "C:\Users\B1880\Anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 583, in start
self.io_loop.start()
File "C:\Users\B1880\Anaconda3\lib\site-packages\tornado\platform\asyncio.py", line 153, in start
self.asyncio_loop.run_forever()
File "C:\Users\B1880\Anaconda3\lib\asyncio\base_events.py", line 538, in run_forever
self._run_once()
File "C:\Users\B1880\Anaconda3\lib\asyncio\base_events.py", line 1782, in _run_once
handle._run()
File "C:\Users\B1880\Anaconda3\lib\asyncio\events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "C:\Users\B1880\Anaconda3\lib\site-packages\tornado\ioloop.py", line 690, in <lambda>
lambda f: self._run_callback(functools.partial(callback, future))
File "C:\Users\B1880\Anaconda3\lib\site-packages\tornado\ioloop.py", line 743, in _run_callback
ret = callback()
File "C:\Users\B1880\Anaconda3\lib\site-packages\tornado\gen.py", line 787, in inner
self.run()
File "C:\Users\B1880\Anaconda3\lib\site-packages\tornado\gen.py", line 748, in run
yielded = self.gen.send(value)
File "C:\Users\B1880\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 365, in process_one
yield gen.maybe_future(dispatch(*args))
File "C:\Users\B1880\Anaconda3\lib\site-packages\tornado\gen.py", line 209, in wrapper
yielded = next(result)
File "C:\Users\B1880\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 268, in dispatch_shell
yield gen.maybe_future(handler(stream, idents, msg))
File "C:\Users\B1880\Anaconda3\lib\site-packages\tornado\gen.py", line 209, in wrapper
yielded = next(result)
File "C:\Users\B1880\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 545, in execute_request
user_expressions, allow_stdin,
File "C:\Users\B1880\Anaconda3\lib\site-packages\tornado\gen.py", line 209, in wrapper
yielded = next(result)
File "C:\Users\B1880\Anaconda3\lib\site-packages\ipykernel\ipkernel.py", line 300, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "C:\Users\B1880\Anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 536, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "C:\Users\B1880\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2858, in run_cell
raw_cell, store_history, silent, shell_futures)
File "C:\Users\B1880\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2886, in _run_cell
return runner(coro)
File "C:\Users\B1880\Anaconda3\lib\site-packages\IPython\core\async_helpers.py", line 68, in _pseudo_sync_runner
coro.send(None)
File "C:\Users\B1880\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3063, in run_cell_async
interactivity=interactivity, compiler=compiler, result=result)
File "C:\Users\B1880\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3254, in run_ast_nodes
if (await self.run_code(code, result, async_=asy)):
File "C:\Users\B1880\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-18-5f801bdffa6c>", line 41, in <module>
app.placeOrder(app.nextValidOrderId,contract,order)
File "C:\Users\B1880\AppData\Roaming\Python\Python37\site-packages\ibapi-9.76.1-py3.7.egg\ibapi\client.py", line 847, in placeOrder
self.wrapper.error(orderId, NOT_CONNECTED.code(), NOT_CONNECTED.msg())
File "C:\Users\B1880\AppData\Roaming\Python\Python37\site-packages\ibapi-9.76.1-py3.7.egg\ibapi\wrapper.py", line 56, in error
logger.error("ERROR %s %s %s", reqId, errorCode, errorString)
Unable to print the message and arguments - possible formatting error.
Use the traceback above to help find the error.
활성 및 소켓 클라이언트 활성화가 선택되어 있고 신뢰 IP 주소가 127.0.0.1인지 확인했습니다.이 로깅 오류가 발생하는 이유는 무엇입니까?
편집 : 새 코드는 다음과 같습니다.
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import Order
import threading
import time
class TradingApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def nextValidId(self, orderId):
self.nextValidId = orderId
def websocket_con():
app.run()
app = TradingApp()
app.connect("127.0.0.1", 7497, clientId=1)
con_thread = threading.Thread(target=websocket_con, daemon=True)
con_thread.start()
time.sleep(1)
contract=Contract()
contract.symbol = 'AMD'
contract.secType = 'STK'
contract.currency = 'USD'
contract.exchange = 'SMART'
order=Order()
order.action = 'BUY'
order.orderType = 'LMT'
order.totalQuantity = 1
order.lmtPrice = 10
app.placeOrder(app.nextValidId,contract,order)
time.sleep(5)
지금은 오류가 발생합니다.
ERROR <bound method TradingApp.nextValidId of <__main__.TradingApp object at 0x00000136780E7508>> 504 Not connected
class TradingApp(EWrapper, EClient):
당신이하고 싶었던 것 같아요 . Contract 및 Order 클래스를 확장 할 이유가 없습니다.
메소드 이름을 변경했습니다. 수퍼 클래스와 동일해야합니다. 메서드 이름을 변수 이름으로 재사용하지 마십시오.
#def nextValidOrderId(self, orderId):
def nextValidId(self, orderId):
self.nextValidOrderId = orderId
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다