Page 1 of 1

convert timeseries into highlowdataset

Posted: Fri Nov 14, 2003 12:07 pm
by uls
can i somehow automatically convert a timeseries dataset into a highlowdataset or should i do it manually?

regards&tia
ulrich

Posted: Fri Nov 14, 2003 12:31 pm
by david.gilbert
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:

Posted: Fri Nov 14, 2003 12:33 pm
by uls
can do! :-)

Posted: Mon Nov 17, 2003 2:37 pm
by uls

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

working code

Posted: Mon Nov 17, 2003 7:32 pm
by uls
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);
		
		
	}