ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Comparison graph is not plotting properly
Posted by Pravin on Apr-05-2014 03:24
Attachments:
Hi Team,

I am using chart Director to plot financial charts. I want to display previous day's closing
price over my actual price chart. So that we can different when current market price goes
down/up over the previous day's closing price.
I did this with 'addComparison' functionality, where closing price data set will be compared
with actual price data set. But graph is getting plotted wrongly for closing price data set,
while legend is displaying correct data set value. Please have a look at attached graph, on
referring  'Previous closing price legend', you will understand graph is plotted wrongly for the
same ( the red horizontal line ). Could you please help me in solving this?

Thanks in Advance !!!
Pravin.
liveGraph.py.png

  Re: Comparison graph is not plotting properly
Posted by Peter Kwan on Apr-05-2014 06:32
Hi Parvin,

I cannot find where is the mistake in the comparison line. Would you mind to clarify?

In FinanceChart, I think the most common understanding of "comparison" is to plot two
series, and adjust the second series so that it starts at the same point as the first
series. For example, you may compare the stock of Cisco to Microsoft or to the NASDAQ
index. Without adjusting the series to the same starting point, there can be no meaning
comparison.

Since the second series is adjusted, the price axis does not apply to the second series
(only a percentage axis can be simultaneously applied to both series).

From your chart, the red line does seem to starting at the same point as the "current
price" line. As the price axis does not apply to the comparison line, so there is no
inconsistency as well.

For your case, if you just want to add a horizontal line to represent the previous closing
price, simply add a mark line, like (in C#/Java);

FinanceChart myFinanceChart = new FinanceChart(.......);
.....

XYChart mainChart = myFinanceChart.addMainChart(.......);

//add the mark line to the main price chart
mainChart.yAxis().addMark(previousClosingPrice, 0xff0000, "Previous\\nClose\\n" +
previousClosingPrice);

Hope this can help.

Regards
Peter Kwan

  Re: Comparison graph is not plotting properly
Posted by Pravin on Apr-05-2014 21:56
Hi Peter,

Thanks for your reply!!!

I don't see a way of doing this in python, please find my code below. In python we don't have an option to declare a variable type of particular class (in this case XYChart class ). Could you please help me in this?

Thanks in Advance!!!!
Pravin.



###########################################
###### data is getting stored in corresponding array ########

            row[0] = int(row[0])
            row[1] = float(row[1])
            offset_line[1] = int(offset_line[1])
            #hdl.write("time stamp  is %i & %i " %(offset_line[1],row[0] ))
            timestamp = row[0] + offset_line[1]
            row[2] = float(row[2])
            row[3] = float(row[3])
            row[4] = float(row[4])
            row[5] = int(row[5])
            ts.append(timestamp)
            close.append(row[1])
            high.append(row[2])
            low.append(row[3])
            open.append(row[4])
            volume.append(row[5])
            prev_close.append(pre_close)

compareData = None

resolution = 86400

def drawChart() :

    global ts, volume, high, low, open, close
    global compareData, resolution, hdl


    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



    width = 880
    mainHeight = 255
    indicatorHeight = 80



    # Create the chart object using the selected size
    m = FinanceChart(width)
    m.setPlotAreaStyle(0xD1D1D1, 0xEAEAEA, 0xEAEAEA, 0xEAEAEA, 0xEAEAEA)
    m.setBackground(0xC4C4C4, Transparent, 1)
    m.setDropShadow(0xAAAAAA, 2, 0x7fffffff, 2)

    m.setData(ts, high, low, open, close, volume,
        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))
    m.setDateLabelFormat(yearFormat = "{value|Y}", firstMonthFormat = "<*font=bold*>{value|mmm yyyy}" ,
                         otherMonthFormat = "{value|mmm}" , firstDayFormat = "<*font=bold*>{value|d mmm}"
                        , otherDayFormat="{value|d}" , firstHourFormat = "{value|h:nna}" , otherHourFormat = "{value|h:nna}")

    # A copyright message at the bottom left corner the title area
    m.addPlotAreaTitle(BottomRight,
        "<*font=arial.ttf,size=8*>Intraday chart analysis")


    #
    # 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()


    m.addTypicalPrice(0x000040)


    #
    # Add moving average lines.
    #
    addMovingAvg(m, query["avgType1"].value, avgPeriod1, 0x663300)
    addMovingAvg(m, query["avgType2"].value, avgPeriod2, 0x9900ff)


    #
    # 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


def addMovingAvg(m, avgType, avgPeriod, color) :
    if avgPeriod > 1 :
        if avgType == "SMA" :
            return m.addSimpleMovingAvg(avgPeriod, color)
        elif avgType == "EMA" :
            return m.addExpMovingAvg(avgPeriod, color)
        elif avgType == "TMA" :
            return m.addTriMovingAvg(avgPeriod, color)
        elif avgType == "WMA" :
            return m.addWeightedMovingAvg(avgPeriod, color)
    return None


def addIndicator(m, indicator, height) :
    if indicator == "RSI" :
        return m.addRSI(height, 14, 0x800080, 20, 0xff6666, 0x6666ff)
    elif indicator == "StochRSI" :
        return m.addStochRSI(height, 14, 0x800080, 30, 0xff6666, 0x6666ff)
    elif indicator == "MACD" :
        return m.addMACD(height, 26, 12, 9, 0x0000ff, 0xff00ff, 0x008000)
    elif indicator == "FStoch" :
        return m.addFastStochastic(height, 14, 3, 0x006060, 0x606000)
    elif indicator == "SStoch" :
        return m.addSlowStochastic(height, 14, 3, 0x006060, 0x606000)
    elif indicator == "ATR" :
        return m.addATR(height, 14, 0x808080, 0x0000ff)
    elif indicator == "ADX" :
        return m.addADX(height, 14, 0x008000, 0x800000, 0x000080)
    elif indicator == "DCW" :
        return m.addDonchianWidth(height, 20, 0x0000ff)
    elif indicator == "BBW" :
        return m.addBollingerWidth(height, 20, 2, 0x0000ff)
    elif indicator == "DPO" :
        return m.addDPO(height, 20, 0x0000ff)
    elif indicator == "PVT" :
        return m.addPVT(height, 0x0000ff)
    elif indicator == "Momentum" :
        return m.addMomentum(height, 12, 0x0000ff)
    elif indicator == "Performance" :
        return m.addPerformance(height, 0x0000ff)
    elif indicator == "ROC" :
        return m.addROC(height, 12, 0x0000ff)
    elif indicator == "OBV" :
        return m.addOBV(height, 0x0000ff)
    elif indicator == "AccDist" :
        return m.addAccDist(height, 0x0000ff)
    elif indicator == "CLV" :
        return m.addCLV(height, 0x0000ff)
    elif indicator == "WilliamR" :
        return m.addWilliamR(height, 14, 0x800080, 30, 0xff6666, 0x6666ff)
    elif indicator == "Aroon" :
        return m.addAroon(height, 14, 0x339933, 0x333399)
    elif indicator == "AroonOsc" :
        return m.addAroonOsc(height, 14, 0x0000ff)
    elif indicator == "CCI" :
        return m.addCCI(height, 20, 0x800080, 100, 0xff6666, 0x6666ff)
    elif indicator == "EMV" :
        return m.addEaseOfMovement(height, 9, 0x006060, 0x606000)
    elif indicator == "MDX" :
        return m.addMassIndex(height, 0x800080, 0xff6666, 0x6666ff)
    elif indicator == "CVolatility" :
        return m.addChaikinVolatility(height, 10, 10, 0x0000ff)
    elif indicator == "COscillator" :
        return m.addChaikinOscillator(height, 0x0000ff)
    elif indicator == "CMF" :
        return m.addChaikinMoneyFlow(height, 21, 0x008000)
    elif indicator == "NVI" :
        return m.addNVI(height, 255, 0x0000ff, 0x883333)
    elif indicator == "PVI" :
        return m.addPVI(height, 255, 0x0000ff, 0x883333)
    elif indicator == "MFI" :
        return m.addMFI(height, 14, 0x800080, 30, 0xff6666, 0x6666ff)
    elif indicator == "PVO" :
        return m.addPVO(height, 26, 12, 9, 0x0000ff, 0xff00ff, 0x008000)
    elif indicator == "PPO" :
        return m.addPPO(height, 26, 12, 9, 0x0000ff, 0xff00ff, 0x008000)
    elif indicator == "UO" :
        return m.addUltimateOscillator(height, 7, 14, 28, 0x800080, 20, 0xff6666,
            0x6666ff)
    elif indicator == "Vol" :
        return m.addVolIndicator(height, 0x99ff99, 0xff9999, 0xc0c0c0)
    elif indicator == "TRIX" :
        return m.addTRIX(height, 12, 0x0000ff)
    return None

#/ <summary>
#/ Creates a dummy chart to show an error message.
#/ </summary>
#/ <param name="msg">The error message.
#/ <returns>The BaseChart object containing the error message.</returns>
def errMsg(msg) :
    m = MultiChart(400, 200)
    m.addTitle2(Center, msg, "arial.ttf", 10).setMaxWidth(m.getWidth())
    return m

# create the finance chart
c = drawChart()

# Output the chart
print("Content-type: image/png\\n")
binaryPrint(c.makeChart2(PNG))

  Re: Comparison graph is not plotting properly
Posted by Peter Kwan on Apr-07-2014 23:30
Hi Parvin,

In Python, the code is something like:

mainChart = m.addMainChart(mainHeight)

mainChart.yAxis().addMark(pre_close, 0xff0000, "Previous\\nClose\\n%s" % (pre_close))

Hope this can help.

Regards
Peter Kwan

  Re: Comparison graph is not plotting properly
Posted by Pravin on Apr-08-2014 02:39
wow!!! it worked... Thanks a lot Peter

  Re: Comparison graph is not plotting properly
Posted by Pravin on Apr-14-2014 19:17
Attachments:
Hi Peter,

I got an issue in connection with above query, so replying to same thread. In attached first graph, i am not getting close price mark line, because close price is 22730 and graph does not has a y axis scale above 22700,  so its not drawing the mark line. In attached second graph, you can see the mark line because i have hard codes closePrice as 22600, which is lesser than 22700( graph is having yaxis scale for this ), so its drawing mark line for second graph attached here. Could you please let me know how i can make it draw mark line on both the graphs?

Thanks in Advance,
Pravin.
liveGraph.png
liveGraph_mark.png

  Re: Comparison graph is not plotting properly
Posted by Peter Kwan on Apr-15-2014 02:13
Hi Pravin,

ChartDirector auto-scales the axis based on the data in the chart layers. Because the mark
line is not considered as a layer, it will not be considered when scaling the axis. So it is
possible for the mark to go outside of the axis range.

To ensure the mark is within the axis range, you may add a dummy transparent line layer
containing the mark value. With this method, when ChartDirector auto-scales the y-axis, it
will consider the mark line value (which is now in the chart layers), and so the y-axis will
always include the mark value. The code is like:

#Store the return XYChart object, so we can use it later
myMainPriceChart = m.addMainChart(mainHeight)

#Add an invisible line just to ensure the y-axis will include previousClosingPrice
myMainPriceChart.addLineLayer([previousClosingPrice] * len(timeStamps), Transparent)

Hope this can help.

Regards
Peter Kwan

  Re: Comparison graph is not plotting properly
Posted by Pravin on Apr-15-2014 02:43
Hi Peter,

I worked !!!!  one million thanks to you