Skip to content

Commit fff96fb

Browse files
committed
Add Multi-timeframes with multiple charts
1 parent cbe8c4c commit fff96fb

File tree

4 files changed

+216
-134
lines changed

4 files changed

+216
-134
lines changed

Controller.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,22 @@ def resetCerebro(self):
9999
pass
100100

101101

102+
# Return True if loading is successfull & the error string if False
102103
def loadData(self, dataPath, datetimeFormat, separator):
103104

104105
# Try importing data file
105106
# We should code a widget that ask for options as : separators, date format, and so on...
106107
try:
107108
fileName = os.path.basename(dataPath)
108-
self.dataframes[fileName] = pd.read_csv(dataPath, sep=separator, parse_dates=[0], date_parser=lambda x: pd.to_datetime(x, format=datetimeFormat), skiprows=0, header=0, index_col=0)
109+
self.dataframes[fileName] = pd.read_csv(dataPath,
110+
sep=separator,
111+
parse_dates=[0],
112+
date_parser=lambda x: pd.to_datetime(x, format=datetimeFormat),
113+
skiprows=0,
114+
header=0,
115+
names=["Time", "Open", "High", "Low", "Close", "Volume"],
116+
index_col=0)
117+
109118
except ValueError as err:
110119
return False, "ValueError error:" + str(err)
111120
except AttributeError as err:
@@ -131,14 +140,44 @@ def importData(self, fileNamesOrdered):
131140
# Add data to cerebro : only add data when all files have been selected for multi-timeframes
132141
self.cerebro.adddata(self.data) # Add the data feed
133142

143+
# find the time frame
144+
timeframe = self.findTimeFrame(df)
145+
146+
# Create the chart window for the good timeframe (if it does not already exists?)
147+
self.interface.createChartDock(timeframe)
148+
134149
# Draw charts based on input data
135-
self.interface.drawChart(df)
150+
self.interface.drawChart(df, timeframe)
136151

137152
# Enable run button
138153
self.interface.strategyTesterUI.runBacktestPB.setEnabled(True)
139154

140155
pass
141156

157+
def findTimeFrame(self,df):
158+
159+
if len(df.index) > 2:
160+
dtDiff = df.index[1] - df.index[0]
161+
162+
if dtDiff.seconds == 60:
163+
return "M1"
164+
elif dtDiff.seconds == 300:
165+
return "M5"
166+
elif dtDiff.seconds == 900:
167+
return "M15"
168+
elif dtDiff.seconds == 1800:
169+
return "M30"
170+
elif dtDiff.seconds == 3600:
171+
return "H1"
172+
elif dtDiff.seconds == 14400:
173+
return "H4"
174+
elif dtDiff.seconds == 86400:
175+
return "D"
176+
elif dtDiff.seconds == 604800:
177+
return "W"
178+
179+
pass
180+
142181

143182
def addStrategy(self, strategyName):
144183

@@ -254,4 +293,5 @@ def cashChanged(self, cashString):
254293
self.startingcash = float(cashString)
255294
self.cerebro.broker.setcash(self.startingcash)
256295

257-
pass
296+
pass
297+

finplotWindow.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(self, dockArea, dockChart, interface):
3535
#########
3636
# Prepare the plot widgets
3737
#########
38-
def createPlotWidgets(self):
38+
def createPlotWidgets(self, timeframe):
3939

4040
# fin plot
4141
self.ax0, self.ax_rsi, self.ax_stochasticRsi, self.ax_stochastic, self.axPnL = fplt.create_plot_widget(master=self.dockArea, rows=5, init_zoom_periods=200)
@@ -47,9 +47,9 @@ def createPlotWidgets(self):
4747
self.dockChart.addWidget(self.ax2.ax_widget, 3, 0, 1, 1)
4848
'''
4949

50-
self.interface.dock_rsi.layout.addWidget(self.ax_rsi.ax_widget)
51-
self.interface.dock_stochasticRsi.layout.addWidget(self.ax_stochasticRsi.ax_widget)
52-
self.interface.dock_stochastic.layout.addWidget(self.ax_stochastic.ax_widget)
50+
self.interface.dock_rsi[timeframe].layout.addWidget(self.ax_rsi.ax_widget)
51+
self.interface.dock_stochasticRsi[timeframe].layout.addWidget(self.ax_stochasticRsi.ax_widget)
52+
self.interface.dock_stochastic[timeframe].layout.addWidget(self.ax_stochastic.ax_widget)
5353

5454
# Ax Profit & Loss
5555
self.interface.strategyResultsUI.ResultsTabWidget.widget(1).layout().addWidget(self.axPnL.ax_widget)
@@ -67,6 +67,7 @@ def drawCandles(self):
6767
# Inside plot widget controls
6868
#self.createControlPanel(self.ax0.ax_widget)
6969
pass
70+
7071

7172
def drawSma(self, period):
7273
self.sma_indicator = sma.Sma(self.data, period)

solution.code-workspace

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "."
5+
}
6+
],
7+
"settings": {}
8+
}

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