|
|
|
@ -7,6 +7,7 @@ class EquityAnalyzer(Analyzer): |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, strategy): |
|
|
|
def __init__(self, strategy): |
|
|
|
self.strategy = strategy |
|
|
|
self.strategy = strategy |
|
|
|
|
|
|
|
self.bar_to_pos = [] |
|
|
|
|
|
|
|
|
|
|
|
def get_result(self): |
|
|
|
def get_result(self): |
|
|
|
positions = self.strategy.broker.retired_positions() # TODO also add open positions |
|
|
|
positions = self.strategy.broker.retired_positions() # TODO also add open positions |
|
|
|
@ -15,7 +16,6 @@ class EquityAnalyzer(Analyzer): |
|
|
|
return equity |
|
|
|
return equity |
|
|
|
|
|
|
|
|
|
|
|
def calc_equity(self, positions, bars): |
|
|
|
def calc_equity(self, positions, bars): |
|
|
|
timestamp = bars.timestamp |
|
|
|
|
|
|
|
close = bars.close |
|
|
|
close = bars.close |
|
|
|
|
|
|
|
|
|
|
|
cumulative_pnl = 0 |
|
|
|
cumulative_pnl = 0 |
|
|
|
@ -23,16 +23,16 @@ class EquityAnalyzer(Analyzer): |
|
|
|
equity = [] |
|
|
|
equity = [] |
|
|
|
|
|
|
|
|
|
|
|
prev_p = 0 |
|
|
|
prev_p = 0 |
|
|
|
print(len(close), len(timestamp)) |
|
|
|
self.calculate_lookup_table(positions, len(close)) |
|
|
|
for (p,ts) in zip(close, timestamp): |
|
|
|
for (bar_num, p) in enumerate(close): |
|
|
|
active_positions = self.positions_for_timestamp(positions, ts) |
|
|
|
active_positions = self.positions_for_bar(positions, bar_num) |
|
|
|
for pos in active_positions: |
|
|
|
for pos in active_positions: |
|
|
|
if pos.entry_time() == ts: |
|
|
|
if pos.entry_bar() == bar_num: |
|
|
|
if pos.is_long(): |
|
|
|
if pos.is_long(): |
|
|
|
cumulative_pnl += (p - pos.entry_price()) |
|
|
|
cumulative_pnl += (p - pos.entry_price()) |
|
|
|
else: |
|
|
|
else: |
|
|
|
cumulative_pnl -= (p - pos.entry_price()) |
|
|
|
cumulative_pnl -= (p - pos.entry_price()) |
|
|
|
elif pos.exit_time() == ts: |
|
|
|
elif pos.exit_bar() == bar_num: |
|
|
|
if pos.is_long(): |
|
|
|
if pos.is_long(): |
|
|
|
cumulative_pnl += (pos.exit_price() - prev_p) |
|
|
|
cumulative_pnl += (pos.exit_price() - prev_p) |
|
|
|
else: |
|
|
|
else: |
|
|
|
@ -47,12 +47,17 @@ class EquityAnalyzer(Analyzer): |
|
|
|
prev_p = p |
|
|
|
prev_p = p |
|
|
|
return equity |
|
|
|
return equity |
|
|
|
|
|
|
|
|
|
|
|
def positions_for_timestamp(self, positions, timestamp): |
|
|
|
def positions_for_bar(self, positions, bar_num): |
|
|
|
result = [] |
|
|
|
result = [] |
|
|
|
for p in positions: |
|
|
|
for pos_index in self.bar_to_pos[bar_num]: |
|
|
|
if p.entry_time() <= timestamp and p.exit_time() >= timestamp: |
|
|
|
pos = positions[pos_index] |
|
|
|
result.append(p) |
|
|
|
result.append(pos) |
|
|
|
return result |
|
|
|
return result |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def calculate_lookup_table(self, positions, length): |
|
|
|
|
|
|
|
self.bar_to_pos = [] |
|
|
|
|
|
|
|
for i in range(0, length): |
|
|
|
|
|
|
|
self.bar_to_pos.append([]) |
|
|
|
|
|
|
|
for pos_index, pos in enumerate(positions): |
|
|
|
|
|
|
|
for i in range(pos.entry_bar(), pos.exit_bar() + 1): |
|
|
|
|
|
|
|
self.bar_to_pos[i].append(pos_index) |
|
|
|
|