user9280506
user9280506

Reputation:

Parse JSON from Bittrex

I have this code:

import urllib2
import json
from matplotlib.finance import candlestick2_ohlc
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import datetime as datetime
import numpy as np
import csv

l = []
date = []
date_intermediaire = []

response = urllib2.urlopen('https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-ETH&tickInterval=thirtyMin&_=')
data = json.load(response)

where data['result'][:]['T'] contains the time, data['result'][:]['V'] contains the volume ,data['result'][i]['O'] contains the opens, data['result'][i]['C'] contains the close,data['result'][:]['L'] contains the low, data['result'][:]['H'] contains the high

And actually I would like to make easier this defining r like this:

r['date'][i] = data['result'][i]['T']
r['open'][i] = data['result'][i]['O']
r['close'][i] = data['result'][i]['C']
r['max'][i] = data['result'][i]['H']
r['min'][i] = data['result'][i]['L']
r['volume'][i] = data['result'][i]['V']

But I don't know how to do this in Python.

Upvotes: 1

Views: 204

Answers (3)

Willian Vieira
Willian Vieira

Reputation: 736

you can work with pandas:

import pandas as pd
...
df = pd.DataFrame.from_dict(data['result']) 
df.columns=['volume','close','max','min','open','date','last_column'] 
df.head()

output:

      volume     close       max       min      open                 date  \
0  19.949007  0.056340  0.056560  0.056302  0.056340  2018-03-29T02:00:00   
1  28.811991  0.056302  0.056540  0.056301  0.056350  2018-03-29T02:30:00   
2  17.516028  0.056170  0.056488  0.056150  0.056302  2018-03-29T03:00:00   
3  33.393220  0.056030  0.056450  0.056020  0.056450  2018-03-29T03:30:00   
4  29.046574  0.055430  0.056140  0.055300  0.056030  2018-03-29T04:00:00   

   last_column  
0   354.123891  
1   511.145471  
2   311.468766  
3   594.549165  
4   521.328565  

Upvotes: 1

OneCricketeer
OneCricketeer

Reputation: 191738

If I understand correctly, you don't know what how to loop over the dictionary or list in the parsed JSON

results = data['result']
r = []
for res in results:
    value = {}
    value['date']= res['T']
     # TODO: Get other values 
    r.append(value)

Upvotes: 0

SCB
SCB

Reputation: 6149

You want to "reduce" your data here. You could use functools.reduce() here, but it probably would be easier to just write your own function. Something that cycles over each result and appends it to the relevant column.

def map_response(data):
    retval = {
      'date': [],
      'open': [],
      # ...
    }
    for result in data['result']:
       retval['date'].append(result['T'])
       retval['open'].append(result['O'])
       # ...

    return retval

You could probably also automate this a little bit more by describing the mappings.

def map_response(data):
    mappings = {'T': 'date', 'O': 'open', '...': '...'}
    retval = {val: [] for val in mappings.values()}
    for result in data['result']:
        for key, value: result.items():
             retval[mappings[key]] = value

    return retval

But to me that looks less clear, just saves repetition.

Upvotes: 0

Related Questions