-
Notifications
You must be signed in to change notification settings - Fork 3
/
portfolio.py
136 lines (114 loc) · 4.54 KB
/
portfolio.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
131
132
133
134
135
136
from settings import ACCESS_TOKEN, ACCOUNT_ID, DOMAIN
import oandapy
from datetime import datetime
import pandas as pd
class Portfolio():
def __init__(self, status):
status["open_position"] = False
status["position"] = 0
self.status = status
self.order_count = 0
self.total_profit = 0
self.total_loss = 0
self.spread = 0.00005
self.current_pnl = 0
self.win_count = 0
self.lose_count = 0
status["opening_price"] = 0
status["executed_price"] = 0
status["unrealized_pnl"] = 0
status["realized_pnl"] = 0
self.rpnl = pd.DataFrame()
self.oanda = oandapy.API(DOMAIN, ACCESS_TOKEN)
def update_portfolio(self, event):
# Update position upon successful order
self.order_count += 1
if event.side == "buy":
self.status["position"] += self.status["units"]
else:
self.status["position"] -= self.status["units"]
if self.status["position"] == 0:
self.status["open_position"] = False
self.status["close_time"] = event.time
self.calculate_realized_pnl(event)
if self.status["is_sim"]:
self.rpnl.loc[event.time, "rpnl"] = self.status["realized_pnl"]
else:
self.status["opening_price"] = self.status["executed_price"]
self.status["opening_time"] = event.time
self.status["open_position"] = True
def calculate_realized_pnl(self, event):
self.current_pnl = self.status["units"] * (
(self.status["opening_price"] - self.status["executed_price"]
- self.spread)
if event.side == "buy" else
(self.status["executed_price"] - self.status["opening_price"]
- self.spread))
self.status["realized_pnl"] += self.current_pnl
if self.is_win():
self.total_profit += self.current_pnl
self.win_count += 1
else:
self.total_loss -= self.current_pnl
self.lose_count += 1
def is_win(self):
return self.current_pnl > 0
def print_current_status(self, event, pnl):
print("[%s] no=%2s open_price=%.7s exe_price=%.7s PnL=%.7s" % (
event.time,
self.order_count,
round(self.status["opening_price"], 6),
round(self.status["executed_price"], 6),
round(pnl, 5)
))
class PortfolioRemote(Portfolio):
def show_current_status(self, event):
self.calculate_unrealized_pnl(event)
self.calculate_unrealized_pip(event)
self.print_status(event)
def calculate_unrealized_pnl(self, event):
if self.status["open_position"]:
# Retrieve position from server
pos = self.oanda.get_position(ACCOUNT_ID,
event.instrument)
units = pos["units"]
side = pos["side"]
avg_price = float(pos["avgPrice"])
self.status["unrealized_pnl"] = units * (
(event.bid - avg_price)
if (side == "buy")
else (avg_price - event.ask))
else:
self.status["unrealized_pnl"] = 0
def calculate_unrealized_pip(self, event):
if self.status["open_position"]:
if self.status["position"] > 0:
self.status["unrealized_pip"] \
= (event.bid - self.status["opening_price"]
- self.spread) * 10000
else:
self.status["unrealized_pip"] \
= (self.status["opening_price"] - event.bid
- self.spread) * 10000
else:
self.status["unrealized_pip"] = 0
def print_status(self, event):
# print("[%s] %s pos=%s RPnL=%s UPnL=%s UPip=%s" % (
print("[%s] %s pos=%s bid=%s cur=%s UPip=%s" % (
datetime.now().time(),
event.instrument,
self.status["position"],
round(event.bid, 5),
round(self.status["opening_price"], 5),
# round(self.status["realized_pnl"], 5),
# round(self.status["unrealized_pnl"], 5),
round(self.status["unrealized_pip"], 2)
))
class PortfolioLocal(Portfolio):
def show_current_status(self, event):
print("[%s] %s pos=%s RPnL=%s UPnL=%s" % (
datetime.now().time(),
event.instrument,
self.status["position"],
round(self.status["realized_pnl"], 5),
round(self.status["unrealized_pnl"], 5)))