【发布时间】:2021-05-08 12:02:51
【问题描述】:
我对编码和 aws chalice 还是很陌生。我尝试编写一个从交易视图获取消息并根据信号执行订单的代码。 我在本地测试了代码,一切正常,但是当我测试 Rest API 时,出现以下错误: {"message":"缺少身份验证令牌"} 我通过“aws configure”设置了我的凭据,如下所述:https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html 我还在我的 aws 文件夹中创建了一个 config.txt 文件,并通过“aws configure get”检查了我的设置,它们很好。 一开始的索引功能也起作用了,所以我的代码应该有问题吗? 我更改了一些值并删除了一些函数和策略部分,但代码看起来有点像这样:
from chalice import Chalice
from datetime import datetime
from binance.client import Client
from binance.enums import *
import ccxt
exchange = ccxt.binance({
'apiKey': 'KEY',
'secret': 'SECRET',
'enableRateLimit': True,
'options': {
'defaultType': 'future',
},
})
def buy_order(quantity, symbol, order_type = ORDER_TYPE_MARKET,side=SIDE_BUY,recvWindow=5000):
try:
print("sending order")
order = client.futures_create_order(symbol = symbol, type = order_type, side = side, quantity = quantity,recvWindow=recvWindow)
print(order)
except Exception as e:
print("an exception occured - {}".format(e))
return False
return True
app = Chalice(app_name='tradingview-webhook-alert')
indicator1 = "x"
indicator2 = "y"
TRADE_SYMBOL = "Test123"
in_position = False
def diff_time(time1, time2):
fmt = '%Y-%m-%dT%H:%M:%SZ'
tstamp1 = datetime.strptime(time1, fmt)
tstamp2 = datetime.strptime(time2, fmt)
if tstamp1 > tstamp2:
td = tstamp1 - tstamp2
else:
td = tstamp2 - tstamp1
td_mins = int(round(td.total_seconds() / 60))
return td_mins
@app.route('/test123', methods=['POST'])
def test123():
global indicator1, indicator2
request = app.current_request
message = request.json_body
indicator = message["indicator"]
price = message["price"]
value = message["value"]
if indicator == "indicator1":
indicator1 = value
if indicator == "indicator2":
indicator2 = value
if in_position == False:
if (indicator1 >123) & (indicator2 < 321):
balance = exchange.fetch_free_balance()
usd = float(balance['USDT'])
TRADE_QUANTITY = (usd / price)*0.1
order_succeeded = buy_order(TRADE_QUANTITY, TRADE_SYMBOL)
if order_succeeded:
in_position = True
return {"test": "123"}
我使用 Insomnia 在本地对其进行了测试,并在那里和我的浏览器中尝试了 Rest API 链接,两者都显示相同的错误消息。我的测试方法是错误的还是代码?但即便如此,当我再次从头开始包含索引函数时,为什么 Rest API 链接不起作用?如果我从头开始尝试索引功能,我会得到 {"message": "Internal server error"} 。 这可能是一个非常基本的问题,但我在网上找不到答案。 任何帮助将不胜感激!
【问题讨论】:
标签: python amazon-web-services chalice