-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
157 lines (129 loc) · 5.46 KB
/
bot.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
import time
import logging
import json
import random
import configuration as config
from statistics import median, mean
from iqoptionapi.stable_api import IQ_Option
from stream import Stream
from threading import Thread
from graphic_analysis import GraphicAnalysis
from strategies.dark_cloud import DarkCloud
from collections import namedtuple
from candle import Candle
from strategies.hammer import Hammer
from strategies.only_otc import Otc100
from strategies.first_otc import FirstOtc
from strategies.second_otc import SecondOtc
from candle_chart import CandleChart
from strategies.simple_rsi import SimpleRSI
from strategies.rsi_stoch import RSIStoch
from strategies.patter_m1 import PatternM1
from risk_management.b import B
import configuration as config
import math
from promise import Promise
from active import Active
from api.buy import Buy
from api.dispacher import Dispacher
from api.operation_info import OperationInfo
from api.list_info_data import ListInfoData
from api.timesync import TimeSync
from risk_management.matingale import Martingale
class Bot(Thread):
def __init__(self, profile, api, dispacher: Dispacher, timesync: TimeSync, active: Active, check_in_period=.2):
self.buy = Buy(api.api, dispacher, timesync)
self.operation_info = OperationInfo(api.api, dispacher, timesync)
self.list_info_data = ListInfoData(api.api, dispacher, timesync)
self.profile = profile
self.api = api
self.active = active
self.candle_size = config.CANDLE_SIZE
self.max_candles = config.MAX_CANDLES
self.check_in_period = check_in_period
# self.risk_management: B = B()
self.risk_management: Martingale = Martingale()
self.do_stop = False
self.check_time = time.time()
self.id_number = None
self.last_buy = 0
# self.chart = CandleChart(active.name, 10)
self.graphic_analysis = GraphicAnalysis()
# dark_cloud = DarkCloud(active, self.graphic_analysis)
# hammer = Hammer(active, self.graphic_analysis)
# otc100 = Otc100(api, active, self.graphic_analysis)
first_otc = FirstOtc(api, active, self.graphic_analysis)
# simple_rsi = SimpleRSI(api, active, self.graphic_analysis)
# second_otc = SecondOtc(api, active, self.graphic_analysis)
# rsi_stoch = RSIStoch(api, active, self.graphic_analysis)
# petternM1 = PatternM1(api, active, self.graphic_analysis)
self.strategies = []
# self.strategies.append(dark_cloud)
# self.strategies.append(hammer)
# self.strategies.append(otc100)
self.strategies.append(first_otc)
# self.strategies.append(second_otc)
# self.strategies.append(simple_rsi)
# self.strategies.append(rsi_stoch)
# self.strategies.append(petternM1)
print('Robo {} criado.'.format(active.name))
super(Bot, self).__init__()
def run(self):
while not self.do_stop:
time.sleep(self.check_in_period)
if not self.active.enabled or not self.active.is_profitable():
self.stop()
else:
self.check()
self.stop_stream()
print('Stop Robot {}, {}'.format(self.active.name, self.active.enabled))
def check(self):
candles = self.get_candles()
# self.chart.setCandles(candles)
self.graphic_analysis.set_candles(candles)
probability = self.analize_strategies()
if(abs(probability) >= config.OPERATION_WHEN_WIN):
# balance = self.profile.get_balance()
self.profile.refresh_actives()
# operation_money = max(balance * config.OPERATION_MONEY, 1)
entry = self.risk_management.get_next_entry(self.active.profit)
money = max(config.OPERATION_MONEY * entry, 1)
action = 'call' if probability > 0 else 'put'
check, result = self.buy(money, self.active.code, action)
message = self.get_buy_message(check, action, money)
print(message)
if(check):
self.last_buy = time.time()
self.check_win()
def get_candles(self):
candles: list = self.api.get_realtime_candles(
self.active.name, self.candle_size)
return list(map(lambda key: Candle(candles[key]), candles))
def analize_strategies(self):
probabilities = list(map((lambda x: x.analize()), self.strategies))
probabilities = list(filter((lambda x: x != 0), probabilities))
if(len(probabilities) == 0):
return 0
else:
return mean(probabilities)
def get_buy_message(self, success, action, money):
message = '{} to buy {}, action {}, money ${}'
status = 'success' if success else 'failed'
return message.format(status, self.active.name, action, money)
def stop_stream(self):
try:
self.api.stop_candles_stream(self.active.name, config.CANDLE_SIZE)
except:
pass
def check_win(self):
result = self.list_info_data(self.buy.id)
if result != None:
if result == 'win':
self.risk_management.add_win()
elif result == 'loose':
self.risk_management.add_loose()
print('{} operation {}: {}'.format(result, self.active.name, self.buy.id))
else:
print('error {}({}) was not possible to verify status'.format(self.active.name, self.buy.id))
def stop(self):
self.do_stop = True