Skip to content

Commit 177e7ba

Browse files
committed
本地数据存在的时候,追加最近几天的
1 parent f35d30c commit 177e7ba

File tree

8 files changed

+305
-135
lines changed

8 files changed

+305
-135
lines changed

bin/TurtleTrade.apk

1.01 KB
Binary file not shown.

src/com/stock/data/PriceBar.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,32 @@ public class PriceBar {
2929
public double volume;
3030

3131
public Calendar start;
32-
// public Date start = new Date();
3332
public int minutes;
3433

3534
public PriceBar() {
3635
start = Calendar.getInstance();
37-
start.setTime(new Date());
38-
start.add(Calendar.YEAR, -1);
36+
// start.setTime(new Date());
37+
// start.add(Calendar.YEAR, -1);
3938
}
4039

4140
public PriceBar(int minutes) {
4241
start = Calendar.getInstance();
4342
this.minutes = minutes;
4443
}
4544

46-
public PriceBar(Calendar start, int minutes) {
47-
start = Calendar.getInstance();
48-
this.start = start;
49-
this.minutes = minutes;
50-
}
51-
5245
public PriceBar clone() {
53-
PriceBar bar = new PriceBar(this.start, this.minutes);
46+
PriceBar bar = new PriceBar();
47+
48+
bar.start = this.start;
49+
bar.minutes = this.minutes;
50+
5451
bar.close = this.close;
5552
bar.high = this.high;
5653
bar.low = this.low;
5754
bar.open = this.open;
55+
5856
bar.volume = this.volume;
57+
5958
return bar;
6059
}
6160

@@ -118,3 +117,9 @@ public void setDate(Date date) {
118117
this.start.setTime(date);
119118
}
120119
}
120+
121+
//public PriceBar(Calendar start, int minutes) {
122+
// start = Calendar.getInstance();
123+
// this.start = start;
124+
// this.minutes = minutes;
125+
//}

src/com/stock/data/StockData.java

Lines changed: 168 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,27 @@
33
import java.io.IOException;
44
import java.text.ParseException;
55

6-
import java.util.ArrayList;
76
import java.util.Calendar;
87
import java.util.Date;
9-
import java.util.GregorianCalendar;
10-
import java.util.Iterator;
118
import java.util.List;
9+
import java.util.ArrayList;
10+
import java.util.Iterator;
1211
import java.util.ListIterator;
1312
import java.util.Locale;
1413

1514
import com.stock.source.DataSource;
1615
import com.stock.source.YahooStock;
1716

