forked from TreborNamor/TradingView-Machine-Learning-GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_functions.py
1066 lines (999 loc) · 44.1 KB
/
my_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import time
from selenium.common.exceptions import (ElementNotInteractableException,
NoSuchElementException)
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from termcolor import colored
from profit import profits
from TradeViewGUI import Main
class Functions(Main):
"""You will find click, get, find, and show_me functions here."""
# Find Functions
def find_best_stoploss(self):
best_in_dict = max(profits, key=profits.get)
return best_in_dict
def find_best_takeprofit(self):
best_in_dict = max(profits, key=profits.get)
return best_in_dict
def find_best_key_both(self):
best_in_dict = max(profits)
return best_in_dict
# Click Functions
def click_settings_button(self, wait):
"""click settings button."""
try:
wait.until(
EC.visibility_of_element_located(
(
By.XPATH,
"//*[@class='icon-button "
"js-backtesting-open-format-dialog "
"apply-common-tooltip']",
)
)
)
settings_button = self.driver.find_element_by_xpath(
"//*[@class='icon-button js-backtesting-open-format-dialog "
"apply-common-tooltip']"
)
settings_button.click()
except AttributeError:
pass
def click_strategy_tester(self, wait):
"""check if strategy tester tab is active if not click to open tab."""
try:
wait.until(
EC.visibility_of_element_located(
(By.XPATH, "//*[@class='title-37voAVwR']")
)
)
strategy_tester_tab = self.driver.find_elements_by_xpath(
"//*[@class='title-37voAVwR']"
)
for index, web_element in enumerate(strategy_tester_tab):
if web_element.text == "Strategy Tester":
active_tab = strategy_tester_tab[index].get_attribute(
"data-active")
if active_tab == "false":
strategy_tester_tab[index].click()
break
except (IndexError, NoSuchElementException, ElementNotInteractableException):
print(
"Could Not Click Strategy Tester Tab. Please Check web element XPATH."
)
def click_overview(self):
try:
strategy_tester_tab = self.driver.find_elements_by_xpath(
"//*[@class='title-37voAVwR']"
)
for index, web_element in enumerate(strategy_tester_tab):
if web_element.text == "Strategy Tester":
active_tab = strategy_tester_tab[index].get_attribute(
"data-active")
if active_tab == "false":
strategy_tester_tab[index].click()
# time.sleep(.3)
overview = self.driver.find_element_by_class_name(
"report-tabs"
).find_elements_by_tag_name("li")[0]
overview.click()
else:
overview = self.driver.find_element_by_class_name(
"report-tabs"
).find_elements_by_tag_name("li")[0]
overview.click()
break
except (IndexError, NoSuchElementException, ElementNotInteractableException):
print(
"Could Not Click Strategy Tester Tab. Please Check web element XPATH."
)
def click_performance_summary(self):
"""click perfromance summary tab."""
try:
strategy_tester_tab = self.driver.find_elements_by_xpath(
"//*[@class='title-37voAVwR']"
)
for index, web_element in enumerate(strategy_tester_tab):
if web_element.text == "Strategy Tester":
active_tab = strategy_tester_tab[index].get_attribute(
"data-active")
if active_tab == "false":
strategy_tester_tab[index].click()
# time.sleep(.3)
performance_tab = self.driver.find_elements_by_class_name(
"report-tabs").find_elements_by_tag_name("li")[1]
performance_tab.click()
else:
performance_tab = self.driver.find_element_by_class_name(
"report-tabs").find_elements_by_tag_name("li")[1]
performance_tab.click()
break
except (IndexError, NoSuchElementException, ElementNotInteractableException):
print(
"Could Not Click Strategy Tester Tab. Please Check web element XPATH."
)
def click_list_of_trades(self):
"""click list of trades tab."""
try:
strategy_tester_tab = self.driver.find_elements_by_xpath(
"//*[@class='title-37voAVwR']"
)
for index, web_element in enumerate(strategy_tester_tab):
if web_element.text == "Strategy Tester":
active_tab = strategy_tester_tab[index].get_attribute(
"data-active")
if active_tab == "false":
strategy_tester_tab[index].click()
# time.sleep(.3)
list_of_trades = self.driver.find_element_by_class_name(
"report-tabs").find_elements_by_tag_name("li")[2]
list_of_trades.click()
else:
list_of_trades = self.driver.find_element_by_class_name(
"report-tabs").find_elements_by_tag_name("li")[2]
list_of_trades.click()
break
except (IndexError, NoSuchElementException, ElementNotInteractableException):
print(
"Could Not Click Strategy Tester Tab. Please Check web element XPATH."
)
def click_long_stoploss_input(self, count, wait):
"""click short stoploss input."""
wait.until(EC.visibility_of_element_located(
(By.XPATH, "//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']")))
stoploss_input_box = self.driver.find_elements_by_xpath(
"//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']")[0]
stoploss_input_box.send_keys(
Keys.BACK_SPACE +
Keys.BACK_SPACE +
Keys.BACK_SPACE +
Keys.BACK_SPACE)
stoploss_input_box.send_keys(str(count))
stoploss_input_box.send_keys(Keys.ENTER)
time.sleep(.5)
ok_button = self.driver.find_element_by_name("submit")
ok_button.click()
def click_long_takeprofit_input(self, count, wait):
"""click long take profit input."""
wait.until(EC.visibility_of_element_located(
(By.XPATH, "//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']")))
takeprofit_input_box = self.driver.find_elements_by_xpath(
"//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']")[1]
takeprofit_input_box.send_keys(
Keys.BACK_SPACE +
Keys.BACK_SPACE +
Keys.BACK_SPACE +
Keys.BACK_SPACE)
takeprofit_input_box.send_keys(str(count))
takeprofit_input_box.send_keys(Keys.ENTER)
time.sleep(.5)
ok_button = self.driver.find_element_by_name("submit")
ok_button.click()
def click_short_stoploss_input(self, count, wait):
"""click short stoploss input."""
wait.until(EC.visibility_of_element_located(
(By.XPATH, "//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']")))
stoploss_input_box = self.driver.find_elements_by_xpath(
"//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']")[2]
stoploss_input_box.send_keys(
Keys.BACK_SPACE +
Keys.BACK_SPACE +
Keys.BACK_SPACE +
Keys.BACK_SPACE)
stoploss_input_box.send_keys(str(count))
stoploss_input_box.send_keys(Keys.ENTER)
time.sleep(.5)
ok_button = self.driver.find_element_by_name("submit")
ok_button.click()
def click_short_takeprofit_input(self, count, wait):
"""click short take profit input."""
wait.until(EC.visibility_of_element_located(
(By.XPATH, "//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']")))
stoploss_input_box = self.driver.find_elements_by_xpath(
"//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']")[3]
stoploss_input_box.send_keys(
Keys.BACK_SPACE +
Keys.BACK_SPACE +
Keys.BACK_SPACE +
Keys.BACK_SPACE)
stoploss_input_box.send_keys(str(count))
stoploss_input_box.send_keys(Keys.ENTER)
time.sleep(.5)
ok_button = self.driver.find_element_by_name("submit")
ok_button.click()
def click_long_inputs(
self,
long_stoploss_value,
long_takeprofit_value,
wait):
"""click both long inputs."""
wait.until(EC.visibility_of_element_located(
(By.XPATH, "//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']")))
stoploss_input_box = self.driver.find_elements_by_xpath(
"//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']")[0]
takeprofit_input_box = self.driver.find_elements_by_xpath(
"//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']")[1]
stoploss_input_box.send_keys(
Keys.BACK_SPACE +
Keys.BACK_SPACE +
Keys.BACK_SPACE +
Keys.BACK_SPACE)
stoploss_input_box.send_keys(str(long_stoploss_value))
takeprofit_input_box.send_keys(
Keys.BACK_SPACE +
Keys.BACK_SPACE +
Keys.BACK_SPACE +
Keys.BACK_SPACE)
takeprofit_input_box.send_keys(str(long_takeprofit_value))
takeprofit_input_box.send_keys(Keys.ENTER)
time.sleep(.5)
ok_button = self.driver.find_element_by_name("submit")
ok_button.click()
def click_short_inputs(
self,
short_stoploss_value,
short_takeprofit_value,
wait):
"""click both short inputs."""
wait.until(EC.visibility_of_element_located(
(By.XPATH, "//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']")))
stoploss_input_box = self.driver.find_elements_by_xpath(
"//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']")[2]
takeprofit_input_box = self.driver.find_elements_by_xpath(
"//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']")[3]
stoploss_input_box.send_keys(
Keys.BACK_SPACE +
Keys.BACK_SPACE +
Keys.BACK_SPACE +
Keys.BACK_SPACE)
stoploss_input_box.send_keys(str(short_stoploss_value))
takeprofit_input_box.send_keys(
Keys.BACK_SPACE +
Keys.BACK_SPACE +
Keys.BACK_SPACE +
Keys.BACK_SPACE)
takeprofit_input_box.send_keys(str(short_takeprofit_value))
takeprofit_input_box.send_keys(Keys.ENTER)
time.sleep(.5)
ok_button = self.driver.find_element_by_name("submit")
ok_button.click()
def click_all_inputs(
self,
long_stoploss_value,
long_takeprofit_value,
short_stoploss_value,
short_takeprofit_value,
wait,
):
"""click short stoploss input."""
wait.until(EC.visibility_of_element_located(
(By.XPATH, "//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']")))
long_stoploss_input_box = self.driver.find_elements_by_xpath(
"//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']"
)[0]
long_takeprofit_input_box = self.driver.find_elements_by_xpath(
"//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']"
)[1]
short_stoploss_input_box = self.driver.find_elements_by_xpath(
"//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']"
)[2]
short_takeprofit_input_box = self.driver.find_elements_by_xpath(
"//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']"
)[3]
long_stoploss_input_box.send_keys(Keys.BACK_SPACE * 8)
long_stoploss_input_box.send_keys(str(long_stoploss_value))
long_takeprofit_input_box.send_keys(Keys.BACK_SPACE * 8)
long_takeprofit_input_box.send_keys(str(long_takeprofit_value))
short_stoploss_input_box.send_keys(Keys.BACK_SPACE * 8)
short_stoploss_input_box.send_keys(str(short_stoploss_value))
short_takeprofit_input_box.send_keys(Keys.BACK_SPACE * 8)
short_takeprofit_input_box.send_keys(str(short_takeprofit_value))
short_takeprofit_input_box.send_keys(Keys.ENTER)
time.sleep(0.5)
ok_button = self.driver.find_element_by_name("submit")
ok_button.click()
def click_input_tab(self):
"""making sure the input tab is clicked."""
try:
input_tab = self.driver.find_elements_by_xpath(
"//*[@class='tab-1KEqJy8_ withHover-1KEqJy8_ tab-3I2ohC86']"
)[0]
if input_tab.get_attribute("data-value") == "inputs":
input_tab.click()
except IndexError:
pass
def click_ok_button(self):
time.sleep(0.5)
ok_button = self.driver.find_element_by_name("submit")
ok_button.click()
def click_enable_both_checkboxes(self):
long_checkbox = self.driver.find_elements_by_xpath(
"//*[@class='input-24iGIobO']")[0]
short_checkbox = self.driver.find_elements_by_xpath(
"//*[@class='input-24iGIobO']")[1]
if not long_checkbox.get_attribute("checked"):
click_long_checkbox = self.driver.find_elements_by_xpath(
"//*[@class='box-3574HVnv check-382c8Fu1']"
)[0]
click_long_checkbox.click()
if not short_checkbox.get_attribute("checked"):
click_short_checkbox = self.driver.find_elements_by_xpath(
"//*[@class='box-3574HVnv check-382c8Fu1']"
)[1]
click_short_checkbox.click()
def click_enable_long_strategy_checkbox(self):
long_checkbox = self.driver.find_elements_by_xpath(
"//*[@class='input-24iGIobO']")[0]
short_checkbox = self.driver.find_elements_by_xpath(
"//*[@class='input-24iGIobO']")[1]
if not long_checkbox.get_attribute("checked"):
click_long_checkbox = self.driver.find_elements_by_xpath(
"//*[@class='box-3574HVnv check-382c8Fu1']"
)[0]
click_long_checkbox.click()
if short_checkbox.get_attribute("checked"):
click_short_checkbox = self.driver.find_elements_by_xpath(
"//*[@class='box-3574HVnv check-382c8Fu1']"
)[1]
click_short_checkbox.click()
def click_enable_short_strategy_checkbox(self):
long_checkbox = self.driver.find_elements_by_xpath(
"//*[@class='input-24iGIobO']")[0]
short_checkbox = self.driver.find_elements_by_xpath(
"//*[@class='input-24iGIobO']")[1]
if long_checkbox.get_attribute("checked"):
click_long_checkbox = self.driver.find_elements_by_xpath(
"//*[@class='box-3574HVnv check-382c8Fu1']"
)[0]
click_long_checkbox.click()
if not short_checkbox.get_attribute("checked"):
click_short_checkbox = self.driver.find_elements_by_xpath(
"//*[@class='box-3574HVnv check-382c8Fu1']"
)[1]
click_short_checkbox.click()
def click_rest_all_inputs(self):
"""click short stoploss input."""
long_stoploss_input_box = self.driver.find_elements_by_xpath(
"//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']"
)[0]
long_takeprofit_input_box = self.driver.find_elements_by_xpath(
"//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']"
)[1]
short_stoploss_input_box = self.driver.find_elements_by_xpath(
"//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']"
)[2]
short_takeprofit_input_box = self.driver.find_elements_by_xpath(
"//*[@class='input-3bEGcMc9 with-end-slot-S5RrC8PC']"
)[3]
long_stoploss_input_box.send_keys(Keys.BACK_SPACE * 8)
long_stoploss_input_box.send_keys(str("50"))
long_takeprofit_input_box.send_keys(Keys.BACK_SPACE * 8)
long_takeprofit_input_box.send_keys(str("50"))
short_stoploss_input_box.send_keys(Keys.BACK_SPACE * 8)
short_stoploss_input_box.send_keys(str("50"))
short_takeprofit_input_box.send_keys(Keys.BACK_SPACE * 8)
short_takeprofit_input_box.send_keys(str("50"))
short_takeprofit_input_box.send_keys(Keys.ENTER)
# Get Functions
def get_net_all(
self,
long_stoploss_value,
long_takeprofit_value,
short_stoploss_value,
short_takeprofit_value,
wait,
):
wait.until(EC.visibility_of_element_located(
(By.CLASS_NAME, "additional_percent_value")))
try:
check = self.driver.find_elements_by_class_name(
"additional_percent_value")[0]
check.find_element_by_xpath('./span[contains(@class, "neg")]')
negative = True
except NoSuchElementException:
negative = False
if negative:
net_profit = self.driver.find_elements_by_class_name(
"additional_percent_value"
)[0].text.split(" %")
net_value = -float(net_profit[0])
profits.update(
{
-net_value: [
"Long Stoploss:",
long_stoploss_value,
"Long Take Profit:",
long_takeprofit_value,
"Short Stoploss:",
short_stoploss_value,
"Short Take Profit:",
short_takeprofit_value,
]
}
)
print(
colored(
f"Net Profit: -{net_value}% --> Long Stoploss: {long_stoploss_value}, Long Take Profit: {long_takeprofit_value}, Short Stoploss: {short_stoploss_value}, Short Take Profit: {short_takeprofit_value}",
"red",
)
)
else:
net_profit = self.driver.find_elements_by_class_name(
"additional_percent_value"
)[0].text.split(" %")
net_value = float(net_profit[0])
profits.update(
{
net_value: [
"Long Stoploss:",
long_stoploss_value,
"Long Take Profit:",
long_takeprofit_value,
"Short Stoploss:",
short_stoploss_value,
"Short Take Profit:",
short_takeprofit_value,
]
}
)
print(
colored(
f"Net Profit: {net_value}% --> Long Stoploss: {long_stoploss_value}, Long Take Profit: {long_takeprofit_value}, Short Stoploss: {short_stoploss_value}, Short Take Profit: {short_takeprofit_value}",
"green",
)
)
return net_profit
def get_net_both(self, stoploss_value, takeprofit_value, wait):
wait.until(EC.visibility_of_element_located(
(By.CLASS_NAME, "additional_percent_value")))
try:
time.sleep(0.5)
check = self.driver.find_elements_by_class_name(
"additional_percent_value")[0]
check.find_element_by_xpath('./span[contains(@class, "neg")]')
negative = True
except NoSuchElementException:
negative = False
if negative:
net_profit = self.driver.find_elements_by_class_name(
"additional_percent_value"
)[0].text.split(" %")
net_value = -float(net_profit[0])
profits.update(
{
-net_value: [
"Stoploss:",
stoploss_value,
"Take Profit:",
takeprofit_value,
]
}
)
print(
colored(
f"Net Profit: -{net_value}% --> Stoploss: {stoploss_value}, Take Profit: {takeprofit_value}",
"red",
))
else:
net_profit = self.driver.find_elements_by_class_name(
"additional_percent_value"
)[0].text.split(" %")
net_value = float(net_profit[0])
profits.update(
{net_value: ["Stoploss:", stoploss_value, "Take Profit:", takeprofit_value]}
)
print(
colored(
f"Net Profit: {net_value}% --> Stoploss: {stoploss_value}, Take Profit: {takeprofit_value}",
"green",
))
return net_profit
def get_net_profit_stoploss(self, count, wait):
wait.until(EC.visibility_of_element_located(
(By.CLASS_NAME, "additional_percent_value")))
try:
time.sleep(0.5)
check = self.driver.find_elements_by_class_name(
"additional_percent_value")[0]
check.find_element_by_xpath('./span[contains(@class, "neg")]')
negative = True
except NoSuchElementException:
negative = False
if negative:
net_profit = self.driver.find_elements_by_class_name(
"additional_percent_value"
)[0].text.split(" %")
net_value = -float(net_profit[0])
profits.update({count: -net_value})
print(
colored(
f"Stoploss: {count}%, Net Profit: {net_value}%",
"red"))
else:
net_profit = self.driver.find_elements_by_class_name(
"additional_percent_value"
)[0].text.split(" %")
net_value = float(net_profit[0])
profits.update({count: net_value})
print(
colored(
f"Stoploss: {count}%, Net Profit: {net_value}%",
"green"))
return net_profit
def get_net_profit_takeprofit(self, count, wait):
try:
wait.until(
EC.visibility_of_element_located(
(By.CLASS_NAME, "additional_percent_value")
)
)
time.sleep(0.5)
check = self.driver.find_elements_by_class_name(
"additional_percent_value")[0]
check.find_element_by_xpath('./span[contains(@class, "neg")]')
negative = True
except (NoSuchElementException, IndexError):
negative = False
if negative:
net_profit = self.driver.find_elements_by_class_name(
"additional_percent_value"
)[0].text.split(" %")
net_value = -float(net_profit[0])
profits.update({count: -net_value})
print(
colored(
f"Take Profit: {count}%, Net Profit: {net_value}%",
"red"))
else:
net_profit = self.driver.find_elements_by_class_name(
"additional_percent_value"
)[0].text.split(" %")
net_value = float(net_profit[0])
profits.update({count: net_value})
print(
colored(
f"Take Profit: {count}%, Net Profit: {net_value}%",
"green"))
return net_profit
def get_win_rate(self, count, wait):
wait.until(EC.visibility_of_element_located(
(By.CLASS_NAME, "additional_percent_value")))
try:
win_rate = self.driver.find_elements_by_class_name(
"additional_percent_value")[1]
win_rate.find_element_by_xpath('./span[contains(@class, "neg")]')
negative = True
except NoSuchElementException:
negative = False
if negative:
win_rate = self.driver.find_elements_by_class_name(
"additional_percent_value")[1].text.split(" %")
net_value = float(win_rate[0])
profits.update({count: -net_value})
negative_color = {count: net_value}
print(colored(f"{negative_color}", "red"))
else:
win_rate = self.driver.find_elements_by_class_name(
"additional_percent_value")[1].text.split(" %")
net_value = float(win_rate[0])
profits.update({count: net_value})
positive_color = {count: net_value}
print(colored(f"{positive_color}", "green"))
return win_rate
# Show Me Functions
def print_best_stoploss(self):
try:
best_stoploss = max(profits, key=profits.get)
max_percentage = profits[best_stoploss]
if max_percentage > 0:
profitable = colored(str(best_stoploss) + " %", 'green')
print(f"Best Stoploss: " + str(profitable))
else:
profitable = colored(str(best_stoploss) + " %", 'red')
print(f"Best Stoploss: " + str(profitable))
except (UnboundLocalError, ValueError):
print("error printing stoploss.")
def print_best_takeprofit(self):
try:
best_takeprofit = max(profits, key=profits.get)
max_percentage = profits[best_takeprofit]
if max_percentage > 0:
profitable = colored(str(best_takeprofit) + " %", 'green')
print(f"Best Take Profit: " + str(profitable))
else:
profitable = colored(str(best_takeprofit) + " %", 'red')
print(f"Best Take Profit: " + str(profitable))
except (UnboundLocalError, ValueError):
print("error printing take profit.")
def print_best_both(self):
try:
best_key = self.find_best_key_both()
best_stoploss = profits[best_key][1]
best_takeprofit = profits[best_key][3]
print(f"Best Stop Loss: {best_stoploss}")
print(f"Best Take Profit: {best_takeprofit}\n")
except (UnboundLocalError, ValueError):
print("error printing stoploss.")
def print_best_all(self):
try:
best_key = self.find_best_key_both()
best_long_stoploss = profits[best_key][1]
best_long_takeprofit = profits[best_key][3]
best_short_stoploss = profits[best_key][5]
best_short_takeprofit = profits[best_key][7]
print(f"Best Long Stop Loss: {best_long_stoploss}")
print(f"Best Long Take Profit: {best_long_takeprofit}")
print(f"Best Short Stop Loss: {best_short_stoploss}")
print(f"Best Short Take Profit: {best_short_takeprofit}\n")
except (UnboundLocalError, ValueError):
print("error printing stoploss.")
def print_net_profit(self):
net_profit = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[0]
.find_element_by_class_name("additional_percent_value")
)
try:
negative = net_profit.find_element_by_class_name("neg")
if negative:
display = colored(f"{net_profit.text}", "red")
print(f"Net Profit: {display}")
except NoSuchElementException:
display = colored(f"{net_profit.text}", "green")
print(f"Net Profit: {display}")
def print_gross_profit(self):
gross_profit = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[1]
.find_element_by_class_name("additional_percent_value")
)
try:
negative = gross_profit.find_element_by_class_name("neg")
if negative:
display = colored(f"{gross_profit.text}", "red")
print(f"Gross Profit: {display}")
except NoSuchElementException:
display = colored(f"{gross_profit.text}", "green")
print(f"Gross Profit: {display}")
def print_gross_loss(self):
gross_loss = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[2]
.find_element_by_class_name("additional_percent_value")
)
try:
negative = gross_loss.find_element_by_class_name("neg")
if negative:
display = colored(f"{gross_loss.text}", "red")
print(f"Gross Loss: {display}")
except NoSuchElementException:
display = colored(f"{gross_loss.text}", "green")
print(f"Gross Loss: {display}")
def print_max_drawdown(self):
max_drawdown = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[3]
.find_element_by_class_name("additional_percent_value")
)
try:
negative = max_drawdown.find_element_by_class_name("neg")
if negative:
display = colored(f'{max_drawdown.text}', 'red')
print(f'Max Drawdown: {display}')
except NoSuchElementException:
display = colored(f'{max_drawdown.text}', 'green')
print(f'Max Drawdown: {display}')
def print_buy_and_hold_return(self):
buy_and_hold_return = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[4]
.find_element_by_class_name("additional_percent_value")
)
try:
negative = buy_and_hold_return.find_element_by_class_name("neg")
if negative:
display = colored(f'{buy_and_hold_return.text}', 'red')
print(f'Buy & Hold Return: {display}')
except NoSuchElementException:
display = colored(f'{buy_and_hold_return.text}', 'green')
print(f'Buy & Hold Return: {display}')
def print_sharpe_ratio(self):
try:
negative = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[5]
.find_elements_by_tag_name("td")[1]
.find_element_by_class_name("neg")
)
if negative:
display = colored(f"{negative.text}", "red")
print(f"Sharpe Ratio: {display}")
except NoSuchElementException:
sharpe_ratio = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[5]
.find_elements_by_tag_name("td")[1]
)
display = colored(f"{sharpe_ratio.text}", "green")
print(f"Sharpe Ratio: {display}")
def print_sortino_ratio(self):
try:
negative = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[6]
.find_elements_by_tag_name("td")[1]
.find_element_by_class_name("neg")
)
if negative:
display = colored(f"{negative.text}", "red")
print(f"Sortino Ratio: {display}")
except NoSuchElementException:
sortino_ratio = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[6]
.find_elements_by_tag_name("td")[1]
)
display = colored(f"{sortino_ratio.text}", "green")
print(f"Sortino Ratio: {display}")
def print_profit_factor(self):
profit_factor = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[7]
.find_elements_by_tag_name("td")[1]
)
try:
negative = profit_factor.find_element_by_class_name("neg")
if negative:
display = colored(f"{profit_factor.text}", "red")
print(f"Profit Factor: {display}")
except NoSuchElementException:
display = colored(f"{profit_factor.text}", "green")
print(f"Profit Factor: {display}")
def print_max_contracts_held(self):
max_contracts_held = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[8]
.find_elements_by_tag_name("td")[1]
)
try:
negative = max_contracts_held.find_element_by_class_name("neg")
if negative:
display = colored(f"{max_contracts_held.text}", "red")
print(f"Max Contracts Held: {display}")
except NoSuchElementException:
display = colored(f"{max_contracts_held.text}", "green")
print(f"Max Contracts Held: {display}")
def print_open_pl(self):
open_pl = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[9]
.find_element_by_class_name("additional_percent_value")
)
try:
negative = open_pl.find_element_by_class_name("neg")
if negative:
display = colored(f"{open_pl.text}", "red")
print(f"Open PL: {display}")
except NoSuchElementException:
display = colored(f"{open_pl.text}", "green")
print(f"Open PL: {display}")
def print_commission_paid(self):
commission_paid = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[10]
.find_elements_by_tag_name("td")[1]
)
print(f"Commission Paid: {commission_paid.text}")
def print_total_closed_trades(self):
total_closed_trades = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[11]
.find_elements_by_tag_name("td")[1]
)
print(f"Total Closed Trades: {total_closed_trades.text}")
def print_total_open_trades(self):
total_open_trades = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[12]
.find_elements_by_tag_name("td")[1]
)
print(f"Total Open Trades: {total_open_trades.text}")
def print_number_winning_trades(self):
number_winning_trades = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[13]
.find_elements_by_tag_name("td")[1]
)
print(f"Number Winning Trades: {number_winning_trades.text}")
def print_number_losing_trades(self):
number_losing_trades = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[14]
.find_elements_by_tag_name("td")[1]
)
print(f"Number Losing Trades: {number_losing_trades.text}")
def print_percent_profitable(self):
percent_profitable = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[15]
.find_elements_by_tag_name("td")[1]
)
print(f"Percent Profitable: {percent_profitable.text}")
def print_avg_trade(self):
avg_trade = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[16]
.find_element_by_class_name("additional_percent_value")
)
try:
negative = avg_trade.find_element_by_class_name("neg")
if negative:
display = colored(f"{avg_trade.text}", "red")
print(f"Avg Trade: {display}")
except NoSuchElementException:
display = colored(f"{avg_trade.text}", "green")
print(f"Avg Trade: {display}")
def print_avg_win_trade(self):
try:
negative = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[17]
.find_element_by_class_name("additional_percent_value")
.find_element_by_class_name("neg")
)
if negative:
display = colored(f"{negative.text}", "red")
print(f"Avg Win Trade: {display}")
except NoSuchElementException:
avg_win_trade = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[17]
.find_element_by_class_name("additional_percent_value")
)
display = colored(f"{avg_win_trade.text}", "green")
print(f"Avg Win Trade: {display}")
def print_avg_loss_trade(self):
avg_loss_trade = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[18]
.find_element_by_class_name("additional_percent_value")
)
try:
negative = avg_loss_trade.find_element_by_class_name("neg")
if negative:
display = colored(f"{avg_loss_trade.text}", "red")
print(f"Avg Loss Trade: {display}")
except NoSuchElementException:
display = colored(f"{avg_loss_trade.text}", "green")
print(f"Avg Loss Trade: {display}")
def print_win_loss_ratio(self):
win_loss_ratio = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[19]
.find_elements_by_tag_name("td")[1]
)
print(f"Win/Loss Ratio: {win_loss_ratio.text}")
def print_largest_winning_trade(self):
largest_winning_trade = (
self.driver.find_element_by_class_name("report-data")
.find_element_by_tag_name("table")
.find_element_by_tag_name("tbody")
.find_elements_by_tag_name("tr")[20]
.find_element_by_class_name("additional_percent_value")
)
try:
negative = largest_winning_trade.find_element_by_class_name("neg")
if negative:
display = colored(f"{largest_winning_trade.text}", "red")
print(f"Largest Win Trade: {display}")
except NoSuchElementException:
display = colored(f"{largest_winning_trade.text}", "green")
print(f"Largest Win Trade: {display}")