如何解决如何修复在本地服务器上正常运行但部署到AWS后无法运行的程序?
我对编程比较了解,尤其是在通过亚马逊Web服务运行发布请求和使用API请求方面遇到的问题。
我目前在下面编写了一个程序。
from chalice import Chalice
import requests,json
import alpaca_Trade_api as Tradeapi
app = Chalice(app_name='Tradingview-webhook-alerts')
API_KEY = 'API'
SECRET_KEY = 'SECRET'
BASE_URL = "https://paper-api.alpaca.markets"
ORDERS_URL = "{}/v2/orders".format(BASE_URL)
HEADERS = {'APCA-API-KEY-ID': API_KEY,'APCA-API-secret-key': SECRET_KEY}
@app.route('/GDX',methods=['POST'])
def GDX():
request = app.current_request
webhook_message = request.json_body
p = 1-(webhook_message['close'] / webhook_message['high'])
if p<.0175: #if the high price for the 15m candle is 3% higher than the close,thee excution will not occur
data = {
"symbol": webhook_message['ticker'],#want it to access whatever the payload message is,payload message I believe is what the alert from Trading_View will send
"qty": 8,"side": "buy","type": "limit","limit_price": webhook_message['close'],"time_in_force": "gtc","order_class": "bracket",#need to find out the average max profit per Trade
"take_profit": {
"limit_price": webhook_message['close'] * 1.0085 #take 0.6%% profit
},"stop_loss": {
"stop_price": webhook_message['close'] * 0.95,#stop loss of 6%
"limit_price": webhook_message['close'] * 0.93
}
}
r = requests.post(ORDERS_URL,json=data,headers=HEADERS)
response = json.loads(r.content)
print(response)
print(p)
return {
'message': 'I bought the stock!','webhook_message': webhook_message
}
else:
return{
'message': 'stock not purchased','webhook_message': webhook_message
}
@app.route('/buy_SLV',methods=['POST'])
def buy_stock():
request = app.current_request
webhook_message = request.json_body
data = {
"symbol": webhook_message['ticker'],payload message I believe is what the alert from Trading_View will send
"qty": 4,"take_profit": {
"limit_price": webhook_message['close'] * 1.008 #take 1% profit
},"stop_loss": {
"stop_price": webhook_message['close'] * 0.95,#stop loss of 2%
"limit_price": webhook_message['close'] * 0.94
}
}
r = requests.post(ORDERS_URL,headers=HEADERS)
response = json.loads(r.content)
print(response)
print(response.keys())
return {
'message': 'I bought the stock!','webhook_message': webhook_message
}
@app.route('/GDX_UpperBB',methods=['POST'])
def GDX_UpperBB():
request = app.current_request
webhook_message = request.json_body
api = Tradeapi.REST(API_KEY,SECRET_KEY,base_url=BASE_URL)
ids = []
orders = api.list_orders(
limit=100,nested=True # show nested multi-leg orders
)
GDX_orders = [o for o in orders if o.symbol == 'GDX']
for i in GDX_orders:
ids.append(i.id)
if len(orders)>0:
print(ids)
for i in ids:
api.cancel_order(i)
else:
print('there are no orders')
return{
'message': 'I bought the stock!','webhook_message': webhook_message
}
前两种方法运行良好(通过AWS和在本地服务器上)。第三种方法(GDX_UpperBB)是什么导致一切停止工作。当我在本地服务器上运行该程序并调用GDX_UpperBB方法时,它的执行没有问题。但是当我通过chalice通过亚马逊网络服务API部署程序时,得到了502 BadGateway响应,并返回了"message": "Internal server error"
。
当我进入AWS并测试方法时,这是我得到的控制台响应(我删除了响应的前半部分,因为响应时间很长,并且所有内容都说运行成功了)
Mon Oct 26 00:46:51 UTC 2020 : Received response. Status: 200,Integration latency: 15 ms
Mon Oct 26 00:46:51 UTC 2020 : Endpoint response headers: {Date=Mon,26 Oct 2020 00:46:51 GMT,Content-Type=application/json,Content-Length=127,Connection=keep-alive,x-amzn-RequestId=621b56f9-6bee-43af-8fe2-7f2cbeb7420e,X-Amz-Function-Error=Unhandled,x-amzn-Remapped-Content-Length=0,X-Amz-Executed-Version=$LATEST,X-Amzn-Trace-Id=root=1-5f961c7b-0a211c4e04be837554d0857f;sampled=0}
Mon Oct 26 00:46:51 UTC 2020 : Endpoint response body before transformations: {"errorMessage": "Unable to import module 'app': No module named 'alpaca_Trade_api'","errorType": "Runtime.ImportModuleError"}
Mon Oct 26 00:46:51 UTC 2020 : Lambda execution Failed with status 200 due to customer function error: Unable to import module 'app': No module named 'alpaca_Trade_api'. Lambda request id: 621b56f9-6bee-43af-8fe2-7f2cbeb7420e
Mon Oct 26 00:46:51 UTC 2020 : Method completed with status: 502
感谢所有帮助。
解决方法
您需要将依赖项添加到requirements.txt文件管理器中,或将代码添加到主项目目录之外的供应商目录中
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。