-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
130 lines (90 loc) · 3.16 KB
/
test.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import unittest
from strategy import *
from price import *
from order import *
from account import *
import datetime
t = datetime.datetime.now()
def to_date(str):
return datetime.datetime.strptime(str, "%Y-%m-%d")
def pips_to_pnl(pips, amount, pips_precision=4):
return (1.0 / pow(10, pips_precision)) * pips * amount
class Testpnl(unittest.TestCase):
amount = 10000
def setUp(self):
pass
def test_pnl(self):
o = Order("BUY", self.amount, 1.2, 10, 10)
self.assertTrue(o.status == None)
o.open(t)
self.assertEqual(o.status, 'opened')
o.close(t, 1.2010) # 10 pips
self.assertEqual(o.status, 'closed')
actual = o.pnl()
self.assertAlmostEqual(10 - SPREAD, actual)
raw_pnl = pips_to_pnl(10, self.amount)
spread_amount = pips_to_pnl(SPREAD, self.amount)
net_pnl = raw_pnl - spread_amount
self.assertAlmostEqual(actual, net_pnl)
def test_buy_sl(self):
o = Order("BUY", self.amount, 1.2, 15, 20)
o.open(t)
o.close(t, 1.0) # -15 pips
actual = o.pnl()
self.assertAlmostEqual(-15 - SPREAD, actual)
def test_buy_tp(self):
o = Order("BUY", self.amount, 1.2, 15, 20)
o.open(t)
o.close(t, 2.0) # +20 pips
actual = o.pnl()
self.assertAlmostEqual(+20 - SPREAD, actual)
def test_sell_sl(self):
o = Order("SELL", self.amount, 1.2, 15, 20)
o.open(t)
o.close(t, 2.0) # -15 pips
actual = o.pnl()
self.assertAlmostEqual(-15 - SPREAD, actual)
def test_sell_tp(self):
o = Order("SELL", self.amount, 1.2, 15, 20)
o.open(t)
o.close(t, 1.0) # +20 pips
actual = o.pnl()
self.assertAlmostEqual(20 - SPREAD, actual)
def test_open_order_no_change(self):
a = Account()
ts = datetime.date(2011, 1, 29)
p = Price(ts, 10, 8, 13, 10)
o = Order("BUY", 10000, 7.5, 0.1, 0.1)
a.add_order(o)
a.execute_orders_with_price_change(p)
self.assertEqual(o.status, None)
def test_order_opening(self):
a = Account()
ts = datetime.date(2011, 1, 29)
p = Price(ts, 10, 8, 13, 10)
o = Order("BUY", 10000, 9, 0.1, 0.1)
a.add_order(o)
a.execute_orders_with_price_change(p)
self.assertEqual(o.status, "opened")
def test_order_close_loss(self):
a = Account()
ts = datetime.date(2011, 1, 29)
p = Price(ts, 10, 8, 13, 10)
o = Order("BUY", 10000, 9, 1, 1)
o.open(t)
a.add_order(o)
a.execute_orders_with_price_change(p)
self.assertEqual(o.status, "closed")
self.assertTrue(o.pnl() < 0)
def test_order_close_win(self):
a = Account()
ts = datetime.date(2011, 1, 29)
p = Price(ts, 10, 8, 15, 10)
o = Order("BUY", 10000, 9, 100000, 5000)
o.open(t)
a.add_order(o)
a.execute_orders_with_price_change(p)
self.assertEqual(o.status, "closed")
self.assertTrue(o.pnl() > 0)
if __name__ == '__main__':
unittest.main()