package chinastock.data.TDX;
import chinastock.GlobalConfig;
import chinastock.datatype.Candle;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
import datatype.MyDate;
public class Converter {
private static String dataDir=GlobalConfig.myData;
public static List<Candle> getCandles(File f,boolean latestInHead)
{
List<Candle> list=new ArrayList<Candle>();
try
{
list=getCandles0(f,latestInHead,null);
}catch(Exception e){
e.printStackTrace();
}
return list;
}
private static List<Candle> getCandles0(File f,boolean latestInHead,MyDate myDate) throws Exception
{
List<Candle> list=new ArrayList<Candle>();
FileInputStream raf=new FileInputStream(f);
String oriFN=f.getName();
String code=oriFN.substring(0,oriFN.length()-4);
byte[] intBuffer=new byte[4];
long fileLen=f.length() ;
while(fileLen>0)
{
fileLen-=32;
try {
raf.read(intBuffer);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
break;
}
int date = ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt();
int year=date/10000;
date%=10000;
int month=date/100;
int day=date%100;
raf.read(intBuffer);
int open=ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt();
String sopen=convertTDXIntToString(open);
raf.read(intBuffer);
int high=ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt();
String shigh=convertTDXIntToString(high);
raf.read(intBuffer);
int low=ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt();
String slow=convertTDXIntToString(low);
raf.read(intBuffer);
int close=ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt();
String sclose=convertTDXIntToString(close);
raf.read(intBuffer);
float amount=ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getFloat();
raf.read(intBuffer);
int vol=ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt();
raf.read(intBuffer);
int reserve=ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt();
Candle c=new Candle(new MyDate(year,month,day),sopen,
sclose,shigh,slow,vol );
// c.setCode(code);
list.add(0,c);
}
for(int i=0;i<list.size()-1;i++)
{
Candle c=list.get(i),next=list.get(i+1);
double rise=(c.close.doubleValue()-next.close.doubleValue())/next.close.doubleValue();
rise=rise*100;
String s=Double.toString(rise);
int index=s.indexOf(".");
if(index!=-1)
{
int tail=s.length()-index-1;
if(tail>2)
s=s.substring(0,index+3);
}
// c.riseRate=new BigDecimal(s);
}
raf.close();
return list;
}
private static String convertTDXIntToString(int price)
{
String s=Integer.toString(price);
if(s.length()<2)
s="0"+s;
if(s.length()<3)
s="0"+s;
s=s.substring(0,s.length()-2)+"."+s.substring(s.length()-2);
return s;
}
public static void main(String[] args) throws Exception
{
File sh=new File(GlobalConfig.SH_TDX);
File sz=new File(GlobalConfig.SZ_TDX);
File mydata=new File(GlobalConfig.myData);
for(File f:sh.listFiles())
{
System.out.println(f.getCanonicalPath());
List<Candle> list=getCandles(f,true);
String name=f.getName();
File df=new File(mydata+File.separator+name);
BufferedWriter w=new BufferedWriter(new FileWriter(df));
w.write("yyyy-mm-dd | open | close | hiogh | low | volume \n");
for(Candle c:list)
{
w.write(c+"\n");
}
w.close();
}
for(File f:sz.listFiles())
{
System.out.println(f.getCanonicalPath());
List<Candle> list=getCandles(f,true);
String name=f.getName();
File df=new File(mydata+File.separator+name);
BufferedWriter w=new BufferedWriter(new FileWriter(df));
w.write("yyyy-mm-dd | open | close | hiogh | low | volume \n");
for(Candle c:list)
{
w.write(c+"\n");
}
w.close();
}
}
}