def GetChaikin(history, days=20, start_date=None): bins_price = [] bins_rsi = [] nday = 0 ticker = 'N/A' first_date = None t = base.GetTime(start_date) chaikin = 0.0 vol = 0.0 for h in history: #print h this_t = base.GetTime(h['Date']) if this_t <= t: ticker = h['Symbol'] if first_date == None: first_date = h['Date'] mult = ((float(h['Close']) - float(h['Low'])) - (float(h['High']) - float(h['Close']))) if (float(h['High']) - float(h['Low'])) != 0.0: mult /= (float(h['High']) - float(h['Low'])) chaikin += mult * float(h['Volume']) vol += float(h['Volume']) nday += 1 if nday == days: break # std deviation if vol == 0.0: vol = 1.0 return chaikin / vol
def GetStochastic(history, days=14, start_date=None): t = base.GetTime(start_date) this_date_index = None this_date_index_n = 0 #print 'start_date',start_date if len(history) < 1: return 0.0, 0.0, 0.0 #stoch,over_bought_today,over_sold_today for hindex in range(0, len(history)): h = history[len(history) - hindex - 1] this_t = base.GetTime(h['Date']) #print 'this: ',h['Date'] if this_t <= t: this_date_index = hindex this_date_index_n += 1 else: break #sys.exit(0) iter_days = 0 iter_days_first = 0 last_ndays_highs = [] last_ndays_lows = [] my_today = 0.0 if True: beginning = this_date_index_n - days if len(history) < this_date_index_n - days: beginning = 1 #print 'beginning:',beginning for hindex in range(beginning, len(history)): h = history[len(history) - hindex - 1] #print h a = float(h['Close']) iter_days_first += 1 if iter_days_first < days: last_ndays_highs += [float(h['High'])] last_ndays_lows += [float(h['Low'])] elif iter_days_first == days: last_ndays_highs += [float(h['High'])] last_ndays_lows += [float(h['Low'])] my_today = float(h['Close']) break #print last_ndays my_max = max(last_ndays_highs) my_min = min(last_ndays_lows) stoch = 0.0 #print start_date #print 'my_today ',my_today,' min: ',my_min,' my_max: ',my_max if my_max - my_min > 0.0: stoch = 100.0 * (my_today - my_min) / (my_max - my_min) over_bought_today = 0.8 * my_max + 0.2 * my_min #over_bought_today = (-1.0*my_min*(0.8) + my_min)/(1.-0.8) #over_sold_today = (my_max*0.2 + my_min)/1.2 over_sold_today = 0.2 * my_max + 0.8 * my_min #print stoch,' ',over_sold_today return stoch, over_bought_today, over_sold_today
def GetVolatility(history, days=22, start_date=None): bins_price = [] bins_rsi = [] nday = 0 ticker = 'N/A' first_date = None t = base.GetTime(start_date) for h in history: this_t = base.GetTime(h['Date']) if this_t <= t: ticker = h['Symbol'] if first_date == None: first_date = h['Date'] bins_price = [float(h['Close'])] + bins_price nday += 1 if nday == days: break # std deviation return base.pstdev(bins_price)
def GetExpMovingAverage(history, days = 50, start_date=None): t=base.GetTime(start_date) k = 2.0 / (days + 1.0); #tod = datetime.datetime.now() #d = datetime.timedelta(days = days) #tdt = datetime.datetime.fromtimestamp(time.mktime(t)) #a = tdt - d #print 'AAA: ',a #avg=0.0 iter_days = 0 prev_ema=-1.0 #print 'Start and today: ',start_date this_date_index = 0 for hindex in range(0,len(history)): h = history[len(history)-hindex-1] if 'Date' not in h: continue this_t = base.GetTime(h['Date']) #this_ttd = datetime.datetime.fromtimestamp(time.mktime(this_t)) if this_t<=t: this_date_index=hindex else: break #print this_date_index,' days ',days if (this_date_index-days+1)>=0: #print this_date_index-days+1,' ',len(history) for hindex in range(this_date_index-days+1,len(history)): h = history[len(history)-hindex-1] if 'Date' not in h: continue this_t = base.GetTime(h['Date']) if this_t<=t: iter_days+=1 if prev_ema<0.0: prev_ema=float(h['Close']) else: #EMA = Price(t) * k + EMA(y) * (1-k) prev_ema=(float(h['Close'])-prev_ema)*k + prev_ema #print h['Date'],' ',h['Close'],' ',prev_ema if iter_days==days: break return prev_ema
def GetAverage(history, days = 50, start_date=None): t=base.GetTime(start_date) line=[] avg=0.0 iter_days = 0 for h in history: if 'Date' not in h: print 'WARNING could not find date' continue this_t = base.GetTime(h['Date']) if this_t<=t: iter_days+=1 avg+=float(h['Close']) if days==20: line+=[h['Close']] if iter_days==days: break if iter_days>0.0: avg /= iter_days #if days==20: # print line # print avg return avg
def GetRSI(history, days=14, start_date=None): t = base.GetTime(start_date) this_date_index = None for hindex in range(0, len(history)): h = history[len(history) - hindex - 1] this_t = base.GetTime(h['Date']) if this_t <= t: this_date_index = hindex else: break list_avg_gains = [] list_avg_losses = [] avg_gain = 0.0 avg_loss = 0.0 iter_days = 0 iter_days_first = 0 if True: beginning = 1 last_close = 0.0 if len(history) > 250: beginning = len(history) - 250 for hindex in range(beginning, len(history)): h = history[len(history) - hindex - 1] hn1 = history[len(history) - hindex] # day before #a=h['Change'] #a=float(h['Close'])-float(h['Open']) last_close = float(h['Close']) a = float(h['Close']) - float(hn1['Close']) ag = 0.0 al = 0.0 if a > 0.0: ag = a else: al = a iter_days_first += 1 if iter_days_first < days: if a > 0.0: avg_gain += a else: avg_loss += a elif iter_days_first == days: avg_gain /= float(days) avg_loss /= float(days) else: avg_gain = (avg_gain * float(days - 1) + ag) / float(days) avg_loss = (avg_loss * float(days - 1) + al) / float(days) if (this_date_index - days + 1) <= hindex: iter_days += 1 list_avg_gains += [avg_gain] list_avg_losses += [avg_loss] if iter_days == days: break ag = 0.0 al = 0.0 if len(list_avg_gains) > 1: ag = list_avg_gains[len(list_avg_gains) - 1] if len(list_avg_losses) > 1: al = abs(list_avg_losses[len(list_avg_losses) - 1]) rsi = 50.0 if al != 0.0: rsi = 100.0 - 100.0 / (1.0 + ag / al) # solve the for the upper and lower values #upper_ag = float(days)*0.7-avg_gain*float(days-1) #lower_al = float(days)*avg_loss-avg_loss*float(days-1) # Try these #rsi = 100.0 - 100.0/(1.0+ag/al) #agnn = (1-C)/C*al #agnn=(ag*float(days-1) + XX)/float(days) #XX = (1-C)/C*al*float(days) - ag*float(days-1) #avg_loss=(avg_loss*float(days-1) + XX)/float(days) # Gain Required C = (1.0 - 70.0 / 100.0) #ag = (1.0-C)*al/C #ag = (avg_gain*float(days-1) + XX)/float(days) #upper_ag = (1.0-C)*al/C*float(days)-avg_gain*float(days-1) upper_ag = (1 - C) / C * al * float(days) - ag * float(days - 1) # Loss Required #ag*C/(1.0-C) = alnnn #alnnn=(al*float(days-1) + XX)/float(days) #ag*C/(1.0-C)*float(days)-al*float(days-1) = ( XX) C = (1.0 - 30.0 / 100.0) #lower_al = float(days)*ag/(1.0/C-1.0)-al*float(days-1) lower_al = ag * C / (1.0 - C) * float(days) - al * float(days - 1) #print last_close,' ',upper_ag,' ',lower_al return rsi, (last_close + upper_ag), (last_close - lower_al)
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()) ]
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...')
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()) ]
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