示例#1
0
class ButterflyHistogram():
    def __init__(self, strike_intervals):
        self.intervals = strike_intervals
        self.prices = [0 for x in self.intervals][:-1]
        self.price_sum = 0
        self.probs = [1.0/len(self.intervals) for x in self.intervals]
        self.gpbase = GNUPlotBase()
        self.gpbase.xmin = strike_intervals[0][0]*.9
        self.gpbase.xmax = strike_intervals[-1][-1]*1.1
        self.gpbase.ymin = 0
        self.gpbase.ymax = 1
        self.datafile = 'butterhist.dat'
        self.line = '%0.2f %0.3f\n'
        self.xticks = [x[0] for x in strike_intervals 
                                         if strike_intervals.index(x) % 2 == 0]
        self.xticks.append(strike_intervals[-1][-1])
        self.yticks = [x/10.0 for x in range(1, 10)]

    def update_price(self, price, index):
        if price > 0: self.prices[index] = price
        else: self.prices[index] = 0
        self.calculate_probabilities()

    def sum_prices(self):
        self.price_sum = 0
        for x in self.prices: self.price_sum += x

    def calculate_probabilities(self):
        self.sum_prices()
        if self.price_sum == 0:
            f = open(self.datafile, 'w')
            f.write('0 0\n')
            f.close()
            return None
        f = open(self.datafile, 'w')
        self.probs[0] = 0.5*self.prices[0]/self.price_sum
        f.write(self.line % (self.mid(self.intervals[0]), self.probs[0]))
        for i in range(1, len(self.probs)-1):
            self.probs[i] = self.mid(self.prices[i-1:i+1])/self.price_sum
            f.write(self.line % (self.mid(self.intervals[i]), self.probs[i]))
        self.probs[-1] = 0.5*self.prices[-1]/self.price_sum
        f.write(self.line % (self.mid(self.intervals[-1]), self.probs[-1]))
        f.close()

    def mid(self, interval):
        return (interval[1] + interval[0])/2.0

    def plot_histogram(self, fname=None, timestamp=None):
        commands = []
        commands.append(self.gpbase.set_output(fname=fname))
        commands.append(self.gpbase.gen_ticks(self.xticks, self.yticks))
        commands.append(self.gpbase.gen_header(ylabel='Probability',
                        timestamp=timestamp))
        commands.append('plot "%s" with boxes' % self.datafile)
        return '\n'.join(commands)
示例#2
0
 def __init__(self, strike_intervals):
     self.intervals = strike_intervals
     self.prices = [0 for x in self.intervals][:-1]
     self.price_sum = 0
     self.probs = [1.0/len(self.intervals) for x in self.intervals]
     self.gpbase = GNUPlotBase()
     self.gpbase.xmin = strike_intervals[0][0]*.9
     self.gpbase.xmax = strike_intervals[-1][-1]*1.1
     self.gpbase.ymin = 0
     self.gpbase.ymax = 1
     self.datafile = 'butterhist.dat'
     self.line = '%0.2f %0.3f\n'
     self.xticks = [x[0] for x in strike_intervals 
                                      if strike_intervals.index(x) % 2 == 0]
     self.xticks.append(strike_intervals[-1][-1])
     self.yticks = [x/10.0 for x in range(1, 10)]
if __name__ == "__main__":
    datfname = sys.argv[1]
    jpgfname = sys.argv[2]
    start = sys.argv[3]
    end = sys.argv[4]
    increment = sys.argv[5]
    dt = datfname.split('.dat')[0].split('_')[2]
    if float(increment) % 1 == 0:
        start = int(start)
        end = int(end)
        increment = int(increment)
        xtics = range(start, end+increment, increment)
    elif float(increment) == 0.5:
        start = int(float(start)*10.0)
        end = int(float(end)*10.0)
        increment = 5
        strikes = range(start, end+increment, increment)
        xticks = [x/10.0 for x in strikes]
    yticks = [x/10.0 for x in range(0, 11)]
    gpb = GNUPlotBase()
    gpb.xmin = start*.9
    gpb.xmax = end*1.1
    gpb.ymin = 0
    gpb.ymax = 1
    commands = []
    commands.append(gpb.set_output(fname=jpgfname))
    commands.append(gpb.gen_header(ylabel='Probability', timestamp=dt))
    commands.append(gpb.gen_ticks(xticks, yticks))
    commands.append('plot "%s" with boxes' % datfname)
    print '\n'.join(commands)
    jpgfname = sys.argv[2]
    start = sys.argv[3]
    end = sys.argv[4]
    increment = sys.argv[5]
    dt = datfname.split('.dat')[0].split('_')[2]
    if float(increment) % 1 == 0:
        start = int(start)
        end = int(end)
        increment = int(increment)
        xtics = range(start, end+increment, increment*2)
    elif float(increment) == 0.5:
        start = int(float(start)*10.0)
        end = int(float(end)*10.0)
        increment = 5
        strikes = range(start, end+increment, increment*2)
        xticks = [x/10.0 for x in strikes]
    gpb = GNUPlotBase()
    gpb.xmin = start*.9
    gpb.xmax = end*1.1
    gpb.ymin = 0
    gpb.ymax = xticks[1] - xticks[0]
    yticks = [float(gpb.ymax)/5.0*x for x in range(0, 6)]
    commands = []
    commands.append(gpb.set_output(fname=jpgfname))
    commands.append(gpb.gpbase.gen_header(xlabel='Spot Price at Expiry',
                                          ylabel='Butterfly Price',
                                          timestamp=timestamp))
    commands.append(gpb.gen_ticks(xticks, yticks))
    commands.append('plot "%s" with boxes' % datfname)
    print '\n'.join(commands)