-1

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:

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&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?

1 Answers1

-1

Maybe you can use Python to get the date, and create a global variable, and after the print statement, you can set the variable to the current date, and the coffee will check if it has already sent, that way it doesn't execute more than once in a day.

import datetime
# This Gets The Day Of The Month
todaysDateNumber = int(datetime.datetime.now().strftime("%d"))
dateNumberTradeSent = 0
if todaysDateNumber == dateNumberTradeSent:
    print("The API has already been used once today, try again tomorrow!")
    return
else:
    # Call Your Trade Sending Code Here
    # After The Print Statement That Shows That The Trade Was Sent:
    global dateNumberTradeSent
    dateNumberTradeSent = int(datetime.datetime.now().strftime("%d"))
Da Mahdi03
  • 1,468
  • 1
  • 9
  • 18
  • Perhaps, but is there a way to use poloniex server time instead? Because that's the time I need it to go by. I don't want it to place a second trade until it reaches exactly midnight poloniex server time. This may provide what would be needed: https://poloniex.com/support/api/#rest_public – A C Marston Oct 12 '18 at 03:10
  • To make things more easy to understand, I've updated the question with the full script. – A C Marston Oct 12 '18 at 03:24