-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
129 lines (112 loc) · 5.01 KB
/
main.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
from binance.client import Client
import silence_tensorflow.auto
from binanceAPI.position_utilities import enter_long, enter_short
from config import api_key, secret_key
from indicators.fetch_all_indicators import fetch_all_indicators
from data.io_utilities import print_with_color, calculateWR, print_position_message
from tensorflow_utilities.tensor_model import TensorModel
from time import sleep
from data.data_functions import save_position, save_result
import copy
# BinanceAPI Connection
client = Client(api_key, secret_key)
# csv path initialization
csv_path_position = "./data/mera_dataset.csv"
csv_path_result = "./data/mera_result.csv"
indicator_position = None
indicator_check = None
tp_count = 0
sl_count = 0
tp_price = 0
sl_price = 0
do_not_enter_long = False
do_not_enter_short = False
on_long = False
on_short = False
prediction = None
# Global Functions
def close_position(isTP):
global on_long
global on_short
global tp_count
global sl_count
global indicator_position
global prediction
global csv_path_position
global csv_path_result
state = ""
if (on_long and isTP) or (on_short and (not isTP)):
state = "LONG"
elif (on_long and (not isTP)) or (on_short and isTP):
state = "SHORT"
save_position(csv_path_position, state, indicator_position)
save_result(csv_path_result, indicator_position.date, state, prediction)
on_long = False
on_short = False
if isTP:
tp_count = tp_count + 1
print_with_color("green", "Position closed with TP")
print_with_color("yellow", "TP: " + str(tp_count) + " SL: " +
str(sl_count) + " Win-Rate: " + calculateWR(tp_count, sl_count))
else:
sl_count = sl_count + 1
print_with_color("red", "Position closed with SL")
print_with_color("yellow", "TP: " + str(tp_count) + " SL: " +
str(sl_count) + " Win-Rate: " + calculateWR(tp_count, sl_count))
print_with_color("cyan", "MeraBot is running...")
# initialization
indicator_check = fetch_all_indicators(client)
if (indicator_check.price < indicator_check.ema_100):
do_not_enter_long = True
print_with_color("yellow", "LONG BLOCKED")
else:
do_not_enter_short = True
print_with_color("yellow", "SHORT BLOCKED")
while True:
try:
sleep(10)
indicator_check = fetch_all_indicators(client)
if not (on_long or on_short):
if (not do_not_enter_long) and (indicator_check.macd_12 > indicator_check.macd_26) and \
(indicator_check.macd_12 < 0) and (indicator_check.rsi_6 > 50) and \
(indicator_check.price < indicator_check.ema_100):
do_not_enter_short = False
do_not_enter_long = True
indicator_position = indicator_check
accuracy, prediction = TensorModel(csv_path_position).process_model(indicator_check)
if prediction == "LONG":
on_long = True
indicator_position = copy.deepcopy(indicator_check)
tp_price, sl_price = enter_long(client)
print_with_color("yellow", "Entered " + prediction + " Current: " +
str(round(indicator_position.price, 2)) + " TP_PRICE: " + str(round(tp_price, 2)) +
" SL_PRICE: " + str(round(sl_price, 2)))
print_position_message(indicator_position, prediction)
else:
print_with_color("yellow", "LONG is Blocked")
elif (not do_not_enter_short) and (indicator_check.macd_12 < indicator_check.macd_26) and \
(indicator_check.macd_12 > 0) and (indicator_check.rsi_6 < 50) and \
(indicator_check.price > indicator_check.ema_100):
do_not_enter_long = False
do_not_enter_short = True
accuracy, prediction = TensorModel(csv_path_position).process_model(indicator_check)
if prediction == "SHORT":
on_short = True
indicator_position = copy.deepcopy(indicator_check)
tp_price, sl_price = enter_short(client)
print_with_color("yellow", "Entered " + prediction + " Current: " +
str(round(indicator_position.price, 2)) + " TP_PRICE: " + str(round(tp_price, 2)) +
" SL_PRICE: " + str(round(sl_price, 2)))
print_position_message(indicator_position, prediction)
else:
print_with_color("yellow", "SHORT is Blocked")
else:
if (on_long and indicator_check.price > tp_price) or \
(on_short and indicator_check.price < tp_price):
close_position(True)
elif (on_long and indicator_check.price < sl_price) or \
(on_short and indicator_check.price > sl_price):
close_position(False)
except Exception as e:
error_message = str(e)
print_with_color("yellow", error_message)