我正在尝试找到一些简单的设置来将交易从 TradingView 的警报发送到盈透证券。我已经设置了本地主机,运行了 ngrok,并且通过观看一些 YTB 视频创建了下面的代码。我还在交易视图的警报中添加了 webhook 的 URL。问题是每次出现警报时,我都会在 ngrok 中收到 500 内部服务器错误消息。并且交易不会执行。
**这是我尝试过的代码: **
`#imports
from datetime import datetime
from sanic import Sanic
from sanic import response
from ib_insync import *
#Create Sanic object called qpp
app = Sanic(__name__)
#app.ib = None
app.ctx.ib = None
#Create root / homepage
@app.route('/')
async def root(request):
return response.text('online')
#Listen for signals and submit BUY orders (webhook)
@app.route('/buy', methods=[ 'POST' ])
async def buy(request):
if request.method == 'POST':
#Check if we need to reconnect
await checkIfReconnect()
#Parse signal data
data = request.jason
order = MarketOrder('BUY', 0.00015)
contract = Crypto('BTC', 'PAXOS', 'USD')
trade = app.ib.placeOrder(contract,order)
#Wait for the order to be processed and check status
app.ctx.ib.sleep(1)
print(trade)
#Listen for signals and submit SELL orders (webhook)
@app.route('/sell', methods=[ 'POST' ])
async def sell(request):
if request.method == 'POST':
#Check if we need to reconnect
await checkIfReconnect()
#Parse signal data
data = request.jason
order = MarketOrder('SELL', 0.00015)
contract = Crypto('BTC', 'PAXOS', 'USD')
trade = app.ib.placeOrder(contract,order)
#Wait for the order to be processed and check status
app.ctx.ib.sleep(1)
print(trade)
#Reconnect to IB if connection is lost
async def checkIfReconnect():
if not app.ib.isDisconnected() or not app.ib.isConnected():
app.ib.disconnect()
app.ib = IB()
app.ib.connect('127.0.0.1',7496,clientId=1)
#Run app
if _name_ == '__main__':
#Connect to IB
app.ctx.ib = IB()
app.ctx.ib.connect('127.0.0.1',7496,clientId=1)
app.run(port=8080)`
**这是我得到的错误: ** { "description": "内部服务器错误", “状态”:500, "message": "应用程序遇到意外错误,无法继续。" }
**我还在终端中看到这些错误: **
ERROR: Exception occurred while handling uri: 'http://MYURL/buy'
Traceback (most recent call last):
File "handle_request", line 102, in handle_request
if TYPE_CHECKING:
File "/Users/MYURL/#imports.py", line 26, in buy
await checkIfReconnect()
File "/Users/MYURL/#imports.py", line 56, in checkIfReconnect
if not app.ib.isDisconnected() or not app.ib.isConnected():
^^^^^^
AttributeError: 'Sanic' object has no attribute 'ib'
**我正在使用: **
MacOS High Sierra 10.13.6
Visual Studio Code Version: 1.85.2 (Universal)
Trader Workstation Version: Stable (10.19.2o) 20240612
Ngrok: 3.15.1
来自ngrok的PM在这里。根据错误,
Sanic
对象(app
)没有id
属性,根据文档它没有:https://sanic.readthedocs.io/en/stable/sanic /api/app.html#module-sanic.app