Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

candelstick chart matplotlib

import pandas as pd
import matplotlib.pyplot as plt
  
# DataFrame to represent opening , closing, high 
# and low prices of a stock for a week
stock_prices = pd.DataFrame({'open': [36, 56, 45, 29, 65, 66, 67],
                             'close': [29, 72, 11, 4, 23, 68, 45],
                             'high': [42, 73, 61, 62, 73, 56, 55],
                             'low': [22, 11, 10, 2, 13, 24, 25]},
                            index=pd.date_range(
                              "2021-11-10", periods=7, freq="d"))
  
  
plt.figure()
  
# "up" dataframe will store the stock_prices 
# when the closing stock price is greater
# than or equal to the opening stock prices
up = stock_prices[stock_prices.close >= stock_prices.open]
  
# "down" dataframe will store the stock_prices
# when the closing stock price is
# lesser than the opening stock prices
down = stock_prices[stock_prices.close < stock_prices.open]
  
# When the stock prices have decreased, then it
# will be represented by blue color candlestick
col1 = 'blue'
  
# When the stock prices have increased, then it 
# will be represented by green color candlestick
col2 = 'green'
  
# Setting width of candlestick elements
width = .3
width2 = .03
  
# Plotting up prices of the stock
plt.bar(up.index, up.close-up.open, width, bottom=up.open, color=col1)
plt.bar(up.index, up.high-up.close, width2, bottom=up.close, color=col1)
plt.bar(up.index, up.low-up.open, width2, bottom=up.open, color=col1)
  
# Plotting down prices of the stock
plt.bar(down.index, down.close-down.open, width, bottom=down.open, color=col2)
plt.bar(down.index, down.high-down.open, width2, bottom=down.open, color=col2)
plt.bar(down.index, down.low-down.close, width2, bottom=down.close, color=col2)
  
# rotating the x-axis tick labels at 30degree 
# towards right
plt.xticks(rotation=30, ha='right')
  
# displaying candlestick chart of stock data 
# of a week
plt.show()
Comment

PREVIOUS NEXT
Code Example
Python :: list duplicate files in folder python 
Python :: python sort dict by sub value 
Python :: pandas rolling list 
Python :: vue django delimiters 
Python :: create schema for table for django 
Python :: how to dynamically append value in a list in python 
Python :: All objects and constants needed to use the ldap3 library can be imported from the ldap3 namespace 
Python :: size of variable 
Python :: python netcdf double 
Python :: weigted average in pandas 
Python :: python message from byte 
Python :: Python 2.7 to 3.x Linux 
Python :: range function in python use cases 
Python :: screen.blit() arguments 
Python :: how to make download link in Jupyter appmode 
Python :: ROC plot for h2o package 
Python :: python how to dump exception stak 
Python :: sklearn isolationforest 
Python :: code help 
Python :: new sytax in python 3 
Python :: google video processor python nmp 
Python :: sqlalchemy date beween 
Python :: discord py replace characters from string 
Python :: pyspark imputer 
Python :: python certain charaacter in string 
Python :: google popup not opening 
Python :: python threadpool map exception 
Python :: import * with __import__ 
Python :: how to combine sets using update() Function 
Python :: pygame lerp 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =