Example #1
0
def Draw(history, ref_hist, days=14, start_date=None):
    base.Style(ROOT)
    c1, pads, padScaling, ratioPadScaling = base.DoRatio(ROOT)

    t = ma.GetTime(start_date)

    x_axis = []
    bins_price = []
    bins_refprice = []
    bins_correl = []
    nday = 0
    ticker = 'N/A'
    first_date = None
    it = 0
    h1 = None
    total_correl = GetCorrelation(history, ref_hist, days, start_date)
    for h in history:
        this_t = ma.GetTime(h['Date'])
        #h1={}
        #h1['Close']=float(h['Close'])
        #if it>0:
        #    h1=history[it-1]
        it += 1
        if this_t <= t:
            if h1 == None:
                h1 = history[it - 1]
            x_axis += [nday]
            ticker = h['Symbol']
            if first_date == None:
                first_date = h['Date']
            #bins_price=[(float(h1['Close'])-float(h['Close']))/float(h1['Close'])]+bins_price
            bins_price = [(float(h['Close']))] + bins_price
            bins_correl = [GetCorrelation(history, ref_hist, 10, h['Date'])
                           ] + bins_correl
            nday += 1
            if nday == days:
                x_axis += [nday]
                break
    nday = 0
    it = 0
    h1 = None
    for h in ref_hist:
        this_t = ma.GetTime(h['Date'])
        #h1={}
        #h1['Close']=float(h['Close'])

        #if it>0:
        #    h1=ref_hist[it-1]
        it += 1
        if this_t <= t:
            if h1 == None:
                h1 = ref_hist[it - 1]

            #bins_refprice=[float(h['Close'])]+bins_refprice
            #bins_refprice=[(float(h1['Close'])-float(h['Close']))/float(h1['Close'])]+bins_refprice
            bins_refprice = [float(h['Close'])] + bins_refprice
            nday += 1
            if nday == days:
                break

    h0 = bins_refprice[0]
    for h in range(0, len(bins_refprice)):
        bins_refprice[h] = (bins_refprice[h] - h0) / h0
    h0 = bins_price[0]
    for h in range(0, len(bins_price)):
        bins_price[h] = (bins_price[h] - h0) / h0
    # start plotting
    runArray_day = array('d', x_axis)
    hprice = ROOT.TH1F('price', 'price', len(runArray_day) - 1, runArray_day)
    hrefprice = ROOT.TH1F('refprice', 'refprice',
                          len(runArray_day) - 1, runArray_day)
    hcorr = ROOT.TH1F('correl', 'correl', len(runArray_day) - 1, runArray_day)
    hprice.GetXaxis().SetTitle('Days')
    hprice.GetYaxis().SetTitle('Relative Daily Change for ' + ticker)
    hcorr.GetYaxis().SetTitle('Pearson Coef.')
    if start_date:
        hprice.GetXaxis().SetTitle('Days since ' + start_date)

    for i in range(0, len(x_axis) - 1):
        hprice.SetBinContent(i + 1, bins_price[i])
        hprice.SetBinError(i + 1, 0.0)
    for i in range(0, len(x_axis) - 1):
        hrefprice.SetBinContent(i + 1, bins_refprice[i])
        hrefprice.SetBinError(i + 1, 0.0)
    for i in range(0, len(x_axis) - 1):
        hcorr.SetBinContent(i + 1, bins_correl[i])
        hcorr.SetBinError(i + 1, 0.0)

    hrefprice.SetLineColor(2)
    hrefprice.SetMarkerColor(2)
    hcorr.SetLineColor(2)
    hcorr.SetMarkerColor(2)
    hcorr.GetYaxis().SetRangeUser(-1.0, 1.0)
    hcorr.GetYaxis().SetNdivisions(507)

    # Format
    base.Format([hprice, hrefprice], ROOT, True, padScaling, hist_name='')
    base.Format([hcorr], ROOT, True, ratioPadScaling, hist_name='')

    # Draw
    pads[0].cd()
    hprice.Draw('lp')
    hrefprice.Draw('lp same')

    # ratio pad
    pads[1].cd()
    hcorr.Draw('lp')

    # ratio legend
    legr = ROOT.TLegend(0.1, 0.06, 0.4, 0.26)
    legr.SetBorderSize(0)
    legr.SetFillStyle(0)
    legr.AddEntry(hcorr, "10 day Pearson Coef.")
    legr.Draw()

    # legend
    pads[0].cd()
    leg = ROOT.TLegend(0.18, 0.1, 0.55, 0.3)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)
    leg.AddEntry(hprice, "Closing Price with tot. corr %0.3f" % (total_correl))
    leg.AddEntry(hrefprice, "DJIA")
    leg.Draw()

    # finish
    c1.Update()
    if start_date == None:
        start_date = first_date
    c1.SaveAs(out_path + '/corr/' + ticker + '_' + start_date + '.' +
              out_file_type)
    if WAIT:
        c1.WaitPrimitive()
        raw_input('waiting...')
Example #2
0
def Draw(history, days=14, start_date=None):
    base.Style(ROOT)
    c1, pads, padScaling, ratioPadScaling = base.DoRatio(ROOT)

    t = base.GetTime(start_date)

    x_axis = []
    bins_price = []
    bins_rsi = []
    bins_stoch = []
    all_rsi = []
    all_stoch = []
    nday = 0
    ticker = 'N/A'
    first_date = None
    for h in history:
        this_t = base.GetTime(h['Date'])
        if this_t <= t:
            x_axis += [nday]
            ticker = h['Symbol']
            if first_date == None:
                first_date = h['Date']
            bins_price = [float(h['Close'])] + bins_price
            this_rsi = GetRSI(history, 14, h['Date'])
            bins_rsi = [this_rsi[0]] + bins_rsi
            all_rsi = [this_rsi] + all_rsi
            this_stoch = GetStochastic(history, 14, h['Date'])
            bins_stoch = [this_stoch[0]] + bins_stoch
            all_stoch = [this_stoch] + all_stoch
            nday += 1
            if nday == days:
                x_axis += [nday]
                break

    # start plotting
    runArray_day = array('d', x_axis)
    hprice = ROOT.TH1F('price', 'price', len(runArray_day) - 1, runArray_day)
    hrsi = ROOT.TH1F('rsi', 'rsi', len(runArray_day) - 1, runArray_day)
    hprice.GetXaxis().SetTitle('Days')
    hprice.GetYaxis().SetTitle('Price for ' + ticker)
    hrsi.GetYaxis().SetTitle('RSI')
    hstoch = ROOT.TH1F('stoch', 'stoch', len(runArray_day) - 1, runArray_day)
    hstoch.GetYaxis().SetTitle('Stochastic')
    hstochma = ROOT.TH1F('stochma', 'stochma',
                         len(runArray_day) - 1, runArray_day)
    hstochma.GetYaxis().SetTitle('Stochastic MA')
    if start_date:
        hprice.GetXaxis().SetTitle('Days since ' + start_date)

    bins_stoch_ma = ma.GetMovingAverageFromListAndReturnList(bins_stoch, 3)
    for i in range(0, len(x_axis) - 1):
        hprice.SetBinContent(i + 1, bins_price[i])
        hprice.SetBinError(i + 1, 0.0)
    #print len(bins_stoch),' for this many: ',len(x_axis)
    #print bins_stoch
    for i in range(0, len(x_axis) - 1):
        hrsi.SetBinContent(i + 1, bins_rsi[i])
        hrsi.SetBinError(i + 1, 0.0)
        hstoch.SetBinContent(i + 1, bins_stoch[i])
        hstoch.SetBinError(i + 1, 0.0)
        hstochma.SetBinContent(i + 1, bins_stoch_ma[i])
        hstochma.SetBinError(i + 1, 0.0)

    hrsi.SetLineColor(2)
    hrsi.SetMarkerColor(2)
    hrsi.GetYaxis().SetRangeUser(0.0, 100.0)
    hrsi.GetYaxis().SetNdivisions(507)
    hstoch.SetLineColor(2)
    hstoch.SetMarkerColor(2)
    hstoch.GetYaxis().SetRangeUser(0.0, 100.0)
    hstoch.GetYaxis().SetNdivisions(507)
    hstochma.SetLineColor(3)
    hstochma.SetMarkerColor(3)
    hstochma.GetYaxis().SetRangeUser(0.0, 100.0)
    hstochma.GetYaxis().SetNdivisions(507)

    # Format
    base.Format([hprice], ROOT, True, padScaling, hist_name='')
    base.Format([hrsi, hstoch, hstochma],
                ROOT,
                True,
                ratioPadScaling,
                hist_name='')

    # Draw
    pads[0].cd()
    hprice.Draw('lp')

    # ratio pad
    pads[1].cd()
    l30 = ROOT.TLine(0.0, 30.0, float(days), 30.0)
    l50 = ROOT.TLine(0.0, 50.0, float(days), 50.0)
    l70 = ROOT.TLine(0.0, 70.0, float(days), 70.0)
    l20 = ROOT.TLine(0.0, 20.0, float(days), 20.0)
    l80 = ROOT.TLine(0.0, 80.0, float(days), 80.0)
    hrsi.Draw('lp')
    l50.SetLineWidth(2)
    l30.SetLineWidth(2)
    l70.SetLineWidth(2)
    l30.SetLineStyle(2)
    l70.SetLineStyle(2)
    l20.SetLineWidth(2)
    l80.SetLineWidth(2)
    l20.SetLineStyle(2)
    l80.SetLineStyle(2)
    l30.Draw()
    l50.Draw()
    l70.Draw()

    # ratio legend
    legr = ROOT.TLegend(0.1, 0.06, 0.4, 0.26)
    legr.SetBorderSize(0)
    legr.SetFillStyle(0)
    legr.AddEntry(hrsi, "14 day RSI")
    legr.Draw()

    # legend
    pads[0].cd()
    leg = ROOT.TLegend(0.18, 0.1, 0.55, 0.3)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)
    leg.AddEntry(hprice, "Closing Price")
    leg.Draw()

    # finish
    c1.Update()
    if start_date == None:
        start_date = first_date
    c1.SaveAs(out_path + '/rsi/' + ticker + '_' + start_date + '.' +
              out_file_type)
    if WAIT:
        c1.WaitPrimitive()
        raw_input('waiting...')

    # draw the stochastic
    pads[1].cd()
    hstoch.Draw('lp')
    hstochma.Draw('lp same')
    l20.Draw()
    l50.Draw()
    l80.Draw()

    # ratio legend
    legr.Clear()
    legr.AddEntry(hstoch, "14 day Stochastic")
    legr.AddEntry(hstochma, "3 day MA of Stoch.")
    legr.Draw()

    # finish
    c1.Update()
    if start_date == None:
        start_date = first_date
    c1.SaveAs(out_path + '/stoch/' + ticker + '_' + start_date + '.' +
              out_file_type)
    if WAIT:
        c1.WaitPrimitive()
        raw_input('waiting...')

    #print all_rsi[len(all_rsi)-1]
    return [
        ticker,
        hrsi.GetBinContent(hrsi.GetNbinsX()),
        all_rsi[len(all_rsi) - 1][1],  # over bought
        all_rsi[len(all_rsi) - 1][2],  # over sold
        hstoch.GetBinContent(hstoch.GetNbinsX()),
        all_stoch[len(all_stoch) - 1][1],  # over bought
        all_stoch[len(all_stoch) - 1][2],  # over sold 
        hstochma.GetBinContent(hstochma.GetNbinsX())
    ]
Example #3
0
def Draw(history, days=50, start_date=None):
    base.Style(ROOT)
    c1, pads, padScaling, ratioPadScaling = base.DoRatio(ROOT)

    t = base.GetTime(start_date)

    x_axis = []
    bins_price = []
    bins_macd_12day = []
    bins_macd_26day = []
    bins_macd_9day = []
    bins_macd_diff = []
    bins_macd_9diff = []
    bins_ppo = []  # percentage price oscillator
    bins_ppo_9ema = []  # percentage price oscillator
    bins_ppo_signal = []  # signal line
    nday = 0
    ticker = 'N/A'
    first_date = None
    for h in history:
        this_t = base.GetTime(h['Date'])
        if this_t <= t:
            x_axis += [nday]
            ticker = h['Symbol']
            if first_date == None:
                first_date = h['Date']
            bins_price = [float(h['Close'])] + bins_price
            nineday = ma.GetExpMovingAverage(history, 9, h['Date'])
            twelveday = ma.GetExpMovingAverage(history, 12, h['Date'])
            twentysixday = ma.GetExpMovingAverage(history, 26, h['Date'])
            bins_macd_9day = [nineday] + bins_macd_9day
            bins_macd_12day = [twelveday] + bins_macd_12day
            bins_macd_26day = [twentysixday] + bins_macd_26day
            bins_macd_diff = [twelveday - twentysixday] + bins_macd_diff
            if twentysixday > 0.0:
                bins_ppo = [100.0 * (twelveday - twentysixday) / twentysixday
                            ] + bins_ppo
            else:
                bins_ppo = [0.0] + bins_ppo

            nday += 1
            if nday == days:
                x_axis += [nday]
                break
    for m in range(0, len(bins_macd_diff)):
        bins_macd_9diff += [
            ma.GetExpMovingAverageFromList(bins_macd_diff, 9, m)
        ]
    for m in range(0, len(bins_ppo)):
        bins_ppo_9ema += [ma.GetExpMovingAverageFromList(bins_ppo, 9, m)]

    # start plotting
    runArray_day = array('d', x_axis)
    hprice = ROOT.TH1F('price', 'price', len(runArray_day) - 1, runArray_day)
    hmacd9day = ROOT.TH1F('macd9day', 'macd9day',
                          len(runArray_day) - 1, runArray_day)
    hmacd12day = ROOT.TH1F('macd12day', 'macd12day',
                           len(runArray_day) - 1, runArray_day)
    hmacd26day = ROOT.TH1F('macd26day', 'macd26day',
                           len(runArray_day) - 1, runArray_day)
    hmacddiff = ROOT.TH1F('macddiff', 'macddiff',
                          len(runArray_day) - 1, runArray_day)
    hmacd9diff = ROOT.TH1F('macd9diff', 'macd9diff',
                           len(runArray_day) - 1, runArray_day)
    hppo = ROOT.TH1F('ppo', 'ppo', len(runArray_day) - 1, runArray_day)
    hppo9dayema = ROOT.TH1F('ppo9dayema', 'ppo9dayema',
                            len(runArray_day) - 1, runArray_day)
    hdiff = ROOT.TH1F('diff', 'diff', len(runArray_day) - 1, runArray_day)
    hdiffppo = ROOT.TH1F('diffppo', 'diffppo',
                         len(runArray_day) - 1, runArray_day)
    hprice.GetXaxis().SetTitle('Days')
    hprice.GetYaxis().SetTitle('Price for ' + ticker)
    if start_date:
        hprice.GetXaxis().SetTitle('Days since ' + start_date)
        hmacddiff.GetXaxis().SetTitle('Days since ' + start_date)
        hmacddiff.GetYaxis().SetTitle('MACD')
        hppo.GetYaxis().SetTitle('PPO')
        hppo.GetXaxis().SetTitle('Days since ' + start_date)

    for i in range(0, len(x_axis) - 1):
        hprice.SetBinContent(i + 1, bins_price[i])
        hprice.SetBinError(i + 1, 0.0)
    for i in range(0, len(x_axis) - 1):
        hmacd9day.SetBinContent(i + 1, bins_macd_9day[i])
        hmacd9day.SetBinError(i + 1, 0.0)
    for i in range(0, len(x_axis) - 1):
        hmacd12day.SetBinContent(i + 1, bins_macd_12day[i])
        hmacd12day.SetBinError(i + 1, 0.0)
    for i in range(0, len(x_axis) - 1):
        hmacddiff.SetBinContent(i + 1, bins_macd_diff[i])
        hmacddiff.SetBinError(i + 1, 0.0)
    for i in range(0, len(x_axis) - 1):
        hmacd9diff.SetBinContent(i + 1, bins_macd_9diff[i])
        hmacd9diff.SetBinError(i + 1, 0.0)
    for i in range(0, len(x_axis) - 1):
        hdiff.SetBinContent(i + 1, bins_macd_diff[i] - bins_macd_9diff[i])
        hdiff.SetBinError(i + 1, 0.0)
    for i in range(0, len(x_axis) - 1):
        hdiffppo.SetBinContent(i + 1, bins_ppo[i] - bins_ppo_9ema[i])
        hdiffppo.SetBinError(i + 1, 0.0)
    for i in range(0, len(x_axis) - 1):
        hmacd26day.SetBinContent(i + 1, bins_macd_26day[i])
        hmacd26day.SetBinError(i + 1, 0.0)
    for i in range(0, len(x_axis) - 1):
        hppo.SetBinContent(i + 1, bins_ppo[i])
        hppo.SetBinError(i + 1, 0.0)
    for i in range(0, len(x_axis) - 1):
        hppo9dayema.SetBinContent(i + 1, bins_ppo_9ema[i])
        hppo9dayema.SetBinError(i + 1, 0.0)

    hmacd9day.SetLineColor(2)
    hmacd9day.SetMarkerColor(2)
    hmacd12day.SetLineColor(3)
    hmacd12day.SetMarkerColor(3)
    hmacd26day.SetLineColor(4)
    hmacd26day.SetMarkerColor(4)
    hmacddiff.SetLineColor(1)
    hmacddiff.SetMarkerColor(1)
    hmacd9diff.SetLineColor(2)
    hmacd9diff.SetMarkerColor(2)
    hppo.SetLineColor(4)
    hppo.SetMarkerColor(4)
    hppo9dayema.SetLineColor(5)
    hppo9dayema.SetMarkerColor(5)
    hdiffppo.SetLineColor(6)
    hdiffppo.SetMarkerColor(6)
    hdiffppo.SetFillColor(6)
    hdiffppo.SetFillStyle(1001)
    hdiff.SetLineColor(3)
    hdiff.SetMarkerColor(3)
    hdiff.SetFillColor(3)
    hdiff.SetFillStyle(1001)

    # Format
    base.Format([hprice, hmacd9day, hmacd12day, hmacd26day],
                ROOT,
                True,
                padScaling,
                hist_name='')
    base.Format([hmacddiff, hmacd9diff, hdiff, hppo, hppo9dayema, hdiffppo],
                ROOT,
                True,
                ratioPadScaling,
                hist_name='')

    # Draw
    pads[0].cd()
    hprice.Draw('lp')
    hmacd9day.Draw('lpsame')
    hmacd12day.Draw('lpsame')
    hmacd26day.Draw('lpsame')
    # ratio pad
    pads[1].cd()
    hmacddiff.Draw('lp')
    hdiff.Draw("HIST same")
    hmacddiff.Draw('lp same')
    hmacd9diff.Draw('lpsame')
    #hppo.Draw('lpsame')
    #hppo9dayema.Draw('lpsame')
    #hdiffppo.Draw('lpsame')

    # ratio legend
    legr = ROOT.TLegend(0.1, 0.06, 0.4, 0.26)
    legr.SetBorderSize(0)
    legr.SetFillStyle(0)
    legr.AddEntry(hmacddiff, "12 Day MACD")
    #legr.AddEntry(hmacd9diff, "9 Day MACD");
    legr.AddEntry(hmacd9diff, "Signal Line")
    legr.AddEntry(hdiff, "Diff")
    #legr.AddEntry(hppo, "PPO");
    #legr.AddEntry(hppo9dayema, "PPO Signal Line");
    #legr.AddEntry(hdiffppo, "PPO Indicator");
    legr.Draw()

    # legend
    pads[0].cd()
    leg = ROOT.TLegend(0.18, 0.1, 0.55, 0.3)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)
    leg.AddEntry(hprice, "Closing Price")
    leg.AddEntry(hmacd9day, "9 Day ExpMA")
    leg.AddEntry(hmacd12day, "12 Day ExpMA")
    leg.AddEntry(hmacd26day, "26 Day ExpMA")
    leg.Draw()

    #decision_100day,decision_ndays_100days = AnalyzeMA(bins_ma_100day, bins_ma_20day)
    #decision_50day,decision_ndays_50days = AnalyzeMA(bins_ma_50day, bins_ma_20day)

    # finish
    c1.Update()
    if start_date == None:
        start_date = first_date
    c1.SaveAs(out_path + '/macd/' + ticker + '_' + start_date + '.' +
              out_file_type)
    if WAIT:
        c1.WaitPrimitive()
        raw_input('waiting...')

    my_buy_date = -999
    my_sell_date = -999
    for a in range(0, hdiff.GetNbinsX()):
        if hdiff.GetBinContent(hdiff.GetNbinsX(
        ) - a) > 0.0 and hdiff.GetBinContent(hdiff.GetNbinsX() - a -
                                             1) < 0.0 and my_buy_date < 0.0:
            my_buy_date = a
        if hdiff.GetBinContent(hdiff.GetNbinsX(
        ) - a) < 0.0 and hdiff.GetBinContent(hdiff.GetNbinsX() - a -
                                             1) > 0.0 and my_sell_date < 0.0:
            my_sell_date = a

    return [
        ticker, my_buy_date, my_sell_date,
        hdiff.GetBinContent(hdiff.GetNbinsX())
    ]
Example #4
0
def Draw(history, days=14, start_date=None, isVolume=0):
    base.Style(ROOT)
    c1, pads, padScaling, ratioPadScaling = base.DoRatio(ROOT)

    t = base.GetTime(start_date)

    x_axis = []
    x_e_axis = []
    bins_price = []
    bins_eh_price = []
    bins_el_price = []
    bins_rsi = []
    bins_volt = []
    bins_chaikin = []
    nday = 0
    ticker = 'N/A'
    first_date = None
    hindex = 0
    for h in history:
        this_t = base.GetTime(h['Date'])
        if this_t <= t:
            if nday > 0:
                x_axis += [float(nday)]
            x_e_axis += [0.0]
            ticker = h['Symbol']
            if first_date == None:
                first_date = h['Date']
            bins_price = [float(h['Close'])] + bins_price
            bins_eh_price = [float(h['High']) - float(h['Close'])
                             ] + bins_eh_price
            bins_el_price = [float(h['Close']) - float(h['Low'])
                             ] + bins_el_price
            bins_volt = [GetVolatility(history, 22, h['Date'])] + bins_volt
            bins_chaikin = [GetChaikin(history, 20, h['Date'])] + bins_chaikin

            a = 0.0
            if hindex > 0:
                hn1 = history[len(history) - hindex]
                a = float(h['Close']) - float(hn1['Close'])
            #bins_rsi = [ float(h['Volume'])]+bins_rsi
            if a > 0.0:
                bins_rsi = [float(h['Volume'])] + bins_rsi
            else:
                bins_rsi = [-1.0 * float(h['Volume'])] + bins_rsi

            nday += 1
            if nday == days:
                x_axis += [float(nday)]
                break
            hindex += 1

    # start plotting
    #print x_axis
    #print bins_price
    #print 'np: ',len(bins_price),' ',len(x_axis)
    runArray_day = array('d', x_axis)
    runArray_price = array('d', bins_price)
    runArray_e_day = array('d', x_e_axis)
    runArray_el_price = array('d', bins_el_price)
    runArray_eh_price = array('d', bins_eh_price)
    #hprice = ROOT.TH1F('price','price',len(runArray_day)-1,runArray_day)
    hprice = ROOT.TGraphAsymmErrors(len(x_axis), runArray_day, runArray_price,
                                    runArray_e_day, runArray_e_day,
                                    runArray_el_price, runArray_eh_price)
    hprice.SetName('price')
    hrsi = ROOT.TH1F('volume', 'volume', len(runArray_day) - 1, runArray_day)
    hvolt = ROOT.TH1F('volatility', 'volatility',
                      len(runArray_day) - 1, runArray_day)
    hchaikin = ROOT.TH1F('chaikin', 'chaikin',
                         len(runArray_day) - 1, runArray_day)
    hprice.GetXaxis().SetTitle('Days')
    hvolt.GetXaxis().SetTitle('Days')
    hrsi.GetXaxis().SetTitle('Days')
    hchaikin.GetXaxis().SetTitle('Days')
    hprice.GetYaxis().SetTitle('Price for ' + ticker)
    hrsi.GetYaxis().SetTitle('Volume')
    hvolt.GetYaxis().SetTitle('Volatility')
    hchaikin.GetYaxis().SetTitle('CMF')
    if start_date:
        hprice.GetXaxis().SetTitle('Days since ' + start_date)

    #for i in range(0,len(x_axis)-1):
    #    hprice.SetBinContent(i+1,bins_price[i])
    #    hprice.SetBinError(i+1,0.0)
    for i in range(0, len(x_axis) - 1):
        hrsi.SetBinContent(i + 1, bins_rsi[i])
        hrsi.SetBinError(i + 1, 0.0)
    for i in range(0, len(x_axis) - 1):
        hvolt.SetBinContent(i + 1, bins_volt[i])
        hvolt.SetBinError(i + 1, 0.0)
    for i in range(0, len(x_axis) - 1):
        hchaikin.SetBinContent(i + 1, bins_chaikin[i])
        hchaikin.SetBinError(i + 1, 0.0)

    hrsi.SetLineColor(2)
    hrsi.SetMarkerColor(2)
    hvolt.SetLineColor(3)
    hvolt.SetMarkerColor(3)
    hchaikin.SetLineColor(4)
    hchaikin.SetMarkerColor(4)
    #hrsi.GetYaxis().SetRangeUser(0.0,100.0)
    hrsi.GetYaxis().SetNdivisions(507)
    hvolt.GetYaxis().SetNdivisions(507)
    hchaikin.GetYaxis().SetNdivisions(507)

    # Format
    base.Format([hprice], ROOT, True, padScaling, hist_name='')
    base.Format([hrsi, hvolt, hchaikin],
                ROOT,
                True,
                ratioPadScaling,
                hist_name='')

    # Draw
    pads[0].cd()
    hprice.SetLineColor(2)
    hprice.SetFillColor(0)
    hprice.GetXaxis().SetRangeUser(0.0, x_axis[len(x_axis) - 1])
    #hprice.Draw('lp')
    hprice.Draw()
    hprice.Draw('same pl e2')

    # ratio pad
    pads[1].cd()
    #l30 = ROOT.TLine(0.0,30.0,float(days),30.0)
    #l50 = ROOT.TLine(0.0,50.0,float(days),50.0)
    #l70 = ROOT.TLine(0.0,70.0,float(days),70.0)
    if isVolume == 0:
        hrsi.Draw('lp')
    elif isVolume == 1:
        hvolt.Draw('lp ')
    elif isVolume == 2:
        hchaikin.Draw('lp ')
        l05 = ROOT.TLine(0.0, 0.05, float(days), 0.05)
        l05.SetLineWidth(2)
        l05.SetLineColor(3)
        l05.SetLineStyle(2)
        l05.Draw()
        m05 = ROOT.TLine(0.0, -0.05, float(days), -0.05)
        m05.SetLineWidth(2)
        m05.SetLineColor(2)
        m05.SetLineStyle(2)
        m05.Draw()

    # ratio legend
    legr = ROOT.TLegend(0.1, 0.06, 0.4, 0.26)
    legr.SetBorderSize(0)
    legr.SetFillStyle(0)
    if isVolume == 0:
        legr.AddEntry(hrsi, "Volume")
    elif isVolume == 1:
        legr.AddEntry(hvolt, "Volatility")
    elif isVolume == 2:
        legr.AddEntry(hchaikin, "CMF")
    legr.Draw()

    # legend
    pads[0].cd()
    leg = ROOT.TLegend(0.18, 0.1, 0.55, 0.3)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)
    leg.AddEntry(hprice, "Closing Price")
    leg.Draw()

    # finish
    c1.Update()
    if start_date == None:
        start_date = first_date
    if isVolume == 0:
        c1.SaveAs(out_path + '/obv/' + ticker + '_' + start_date + '.' +
                  out_file_type)
    elif isVolume == 1:
        c1.SaveAs(out_path + '/obv/' + ticker + '_' + start_date + 'volt.' +
                  out_file_type)
    elif isVolume == 2:
        c1.SaveAs(out_path + '/obv/' + ticker + '_' + start_date + 'chaikin.' +
                  out_file_type)
    if WAIT:
        c1.WaitPrimitive()
        raw_input('waiting...')
Example #5
0
def Draw(history, days = 50, start_date=None):
    base.Style(ROOT)
    c1 = ROOT.TCanvas("c1","stocks",50,50,600,600);
    t = base.GetTime(start_date)

    Nbolganger=2.0
    NbolgangerMA=20
    
    x_axis=[]
    x_e_axis=[]
    bins_price=[]
    bins_price_for_stddev=[]    
    bins_eh_price=[]
    bins_el_price=[]
    bins_ma_50day=[]
    bins_ma_20day=[]
    bins_ma_bol=[]
    bins_ma_bolsigma=[]        
    bins_ma_100day=[]
    bins_ma_200day=[]
    nday=0
    ticker='N/A'
    first_date=None
    jan1_price = -1.0
    jan1_date = time.localtime()
    for h in history:
        this_t = base.GetTime(h['Date'])
        # get the first price of the year
        if jan1_date.tm_year==this_t.tm_year and this_t.tm_mon==1 and (this_t.tm_mday==1 or this_t.tm_mday==2 or this_t.tm_mday==3):
            jan1_price=h['Open']
        
        if this_t<=t:
            if (nday)<(days):
                x_axis+=[nday]
                x_e_axis+=[0.0]
            ticker = h['Symbol']
            if first_date==None:
                first_date=h['Date']
            bins_ma_bol =[GetAverage(history, NbolgangerMA, h['Date'])]+bins_ma_bol
            bins_price_for_stddev=[float(h['Close'])]+bins_price
            if (nday)<(days):
                bins_price=[float(h['Close'])]+bins_price
                bins_eh_price=[float(h['High'])-float(h['Close'])]+bins_eh_price
                bins_el_price=[float(h['Close'])-float(h['Low'])]+bins_el_price
                bins_ma_20day =[GetAverage(history, 20, h['Date'])]+bins_ma_20day                
                bins_ma_50day =[GetAverage(history, 50, h['Date'])]+bins_ma_50day
                bins_ma_100day=[GetAverage(history, 100, h['Date'])]+bins_ma_100day
                bins_ma_200day=[GetAverage(history, 200, h['Date'])]+bins_ma_200day
            nday+=1
            if nday==days:
                x_axis+=[nday]
            if (nday)==(days+NbolgangerMA):
                break;
    #print len(bins_ma_bol)
    #print len(x_axis)
    #print len(bins_ma_20day)    
    for i in range(0,len(bins_ma_bol)-NbolgangerMA):        
        bins_ma_bolsigma =[GetStdDev(i, NbolgangerMA, Nbolganger, bins_price_for_stddev, h['Date'])]+bins_ma_bolsigma
    #print len(bins_ma_bolsigma)
    # start plotting
    if len(bins_price)>0:
        #bins_price+=[bins_price[0]]
        bins_price=[bins_price[0]]+bins_price
    #print bins_price
    #print x_axis[1:len(x_axis)]
    runArray_day = array('d',x_axis)
    runArray_e_day = array('d',x_e_axis)
    runArray_price = array('d',bins_price)
    #print len(runArray_price)
    #print len(x_axis)
    runArray_el_price =  array('d',bins_el_price)
    runArray_eh_price =  array('d',bins_eh_price)
    if len(runArray_day)==0:
        return []
    #hprice = ROOT.TH1F('price','price',len(runArray_day)-1,runArray_day)
    hprice = ROOT.TGraphAsymmErrors(len(x_axis),runArray_day,runArray_price,
                                    runArray_e_day,
                                    runArray_e_day,runArray_el_price,runArray_eh_price)
    hbolbandsize = ROOT.TH1F('bolbandsize','bolbandsize',len(runArray_day)-1,runArray_day)
    hbolbandup = ROOT.TH1F('bolbandup','bolbandup',len(runArray_day)-1,runArray_day)
    hbolma = ROOT.TH1F('bolma','bolma',len(runArray_day)-1,runArray_day)
    hbolpercentb = ROOT.TH1F('bolpercentb','bolpercentb',len(runArray_day)-1,runArray_day)
    hbolbanddw = ROOT.TH1F('bolbanddw','bolbanddw',len(runArray_day)-1,runArray_day)            
    hma20day = ROOT.TH1F('ma20day','ma20day',len(runArray_day)-1,runArray_day)
    hma50day = ROOT.TH1F('ma50day','ma50day',len(runArray_day)-1,runArray_day)
    hma100day = ROOT.TH1F('ma100day','ma100day',len(runArray_day)-1,runArray_day)
    hma200day = ROOT.TH1F('ma200day','ma200day',len(runArray_day)-1,runArray_day)
    hprice.GetXaxis().SetTitle('Days')
    hprice.GetYaxis().SetTitle('Price for '+ticker)
    max_yrange = max(bins_price)
    min_yrange = min(bins_price)
    hprice.GetYaxis().SetRangeUser(min_yrange*0.93,max_yrange*1.07)
        
    if start_date:
        hprice.GetXaxis().SetTitle('Days since '+start_date)

    #for i in range(0,len(x_axis)-1):
    #    hprice.SetBinContent(i+1,bins_price[i])
    #print bins_ma_bolsigma
    #print bins_ma_bol
    ashift = len(bins_ma_bol)-len(x_axis)+1
    for i in range(0,len(x_axis)-1):
        itmp=i
        #print 'bol:',i+1,(ashift+itmp),bins_ma_bol[ashift+itmp],len(bins_ma_bol)        
        #print bins_ma_bol[ashift+itmp],bins_ma_bolsigma[itmp]
        hbolbanddw.SetBinContent(i+1,bins_ma_bol[ashift+itmp]-bins_ma_bolsigma[itmp])
        hbolbandup.SetBinContent(i+1,bins_ma_bol[ashift+itmp]+bins_ma_bolsigma[itmp])
        hbolma.SetBinError(i+1,0.0)
        hbolbandup.SetBinError(i+1,0.0)
        hbolbandsize.SetBinError(i+1,0.0)
        hbolpercentb.SetBinError(i+1,0.0)
        hbolbanddw.SetBinError(i+1,0.0)
        hbolma.SetBinContent(i+1,bins_ma_bol[ashift+itmp])
        
        if bins_ma_bol[itmp]>0.0:
            hbolbandsize.SetBinContent(i+1,2.0*bins_ma_bolsigma[itmp]/bins_ma_bol[ashift+itmp])
        if bins_ma_bolsigma[itmp]>0.0:
            hbolpercentb.SetBinContent(i+1,(bins_price[itmp+1]-(bins_ma_bol[ashift+itmp]-bins_ma_bolsigma[itmp]))/(2.0*bins_ma_bolsigma[itmp]))            
    for i in range(0,len(x_axis)-1):
        hma20day.SetBinContent(i+1,bins_ma_20day[i])
    for i in range(0,len(x_axis)-1):
        hma50day.SetBinContent(i+1,bins_ma_50day[i])
    for i in range(0,len(x_axis)-1):
        hma200day.SetBinContent(i+1,bins_ma_200day[i])
    for i in range(0,len(x_axis)-1):
        hma100day.SetBinContent(i+1,bins_ma_100day[i])        

    hma20day.SetLineColor(2)
    hma20day.SetMarkerColor(2)
    hbolbandsize.SetLineColor(2)
    hbolbandsize.SetMarkerColor(2)    
    hma50day.SetLineColor(3)
    hma50day.SetMarkerColor(3)
    hma100day.SetLineColor(4)
    hma100day.SetMarkerColor(4)
    hma200day.SetLineColor(6)
    hma200day.SetMarkerColor(6)

    hprice.SetLineColor(1)
    hprice.SetFillColor(1)
    hprice.GetXaxis().SetRangeUser(0,float(days))
    hprice.Draw()
    hprice.Draw('same lp e2')
    hma20day.Draw('lpsame')
    hma50day.Draw('lpsame')
    hma100day.Draw('lpsame')
    hma200day.Draw('lpsame')

    # legend
    hprice_tmp = hprice.Clone()
    hprice_tmp.SetLineColor(hprice.GetLineColor())
    hprice_tmp.SetMarkerColor(hprice.GetMarkerColor())
    hprice_tmp.SetFillColor(0)
    leg = ROOT.TLegend(0.45, 0.2, 0.85, 0.4);
    leg.SetBorderSize(0);
    leg.SetFillStyle(0);
    leg1 = ROOT.TLegend(0.45, 0.1, 0.85, 0.3);
    leg1.SetBorderSize(0);
    leg1.SetFillStyle(0);
    leg.AddEntry(hprice_tmp, "Closing Price");
    leg.AddEntry(hma20day, "20 Day MA");
    leg.AddEntry(hma50day, "50 Day MA");
    leg.AddEntry(hma100day, "100 Day MA");
    leg.AddEntry(hma200day, "200 Day MA");
    leg.Draw();

    decision_100day,decision_ndays_100days = AnalyzeMA(bins_ma_100day, bins_ma_20day)
    decision_50day,decision_ndays_50days = AnalyzeMA(bins_ma_50day, bins_ma_20day)
    
    # finish    
    c1.Update()
    if start_date==None:
        start_date = first_date
    new_ticker = ticker.replace('%5e','_')  
    c1.SaveAs(out_path_www+'/ma/'+new_ticker+'_'+start_date+'.'+out_file_type)
    if WAIT:
        c1.WaitPrimitive()
        raw_input('waiting...')
    # bolanger band size
    del c1
    c1,pads,padScaling,ratioPadScaling = base.DoRatio(ROOT)
    # Format
    base.Format([hprice,hbolbandup,hbolbanddw,hbolma],ROOT,True, padScaling,hist_name='')
    base.Format([hbolbandsize,hbolpercentb],ROOT,True, ratioPadScaling,hist_name='')    
    hbolbandsize.GetXaxis().SetTitle('Days since '+start_date)
    hbolpercentb.GetXaxis().SetTitle('Days since '+start_date)
    hbolbandsize.GetYaxis().SetTitle('Bolanger Band Size')
    hbolpercentb.GetYaxis().SetTitle('Percent b')
    hprice.GetXaxis().SetRangeUser(0,float(days))
    hbolbandup.SetLineColor(3)
    hbolbandup.SetMarkerColor(3)
    hbolma.SetLineColor(2)
    hbolma.SetMarkerColor(2)
    hbolma.SetLineStyle(2)
    hbolbandup.SetLineStyle(1)
    hbolbanddw.SetLineStyle(1)
    hbolbanddw.SetLineColor(4)
    hbolbanddw.SetMarkerColor(4)
    pads[0].cd()
    hprice.Draw()
    hprice.Draw('lp same e2')
    hbolma.Draw('same lp')
    hbolbandup.Draw('same lp')
    hbolbanddw.Draw('same lp')
    hprice.Draw('lp same e2')    
    #hbolbandsize.Draw()
    leg1.Clear()
    leg1.AddEntry(hbolma,'%s MA' %(NbolgangerMA))
    leg1.AddEntry(hbolbandup,'Bolanger Band')
    leg1.Draw()
    pads[1].cd()
    hbolpercentb.SetLineColor(2)
    hbolpercentb.SetMarkerColor(2)
    hbolbandsize.SetLineColor(3)
    hbolbandsize.SetMarkerColor(3)

    hbolbandsize.Scale(5.0)
    hbolpercentb.Draw()
    hbolbandsize.Draw('same')
    leg.Clear()
    leg.AddEntry(hbolpercentb,'Percent b' )
    leg.AddEntry(hbolbandsize,'5*Bolanger Band Size')
    leg.SetY1(0.4)
    leg.SetY2(0.6)
    leg.Draw()
    
    
    c1.Update()
    if start_date==None:
        start_date = first_date
    new_ticker = ticker.replace('%5e','_')
    c1.SaveAs(out_path_www+'/ma/'+new_ticker+'_'+start_date+'bol.'+out_file_type)
    if WAIT:
        c1.WaitPrimitive()
        raw_input('waiting...')
    aa =  [[decision_100day,decision_ndays_100days],
            [decision_50day,decision_ndays_50days],
            bins_ma_20day[len(bins_ma_20day)-1],
            bins_ma_50day[len(bins_ma_50day)-1],
            bins_ma_100day[len(bins_ma_100day)-1],
            bins_ma_200day[len(bins_ma_200day)-1],
            hbolpercentb.GetBinContent(hbolpercentb.GetNbinsX()),
           0.2*hbolbandsize.GetBinContent(hbolbandsize.GetNbinsX())/4.0*bins_ma_20day[len(bins_ma_20day)-1],
           ticker,jan1_price]
    #print aa
    return aa