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
#3
We can substitute some other value for it.we become what we behold
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create X Number of Variables and Assign Data RockBlok 8 872 Nov-14-2023, 08:46 AM
Last Post: perfringo
  Better python library to create ER Diagram by using pandas data frames as tables klllmmm 0 989 Oct-19-2023, 01:01 PM
Last Post: klllmmm
  Likert survey data plot error Andrzej_Andrzej 6 1,307 Jul-16-2023, 10:11 PM
Last Post: deanhystad
  Plot a pandas data fram via pyqtgraph with an modul import and qt designer widget Nietzsche 0 802 May-29-2023, 02:42 PM
Last Post: Nietzsche
Thumbs Up Python 3 Jupyter notebook ternary plot data nicholas 0 897 Jan-21-2023, 05:01 PM
Last Post: nicholas
  Create a function for writing to SQL data to csv mg24 4 1,111 Oct-01-2022, 04:30 AM
Last Post: mg24
  Exporting Stock Fundamental Data to a CSV file with yahoo_fin DustinKlent 2 4,638 Aug-01-2022, 06:08 PM
Last Post: paulyan
  SMA (simple moving avg) Not receiving Data (stock prices). gdbengo 2 1,407 Jul-31-2022, 08:20 PM
Last Post: paulyan
  Yfinance - Intraday stock data with yf.download diogo_80 2 5,872 Apr-29-2022, 05:07 AM
Last Post: processingclouds
  simple html page with update data korenron 3 2,586 Nov-15-2021, 09:31 AM
Last Post: jamesaarr

Forum Jump:

User Panel Messages

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