convert timeseries into highlowdataset

Discussion about JFreeChart related to stockmarket charts.
Locked
uls
Posts: 25
Joined: Fri Oct 17, 2003 10:46 am

convert timeseries into highlowdataset

Post by uls » Fri Nov 14, 2003 12:07 pm

can i somehow automatically convert a timeseries dataset into a highlowdataset or should i do it manually?

regards&tia
ulrich

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Fri Nov 14, 2003 12:31 pm

There's no code to do it for you, but it would make an excellent utility function if you wanted to write something that's fairly generalized and contribute it to the project. :wink:
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

uls
Posts: 25
Joined: Fri Oct 17, 2003 10:46 am

Post by uls » Fri Nov 14, 2003 12:33 pm

can do! :-)

uls
Posts: 25
Joined: Fri Oct 17, 2003 10:46 am

Post by uls » Mon Nov 17, 2003 2:37 pm

Code: Select all


/**
	 * candlewidth specifies width of candles in milliseconds
	 * @param in
	 * @param candlewidth
	 * @return
	 */
	public DefaultHighLowDataset convertToDefaultHighLowDataset(TimeSeries in, long candlewidth){
		Date firstDate=in.getTimePeriod(0).getStart();

		Collection c=in.getTimePeriods();
		int n=c.size();
		
		long numberOfCandles=(in.getTimePeriod(n-1).getLastMillisecond()-in.getTimePeriod(0).getFirstMillisecond())/candlewidth;	
		
		Date[] dates=new Date[(int)numberOfCandles];
		double[] opens=new double[(int)numberOfCandles];
		double[] highs=new double[(int)numberOfCandles];
		double[] lows=new double[(int)numberOfCandles];
		double[] closes=new double[(int)numberOfCandles];
		double[] volumes=new double[(int)numberOfCandles];

		long msindex=in.getTimePeriod(0).getFirstMillisecond();
		
		//now cycle through all entries and get open, hi, low and close for a span
		double open=0.0, hi=0.0, low=0.0, close=0.0;
		int z=0;
		for(int i=0;i<numberOfCandles;i++){
			if(z<(n-1)){
				//set low, hi, open
				open=hi=low=in.getDataItem(z).getValue().doubleValue();
				System.out.println(""+z);
				long h1=in.getTimePeriod(0).getFirstMillisecond();
				while(h1>=((i*candlewidth)+msindex)){
					//ok, value is in the current timespan
	
					if(in.getDataItem(z).getValue().doubleValue()>hi)hi=in.getDataItem(z).getValue().doubleValue();
					if(in.getDataItem(z).getValue().doubleValue()<low)low=in.getDataItem(z).getValue().doubleValue();
					
					z++;	
					h1=in.getTimePeriod(z).getFirstMillisecond();
				}
				//set close
				close=hi=low=in.getDataItem(z).getValue().doubleValue();
				
				//set all values
				dates[i]=new Date(msindex+i*candlewidth);
				opens[i]=open;
				highs[i]=hi;
				lows[i]=low;
				closes[i]=close;
				volumes[i]=0;
			}
		}
		
			
		
		DefaultHighLowDataset ret=new DefaultHighLowDataset("name", dates, opens, highs, lows, closes, volumes);
	
		
		
		return ret;
	}

	public void convert(){
		chart=ChartFactory.createCandlestickChart("", "time", "value", convertToDefaultHighLowDataset(x1, 15000), true);
	}		



it doesn't work ... i always get an array out of bounds exception ... in getTimePeriod(n); ...

getTimePeriod(0) works though ...

uls
Posts: 25
Joined: Fri Oct 17, 2003 10:46 am

working code

Post by uls » Mon Nov 17, 2003 7:32 pm

working code:

Code: Select all

	
	/**
	 * candlewidth specifies width of candles in milliseconds
	 * @param in
	 * @param candlewidth
	 * @return
	 */
	public DefaultHighLowDataset convertToDefaultHighLowDataset(TimeSeries in, long candlewidth){
		Date firstDate=in.getTimePeriod(0).getStart();

		Collection c=in.getTimePeriods();
		int n=c.size();
		
		long numberOfCandles=(in.getTimePeriod(n-1).getLastMillisecond()-in.getTimePeriod(0).getFirstMillisecond())/candlewidth;	
		//long numberOfCandles=1;
		Date[] dates=new Date[(int)numberOfCandles];
		double[] opens=new double[(int)numberOfCandles];
		double[] highs=new double[(int)numberOfCandles];
		double[] lows=new double[(int)numberOfCandles];
		double[] closes=new double[(int)numberOfCandles];
		double[] volumes=new double[(int)numberOfCandles];

		long msindex=in.getTimePeriod(0).getFirstMillisecond();
		
		//now cycle through all entries and get open, hi, low and close for a span
		double open=0.0, hi=0.0, low=0.0, close=0.0;
		int z=0;
		for(int i=0;i<numberOfCandles;i++){
		
			
			
			
			if(z<(n-1)){
				//set low, hi, open
				
				
				long h1=in.getTimePeriod(z).getFirstMillisecond();
				
				while(h1<=((i*candlewidth)+msindex) & (z<(n-1))){
					//ok, value is in the current timespan
					z++;	
					h1=in.getTimePeriod(z).getFirstMillisecond();
				}
				System.out.println("start of candle found");
				close=open=hi=low=in.getDataItem(z).getValue().doubleValue();
				int y=z;
				long h2=in.getTimePeriod(y).getFirstMillisecond();
				if(z<n-2){
					long h3=((i+1)*candlewidth)+msindex;
					while(h2<=h3){
						y++;
						if(in.getDataItem(y).getValue().doubleValue()>hi)hi=in.getDataItem(y).getValue().doubleValue();
						if(in.getDataItem(y).getValue().doubleValue()<low)low=in.getDataItem(y).getValue().doubleValue();
						h2=in.getTimePeriod(y).getFirstMillisecond();
					}
					System.out.println("End of candle found");
				}
				//set close
				//close=hi=low=open=1.0;
				//hi=open+Math.random();
				close=in.getDataItem(y).getValue().doubleValue();
				//set all values
				dates[i]=new Date(msindex+i*candlewidth);
				opens[i]=open;
				highs[i]=hi;
				lows[i]=low;
				closes[i]=close;
				volumes[i]=0;
				System.out.println(dates[i]+"/"+opens[i]+"/"+highs[i]+"/"+lows[i]+"/"+closes[i]+"/"+volumes[i]);
			}
		}
		
		DefaultHighLowDataset ret=new DefaultHighLowDataset("name", dates, highs, lows, opens, closes, volumes);
		System.out.println(dates.length+"/"+opens.length+"/"+highs.length+"/"+lows.length+"/"+closes.length+"/"+volumes.length);
		return ret;
		
	}

	public void convert(){
		chart=ChartFactory.createCandlestickChart("", "time", "value", convertToDefaultHighLowDataset(x1, 600000), true);
		chartPanel.setChart(chart);
		
		
	}


Locked