def drawChart(): global timeStamps, volData, highData, lowData, openData, closeData global compareData, resolution # In this demo, we just assume we plot up to the latest time. So end date is now. endDate = chartTime2(time.time()) # If the trading day has not yet started (before 9:30am), or if the end date is on on Sat or # Sun, we set the end date to 4:00pm of the last trading day while (endDate % 86400 < 9 * 3600 + 30 * 60) or ( getChartWeekDay(endDate) == 0) or (getChartWeekDay(endDate) == 6): endDate = endDate - endDate % 86400 - 86400 + 16 * 3600 # The duration selected by the user durationInDays = int(query["TimeRange"].value) # Compute the start date by subtracting the duration from the end date. startDate = endDate if durationInDays >= 30: # More or equal to 30 days - so we use months as the unit YMD = getChartYMD(endDate) startMonth = int(YMD / 100) % 100 - int(durationInDays / 30) startYear = int(YMD / 10000) while startMonth < 1: startYear = startYear - 1 startMonth = startMonth + 12 startDate = chartTime(startYear, startMonth, 1) else: # Less than 30 days - use day as the unit. The starting point of the axis is always at the # start of the day (9:30am). Note that we use trading days, so we skip Sat and Sun in # counting the days. startDate = endDate - endDate % 86400 + 9 * 3600 + 30 * 60 for i in range(1, durationInDays): if getChartWeekDay(startDate) == 1: startDate = startDate - 3 * 86400 else: startDate = startDate - 86400 # The moving average periods selected by the user. avgPeriod1 = 0 try: avgPeriod1 = int(query["movAvg1"].value) except: pass avgPeriod2 = 0 try: avgPeriod2 = int(query["movAvg2"].value) except: pass if avgPeriod1 < 0: avgPeriod1 = 0 elif avgPeriod1 > 300: avgPeriod1 = 300 if avgPeriod2 < 0: avgPeriod2 = 0 elif avgPeriod2 > 300: avgPeriod2 = 300 # We need extra leading data points in order to compute moving averages. extraPoints = 20 if avgPeriod1 > extraPoints: extraPoints = avgPeriod1 if avgPeriod2 > extraPoints: extraPoints = avgPeriod2 # Get the data series to compare with, if any. compareKey = string.strip(query["CompareWith"].value) compareData = None if getData(compareKey, startDate, endDate, durationInDays, extraPoints): compareData = closeData # The data series we want to get. tickerKey = string.strip(query["TickerSymbol"].value) if not getData(tickerKey, startDate, endDate, durationInDays, extraPoints): return errMsg("Please enter a valid ticker symbol") # We now confirm the actual number of extra points (data points that are before the start date) # as inferred using actual data from the database. extraPoints = len(timeStamps) for i in range(0, len(timeStamps)): if timeStamps[i] >= startDate: extraPoints = i break # Check if there is any valid data if extraPoints >= len(timeStamps): # No data - just display the no data message. return errMsg("No data available for the specified time period") # In some finance chart presentation style, even if the data for the latest day is not fully # available, the axis for the entire day will still be drawn, where no data will appear near the # end of the axis. if resolution < 86400: # Add extra points to the axis until it reaches the end of the day. The end of day is # assumed to be 16:00 (it depends on the stock exchange). lastTime = timeStamps[len(timeStamps) - 1] extraTrailingPoints = int((16 * 3600 - lastTime % 86400) / resolution) for i in range(0, extraTrailingPoints): timeStamps.append(lastTime + resolution * (i + 1)) # # At this stage, all data are available. We can draw the chart as according to user input. # # # Determine the chart size. In this demo, user can select 4 different chart sizes. Default is # the large chart size. # width = 780 mainHeight = 255 indicatorHeight = 80 size = query["ChartSize"].value if size == "S": # Small chart size width = 450 mainHeight = 160 indicatorHeight = 60 elif size == "M": # Medium chart size width = 620 mainHeight = 215 indicatorHeight = 70 elif size == "H": # Huge chart size width = 1000 mainHeight = 320 indicatorHeight = 90 # Create the chart object using the selected size m = FinanceChart(width) # Set the data into the chart object m.setData(timeStamps, highData, lowData, openData, closeData, volData, extraPoints) # # We configure the title of the chart. In this demo chart design, we put the company name as the # top line of the title with left alignment. # m.addPlotAreaTitle(TopLeft, tickerKey) # We displays the current date as well as the data resolution on the next line. resolutionText = "" if resolution == 30 * 86400: resolutionText = "Monthly" elif resolution == 7 * 86400: resolutionText = "Weekly" elif resolution == 86400: resolutionText = "Daily" elif resolution == 900: resolutionText = "15-min" m.addPlotAreaTitle( BottomLeft, "<*font=arial.ttf,size=8*>%s - %s chart" % (m.formatValue( chartTime2(time.time()), "mmm dd, yyyy"), resolutionText)) # A copyright message at the bottom left corner the title area m.addPlotAreaTitle( BottomRight, "<*font=arial.ttf,size=8*>(c) Advanced Software Engineering") # # Add the first techical indicator according. In this demo, we draw the first indicator on top # of the main chart. # addIndicator(m, query["Indicator1"].value, indicatorHeight) # # Add the main chart # m.addMainChart(mainHeight) # # Set log or linear scale according to user preference # if query["LogScale"].value == "1": m.setLogScale(1) # # Set axis labels to show data values or percentage change to user preference # if query["PercentageScale"].value == "1": m.setPercentageAxis() # # Draw any price line the user has selected # mainType = query["ChartType"].value if mainType == "Close": m.addCloseLine(0x000040) elif mainType == "TP": m.addTypicalPrice(0x000040) elif mainType == "WC": m.addWeightedClose(0x000040) elif mainType == "Median": m.addMedianPrice(0x000040) # # Add comparison line if there is data for comparison # if compareData != None: if len(compareData) > extraPoints: m.addComparison(compareData, 0x0000ff, compareKey) # # Add moving average lines. # addMovingAvg(m, query["avgType1"].value, avgPeriod1, 0x663300) addMovingAvg(m, query["avgType2"].value, avgPeriod2, 0x9900ff) # # Draw candlesticks or OHLC symbols if the user has selected them. # if mainType == "CandleStick": m.addCandleStick(0x33ff33, 0xff3333) elif mainType == "OHLC": m.addHLOC(0x008800, 0xcc0000) # # Add parabolic SAR if necessary # if query["ParabolicSAR"].value == "1": m.addParabolicSAR(0.02, 0.02, 0.2, DiamondShape, 5, 0x008800, 0x000000) # # Add price band/channel/envelop to the chart according to user selection # bandType = query["Band"].value if bandType == "BB": m.addBollingerBand(20, 2, 0x9999ff, 0xc06666ff) elif bandType == "DC": m.addDonchianChannel(20, 0x9999ff, 0xc06666ff) elif bandType == "Envelop": m.addEnvelop(20, 0.1, 0x9999ff, 0xc06666ff) # # Add volume bars to the main chart if necessary # if query["Volume"].value == "1": m.addVolBars(indicatorHeight, 0x99ff99, 0xff9999, 0xc0c0c0) # # Add additional indicators as according to user selection. # addIndicator(m, query["Indicator2"].value, indicatorHeight) addIndicator(m, query["Indicator3"].value, indicatorHeight) addIndicator(m, query["Indicator4"].value, indicatorHeight) return m
volData = rantable.getCol(5) # Create a FinanceChart object of width 640 pixels c = FinanceChart(640) # Add a title to the chart c.addTitle("Finance Chart Demonstration") # Set the data into the finance chart object c.setData(timeStamps, highData, lowData, openData, closeData, volData, extraDays) # Add a slow stochastic chart (75 pixels high) with %K = 14 and %D = 3 c.addSlowStochastic(75, 14, 3, 0x006060, 0x606000) # Add the main chart with 240 pixels in height c.addMainChart(240) # Add a 10 period simple moving average to the main chart, using brown color c.addSimpleMovingAvg(10, 0x663300) # Add a 20 period simple moving average to the main chart, using purple color c.addSimpleMovingAvg(20, 0x9900ff) # Add candlestick symbols to the main chart, using green/red for up/down days c.addCandleStick(0x00ff00, 0xff0000) # Add 20 days donchian channel to the main chart, using light blue (9999ff) as the # border and semi-transparent blue (c06666ff) as the fill color c.addDonchianChannel(20, 0x9999ff, 0xc06666ff) # Add a 75 pixels volume bars sub-chart to the bottom of the main chart, using
buySignal[i] = lowData[i] if (sma5[i - 1] >= sma20[i - 1]) and (sma5[i] < sma20[i]): sellSignal[i] = highData[i] # Create a FinanceChart object of width 640 pixels c = FinanceChart(640) # Add a title to the chart c.addTitle("Finance Chart with Custom Symbols") # Set the data into the finance chart object c.setData(timeStamps, highData, lowData, openData, closeData, volData, extraDays) # Add the main chart with 240 pixels in height mainChart = c.addMainChart(240) # Add buy signal symbols to the main chart, using cyan (0x00ffff) upward pointing arrows as symbols buyLayer = mainChart.addScatterLayer(None, buySignal, "Buy", ArrowShape(0, 1, 0.4, 0.4), 11, 0x00ffff) # Shift the symbol lower by 20 pixels buyLayer.getDataSet(0).setSymbolOffset(0, 20) # Add sell signal symbols to the main chart, using purple (0x9900cc) downward pointing arrows as # symbols sellLayer = mainChart.addScatterLayer(None, sellSignal, "Sell", ArrowShape(180, 1, 0.4, 0.4), 11, 0x9900cc) # Shift the symbol higher by 20 pixels sellLayer.getDataSet(0).setSymbolOffset(0, -20)
# Create a FinanceChart object of width 720 pixels c = FinanceChart(720) # Add a title to the chart c.addTitle("Finance Chart Demonstration") # Disable default legend box, as we are using dynamic legend c.setLegendStyle("normal", 8, Transparent, Transparent) # Set the data into the finance chart object c.setData(timeStamps, highData, lowData, openData, closeData, volData, extraDays) # Add the main chart with 240 pixels in height c.addMainChart(240) # Add a 10 period simple moving average to the main chart, using brown color c.addSimpleMovingAvg(10, 0x663300) # Add a 20 period simple moving average to the main chart, using purple color c.addSimpleMovingAvg(20, 0x9900ff) # Add candlestick symbols to the main chart, using green/red for up/down days c.addCandleStick(0x00ff00, 0xff0000) # Add 20 days bollinger band to the main chart, using light blue (9999ff) as the border and # semi-transparent blue (c06666ff) as the fill color c.addBollingerBand(20, 2, 0x9999ff, 0xc06666ff) # Add a 75 pixels volume bars sub-chart to the bottom of the main chart, using green/red/grey for
vb_high = [x.high for x in bars] vb_low = [x.low for x in bars] vb_close = [x.close for x in bars] #c = FinanceChart(1024) c = FinanceChart(2200) # Add a title to the chart c.addTitle("Basket Divergences - %s" % trade_date.strftime('%D (%a)')) # Set the data into the finance chart object c.setData(ts, es_high, es_low, es_open, es_close, es_vol, None) # Add the main chart with 240 pixels in height es_chart = c.addMainChart(600) # Add HLOC symbols to the main chart, using green/red for up/down days #c.addHLOC('0x008000', '0xcc0000') c.addCandleStick('0x008000', '0xcc0000') # Add a 75 pixels volume bars sub-chart to the bottom of the main chart, using # green/red/grey for up/down/flat days #c.addVolBars(75, '0x99ff99', '0xff9999', '0x808080') #c.addVolIndicator(75, '0x99ff99', '0xff9999', '0x808080') # Add the evolving high and low as line indicators to the chart hod = [max(es_high[:i+1]) for i, v in enumerate(es_high)] lod = [min(es_low[:i+1]) for i, v in enumerate(es_low)] c.addLineIndicator2(es_chart, hod, '0xFF6600', 'HOD') c.addLineIndicator2(es_chart, lod, '0xFF6600', 'LOD')