]> git.proxmox.com Git - sencha-touch.git/blob - src/examples/stockapp/app/store/MovingAverage.js
import Sencha Touch 2.4.2 source
[sencha-touch.git] / src / examples / stockapp / app / store / MovingAverage.js
1 Ext.define("StockApp.store.MovingAverage", {
2 alias: 'store.Apple',
3 requires: ['StockApp.model.Stock', 'Ext.data.reader.Array'],
4 extend: "Ext.data.ArrayStore",
5 config: {
6 model: "StockApp.model.Stock",
7 source: null,
8 window: 50
9 },
10
11 applySource: function (source) {
12 return Ext.StoreManager.lookup(source);
13 },
14
15 updateSource: function (source, oldSource) {
16 if (source) {
17 source.on('refresh', 'onRefreshSource', this);
18 this.setOriginalData(source.getData());
19 }
20 if (oldSource) {
21 oldSource.on('refresh', 'onRefreshSource', this);
22 }
23 },
24
25 onRefreshSource: function () {
26 if (this.getSource()) {
27 this.setOriginalData(this.getSource().getData());
28 }
29 },
30
31 setOriginalData: function (data) {
32 var length = data.length,
33 items = data.items,
34 ma = [],
35 window = this.getWindow(),
36 item, item2,
37 cntDate = 0,
38 cntOpen = 0,
39 cntHigh = 0,
40 cntLow = 0,
41 cntClose = 0,
42 cntVolume = 0,
43 cntAdjClose = 0,
44 i, j;
45 for (i = 0; i < window; i++) {
46 item = items[i].data;
47 cntDate += item.date;
48 cntOpen += item.open;
49 cntHigh += item.high;
50 cntLow += item.low;
51 cntClose += item.close;
52 cntVolume += item.volume;
53 cntAdjClose += item.adjClose;
54 ma.push([
55 cntDate / (i + 1),
56 cntOpen / (i + 1),
57 cntHigh / (i + 1),
58 cntLow / (i + 1),
59 cntClose / (i + 1),
60 cntVolume / (i + 1),
61 cntAdjClose / (i + 1)
62 ]);
63 }
64 for (i = 0, j = window; j < length; i++, j++) {
65 item = items[i].data;
66 item2 = items[j].data;
67 cntDate += item2.date - item.date;
68 cntOpen += item2.open - item.open;
69 cntHigh += item2.high - item.high;
70 cntLow += item2.low - item.low;
71 cntClose += item2.close - item.close;
72 cntVolume += item2.volume - item.volume;
73 cntAdjClose += item2.adjClose - item.adjClose;
74 ma.push([
75 cntDate / window,
76 cntOpen / window,
77 cntHigh / window,
78 cntLow / window,
79 cntClose / window,
80 cntVolume / window,
81 cntAdjClose / window
82 ]);
83 }
84 for (; i < length - 1; i++) {
85 item = items[i].data;
86 cntDate -= item.date;
87 cntOpen -= item.open;
88 cntHigh -= item.high;
89 cntLow -= item.low;
90 cntClose -= item.close;
91 cntVolume -= item.volume;
92 cntAdjClose -= item.adjClose;
93 ma.push([
94 cntDate / (length - i - 1),
95 cntOpen / (length - i - 1),
96 cntHigh / (length - i - 1),
97 cntLow / (length - i - 1),
98 cntClose / (length - i - 1),
99 cntVolume / (length - i - 1),
100 cntAdjClose / (length - i - 1)
101 ]);
102 }
103 item = items[i].data;
104 ma.push([
105 item.date,
106 item.open,
107 item.high,
108 item.low,
109 item.close,
110 item.volume,
111 item.adjClose
112 ]);
113 this.setData(ma);
114 }
115 });