@@ -11,8 +11,13 @@ func (e *BarEnv) OnBar(barMs int64, open, high, low, close, volume, info float64
1111 if e .TimeStop > barMs {
1212 return fmt .Errorf ("%s/%s old Bar Receive: %d, Current: %d" , e .Symbol , e .TimeFrame , barMs , e .TimeStop )
1313 }
14- e .TimeStart = barMs
15- e .TimeStop = barMs + e .TFMSecs
14+ e .OnBar2 (barMs , barMs + e .TFMSecs , open , high , low , close , volume , info )
15+ return nil
16+ }
17+
18+ func (e * BarEnv ) OnBar2 (barMS , endMS int64 , open , high , low , close , volume , info float64 ) {
19+ e .TimeStart = barMS
20+ e .TimeStop = endMS
1621 e .BarNum += 1
1722 if e .Open == nil {
1823 e .Open = e .NewSeries ([]float64 {open })
@@ -26,21 +31,20 @@ func (e *BarEnv) OnBar(barMs int64, open, high, low, close, volume, info float64
2631 e .MaxCache = 1000
2732 }
2833 } else {
29- e .Open .Time = barMs
34+ e .Open .Time = barMS
3035 e .Open .Data = append (e .Open .Data , open )
31- e .High .Time = barMs
36+ e .High .Time = barMS
3237 e .High .Data = append (e .High .Data , high )
33- e .Low .Time = barMs
38+ e .Low .Time = barMS
3439 e .Low .Data = append (e .Low .Data , low )
35- e .Close .Time = barMs
40+ e .Close .Time = barMS
3641 e .Close .Data = append (e .Close .Data , close )
37- e .Volume .Time = barMs
42+ e .Volume .Time = barMS
3843 e .Volume .Data = append (e .Volume .Data , volume )
39- e .Info .Time = barMs
44+ e .Info .Time = barMS
4045 e .Info .Data = append (e .Info .Data , info )
4146 e .TrimOverflow ()
4247 }
43- return nil
4448}
4549
4650func (e * BarEnv ) Reset () {
@@ -126,6 +130,31 @@ func (e *BarEnv) Clone() *BarEnv {
126130 return res
127131}
128132
133+ // ResetTo reset all Series to given(exclude ohlcv)
134+ func (e * BarEnv ) ResetTo (env * BarEnv ) {
135+ rootIds := map [int ]bool {
136+ env .Open .ID : true ,
137+ env .High .ID : true ,
138+ env .Low .ID : true ,
139+ env .Close .ID : true ,
140+ env .Volume .ID : true ,
141+ env .Info .ID : true ,
142+ }
143+ for id , s := range env .Items {
144+ if _ , ok := rootIds [id ]; ok {
145+ continue
146+ }
147+ delete (e .Items , id )
148+ s .CopyTo (e )
149+ }
150+ e .Open .loadEnvSubs ()
151+ e .High .loadEnvSubs ()
152+ e .Low .loadEnvSubs ()
153+ e .Close .loadEnvSubs ()
154+ e .Volume .loadEnvSubs ()
155+ e .Info .loadEnvSubs ()
156+ }
157+
129158func (s * Series ) Set (obj interface {}) * Series {
130159 if ! s .Cached () {
131160 return s .Append (obj )
@@ -411,6 +440,16 @@ func (s *Series) CopyTo(e *BarEnv) *Series {
411440 return res
412441}
413442
443+ func (s * Series ) loadEnvSubs () {
444+ for _ , idMap := range s .Subs {
445+ for id := range idMap {
446+ if dup , ok := s .Env .Items [id ]; ok {
447+ idMap [id ] = dup
448+ }
449+ }
450+ }
451+ }
452+
414453/*
415454Cross 计算最近一次交叉的距离。比较对象必须是常数或Series对象
416455返回值:正数上穿,负数下穿,0表示未知或重合;abs(ret) - 1表示交叉点与当前bar的距离
0 commit comments