-
-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathOptimizeLongTakeprofit.py
112 lines (100 loc) · 3.92 KB
/
OptimizeLongTakeprofit.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
import time
import numpy as np
from web_commands.commands import Functions
from TradeViewGUI import Main
from selenium.common.exceptions import (
NoSuchElementException,
StaleElementReferenceException,
TimeoutException,
)
from selenium.webdriver.support.ui import WebDriverWait
class LongTakeProfit(Functions):
"""Find the best take profit values for your strategy."""
def __init__(self):
Main.__init__(self)
self.driver = self.create_driver()
self.run_script()
def run_script(self):
# Load the website with the web driver.
print("Loading script...\n")
wait = WebDriverWait(self.driver, 15)
self.get_webpage()
# Make sure the strategy tester tab is clicked so automation runs properly.
self.click_strategy_tester(wait)
self.click_overview(wait)
# Make sure we are on the inputs tab and reset values to default settings.
self.click_settings_button(wait)
self.click_input_tab()
self.click_enable_long_strategy_checkbox()
self.click_reset_all_inputs(wait)
self.click_ok_button()
try:
# Create a range variable.
my_range = np.arange(
float(self.minLongTakeprofitValue.text()),
float(self.maxLongTakeprofitValue.text()),
float(self.LongIncrementValue.text()),
)
# Increment through the range.
for number in my_range:
count = round(number, 2)
try:
self.click_settings_button(wait)
self.click_long_takeprofit_input(count, wait)
# Give time for the webpage to refresh data.
time.sleep(1)
self.get_net_profit_takeprofit(count, wait)
except (
StaleElementReferenceException,
TimeoutException,
NoSuchElementException,
) as error:
if error:
count -= 1
continue
except ValueError:
print(
"\nValue Error: Make sure all available text input boxes are filled with a number for the script to run properly.\n"
)
return
# Add the best parameters into your strategy.
self.click_settings_button(wait)
best_key = self.find_best_takeprofit()
self.click_long_takeprofit_input(best_key, wait)
self.driver.implicitly_wait(1)
# Print the results.
print("\n----------Results----------\n")
self.click_overview(wait)
self.print_best_takeprofit()
self.click_performance_summary(wait)
self.print_total_closed_trades()
self.print_win_rate()
self.print_net_profit()
self.print_max_drawdown()
self.print_sharpe_ratio()
self.print_sortino_ratio()
self.print_win_loss_ratio()
self.print_avg_win_trade()
self.print_avg_loss_trade()
self.print_avg_bars_in_winning_trades()
# Uncomment the following lines for additional results.
# print("\n----------More Results----------\n")
# self.print_gross_profit()
# self.print_gross_loss()
# self.print_buy_and_hold_return()
# self.print_max_contracts_held()
# self.print_open_pl()
# self.print_commission_paid()
# self.print_total_open_trades()
# self.print_number_winning_trades()
# self.print_number_losing_trades()
# self.print_percent_profitable()
# self.print_avg_trade()
# self.print_avg_win_trade()
# self.print_avg_loss_trade()
# self.print_largest_winning_trade()
# self.print_largest_losing_trade()
# self.print_avg_bars_in_trades()
# self.print_avg_bars_in_winning_trades()
# self.print_avg_bars_in_losing_trades()
# self.print_margin_calls()