Python Forum
Create simple live plot of stock data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create simple live plot of stock data
#1
The below code us just some start of learning python quicker by using a real world example and trying out different things. It's nothing that will be used later on so nothing more then learning material. Will go from raw dirty to refine using functions etc.

The below code just grabs the current price of a crypto asset and adds it to the dictionary together with the date (in datetime format). I'm trying to create a (live) plot (part not worked on) but I'm a bit stuck in how to make sure equal values in consecutive key/value pairs don't get added to the dictionary. This would result in an unreadable plot.

I have the following questions:

- Is it better to filter the value up front? How can I handle this requirement of not having two consecutive dictionary items with the same value?
- what time format would be best for plotting?

* I have not looked at plot yet
* x is in there just not to test and not have it run forever
* the file save is just there for later usage


import json
import requests
import csv
import datetime
import matplotlib.pyplot as plt
import time

last_trade = {}

for x in range (0,5):
   
    x -= 1
    uri = "https://api.kraken.com/0/public/Ticker?pair=XMREUR"
    date = datetime.datetime.now()
    r = requests.get(uri)
    result = r.json()["result"]['XXMRZEUR']["c"][0]	
    last_trade.update ({date:result})
    print (last_trade)
    time.sleep(180)

with open('ticker.csv', 'w') as csv_file:

    writer = csv.writer(csv_file)
    for key, value in last_trade.items():
    writer.writerow([key, value])


x,y = zip(*sorted(last_trade.items()))
plt.plot(x,y)
Reply
#2
Your code does not run, so I made some changes.

#!/usr/bin/python3
import json
import requests
import csv
import datetime
import matplotlib.pyplot as plt
import time

last_trade = {}

for x in range (0,5):
    x -= 1
    uri = "https://api.kraken.com/0/public/Ticker?pair=XMREUR"
    date = datetime.datetime.now()
    r = requests.get(uri)
    result = r.json()["result"]['XXMRZEUR']["c"][0].
    last_trade.update ({date:result})
    print (last_trade)
    time.sleep(3)

with open('ticker.csv', 'w') as csv_file:
    writer = csv.writer(csv_file)
    for key, value in last_trade.items():
        writer.writerow([key, value])

x,y = zip(*sorted(last_trade.items()))
plt.plot(x,y)
plt.show()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Plotting sum of data files using simple code Laplace12 3 444 Jun-16-2021, 02:06 PM
Last Post: BashBedlam
  xlsxwriter + mplfinance: Plot Stock Chart in Excel Worksheet KMV 1 421 Mar-09-2021, 09:44 PM
Last Post: KMV
  Suggestions for a simple data analysis program t4keheart 0 372 Mar-08-2021, 03:45 PM
Last Post: t4keheart
  Exporting Stock Fundamental Data to a CSV file with yahoo_fin DustinKlent 1 1,444 Mar-02-2021, 02:07 AM
Last Post: detlefschmitt
  Plot data from CSV allen04 2 528 Jan-03-2021, 10:30 AM
Last Post: Axel_Erfurt
  How to plot intraday data of several days in one plot mistermister 3 610 Dec-15-2020, 07:43 PM
Last Post: deanhystad
  I want to create small multiples grouped bar plot, and facing some aesthetics issuess dev_kaur 0 335 Dec-05-2020, 08:49 AM
Last Post: dev_kaur
  An important question is how to create a zigzag in price data? epsilon 0 305 Nov-18-2020, 08:06 PM
Last Post: epsilon
  Overwrite previous live data. Makada 2 648 Nov-07-2020, 07:40 PM
Last Post: Makada
  Creating Complex Color Spectrum Plot From Data JoeDainton123 2 628 Sep-15-2020, 08:09 AM
Last Post: DPaul

Forum Jump:

User Panel Messages

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