29
29
import strategyTesterUI
30
30
31
31
import pandas as pd
32
+ import math
32
33
33
34
class UserInterface :
34
35
@@ -263,10 +264,12 @@ def update_legend_text(self, x, y, ax, data):
263
264
row = data .loc [data .TimeInt == x ]
264
265
265
266
# format html with the candle and set legend
266
- fmt = '<span style="color:#%s">%%.2f </span>' % ('0b0 ' if (row .Open < row .Close ).all () else 'a00 ' )
267
+ fmt = '<span style="color:#%s">%%.5f </span>' % ('0f0 ' if (row .Open < row .Close ).all () else 'd00 ' )
267
268
rawtxt = '<span style="font-size:13px">%%s %%s</span> O%s C%s H%s L%s' % (fmt , fmt , fmt , fmt )
268
269
self .hover_label .setText (rawtxt % ("EUR" , "M15" , row .Open , row .Close , row .High , row .Low ))
269
270
271
+ pass
272
+
270
273
def update_crosshair_text (self ,x , y , xtext , ytext ):
271
274
ytext = '%s (Close%+.2f)' % (ytext , (y - self .data .iloc [x ].Close ))
272
275
return xtext , ytext
@@ -282,7 +285,6 @@ def drawFinPlots(self, data):
282
285
self .ax0 , self .ax1 = fplt .create_plot_widget (master = self .area , rows = 2 , init_zoom_periods = 100 )
283
286
self .area .axs = [self .ax0 , self .ax1 ]
284
287
self .dock_chart .addWidget (self .ax0 .ax_widget , 1 , 0 , 1 , 2 )
285
- #self.dock_1.addWidget(ax1.ax_widget, 1, 0, 1, 2)
286
288
287
289
fplt .candlestick_ochl (data ['Open Close High Low' .split ()], ax = self .ax0 )
288
290
fplt .volume_ocv (data ['Open Close Volume' .split ()], ax = self .ax0 .overlay ())
@@ -291,23 +293,112 @@ def drawFinPlots(self, data):
291
293
fplt .set_time_inspector (self .update_legend_text , ax = self .ax0 , when = 'hover' , data = data )
292
294
#fplt.add_crosshair_info(self.update_crosshair_text, ax=self.ax0)
293
295
294
- # def add_line(p0, p1, color=draw_line_color, width=1, style=None, interactive=False, ax=None):
295
- #fplt.add_line();
296
+ pass
296
297
297
298
#########
298
299
# Draw orders on charts (with arrows)
299
300
#########
300
301
def drawOrders (self , orders ):
301
302
303
+ # Orders need to be stuied to know if an order is an open or a close order, or both...
304
+ # It depends on the order volume and the currently opened positions volume
305
+ currentPositionSize = 0
306
+ open_orders = []
307
+
302
308
for order in orders :
309
+
310
+ ##############
311
+ # Buy
312
+ ##############
303
313
if order .isbuy ():
314
+
304
315
direction = "buy"
316
+
317
+ # Tracer les traites allant des ouvertures de positions vers la fermeture de position
318
+ if currentPositionSize < 0 :
319
+
320
+ # Réduction, cloture, ou invertion de la position
321
+ if order .size == abs (currentPositionSize ): # it's a buy so order.size > 0
322
+ # Cloture de la position
323
+ posOpen = (open_orders [- 1 ].executed .dt ,open_orders [- 1 ].executed .price )
324
+ posClose = (bt .num2date (order .executed .dt ), order .executed .price )
325
+ fplt .add_line (posOpen , posClose , "#30FF30" , 2 , style = "--" )
326
+ pass
327
+
328
+ elif order .size > abs (currentPositionSize ):
329
+ # Fermeture de la position précédente + ouverture d'une position inverse
330
+ pass
331
+
332
+ elif order .size < abs (currentPositionSize ):
333
+ # Réduction de la position courante
334
+ pass
335
+
336
+ elif currentPositionSize > 0 :
337
+ # Augmentation de la postion
338
+ # on enregistre la position pour pouvoir tracer un trait de ce point vers l'ordre de cloture du trade.
339
+ open_orders .append (order )
340
+
341
+ else :
342
+ # Ouverture d'une nouvelle position
343
+ open_orders .append (order )
344
+ pass
345
+
346
+ ##############
347
+ # Sell
348
+ ##############
305
349
elif order .issell ():
306
350
direction = "sell"
307
-
351
+
352
+
353
+ if currentPositionSize < 0 :
354
+ # Augmentation de la postion
355
+
356
+ # on enregistre la position pour pouvoir tracer un trait de ce point vers l'ordre de cloture du trade.
357
+ open_orders .append (order )
358
+
359
+ elif currentPositionSize > 0 :
360
+ # Réduction, cloture, ou invertion de la position
361
+
362
+ if abs (order .size ) == abs (currentPositionSize ): # it's a buy so order.size > 0
363
+ # Cloture de la position
364
+ last_order = open_orders .pop ()
365
+ posOpen = (bt .num2date (last_order .executed .dt ),last_order .executed .price )
366
+ posClose = (bt .num2date (order .executed .dt ), order .executed .price )
367
+
368
+ color = "#555555"
369
+ if order .executed .pnl > 0 :
370
+ color = "#30FF30"
371
+ elif order .executed .pnl < 0 :
372
+ color = "#FF3030"
373
+
374
+ fplt .add_line (posOpen , posClose , color , 2 , style = "--" )
375
+
376
+ pass
377
+
378
+ elif order .size > abs (currentPositionSize ):
379
+ # Réduction de la position courante
380
+ pass
381
+
382
+ elif order .size < abs (currentPositionSize ):
383
+ # Fermeture de la position précédente + ouverture d'une position inverse
384
+ pass
385
+
386
+ else :
387
+ # Ouverture d'une nouvelle position
388
+ open_orders .append (order )
389
+ pass
390
+
391
+ else :
392
+ print ("Unknown order" )
393
+
394
+ # Cumul des positions
395
+ currentPositionSize += order .size
396
+
397
+ # Todo: We could display the size of the order with a label on the chart
308
398
fplt .add_order (bt .num2date (order .executed .dt ), order .executed .price , direction , ax = self .ax0 )
309
399
310
400
pass
401
+
311
402
312
403
#########
313
404
# Show all
@@ -318,6 +409,7 @@ def show(self):
318
409
319
410
self .win .show ()
320
411
self .app .exec_ ()
412
+ pass
321
413
322
414
#########
323
415
# Get strategy running progress bar
0 commit comments