1817
public class StockData extends PriceBar {
19-
20-
public static final int CODE = 1;
18+
public static final int CODE = 1;
2119
public static final int MARKET = 2;
22-
23-
private String code = "601857";
24-
private String market = "ss";
25-
private DataSource datasource;
20+
2621
private List<PriceBar> bar_list = new ArrayList<PriceBar>();
22+
private String code = "601857";
23+
private int market = DataSource.MARKET_SHANGHAI;
2724

28-
public StockData(String code, String market) {
25+
public StockData(String code, int market) {
26+
super();
2927
this.code = code;
3028
this.market = market;
3129
}
@@ -38,63 +36,127 @@ public int size() {
3836
return bar_list.size();
3937
}
4038

39+
public void append(Calendar _end, YahooStock yahoo)
40+
throws IOException {
41+
String cachefile = yahoo.getCacheFile();
42+
String csvfile = yahoo.getStockFile();
43+
44+
List<List<String>> data = yahoo.get(_end, cachefile);
45+
if( data == null || data.size() < 2 )
46+
return;
47+
48+
ArrayList<PriceBar> bars = new ArrayList<PriceBar>();
49+
this.Parse(data, bars);
50+
if( bars.size() == 0 )
51+
return;
52+
53+
Calendar _nstart = bars.get(bars.size()-1).start;
54+
Calendar _oend_ = bar_list.get(0).start;
55+
56+
if( _oend_.before(_nstart) ) {
57+
Iterator<PriceBar> iter = bar_list.iterator();
58+
while( iter.hasNext() )
59+
bars.add(iter.next());
60+
bar_list = bars;
61+
62+
data = Format(bar_list);
63+
yahoo.SaveCSVFile(data, csvfile);
64+
}
65+
}
66+
4167
public void load() {
68+
Calendar yesterday = Calendar.getInstance();
69+
Calendar yearago = Calendar.getInstance();
70+
yesterday.add(Calendar.DATE, -1);
71+
yearago.add(Calendar.YEAR, -1);
72+
Calendar _stt = yearago;
73+
74+
YahooStock yahoo = new YahooStock(code, market);
75+
String csvfile = yahoo.getStockFile();
76+
List<List<String>> data = null;
77+
78+
// first read local file.
4279
try {
43-
datasource = new YahooStock(this.code, this.market);
44-
List<List<String>> data = datasource.LocalData();
45-
46-
if( data == null ) {
47-
data = ((YahooStock)datasource).get(start);
80+
data = yahoo.LocalData(csvfile);
81+
if( data != null ) {
82+
this.Parse(data, bar_list);
83+
if( bar_list != null && bar_list.size() > 0 ) {
84+
_stt = bar_list.get(bar_list.size() - 1).start;
85+
}
4886
}
49-
50-
if( data != null )
51-
this.Parse(data);
5287
} catch (IOException e) {
5388
e.printStackTrace();
5489
}
55-
}
56-
57-
public void load(Date start, Date end)
58-
{
59-
Calendar start_time = new GregorianCalendar();
60-
start_time.setTime(start);
61-
Calendar end_date = new GregorianCalendar();
62-
end_date.setTime(end);
6390

91+
// download from yahoo api
92+
// if there is't local data or start date after a year age
6493
try {
65-
List<List<String>> data = datasource.LocalData();
66-
this.Parse(data);
94+
if( data == null || _stt.after(yearago) ) {
95+
data = yahoo.get(yearago, csvfile);
96+
if( data != null ) {
97+
this.Parse(data, bar_list);
98+
}
99+
}
67100
} catch (IOException e) {
68-
// String url = ((WebSource)datasource).getUrl();
69-
// System.out.println("download data from " + url + " failed.");
70101
e.printStackTrace();
71102
}
103+
104+
// download data from yahoo api
105+
// if there is't recent some days data.
106+
try {
107+
if( bar_list != null && bar_list.size() > 0 ) {
108+
Calendar _end = bar_list.get(0).start;
109+
if( _end.before(yesterday) ) {
110+
_end.add(Calendar.DATE, 1);
111+
append(_end, yahoo);
112+
}
113+
}
114+
} catch (IOException e) {
115+
e.printStackTrace();
116+
}
117+
118+
// load today's data from sina api.
119+
// SinaStock sina = new SinaStock(code, market);
120+
// PriceBar bar = sina.getNewPrice();
121+
// bar_list.add(bar);
122+
123+
this.Statistic();
72124
}
73125

74-
// public void load(Date start, int source)
75-
// {
76-
// if( source == DataSource.SOUREC_LOCAL )
77-
// datasource = new DataSource(this.code, this.market);
78-
// else if( source == DataSource.SOUREC_YAHOO )
79-
// datasource = new YahooStock(this.code, this.market);
80-
// else if( source == DataSource.SOUREC_SINA )
81-
// datasource = new SinaStock(this.code, this.market);
82-
// this.load(start, new Date());
83-
// }
84-
85-
public String getInfo(int field) {
86-
switch(field) {
87-
case CODE:
88-
return this.code;
89-
case MARKET:
90-
return this.market;
126+
public List<List<String>> Format(List<PriceBar> bars) {
127+
List<List<String>> data = new ArrayList<List<String>>();
128+
List<String> head = new ArrayList<String>();
129+
int[] fields = {START, PRICE_OPEN, PRICE_CLOSE, PRICE_HIGH, PRICE_LOW, VOLUME};
130+
String[] heads = {"DATE", "OPEN", "CLOSE", "HIGH", "LOW", "VOLUME"};
131+
132+
for( int i = 0; i < fields.length; i ++ ) {
133+
head.add(heads[i]);
134+
}
135+
data.add(head);
136+
137+
Iterator<PriceBar> iter = bars.iterator();
138+
while( iter.hasNext() ) {
139+
PriceBar bar = iter.next();
140+
List<String> row = new ArrayList<String>();
141+
142+
String date = DataSource.DATE_FORMAT.format(bar.start.getTime());
143+
row.add(date);
144+
145+
for( int i = 1; i < fields.length; i ++ ) {
146+
double d = bar.get(fields[i]);
147+
String s = Double.toString(d);
148+
row.add(s);
149+
}
150+
151+
data.add(row);
91152
}
92-
return null;
153+
154+
return data;
93155
}
94156

95-
public void Parse(List<List<String>> _data) {
157+
protected void Parse(List<List<String>> data, List<PriceBar> bars) {
96158
int[] fields = null;
97-
Iterator<List<String>> rowIter = _data.iterator();
159+
Iterator<List<String>> rowIter = data.iterator();
98160

99161
if( rowIter.hasNext() ) {
100162
List<String> rec = rowIter.next();
@@ -147,13 +209,13 @@ else if( cell.equals("DATE") )
147209
}
148210
}
149211

150-
bar_list.add(bar);
212+
bars.add(bar);
151213
} // end of while
152214

153-
this.Statistic();
215+
// this.Statistic();
154216
}// end of parse
155217

156-
public void Statistic() {
218+
protected void Statistic() {
157219
Iterator<PriceBar> iter = bar_list.iterator();
158220
PriceBar bar = null;
159221

@@ -179,6 +241,9 @@ public void Statistic() {
179241
}
180242
}
181243

244+
/**
245+
* 取价格对数
246+
*/
182247
public void log() {
183248
Iterator< PriceBar > iter = bar_list.iterator();
184249
PriceBar bar = null;
@@ -200,11 +265,14 @@ public void log() {
200265
private PriceBar NewWeekBar(PriceBar dbar) {
201266
PriceBar wbar = new PriceBar(1200);
202267
wbar.start = dbar.start;
268+
203269
wbar.high = dbar.high;
204270
wbar.low = dbar.low;
205271
wbar.open = dbar.open;
206272
wbar.close = dbar.close;
273+
207274
wbar.volume = dbar.volume;
275+
208276
return wbar;
209277
}
210278

@@ -254,9 +322,51 @@ public StockData ToWeekBars() {
254322

255323
return weekbars;
256324
}
257-
258-
public void print() {
259-
datasource.print();
260-
}
261-
262325
}
326+
327+
//public PriceBar todayPrice() {
328+
// SinaStock sina = new SinaStock(code, market);
329+
// return sina.getNewPrice();
330+
//}
331+
332+
//public String getInfo(int field) {
333+
// switch(field) {
334+
// case CODE:
335+
// return this.code;
336+
// case MARKET:
337+
// return this.market;
338+
// }
339+
// return null;
340+
//}
341+
342+
//public void print() {
343+
// datasource.print();
344+
//}
345+
346+
//public void load(Date start, Date end)
347+
//{
348+
// Calendar start_time = new GregorianCalendar();
349+
// start_time.setTime(start);
350+
// Calendar end_date = new GregorianCalendar();
351+
// end_date.setTime(end);
352+
//
353+
// try {
354+
// List<List<String>> data = datasource.LocalData();
355+
// this.Parse(data);
356+
// } catch (IOException e) {
357+
//// String url = ((WebSource)datasource).getUrl();
358+
//// System.out.println("download data from " + url + " failed.");
359+
// e.printStackTrace();
360+
// }
361+
//}
362+
363+
//public void load(Date start, int source)
364+
//{
365+
// if( source == DataSource.SOUREC_LOCAL )
366+
// datasource = new DataSource(this.code, this.market);
367+
// else if( source == DataSource.SOUREC_YAHOO )
368+
// datasource = new YahooStock(this.code, this.market);
369+
// else if( source == DataSource.SOUREC_SINA )
370+
// datasource = new SinaStock(this.code, this.market);
371+
// this.load(start, new Date());
372+
//}

0 commit comments

Comments
 (0)