#! /usr/bin/python2.7

# PlotShares
# Rev 2.1 RKN 17/01/2016
# Graph title includes share name & Shares held note taken from the share data
# file first line.  e.g  1, 1271.00, 800, Rolls Royce  , H , 2014-01-03, RR.L
# Rev 2.0 RKN 08/01/2016
# Rev 1.0 RKN 27/09/2015
# Takes share prices from txt files and plots with 8 & 16 week averages
# Creates temporary csv files for easy import into GnuPlot
# Averages not shown until average period exceeded


import Gnuplot, Gnuplot.funcutils
import numpy as np
from collections import deque

base = "SharesData/"
#base = "TestSharesData/"

filename = base + "sharestoget.txt"

savedshareslist = open(filename, 'r')



for line in savedshareslist:

    av1sum = deque([0,0,0,0,0,0,0,0])
    av2sum = deque([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
    
    countindex = 1
    sharesheld = ""
    fullname = ""

    #print "raw line", str(line)
    lengthofline = len(line)
    # print "lengthofline" , lengthofline
    commapos = line.find(",")

    line = line[0:(commapos-1)]
        
    print "modified string" , line

    savefilename = base + line + ".txt"

    print "file to get data from" , savefilename
        
    result = open(savefilename, 'r')


    
    tempcsvfilename = base + "tempcsv.csv"
        
    graphcsvdataprice = open(tempcsvfilename, 'w')

    tempcsvfilename2 = base + "tempcsv2.csv"
        
    graphcsvdatabuy = open(tempcsvfilename2, 'w')


    tempcsvfilename3 = base + "tempcsv3.csv"
        
    graphcsvdataav1 = open(tempcsvfilename3, 'w')

    tempcsvfilename4 = base + "tempcsv4.csv"
        
    graphcsvdataav2 = open(tempcsvfilename4, 'w')

   



    for graphdata in result:

        commapos = graphdata.find(",")
        secondcommapos = graphdata.find(",", commapos+1)
        thirdcommapos = graphdata.find(",", secondcommapos+1)
        fourthcommapos = graphdata.find(",", thirdcommapos+1)
        fifthcommapos = graphdata.find(",", fourthcommapos+1)
        
        #print commapos
        #print secondcommapos

        countindex = countindex + 1

        
        buydata1 = graphdata[0:commapos]
        buydata2 = graphdata[secondcommapos:thirdcommapos]

        if fourthcommapos - thirdcommapos > 3:
            fullname = graphdata[thirdcommapos:fourthcommapos]
        

        if graphdata.find("H", fourthcommapos,fifthcommapos) > 0:
            sharesheld = "  *** SHARES HELD  ***  "
        

        av1data = graphdata[commapos+1:secondcommapos]

        #print av1data
        
        av1strval = float(av1data)

        av1sum.append(av1strval)
        av2sum.append(av1strval)
        av1sum.popleft()
        av2sum.popleft()

        
        av1data2 = sum(av1sum)/8
        
        
        av2data2 = sum(av2sum)/16

        graphdata = graphdata[0:secondcommapos]


        #print "graphdata" , graphdata
        

        buydata = str(buydata1) +str(buydata2)
        
            
        av1data = str(buydata1) + "," + str(av1data2)
        #print "av1data", av1data

        
        av2data = str(buydata1) + "," + str(av2data2)
        #print "av1data", av2data
        #print "buydata", buydata

        graphcsvdataprice.write(graphdata + "\n")
        graphcsvdatabuy.write(buydata +"\n")
        if countindex >8 :
            graphcsvdataav1.write(av1data +"\n")
        if countindex > 16:
            graphcsvdataav2.write(av2data +"\n")

    graphcsvdataprice.close()
    graphcsvdatabuy.close()
    graphcsvdataav1.close()
    graphcsvdataav2.close()

    # A straightforward use of gnuplot.  The `debug=1' switch can be used
    # in this example so that the commands that are sent to gnuplot
    # are also output on stderr.

    g = Gnuplot.Gnuplot(debug=0)
    g.title(savefilename + fullname + sharesheld) # Sets the title of the graph
    g('set style data lines') # Sets the line style to simple lines
    g('set terminal x11 size 1000,600') #Sets the size of the X11 window
    g('set terminal x11 position 10,10') #Sets the position of the X11 window

    Line1= Gnuplot.Data(np.genfromtxt(str(tempcsvfilename), delimiter = ","))
    Line2= Gnuplot.Data(np.genfromtxt(str(tempcsvfilename2), delimiter = ","))
    Line3= Gnuplot.Data(np.genfromtxt(str(tempcsvfilename3), delimiter = ","))
    Line4= Gnuplot.Data(np.genfromtxt(str(tempcsvfilename4), delimiter = ","))
    
    g.plot(Line1, Line2, Line3, Line4)

    raw_input('Please press return to continue...\n')

    g.reset()


### END OF LISTING  ###
