Skip to content

Commit ed6a77c

Browse files
committed
First draft of PnL Widget
1 parent a290214 commit ed6a77c

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

Controller.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def loadData(self, dataPath):
7373
self.dataframe = pd.read_csv(dataPath, sep='\t', parse_dates=[0], date_parser=lambda x: pd.to_datetime(x, format='%Y-%m-%d %H:%M:%S'),skiprows=0,header=0,index_col=0)
7474

7575
# Datetime first column : 2012-12-28 17:45:00
76-
self.dataframe['TimeInt'] = pd.to_datetime(self.dataframe.index).astype('int64') # use finplot's internal representation, which is ns
76+
#self.dataframe['TimeInt'] = pd.to_datetime(self.dataframe.index).astype('int64') # use finplot's internal representation, which is ns
7777

7878
# Pass it to the backtrader datafeed and add it to the cerebro
7979
self.data = bt.feeds.PandasData(dataname=self.dataframe, timeframe=bt.TimeFrame.Minutes)
@@ -127,6 +127,8 @@ def displayStrategyResults(self):
127127

128128
self.interface.setOrders(self.myOrders)
129129

130+
self.interface.displayPnL( self.strat_results._trades.items() )
131+
130132
pass
131133

132134
def displayUI(self):

finplotWindow.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,16 @@ def __init__(self, dockArea, dockChart):
3030
def createPlotWidgets(self):
3131

3232
# fin plot
33-
self.ax0, self.ax1, self.ax2, self.ax3 = fplt.create_plot_widget(master=self.dockArea, rows=4, init_zoom_periods=100)
34-
self.dockArea.axs = [self.ax0, self.ax1, self.ax2, self.ax3]
33+
self.ax0, self.ax1, self.ax2, self.axPnL = fplt.create_plot_widget(master=self.dockArea, rows=4, init_zoom_periods=200)
34+
self.dockArea.axs = [self.ax0, self.ax1, self.ax2, self.axPnL]
3535
self.dockChart.addWidget(self.ax0.ax_widget, 1, 0, 1, 1)
36+
self.dockChart.addWidget(self.ax1.ax_widget, 2, 0, 1, 1)
37+
self.dockChart.addWidget(self.ax2.ax_widget, 3, 0, 1, 1)
38+
self.dockChart.addWidget(self.axPnL.ax_widget, 4, 0, 1, 1)
39+
40+
self.ax1.ax_widget.hide()
41+
self.ax2.ax_widget.hide()
42+
self.axPnL.ax_widget.hide()
3643
pass
3744

3845
def drawCandles(self):
@@ -254,7 +261,7 @@ def activateDarkMode(self, activated):
254261
#############
255262

256263
def resetPlots(self):
257-
264+
258265
# Entirely reset graph
259266
if (hasattr(self,"ax0")):
260267
self.ax0.reset()
@@ -263,8 +270,8 @@ def resetPlots(self):
263270
self.ax1.reset()
264271
if (hasattr(self,"ax2")):
265272
self.ax2.reset()
266-
if (hasattr(self,"ax3")):
267-
self.ax3.reset()
273+
if (hasattr(self,"axPnL")):
274+
self.axPnL.reset()
268275

269276
pass
270277

@@ -287,8 +294,8 @@ def updateChart(self):
287294
# Finally draw candles
288295
self.drawCandles()
289296

290-
#if self.IndVolumesActivated:
291-
# fplt.volume_ocv(self.data['Open Close Volume'.split()], ax=self.ax0.overlay())
297+
if self.IndVolumesActivated:
298+
fplt.volume_ocv(self.data['Open Close Volume'.split()], ax=self.ax0.overlay())
292299

293300
# Refresh view : auto zoom
294301
fplt.refresh()
@@ -317,4 +324,13 @@ def show(self):
317324
fplt.show(qt_exec=False)
318325

319326
pass
327+
328+
def drawPnL(self, pln_data):
329+
330+
# put an MA on the close price
331+
fplt.plot(pln_data['time'], pln_data['pnlcomm'], ax = self.axPnL)
332+
self.axPnL.ax_widget.show()
333+
self.axPnL.show()
334+
335+
pass
320336

indicators/ichimoku.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def __init__(self, dataFrames, tenkan = 9, kijun = 26, senkou = 52, senkou_lead
6363

6464
def draw(self, ax, tenkan_color = "magenta", kijun_color = "blue", senkou_a_color = "gray", senkou_b_color = "gray", chikou_color = "yellow"):
6565

66+
6667
self.tenkan_sen_plot = fplt.plot(self.tenkan_sen, ax = ax, color=tenkan_color, width=1 )
6768
self.kijun_sen_plot = fplt.plot(self.kijun_sen, ax = ax, color=kijun_color, width=2 )
6869
self.chikou_span_plot = fplt.plot(self.chikou_span, ax = ax, color=chikou_color, width=2 )

userInterface.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
import qdarkstyle
3838

39+
import pandas as pd
40+
3941
class UserInterface:
4042

4143
#########
@@ -400,6 +402,36 @@ def setOrders(self, orders):
400402
pass
401403

402404

405+
#########
406+
# Draw PnL chart
407+
#########
408+
def displayPnL(self, trades):
409+
410+
pnl_data = {}
411+
pnl_data['time'] = []
412+
pnl_data['pnlcomm'] = []
413+
414+
# Prepare data before plotting Pnl
415+
for key, values in trades:
416+
417+
row = 0
418+
for trade in values[0]:
419+
420+
if not trade.isopen:
421+
422+
pnl_data['time'].append( bt.num2date(trade.dtclose) )
423+
pnl_data['pnlcomm'].append(trade.pnlcomm)
424+
425+
row += 1
426+
427+
# index is X axis ?
428+
pnl_data['time'] = pd.to_datetime(pnl_data['time'])
429+
430+
df = pd.DataFrame(data=pnl_data)
431+
432+
self.fpltWindow.drawPnL(df)
433+
pass
434+
403435
#########
404436
# Control panel overlay on top/above of the finplot window
405437
#########

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