(Candle Stick)Trim the Non-Market Trading Day of X-axis

Discussion about JFreeChart related to stockmarket charts.
vuj2010
Posts: 11
Joined: Fri Apr 07, 2006 11:54 pm

SegmentedTimeline not working

Post by vuj2010 » Sat Jul 08, 2006 3:23 am

It is great that you could exclude the holidays. Could you take a look and comment on my code. Don't seem to work for me. Thank you


My code:

SegmentedTimeline timeline = SegmentedTimeline.newMondayThroughFridayTimeline();

Calendar calNow = GregorianCalendar.getInstance();
Date endDate = cal.getTime();

calNow.set(Calendar.HOUR_OF_DAY, 0);
calNow.set(Calendar.MINUTE, 0);
calNow.set(Calendar.SECOND, 0);
calNow.set(Calendar.MILLISECOND, 0);
int iYrNow=calNow.get(Calendar.YEAR);
int iMonthNow=calNow.get(Calendar.DAY_OF_MONTH);
int iDateNow=calNow.get(Calendar.DATE);

calNow.set(iYrNow-5,iMonthNow, iDateNow);
Date startDate = cal.getTime();

Holidays holiday= new Holidays();

timeline.addBaseTimelineException(holiday.IndependenceDayObserved(2006));
timeline.addBaseTimelineExclusions(startDate.getTime(), endDate.getTime());

wzwei28
Posts: 105
Joined: Wed Mar 16, 2005 9:17 am
Location: Malaysia, Earth
Contact:

Post by wzwei28 » Thu Jul 20, 2006 8:12 am

Sorry... no email notify me...so I also don't know u had post... By the way... I try to spend half day on this weekend to post a demo of exclusion public holiday + weekend + non-trading day... Hope I am able to do it...Sorry

wzwei28
Posts: 105
Joined: Wed Mar 16, 2005 9:17 am
Location: Malaysia, Earth
Contact:

Post by wzwei28 » Fri Jul 21, 2006 10:44 am

* Demo : http://chart.n2nconnect.com/FAChart/NNSP.jsp
Requirements: JRE 1.4.2_04 and above and set Timezone to GMT+8:00


Sorry due to time constraint, I only able to do a simple solution(demo) for Non-Trading Day Exclusion (Holiday, suspend-day, Saturday, Sunday)

Anyone who interested can plug in this two class (JHistoricalCSChart & ConfigSegmentedTimeline) and u guys & girls can find the keyword 'ziwei', theres the place I had edited.

However if anyone using Apache Http Server or Tomcat Web Server and willing to direct plug in in Web-INF > classes. U may request from me, and I email whole package source for you, jar also can.

* Version : jfreechart-1.0.0-rc1.jar
My email address is wzwei28@yahoo.com (any comments and query are welcomed).
hope can contribute little effort in open source application.


ConfigSegmentedTimeline.java
------------------------------------

Code: Select all

/* 
 * (C) Copyright 2000-2006, by Wong Zi Wei.
 *
 *
 * Original Author	:  	Wong Zi Wei(WZW);
 * Contributor(s)	:  	-;
 * 
 * Email			:	wzwei28@yahoo.com
 *
 * Version 1.0.1    :	21/07/2006(WZW)	Store and calculate the SegmentedTimeline
 *
 * Error Code		:	001
 *						002
 *						003
 *
 *
 */

package cfg;

import java.util.Date;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;

//---segmentedTimeline---
import java.util.Calendar;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
import org.jfree.chart.axis.DateTickUnit; 
import org.jfree.chart.axis.SegmentedTimeline;

public class ConfigSegmentedTimeline{
	
	private static long m_lFirstMonday = 0;
	private static ArrayList m_alHDate = new ArrayList(); 
	private static SegmentedTimeline m_segmentedTimeline = null;
	private static JFreeChart m_chart = null;
	private static Date m_aTrdDate[] = new Date[1];
	private static Date m_dtDateInit = null;
	
	public void configSegmentedTimeline(){		
	}
	
	public static void run(){
	  //@@ Segmented Timeline - Exclude Non-Trading Day, Suspended date, Saturday & Sunday
	  Date dtInit = getDateInit();
	  JFreeChart chart = getChart();
	  DateAxis axis = (DateAxis) chart.getXYPlot().getDomainAxis();
	  //SegmentedTimeline segmentedTimeline = SegmentedTimeline.newMondayThroughFridayTimeline(); 
	  SegmentedTimeline segmentedTimeline = new SegmentedTimeline(SegmentedTimeline.DAY_SEGMENT_SIZE, 5, 2); 

	  if(dtInit!=null){	    
	    SimpleDateFormat sdfYC = new SimpleDateFormat("yyyy");
	    SimpleDateFormat sdfMC = new SimpleDateFormat("MM");
	    SimpleDateFormat sdfDC = new SimpleDateFormat("dd");
	    int iYC = Integer.parseInt(sdfYC.format(dtInit));
	    int iMC = Integer.parseInt(sdfMC.format(dtInit));
	    int iDC = Integer.parseInt(sdfDC.format(dtInit));
	   	//-Select your Timezone-
		GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT+8")); //use standard GMT + 0
	    //calendar.set(2006, Calendar.MARCH, 25, 12, 0, 0); 
	    Date d1 = calendar.getTime(); 
	    
		//GregorianCalendar calendar = new GregorianCalendar();
		ArrayList alHDate = new ArrayList(); //HDate-Hide Date
		Date aTrdDate[] = getTrdDate();
		Date dtDate = calendar.getTime();
		int iTrdDateLast = aTrdDate.length-1;
		int iIndex = 0;
		if(aTrdDate.length>0){
			Date aTrdDateInit = aTrdDate[0]; 
			Date aTrdDateLast = aTrdDate[iTrdDateLast];
			calendar.setTime(aTrdDateInit); //set init date		
			double dDays = (aTrdDateLast.getTime()-aTrdDateInit.getTime())/1000/60/60/24; //60 minutes, 60 seconds, 24 hour.
			for(int i=0; i<=dDays; i++){
				dtDate = calendar.getTime();	
				if(dtDate.equals(aTrdDate[iIndex])){
					iIndex++;
				}else{
					alHDate.add(dtDate);		
				}
				calendar.add(Calendar.DATE, 1);//increment 1 day		
	    	}
	    }
	    
		int offset = TimeZone.getDefault().getRawOffset();
		TimeZone NO_DST_TIME_ZONE = new SimpleTimeZone(offset, "UTC-" + offset);        
	    Calendar cal = new GregorianCalendar(NO_DST_TIME_ZONE);
	    cal.set(iYC, 0, 1, 0, 0, 0); //can't use iMC and iDC, because the crosshair have bugs in locking data
	    cal.set(Calendar.MILLISECOND, 0);
	    while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
	         cal.add(Calendar.DATE, 1);
	    }
	    long lFirstMonday = cal.getTime().getTime();
	    
	    //Store parameter
	    setFirstMonday(lFirstMonday);
	    setHDate(alHDate);
	    setSegmentedTimeline(segmentedTimeline);
	  }
	    
	}	
	
	private static void setFirstMonday(long vs_lFirstMonday){ m_lFirstMonday = vs_lFirstMonday; }
	private static void setHDate(ArrayList vs_alHDate){ m_alHDate = vs_alHDate; }
	private static void setSegmentedTimeline(SegmentedTimeline vs_segmentedTimeline){ m_segmentedTimeline = vs_segmentedTimeline; }
	public static void setChart(JFreeChart vs_chart){ m_chart = vs_chart; }
	public static void setTrdDate(Date[] vs_aTrdDate){ m_aTrdDate = vs_aTrdDate; }
	public static void setDateInit(Date vs_dtDateInit){ m_dtDateInit = vs_dtDateInit; }
	
	public static long getFirstMonday(){ return m_lFirstMonday; }
	public static ArrayList getHDate(){ return m_alHDate; }
	public static SegmentedTimeline getSegmentedTimeline(){ return m_segmentedTimeline; }	
	public static JFreeChart getChart(){ return m_chart; }
	public static Date[] getTrdDate(){ return m_aTrdDate; }
	public static Date getDateInit(){ return m_dtDateInit; }
	
	public static void main(String[] args){
		
	}
}


JHistoricalCSChart------------------------

Code: Select all

package demo;

import java.awt.Dimension;
import java.util.Date;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.DefaultHighLowDataset;
import org.jfree.data.xy.OHLCDataset;
import org.jfree.date.DateUtilities;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.axis.NumberAxis;

//-ziwei import-
import java.util.ArrayList;
import java.text.SimpleDateFormat;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.SegmentedTimeline;

