Need some help for my python crypto bot

Hello! I’m building my own bot on python, in order to connect to exchange’s api and make it process some information for me. I’m still a beginner on python, i know some stuff but i still have a lot to learn.
I encountered some problems: my bot is able to fetch data from an exchange’s api (Bittrex) and prints the last orders for a currency choosen by me, so far everything is alright, but the problem is that i can’t make it print the orders in real time, i tried to create a while loop with an if statement that tells the bot to print ONLY orders with a bigger timestamp, it works and prints the new orders but the problem is that it keeps printing the same order over and over. I know that’s because of the while loop but i can’t solve it, could anyone give me an hint on how to handle this?
Also, i’d like to make it print the difference between buys and sells, any idea on how i can sort it out? is the piece of code created by me, feel free to correct it or use it if it could be useful!

    import time, requests, json




try:
    BU = requests.session() 
    URL = 'https://bittrex.com/api/v1.1/public/getmarkethistory?market=BTC-DOGE'
    r = BU.get(URL, timeout=(15, 10))
    time.sleep(1)
    MarketPairs = json.loads(r.content) 
    for element in MarketPairs['result']:
        id = element['Id']
        price = element['Price']
        tot = element['Total']
        time = element['TimeStamp']
        type = element['OrderType']

        if time > '2017-12-11T12:30:48.887':
            print type, " ", id, " ", tot, " ", price, " ", time
            time.sleep(1)

except Exception as e:
        print e
        print 'Error... possible timeout'
3 Likes

You need to fetch the api in your loop each time you request a different order.
What is happening with your code is you fetch the data once at the top…
You enter into for loop.
Then the json object is still the same within the loop.
Edit the code and try and post me the results.

3 Likes

I think i understood, so basically i placed in the api request in the wrong place? I should put it into the for loop?

1 Like

Yes.
What should happen is.
Enter into for loop.
Fetch data, get the order & print. Wait for 2 seconds or something(API may have request restrictions), then fetch data again and get the order & print.

2 Likes

Got it. Something like this? `import time, requests, json

import time, requests, json

 for element in MarketPairs['result']:
           BU = requests.session() 
           URL = 'https://bittrex.com/api/v1.1/public/getmarkethistory?market=BTC-DOGE'
           r = BU.get(URL, timeout=(15, 10))
           time.sleep(1)
           MarketPairs = json.loads(r.content) 
       
          id = element['Id']
          price = element['Price']
          tot = element['Total']
          time = element['TimeStamp']
          type = element['OrderType']

      
          print type, " ", id, " ", tot, " ", price, " ", time
          time.sleep(1)
1 Like

Yes… But have you actually run this code?
MarketPairs do not exist… Can’t even enter the for loop.

Also element does not exist.

2 Likes

Yes, this is what i’m trying to figure out right now, the problem is that i if i enter the request into the loop MarketPairs will not be defined anymore, since MarketPairs is the list that includes everything in the order

1 Like

You should decouple the for loop from marketpairs.
What does the for loop depend on? How many times do we need to loop?

2 Likes

Well, since i want it to print the difference between buys and sells it should be streaming for the entire day, so the loop should have no end

1 Like

Ok… So infinite loop.
Have you tried the while loop then?
while True:

1 Like

Well, if i make while True > request > for loop for element[MarketPairs] the result is the same, it still prints the same thing over and over

1 Like

OK. Can you post your updated code now?

1 Like

Tried this, but the error is that list indices must be integers, not str

i mport time, requests, json

while True:

BU = requests.session() 
URL = 'https://bittrex.com/api/v1.1/public/getmarkethistory?market=BTC-DOGE'
r = BU.get(URL, timeout=(15, 10))
time.sleep(1)
MarketPairs = json.loads(r.content)

element = MarketPairs['result']   
id = element['Id']
price = element['Price']
tot = element['Total']
time = element['TimeStamp']
type = element['OrderType']

  
print type, " ", id, " ", tot, " ", price, " ", time
time.sleep(1)
1 Like

What do you think of this?

import requests, json

while True:
import time
BU = requests.session()
URL = 'https://bittrex.com/api/v1.1/public/getmarkethistory?market=BTC-DOGE
r = BU.get(URL, timeout=(15, 10))
time.sleep(1)
MarketPairs = json.loads(r.content)

element = MarketPairs[‘result’]
id = element[0]
price = element[0]
tot = element[0]
time = element[0]
type = element[0]

print type, " ", id, " ", tot, " ", price, " ", time

time.sleep(1)

1 Like

Seems to work, the only problem is that it still prints the same order more times, my idea was to make it compare previous and last order, IF last is different from previous, print last, so that it can’t print 2 times the same order. So i know how to make it logically but not in terms of code yet

1 Like

It is because it is printing the first element in the json file over and over again.(element[0])
How do you know the last order & previous order ? By looking at the timestamp I guess?

1 Like

Exactly, if the timestamp and the total bought is the same of the previous order, it’s printing the same order

1 Like

You clearly need two results to compare then… Last result and current result…
The loop starts with the current result.
Loop ends and the current result becomes the last result…
Then we start a new loop with a current loop, compare it to the last result.

1 Like

So i should add one more loop? The first loop is the one that i already have, the one will read again the API and compare the two results?

1 Like

Yes.
First while loop is infinite…
The other one you should be able to break out from it once you are done with it…
Have a look at this quick:

1 Like

💰 YEN · YouTube ·️ YEN.CAMP 🧠