Python Forum
How can I make this Python script add a limit to trades made with API?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I make this Python script add a limit to trades made with API?
#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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make it so whatever I input into a script gets outputted on a different file spermatozwario 4 1,110 Nov-24-2024, 12:58 PM
Last Post: deanhystad
  Make entire script run again every 45 mo NDillard 0 881 Jan-23-2024, 09:40 PM
Last Post: NDillard
  Trying to make a board with turtle, nothing happens when running script Quascia 3 1,698 Nov-01-2023, 03:11 PM
Last Post: deanhystad
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 7,926 Jun-29-2023, 11:57 AM
Last Post: gologica
  Make console show after script was built with Pyinstaller --NOCONSOLE? H84Gabor 0 1,792 May-05-2022, 12:32 PM
Last Post: H84Gabor
  Make my py script work only on 1 compter tomtom 14 6,163 Feb-20-2022, 06:19 PM
Last Post: DPaul
  Can I Limit Value used by python pajd 9 3,578 Feb-10-2022, 09:54 PM
Last Post: pajd
  My .exe made using Python being detected as a virus 100grassfed 2 4,116 Jun-16-2021, 04:41 AM
Last Post: buran
  How to kill a bash script running as root from a python script? jc_lafleur 4 7,912 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  Make the script read from any directory falahfakhri 2 2,728 Jun-15-2020, 02:18 PM
Last Post: falahfakhri

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020