/
Slippage.py
90 lines (65 loc) · 2.42 KB
/
Slippage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/python
# -*- coding: utf8 -*-
# gb18030
#===============================================================================
# 作者:fasiondog
# 历史:1)20130321, Added by fasiondog
#===============================================================================
import unittest
from test_init import *
class SlippagePython(SlippageBase):
def __init__(self):
super(SlippagePython, self).__init__("SlippagePython")
self._x = 0
def getRealBuyPrice(self, datetime, price):
if self._x < 10:
return 0.0
return 1.0
def getRealSellPrice(self, datetime, price):
if self._x < 10:
return 0.0
return 1.0
def _reset(self):
self._x = 0
def _clone(self):
p = SlippagePython()
p._x = self._x
return p
def _calculate(self):
""" do nothin """
class SlippageTest(unittest.TestCase):
def test_SlippageBase(self):
p = SlippagePython()
self.assertEqual(p.name, "SlippagePython")
self.assertEqual(p.getRealBuyPrice(Datetime(200101010000), 1.0), 0.0)
self.assertEqual(p.getRealSellPrice(Datetime(200101010000), 1.0), 0.0)
self.assertEqual(p._x, 0)
p._x = 10
self.assertEqual(p._x, 10)
self.assertEqual(p.getRealBuyPrice(Datetime(200101010000), 1.0), 1.0)
self.assertEqual(p.getRealSellPrice(Datetime(200101010000), 1.0), 1.0)
p.reset()
self.assertEqual(p._x, 0)
p._x = 10
p_clone = p.clone()
self.assertEqual(p_clone._x, 10)
p.reset()
self.assertEqual(p._x, 0)
self.assertEqual(p_clone._x, 10)
def test_crtSL_func(self):
pass
def test_getRealBuyPrice_func(self, datetime, price):
return 10.0 if datetime == Datetime(200101010000) else 0.0
class TestCrtSL(unittest.TestCase):
def test_crtSL(self):
p = crtSL(test_crtSL_func, params={'n': 10}, name="TestSL")
p.getRealBuyPrice = test_getRealBuyPrice_func
self.assertEqual(p.name, "TestSL")
self.assertEqual(p.getRealBuyPrice(p, Datetime(200101010000), 1.0), 10.0)
self.assertEqual(p.getRealBuyPrice(p, Datetime(200101020000), 1.0), 0.0)
p_clone = p.clone()
self.assertEqual(p_clone.name, "TestSL")
def suite():
return unittest.TestLoader().loadTestsFromTestCase(SlippageTest)
def suiteTestCrtSL():
return unittest.TestLoader().loadTestsFromTestCase(TestCrtSL)