1010 Strategy ,
1111 run
1212)
13- from source import (
13+
14+ from .stock_util import (
15+ buy_monday_sell_friday ,
16+ trade_closed_curbar ,
1417 capital ,
1518 bt1 ,
1619 bt2 ,
1720 bt3 ,
1821 st1 ,
1922 st2
2023)
21- from stock_util import (
22- buy_monday_sell_friday ,
23- trade_closed_curbar ,
24- )
2524
2625fname = os .path .join (os .getcwd (), 'data' , '1MINUTE' , 'TEST' , 'STOCK.csv' )
2726source = pd .read_csv (fname , parse_dates = True , index_col = 0 )
2827
29-
3028class TestOneDataOneCombinationStock (unittest .TestCase ):
3129 """
32- ctx.pos 可平仓位
30+ ctx.pos 可平仓位, 当天买隔天卖,当天不能卖。
3331 """
3432
3533 def test_case (self ):
@@ -58,10 +56,11 @@ def on_bar(self, ctx):
5856 self .equities .append (ctx .equity ())
5957
6058 def test (self , test ):
61- equities , cashes , open_equities , open_casheses , dts = trade_closed_curbar (source , capital * 0.3 , lmg , smg , multi , 1 )
59+ equities , cashes , open_equities , open_casheses , dts = \
60+ trade_closed_curbar (source , capital * 0.3 , lmg , smg , multi , 1 )
6261
6362 test .assertTrue (len (self .cashes ) == len (cashes ), 'cash接口测试失败!' )
64- for i in range (0 , len (self .cashes )): # 最后一根强平了无法比较
63+ for i in range (0 , len (self .cashes )):
6564 test .assertAlmostEqual (self .cashes [i ], open_casheses [i ])
6665 test .assertAlmostEqual (self .equities [i ], open_equities [i ])
6766
@@ -70,7 +69,6 @@ def test(self, test):
7069 test .assertAlmostEqual (hd ['equity' ], equities [i ])
7170 test .assertAlmostEqual (hd ['cash' ], cashes [i ])
7271
73-
7472 class DemoStrategy2 (Strategy ):
7573 """ 限价买多卖空的策略 """
7674
@@ -110,7 +108,6 @@ def test(self, test):
110108 test .assertAlmostEqual (self .cashes [i ], cashes [i ])
111109
112110 class DemoStrategy3 (Strategy ):
113- """ 测试平仓未成交时的持仓,撤单后的持仓,撤单。 """
114111 def on_init (self , ctx ):
115112 """初始化数据"""
116113 pass
@@ -189,7 +186,6 @@ def test(self, test, profile):
189186 count = 0
190187 all_holdings0 = profile .all_holdings (0 )
191188 for i , hd in enumerate (all_holdings0 ):
192- # 刚好最后一根没持仓,无需考虑强平, 见weekday输出
193189 dt = hd ['datetime' ]
194190 if dt in cashes :
195191 test .assertAlmostEqual (hd ['cash' ], cashes [dt ])
@@ -199,11 +195,10 @@ def test(self, test, profile):
199195 count += 1
200196 else :
201197 # 两支股票的混合,总数据长度和source不一样。
202- test .assertAlmostEqual (all_holdings0 [i - 1 ]['cash' ], hd ['cash' ])
203- test .assertAlmostEqual (all_holdings0 [i - 1 ]['equity' ], hd ['equity' ])
198+ test .assertAlmostEqual (all_holdings0 [i - 1 ]['cash' ], hd ['cash' ])
199+ test .assertAlmostEqual (all_holdings0 [i - 1 ]['equity' ], hd ['equity' ])
204200 test .assertTrue (count == len (dts ))
205201
206-
207202 class DemoStrategy2 (Strategy ):
208203 """ 选股,并且时间没对齐的日线数据。 """
209204 def __init__ (self , name ):
@@ -235,21 +230,19 @@ def on_bar(self, ctx):
235230 self .tosells = []
236231
237232 def test (self , test , profile ):
238- # test Strategy2
239233 fname = os .path .join (os .getcwd (), 'data' , '1DAY' , 'SH' , '600521.csv' )
240234 source = pd .read_csv (fname , parse_dates = True , index_col = 0 )
241235 fname = os .path .join (os .getcwd (), 'data' , '1DAY' , 'SH' , '600522.csv' )
242236 source2 = pd .read_csv (fname , parse_dates = True , index_col = 0 )
243237 equities0 , cashes0 , open_equities0 , open_cashes0 , dts = \
244238 buy_monday_sell_friday (source , capital * 0.3 / 2 , 1 , 1 )
245239 equities1 , cashes1 , open_equities1 , open_cashes1 , dts = \
246- buy_monday_sell_friday (source2 , capital * 0.3 / 2 , 1 , 1 )
240+ buy_monday_sell_friday (source2 , capital * 0.3 / 2 , 1 , 1 )
247241 last_equity0 = 0
248- last_equity1 = 0
242+ last_equity1 = 0
249243 last_cash0 = 0
250244 last_cash1 = 0
251245 for i , hd in enumerate (profile .all_holdings (1 )):
252- # 刚好最后一根没持仓,无需考虑强平, 见weekday输出
253246 dt = hd ['datetime' ]
254247 equity = 0
255248 cash = 0
@@ -271,14 +264,13 @@ def test(self, test, profile):
271264 if dt in equities1 :
272265 equity += equities1 [dt ]
273266 cash += cashes1 [dt ]
274- last_equity1 = equities1 [dt ]
267+ last_equity1 = equities1 [dt ]
275268 last_cash1 = cashes1 [dt ]
276269 open_equity += open_equities1 [dt ]
277270 open_cash += open_cashes1 [dt ]
278271 else :
279272 equity += last_equity1
280273 cash += last_cash1
281- # 新的ctx.cash()将会以最近数据的收盘价为准。
282274 open_equity += last_equity1
283275 open_cash += last_cash1
284276
@@ -287,7 +279,6 @@ def test(self, test, profile):
287279 test .assertAlmostEqual (self ._equities [dt ], open_equity )
288280 test .assertAlmostEqual (self ._cashes [dt ], open_cash )
289281
290-
291282 class DemoStrategy3 (Strategy ):
292283 """ 测试平仓未成交时的持仓,撤单后的持仓,撤单。 """
293284 def on_init (self , ctx ):
@@ -304,14 +295,12 @@ def on_bar(self, ctx):
304295 profile = add_strategy ([b1 , b2 , b3 ], {
305296 'capital' : capital ,
306297 'ratio' : [0.3 , 0.3 , 0.4 ]
307- })
298+ }
299+ )
308300 run ()
309301 b1 .test (self , profile )
310302 b2 .test (self , profile )
311303
312304
313-
314-
315-
316305if __name__ == '__main__' :
317306 unittest .main ()
0 commit comments