public class JHistoricalCSChart extends ApplicationFrame{

	//-ziwei import-
	private static cfg.ConfigSegmentedTimeline m_CST;
	
    public JHistoricalCSChart(String s)
    {
        super(s);
        OHLCDataset ohlcdataset = createDataset();
        JFreeChart jfreechart = createChart(ohlcdataset);
        jfreechart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);
        ChartPanel chartpanel = new ChartPanel(jfreechart);
        chartpanel.setRangeZoomable(false);
        chartpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartpanel);
    }

    private static JFreeChart createChart(OHLCDataset ohlcdataset)
    {
        JFreeChart jfreechart = ChartFactory.createCandlestickChart("Historical Non-Trading Day Exclusion", "Time", "Value", ohlcdataset, true);
        jfreechart.setAntiAlias(false);
        
        XYPlot xyplot = jfreechart.getXYPlot();
        //@@ edit: solution is to se the autoRangeIncludesZero to false, so that no more return to 0 for candlestick.
        NumberAxis numberAxis = (NumberAxis) xyplot.getRangeAxis();
		numberAxis.setAutoRangeIncludesZero(false); //exclude 0;
		xyplot.setRangeAxis(numberAxis);
        //@@ edit: when zooming in & out, u will notice the range had move back to 0,
        //@@ edit: set Margin of range(highest & lowest margin)
        ValueAxis rangeAxis = (ValueAxis) xyplot.getRangeAxis();
		rangeAxis.setAutoRange(false);		//no use, because in ValueAxis:In resizeRange(% > 0.0)->setAutoRange(true)->autoAdjustRange() still run it
		//rangeAxis.setRangeWithMargins(20,100);
		rangeAxis.setRange(20,100);
		xyplot.setRangeAxis(rangeAxis);
		
		//-ziwei Edit(Optional)-
		DateAxis domainAxis = (DateAxis) xyplot.getDomainAxis();
		domainAxis.setVerticalTickLabels(true);
		domainAxis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yy"));
		//---Set bar chart to middle---
		domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);		
				
		//==================================================================================
		//@@ Segmented Timeline - Exclude Holiday, Non-Trading Day, Suspended date, Saturday & Sunday
		m_CST.setChart(jfreechart);
		m_CST.run();
		
		SegmentedTimeline segmentedTimeline = m_CST.getSegmentedTimeline();
		long lFirstMonday = m_CST.getFirstMonday();
		ArrayList alHDate = m_CST.getHDate();
		
		segmentedTimeline.setStartTime(lFirstMonday); //Must remake the calendar segment to avoid overlapping (Monday-Friday)
	    segmentedTimeline.addExceptions(alHDate);     //Exclude non-trading day
	    DateAxis axis = (DateAxis) jfreechart.getXYPlot().getDomainAxis();
		axis.setTimeline(segmentedTimeline);
	   
        return jfreechart;
    }

    public static OHLCDataset createDataset()
    {
        Date aDate[] = new Date[9]; //(11-2=9),47
        double ad[] = new double[9];
        double ad1[] = new double[9];
        double ad2[] = new double[9];
        double ad3[] = new double[9];
        double ad4[] = new double[9];
        int i = 1;
        byte byte0 = 2;
     //-------------------------
     //ziwei edit
     //-1st week(monday-friday)-
        aDate[0] = DateUtilities.createDate(2007, 12, 24, 12, 0);
        ad[0] = 47D;
        ad1[0] = 33D;
        ad2[0] = 35D;
        ad3[0] = 33D;
        ad4[0] = 100D;
     //-Holiday Christmas-
     /*
        aDate[1] = DateUtilities.createDate(2007, 12, 25, 12, 0);
        ad[1] = 47D;
        ad1[1] = 32D;
        ad2[1] = 41D;
        ad3[1] = 37D;
        ad4[1] = 150D;
     */
        aDate[1] = DateUtilities.createDate(2007, 12, 26, 12, 0);
        ad[1] = 49D;
        ad1[1] = 43D;
        ad2[1] = 46D;
        ad3[1] = 48D;
        ad4[1] = 70D;
        aDate[2] = DateUtilities.createDate(2007, 12, 27, 12, 0);
        ad[2] = 51D;
        ad1[2] = 39D;
        ad2[2] = 40D;
        ad3[2] = 47D;
        ad4[2] = 200D;
        aDate[3] = DateUtilities.createDate(2007, 12, 28, 12, 0);
        ad[3] = 60D;
        ad1[3] = 40D;
        ad2[3] = 46D;
        ad3[3] = 53D;
        ad4[3] = 120D; 
     
     //-------------------------
     //-2nd week(monday-friday)-          
        aDate[4] = DateUtilities.createDate(2007, 12, 31, 12, 0);
        ad[4] = 62D;
        ad1[4] = 55D;
        ad2[4] = 57D;
        ad3[4] = 61D;
        ad4[4] = 110D;
        
     //-New Year Holiday-
     /*    
        aDate[6] = DateUtilities.createDate(2008, 1, 1, 12, 0);
        ad[6] = 65D;
        ad1[6] = 56D;
        ad2[6] = 62D;
        ad3[6] = 59D;
        ad4[6] = 70D;
      */
        aDate[5] = DateUtilities.createDate(2008, 1, 2, 12, 0);
        ad[5] = 55D;
        ad1[5] = 43D;
        ad2[5] = 45D;
        ad3[5] = 47D;
        ad4[5] = 20D;
        aDate[6] = DateUtilities.createDate(2008, 1, 3, 12, 0);
        ad[6] = 54D;
        ad1[6] = 33D;
        ad2[6] = 40D;
        ad3[6] = 51D;
        ad4[6] = 30D;
        aDate[7] = DateUtilities.createDate(2008, 1, 4, 12, 0);
        ad[7] = 47D;
        ad1[7] = 33D;
        ad2[7] = 35D;
        ad3[7] = 33D;
        ad4[7] = 100D;
      
     //-------------------------   
     //-3rd week(monday-friday)-   
        aDate[8] = DateUtilities.createDate(2008, 1, 7, 12, 0);
        ad[8] = 54D;
        ad1[8] = 38D;
        ad2[8] = 43D;
        ad3[8] = 52D;
        ad4[8] = 50D;
      
     //-------------------------
       
     //---------------------------------------------------
	 //@@ SegmentedTimeline - Exclude Holiday, Non-Trading Day, Suspended date, Saturday & Sunday
		m_CST.setDateInit(aDate[0]);
		m_CST.setTrdDate(aDate);
		
        return new DefaultHighLowDataset("Series 1", aDate, ad, ad1, ad2, ad3, ad4);
    }

    public static JPanel createDemoPanel()
    {
        JFreeChart jfreechart = createChart(createDataset());
        return new ChartPanel(jfreechart);
    }

    public static void main(String args[])
    {
        JHistoricalCSChart JHistoricalCSChart = new JHistoricalCSChart("Historical Non-Trading Day Exclusion");
        JHistoricalCSChart.pack();
        RefineryUtilities.centerFrameOnScreen(JHistoricalCSChart);
        JHistoricalCSChart.setVisible(true);
    }
}


