Skip to content

Commit 1fe994b

Browse files
committed
Wallet display value & equity :)
1 parent 6b4fdf4 commit 1fe994b

File tree

5 files changed

+106
-11
lines changed

5 files changed

+106
-11
lines changed

Controller.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
# local files
3636
import userInterface as Ui
3737

38-
from observers.progressBarObserver import ProgressBarObserver
38+
from observers.SkinokObserver import SkinokObserver
39+
from wallet import Wallet
3940

4041
class Controller:
4142

@@ -49,7 +50,14 @@ def __init__(self):
4950
interface = Ui.UserInterface(self)
5051
self.interface = interface
5152

53+
# Strategie testing wallet (a little bit different from backtrader broker class)
54+
global wallet
55+
wallet = Wallet()
56+
self.wallet = wallet
57+
58+
# Then add obersers and analyzers
5259
self.cerebro.addanalyzer(bt.analyzers.TradeAnalyzer, _name = "ta")
60+
5361
'''
5462
self.cerebro.addanalyzer(bt.analyzers.PyFolio, _name='PyFolio')
5563
self.cerebro.addanalyzer(bt.analyzers.SharpeRatio)
@@ -65,7 +73,8 @@ def __init__(self):
6573
'''
6674

6775
# Add an observer to watch the strat running and update the progress bar values
68-
self.cerebro.addobserver( ProgressBarObserver )
76+
self.cerebro.addobserver( SkinokObserver )
77+
6978
pass
7079

7180
def loadData(self, dataPath):
@@ -127,7 +136,7 @@ def displayStrategyResults(self):
127136

128137
self.interface.setOrders(self.myOrders)
129138

130-
self.interface.displayPnL( self.strat_results._trades.items(), self.dataframe )
139+
self.interface.displayPnL( self.strat_results._trades.items(), self.dataframe, self.wallet )
131140

132141
pass
133142

finplotWindow.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import sys, os
22

3+
from pyqtgraph.graphicsItems.LegendItem import LegendItem
4+
35
from indicators import ichimoku
46

57
sys.path.append('../finplot')
@@ -337,7 +339,10 @@ def show(self):
337339
def drawPnL(self, pln_data):
338340

339341
# put an MA on the close price
340-
fplt.plot(pln_data['time'], pln_data['pnlcomm'], ax = self.axPnL)
342+
#fplt.plot(pln_data['time'], pln_data['pnlcomm'], ax = self.axPnL)
343+
#fplt.plot(pln_data['time'], pln_data['cash'], ax = self.axPnL, legend="Cash")
344+
fplt.plot(pln_data['time'], pln_data['value'], ax = self.axPnL, legend="value")
345+
fplt.plot(pln_data['time'], pln_data['equity'], ax = self.axPnL, legend="equity")
341346
self.axPnL.ax_widget.show()
342347
self.axPnL.show()
343348

observers/progressBarObserver.py renamed to observers/SkinokObserver.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,45 @@
2525

2626
import Controller
2727

28-
class ProgressBarObserver(Observer):
28+
class SkinokObserver(Observer):
29+
30+
lines = ('wallet_value', 'wallet_equity', 'wallet_cash')
2931

30-
lines = ('max', 'value',)
31-
3232
def __init__(self):
33+
34+
# Ui following
3335
self.progressBar = Controller.interface.getProgressBar()
3436
self.progressBar.setMaximum(self.datas[0].close.buflen())
3537
self.progressBar.setValue(0)
3638

39+
# Wallet Management
40+
Controller.wallet.reset()
41+
3742
def next(self):
43+
44+
# Watch trades
45+
pnl = 0
46+
for trade in self._owner._tradespending:
47+
48+
if trade.data not in self.ddatas:
49+
continue
50+
51+
if not trade.isclosed:
52+
continue
53+
54+
pnl += trade.pnl # trade.pnlcomm if self.p.pnlcomm else trade.pnl
55+
56+
# Portfolio update
57+
Controller.wallet.current_value = self.wallet_value = Controller.wallet.current_value + pnl
58+
Controller.wallet.value_list.append( Controller.wallet.current_value )
59+
60+
Controller.wallet.current_equity = self.wallet_equity = self._owner.broker.getvalue()
61+
Controller.wallet.equity_list.append(self._owner.broker.getvalue())
62+
63+
Controller.wallet.current_cash = self.wallet_cash = self._owner.broker.getcash()
64+
Controller.wallet.cash_list.append(self._owner.broker.getcash())
65+
66+
# Progress bar update
3867
self.progressBar.setValue( self.progressBar.value() + 1 )
3968
Controller.interface.app.processEvents()
4069

userInterface.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,13 @@ def setOrders(self, orders):
408408
# A python expert could do waaaaay better optimized on this function
409409
# But anyway... it works...
410410
#########
411-
def displayPnL(self, trades, df):
411+
def displayPnL(self, trades, df, wallet):
412412

413413
pnl_data = {}
414+
415+
'''
414416
pnl_data['time'] = []
415-
pnl_data['pnlcomm'] = []
416-
417+
#pnl_data['pnlcomm'] = []
417418
# temporary
418419
tradesDatetimeIndex = []
419420
tradesPnl = []
@@ -446,12 +447,24 @@ def displayPnL(self, trades, df):
446447
pnl_index = tradesDatetimeIndex.index(timeIndex_datetime)
447448
current_pnl = tradesPnl[pnl_index]
448449
449-
pnl_data['pnlcomm'].append(current_pnl)
450+
#pnl_data['cash'].append(broker.value.array[i])
450451
pnl_data['time'].append(timeIndex)
451452
452453
pass
453454
455+
'''
456+
457+
#pnl_data['cash'] = broker.value.array.tolist()
458+
pnl_data['value'] = wallet.value_list
459+
pnl_data['equity'] = wallet.equity_list
460+
pnl_data['cash'] = wallet.cash_list
461+
pnl_data['time'] = df.index
462+
454463
# Data prepared
464+
#pnl_data['broker'] = broker.plotlines._getvalues()
465+
#taille_cash = len(pnl_data['cash'])
466+
#taille_time = len(pnl_data['time'])
467+
455468
pnl_df = pd.DataFrame(pnl_data)
456469

457470
# draw charts

wallet.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8; py-indent-offset:4 -*-
3+
###############################################################################
4+
#
5+
# Copyright (C) 2021-2025 Skinok
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
#
20+
###############################################################################
21+
class Wallet():
22+
23+
def __init__(self):
24+
25+
self.reset()
26+
27+
pass
28+
29+
def reset(self):
30+
31+
self.current_value = 10000 # todo: change it by initial cash settings
32+
self.current_cash = 10000 # todo: change it by initial cash settings
33+
self.current_equity = 10000 # todo: change it by initial cash settings
34+
35+
self.value_list = []
36+
self.cash_list = []
37+
self.equity_list = []
38+
39+
pass

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy