0

我对编程比较了解,特别是关于我在通过亚马逊网络服务运行帖子请求和使用 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'], #want it to access whatever the payload message is, payload message I believe is what the alert from Trading_View will send
    "qty": 4,
    "side": "buy",
    "type": "limit",
    "limit_price": webhook_message['close'],
    "time_in_force": "gtc",
    "order_class": "bracket",
    "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, json=data, 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 通过 amazon Web 服务 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

感谢所有帮助。

4

1 回答 1

0

您需要将依赖项添加到您的 requirements.txt 文件管理器或将代码添加到主项目目录之外的供应商目录

于 2020-11-11T18:51:51.433 回答