* Well now I am still solving bugs in overlapping and Exclusion of intraday gap (hour. i.e. 08:45-12:45 , rest, 14:30-17:15) and I want to exclude the gap in the rest session and also market close hour(17:15-08:45 another morning) for historical movement chart).

* If anyone have some solution, please email to me wzwei28@yahoo.com, and I help to publish to everyone. Thanks!

wzwei28
Posts: 105
Joined: Wed Mar 16, 2005 9:17 am
Location: Malaysia, Earth
Contact:

Post by wzwei28 » Wed Aug 23, 2006 4:04 am

This is an open source project, so based on the LGPL license, the source code for JFreechart and overriden classes must be provided in the site. Last time I promise to do this before 31/08/06.

Therefore, if anyone interested on the source code, please don't worry... I had promise to put a link for them to download the source code especially trendline ( trend line ) drawing, historical movement, gap exclusion for historical and intraday. I believe this is my little effort to contribute back to open source. Thanks for the emails sent before requesting the source code! Hope open source have a better future.

http://chart.n2nconnect.com/FAChart/NNSP.jsp
I will locate source code link inside the Help link on top right in this two week before 31/08/06.

If any query and comments, u may email to me. I will try my best to help and then post the solution into JFreechart forum for everyone.

best regards,
Wong Zi Wei.

raman011
Posts: 5
Joined: Tue Aug 22, 2006 2:44 pm

Post by raman011 » Mon Sep 25, 2006 8:43 pm

I could not find souce link in the help contents.

Locked