Charting Stocks and Forex in python Part 17 - Overlay a stock's volume in matplotlib

preview_player
Показать описание
This is the seventeenth video in the series for stock price analysis, teaching you how to overlay the volume

data to your candlestick stock price graph in matplotlib. The purpose of the videos in this series is to

teach you how to program your own charting and technical analysis of stocks or Forex.

This is beneficial for you if you plan to do any sort of algorithmic, high-frequency, or any sort of

automated trading.

Рекомендации по теме
Комментарии
Автор

While overlaying the volume with the boxplots, my volume has like 1 month more on the left. 

import time
import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
from matplotlib.finance import candlestick
import matplotlib

9})

eachStock = 'GOOG',


def movingaverage(values, window):
    weights = np.repeat(1.0, window) / window
    smas = np.convolve(values, weights, 'valid')
    return smas


def graphData(stock, MA1, MA2):
    try:
        stockFile = stock + '.txt'
        date, closep, highp, lowp, openp, volume = np.loadtxt(stockFile, delimiter=', ', unpack=True,
                                                              converters={0:

        x = 0
        y = len(date)
        candleAr = []
        while x < y:
            appenLine = date[x], openp[x], closep[x], highp[x], lowp[x], volume[x]
            candleAr.append(appenLine)
            x += 1

        Av1 = movingaverage(closep, MA1)
        Av2 = movingaverage(closep, MA2)

        SP = len(date[MA2 - 1:])

        label1 = str(MA1) + 'SMA'
        label2 = str(MA2) + 'SMA'

        fig =


        ax0 = plt.subplot2grid((5, 4), (0, 0), rowspan=1, colspan=4, axisbg ='#07000d')
       
       
       
       
        ax0.tick_params(axis='x', colors='w')
        ax0.tick_params(axis='y', colors='w')
       
       
        plt.ylabel('RSI')



        ax1 = plt.subplot2grid((5, 4), (1, 0), rowspan=4, colspan=4, axisbg='#07000d')
        candlestick(ax1, candleAr[-SP:], width=0.75, colorup='#9eff15', colordown='#ff1717')

        ax1.plot(date[-SP:], Av1[-SP:], '#5998ff', label=label1, linewidth=1.5)
        ax1.plot(date[-SP:], Av2[-SP:], '#e1edf9', label=label2, linewidth=1.5)



       
       
       
       
       
       
       
        ax1.tick_params(axis='x', colors='w')
        ax1.tick_params(axis='y', colors='w')
        ax1.grid(True, color='w')
        plt.ylabel('Stock Price')
        plt.xlabel('Date')
        plt.legend(loc=3, prop={'size': 7}, fancybox=True, )

        for label in ax1.xaxis.get_ticklabels():
            label.set_rotation(45)


        volumeMin = 0

        ax1v = ax1.twinx()
        ax1v.fill_between(date, volumeMin, volume, facecolor='#00ffe8', alpha=.5)
       
       
       
       
       
        ax1v.set_ylim(0, 4*volume.max())
        ax1v.tick_params(axis='x', colors='w')
        ax1v.tick_params(axis='y', colors='w')
        ax1v.grid(False, color='w')

        plt.suptitle(stock, color='w')

        plt.setp(ax0.get_xticklabels(), visible=True)

        plt.subplots_adjust(left=.09, bottom=.18, right=.94, top=.94, wspace=.20, hspace=.0)

        plt.show()
        fig.savefig('example.png',

    except Exception, e:
        print 'failed main loop', str(e)


for stock in eachStock:
    graphData(stock, 12, 26)
    print 'sleeping...'
    time.sleep(10)

arnoclaes