Hack Forums
[Python] How can I make this Python script add a limit to trades made with API? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] How can I make this Python script add a limit to trades made with API? (/showthread.php?tid=5894524)



How can I make this Python script add a limit to trades made with API? - streetlawkarma - Oct 12, 2018

I'm putting together a python script to make trades on poloniex with the API, and so far I've got it to make trades when certain conditions are met, but I still need it to NOT place anymore trades for the rest of that day (I have the entire script looping every 60 seconds).

So far I have this script:


Code
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 = 'poloniex.com/tradingApi'
key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
secret = 'XXXXXXXXXXXXXXXXXXXXXXXXX'


def main():
   poloniexPrices = urlopen('poloniex.com/public?command=returnTicker').read()
   poloniexjson = json.loads(poloniexPrices)
   poloniexlastP = poloniexjson[The_Currency_Pair]['last']


   poloniexOCHL = urlopen('poloniex.com/public?command=returnChartData&currencyPair=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)

Anyway, after a trade has been placed, I need it to make sure that after the 60 second sleep, it doesn't make a second trade unless it is a new day/the day after the trade was made. (Could I use poloniex server time for this?)

So, if it has got as far as print (ordernumber) that means it has placed a trade. But how do I mark it as placed trade for the day or something and use it in the if float(poloniexlastP) > 0: for the next loop to make sure it doesn't place another one?


RE: How can I make this Python script add a limit to trades made with API? - m0noxide - Oct 12, 2018

There are a couple approaches to this, depending on where you want to get the information from

1. You could query the Poloniex API to get your order history (via the returnTradeHistory endpoint in their API), and check for the time difference
2. You store your own state, in the form of a file, or a simple DB like sqlite, and on startup, query the time difference for your last order
3. Etc..