我正在整理一个python脚本以使用API在poloniex上进行交易,到目前为止,我已经在满足某些条件时进行了交易,但是我仍然需要它不再进行其余交易那天(我的整个脚本每60秒循环一次)。
到目前为止,我有这个脚本:
import requests
import urllib.request
import urllib.parse
import http.client
import hashlib
import hmac
import time
import json
from urllib.request import urlopen
The_Currency_Pair = input('Which Currency Pair?\nPAIRS TO CHOOSE FROM:\nUSDT_BTC\nUSDT_XRP\nUSDT_ETH\nUSDT_BCH\nUSDT_STR\nUSDT_LTC\nUSDT_ETC\nUSDT_XMR\n')
api = 'https://poloniex.com/tradingApi'
key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
secret = 'XXXXXXXXXXXXXXXXXXXXXXXXX'
def main():
poloniexPrices = urlopen('https://poloniex.com/public?command=returnTicker').read()
poloniexjson = json.loads(poloniexPrices)
poloniexlastP = poloniexjson[The_Currency_Pair]['last']
poloniexOCHL = urlopen('https://poloniex.com/public?command=returnChartData¤cyPair=USDT_BCH&start=1538352000&period=86400').read()
poloniexOCHLjson = json.loads(poloniexOCHL)
poloniexlasthigh = poloniexOCHLjson[-2]['high']
print ('Last Price')
print (poloniexlastP)
print ('----------------------------------------')
print ('Last Day High')
print (poloniexlasthigh)
print ('----------------------------------------')
data = {
'command': 'returnBalances',
'nonce' : int(time.time() * 1000)
}
data = urllib.parse.urlencode(data).encode()
signature = hmac.new(secret.encode(), data, hashlib.sha512)
headers = {
'Key' : key,
'Sign': signature.hexdigest()
}
request = urllib.request.Request(
url=api, data=data, headers=headers, method='POST'
)
text = urllib.request.urlopen(request).read().decode()
print ('MY ACCOUNT BALANCE')
try:
print(json.loads(text)['USDT'])
except:
print(text)
print ('-----------------------------------------')
if float(poloniexlastP) > 0:
print ('PLACING TRADE')
print ('-----------------------------------------------')
parms = {"command":"buy",
"currencyPair":The_Currency_Pair,
"rate":100,
"immediateOrCancel":1,
"amount":0.01,
"nonce":int(time.time() * 1000)}
parms = urllib.parse.urlencode(parms).encode()
signature = hmac.new(secret.encode(), parms, hashlib.sha512)
headers = {'Key' : key,
'Sign': signature.hexdigest()}
request = urllib.request.Request(
url=api, data=parms, headers=headers, method='POST')
text = urllib.request.urlopen(request).read().decode()
ordernumber = (json.loads(text)['orderNumber'])
print ('Order Number:')
print (ordernumber)
while True:
main()
time.sleep(60)
无论如何,在进行交易后,我需要确保60秒的睡眠后,除非进行了新的一天/交易完成的第二天,否则就不会进行第二次交易。 (我可以为此使用poloniex服务器时间吗?)
因此,如果它已经达到print (ordernumber)
,则表示已经进行了交易。但是,如何将其标记为当天或某天的交易,并在if float(poloniexlastP) > 0:
中将其用于下一个循环,以确保它不会再进行其他交易?
答案 0 :(得分:0)
您可能想要重置某种标志。也许遵循以下原则:
print ('Order Number:')
print (ordernumber)
return True # You've made a trade
return False # You've didn't make a trade
have_traded = False
while True:
if not have_traded:
have_traded = main()
time.sleep(60)
if new_date_function():
have_traded = False
答案 1 :(得分:0)
如果您正在使用Flask之类的应用服务器,则可以轻松设置限制器
from flask_limiter import Limiter
app = Flask(__name__)
limiter = Limiter(app, global_limits=["100 per hour", "20 per minute"])
然后可以使用定义的限制器装饰每个端点:
@app.route("/slow")
@limiter.limit("1 per day")
def slow():
return "24"
@app.route("/fast")
def fast():
return "42"
@app.route("/ping")
@limiter.exempt
def ping():
return 'PONG'