当一个Webhook触发(打开外汇交易)ngrok发送一个POST来执行我的Python脚本中的IF语句时,我的脚本必须返回一个无效的值给ngrok。ngrok给出的错误是一个500的内部服务错误,而我的flask错误给出的是 "TypeError: 视图函数没有返回一个有效的响应。该函数要么返回None,要么没有返回语句就结束了"。只有当[]被送到if x['position']==[]时,我才会得到这个错误。如果发送另一个值,一切都能正常工作。这个错误对我来说很糟糕,因为它导致POST在很短的时间内被重复发送(约10秒?这对我来说是不好的,因为它是一个交易机器人。在一个波动的市场中,交易在几秒钟内执行,我不希望错误在我的目标被击中后重新打开一个新的交易。我对消除错误后重新发送POST或基本面修复问题持开放态度。我已经尝试把返回的地方,但它似乎没有解决这个问题。愿意接受任何想法! 谢谢你的帮助!这是我的脚本。
这是我的脚本。错误很可能发生在If ['position']函数中,并且同样只有在提供[]的情况下才会发生。
from actions import *
from flask import Flask, request, abort
import oandapyV20
import oandapyV20.endpoints.positions as positions
from exampleauth import exampleAuth
# Create Flask object called app.
app = Flask(__name__)
# Create root to easily let us know its on/working.
@app.route('/')
def root():
return 'online'
@app.route('/webhook', methods=['POST'])
def webhook():
if request.method == 'POST':
accountID, access_token = exampleAuth()
api = oandapyV20.API(access_token=access_token)
client=oandapyV20.API(access_token)
r=positions.OpenPositions(accountID)
x=client.request(r)
if x['positions']==[]:
data = parse_webhook(request.get_data(as_text=True))
sell=data['side']
if data['side']=='sellEURAUD':
exec(open("marketTPSLEURAUDv2sell.py").read())
elif data['side']=='buyEURAUD':
exec(open("marketTPSLEURAUDv2buy.py").read())
elif data['side']=='buyUSDCAD':
exec(open("marketTPSLUSDCADv2buy.py").read())
elif data['side']=='sellUSDCAD':
exec(open("marketTPSLUSDCADv2sell.py").read())
elif data['side']=='buyEURUSD':
exec(open("marketTPSLEURUSDv2buy.py").read())
elif data['side']=='sellEURUSD':
exec(open("marketTPSLEURUSDv2sell.py").read())
else:
return'okay'
else:
return 'okay'
else:
return 'okay'
if __name__ == '__main__':
app.run()
else: return 'okay'
如果您的任何 exec
的被称为,不 return
是执行。 不清楚你想怎么补救。