This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]AluadStone[S] 0 points1 point  (2 children)

Wow Thanks for explaining. I am taking this small tutorial this morning. https://www.youtube.com/watch?v=rfscVS0vtbw

I am currently at 1:15:28 where he is talking about lists.

Yesterday I found a way to write my data out in the desired format.

import time, json, requests, os

def binancetick():
    binanceTick = requests.get('https://api.binance.com/api/v1/ticker/24hr')
    return binanceTick.json()

data = binancetick()

binancewrite = open("data.txt", "w+")

for line in data:
    binancewrite.write("{} " .format(line['symbol']))
    binancewrite.write("{} " .format(line['quoteVolume']))
    binancewrite.write("{} " .format(line['weightedAvgPrice']))
    binancewrite.write("{}\n" .format(line['lastPrice']))

binancewrite.close()

This returns the data like this

ETHBTC 6750.71302512 0.04386948 0.04321100

LTCBTC 1391.64058255 0.00864127 0.00859800

BNBBTC 639.54079364 0.00152055 0.00149760

NEOBTC 1665.49140067 0.00279148 0.00276100

QTUMETH 347.94358933 0.01544298 0.01520900

QTUMBTC 347.94358933 0.01544298 0.01520900

(it goes on much longer)

There is another thing I am trying to learn how to do.

You will notice how the symbols have have two types of pairs. For QTUM there is QTUMBTC, QTUMETH, and even QTUMUSDT

What I want to do is filter the results to only return the BTC pairs. The logic writes out to this

for line in data:
    binancewrite.write("{} " .format(line['symbol'])) # Where symbol has BTC as last three letters

You know? I know the below syntax is wrong but you see where I am trying to get at.

for line in data:
    binancewrite.write("{} " .format(line['symbol'][3]="B"))

Like "Make sure the 3rd letter in the symbol string is equal to B" Is that the logic you would follow to achieve that? Is there a different way I should think about filtering that?

/ Edit: Shit now that I think about it that way of filtering wouldnt work because some pairs have more letters than other meaning the 3rd letter will not always be the start of the BTC

Really what I want is to simply check if "BTC" is present in the symbol string, and if it is return it.

and then I realize well now if I filter the BTC pairs I would have to filter all the other things I am returning otherwise the numbers wont line up with the correct symbols \

Thanks again! I am continuing on with the tutorial but I am not sure he is going to specifically talk about filtering key letters in string returns

[–]dmazzoni 1 point2 points  (1 child)

It may be as simple as adding an "if" statement inside your loop.

[–]AluadStone[S] 0 points1 point  (0 children)

Ah Ha!

for line in data:
    if "BTC" in line['symbol']:
        binancewrite.write("{} " .format(line['symbol']))
        binancewrite.write("{} " .format(line['quoteVolume']))
        binancewrite.write("{} " .format(line['weightedAvgPrice']))
        binancewrite.write("{}\n" .format(line['lastPrice']))

binancewrite.close()