Skip to content

Commit

Permalink
bugs fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkataei committed Oct 3, 2021
1 parent 55623a4 commit 1e0b00b
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 123 deletions.
6 changes: 3 additions & 3 deletions Analysis/diamond.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
}


def signal(data: pd.DataFrame, gain: float, cost: float, coin_id: int, timeframe_id: int, setting: dict):
def signal(data: pd.DataFrame, gain: float, cost: float, coin_id: int, timeframe_id: int, setting: dict, bot_ins):
diamond_tools = Tools(analysis_id=3, timeframe_id=timeframe_id, coin_id=coin_id)

if coin_id in valid_coins_and_times['coins'] \
Expand Down Expand Up @@ -128,7 +128,7 @@ def signal(data: pd.DataFrame, gain: float, cost: float, coin_id: int, timeframe
else:
result = True, "high"
# add signal to database
diamond_tools.signal_process(close=close, gain=gain, result=result, cost=cost)
diamond_tools.signal_process(close=close, gain=gain, result=result, cost=cost, bot_ins=bot_ins)

sell_counter = 0
if old_position == "buy" and old_price < close:
Expand All @@ -151,4 +151,4 @@ def signal(data: pd.DataFrame, gain: float, cost: float, coin_id: int, timeframe
result = False, "medium"
else:
result = False, "high"
diamond_tools.signal_process(close=close, gain=gain, result=result, cost=cost)
diamond_tools.signal_process(close=close, gain=gain, result=result, cost=cost, bot_ins=bot_ins)
18 changes: 9 additions & 9 deletions Analysis/emerald.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import pandas as pd
import pandas_ta as ta
import numpy as np
from Inc import functions
from Inc.functions import get_recommendations, set_recommendation
from Telegram.Client.message import broadcast_messages


Expand All @@ -34,7 +34,7 @@ def get_future_spans():
return ichimoku, get_future_spans()


def signal(data: pd.DataFrame, gain: float, cost: float, coin_id: int, timeframe_id: int, setting: dict):
def signal(data: pd.DataFrame, gain: float, cost: float, coin_id: int, timeframe_id: int, setting: dict, bot_ins):
tenkan = setting['indicators_setting']['ichimoku']['tenkan']
kijun = setting['indicators_setting']['ichimoku']['kijun']
senkou = setting['indicators_setting']['ichimoku']['senkou']
Expand All @@ -48,7 +48,7 @@ def signal(data: pd.DataFrame, gain: float, cost: float, coin_id: int, timeframe
last_ichimoku = np.array(ichimoku.tail(1))[0].astype(float)
close = float(last_ichimoku[5])
try:
query = functions.get_recommendations(analysis_id=1, timeframe_id=timeframe_id, coin_id=coin_id)
query = get_recommendations(analysis_id=1, timeframe_id=timeframe_id, coin_id=coin_id)
old_position = query[0][2]
old_risk = query[0][7]
# when no rows in database
Expand Down Expand Up @@ -92,11 +92,11 @@ def check():
target_price = close * gain + close if result[0] else -close * gain + close
position = 'buy' if result[0] else 'sell'
if old_position != position or old_risk != result[1]:
functions.set_recommendation(analysis_id=1, coin_id=coin_id, timeframe_id=timeframe_id, position=position,
set_recommendation(analysis_id=1, coin_id=coin_id, timeframe_id=timeframe_id, position=position,
target_price=target_price, current_price=close, cost_price=cost, risk=result[1])
broadcast_messages(coin_id=coin_id, analysis_id=1, timeframe_id=timeframe_id, position=position,
target_price=target_price, current_price=close, risk=result[1])
# for transaction in future
# users = functions.get_user_recommendation(connection, coin_id=coin_id, analysis_id=1, timeframe_id=timeframe_id)
# for user in users:
# functions.pay_transaction(db_connection=connection ,cost_price=cost ,username=user ,detail="")
target_price=target_price, current_price=close, risk=result[1], bot_ins=bot_ins)
# for transaction in future
# users = functions.get_user_recommendation(connection, coin_id=coin_id, analysis_id=1, timeframe_id=timeframe_id)
# for user in users:
# functions.pay_transaction(db_connection=connection ,cost_price=cost ,username=user ,detail="")
7 changes: 2 additions & 5 deletions Analysis/ruby.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
from Telegram.Client.message import broadcast_messages


# from Telegram import message


def cross_over(x, y):
return True if x[0] < y < x[1] else False

Expand Down Expand Up @@ -52,7 +49,7 @@ def signal(data: pd.DataFrame, gain: float, cost: float, coin_id: int, timeframe
functions.set_recommendation(analysis_id=2, coin_id=coin_id, timeframe_id=timeframe_id, position=position,
target_price=target_price, current_price=close, cost_price=cost, risk=result[1])
broadcast_messages(coin_id=coin_id, analysis_id=2, timeframe_id=timeframe_id, position=position,
target_price=target_price, current_price=close, risk=result[1])
target_price=target_price, current_price=close, risk=result[1], bot_ins=None)
elif float(last_macd[1, 1]) < np.array(macd_df.tail(delay)["histogram"])[0] and \
old_price < close and old_position == "buy":
result = False, "medium"
Expand All @@ -61,5 +58,5 @@ def signal(data: pd.DataFrame, gain: float, cost: float, coin_id: int, timeframe
functions.set_recommendation(analysis_id=2, coin_id=coin_id, timeframe_id=timeframe_id, position=position,
target_price=target_price, current_price=close, cost_price=cost, risk=result[1])
broadcast_messages(coin_id=coin_id, analysis_id=2, timeframe_id=timeframe_id, position=position,
target_price=target_price, current_price=close, risk=result[1])
target_price=target_price, current_price=close, risk=result[1], bot_ins=None)

55 changes: 36 additions & 19 deletions Analysis/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@
"""
import asyncio
import threading
from Analysis import emerald, diamond
import telebot
from Analysis.emerald import signal as emerald
from Analysis.diamond import signal as diamond
from Interfaces.stream import Stream, append

# master bot already run on vps dont use this @algowatchbot -> address
API_KEY = '1987308624:AAEow3hvRGt4w6ZFmz3bYaQz1J8p-OzRer0'
# @testkourosh2bot -> address // use this bot for test your code
# API_KEY = '1978536410:AAE_RMk3-4r_cLnt_nRcEnZHaSp-vIk9oVo'
_bot_ins = telebot.TeleBot(API_KEY)


class StreamIStrategies(Stream):
def __init__(self, symbol: str, cost: float = 1, gain: float = 0.003):
Expand All @@ -37,11 +45,11 @@ async def stream_30min_candle(self):
setting_emerald = self.get_setting_analysis(analysis_id=1, timeframe_id=1)
self.data_30min = append(data=self.data_30min.tail(100), symbol=self.symbol,
timeframe="30min", candle=c_30m_data)
emerald.signal(data=self.data_30min, gain=self.gain, cost=self.cost, coin_id=self.coin_id,
timeframe_id=1, setting=setting_emerald)
emerald(data=self.data_30min, gain=self.gain, cost=self.cost, coin_id=self.coin_id,
timeframe_id=1, setting=setting_emerald, bot_ins=_bot_ins)
setting_diamond = self.get_setting_analysis(analysis_id=3, timeframe_id=1)
diamond.signal(data=self.data_30min, gain=self.gain, cost=self.cost, coin_id=self.coin_id,
timeframe_id=1, setting=setting_diamond)
diamond(data=self.data_30min, gain=self.gain, cost=self.cost, coin_id=self.coin_id,
timeframe_id=1, setting=setting_diamond, bot_ins=_bot_ins)
await asyncio.sleep(1790)

async def stream_1hour_candle(self):
Expand All @@ -53,8 +61,8 @@ async def stream_1hour_candle(self):
setting_emerald = self.get_setting_analysis(analysis_id=1, timeframe_id=2)
self.data_1hour = append(data=self.data_1hour.tail(100), symbol=self.symbol,
timeframe="1hour", candle=c_1h_data)
emerald.signal(data=self.data_1hour, gain=self.gain, cost=self.cost, coin_id=self.coin_id,
timeframe_id=2, setting=setting_emerald)
emerald(data=self.data_1hour, gain=self.gain, cost=self.cost, coin_id=self.coin_id,
timeframe_id=2, setting=setting_emerald, bot_ins=_bot_ins)
await asyncio.sleep(3590)

async def stream_4hour_candle(self):
Expand All @@ -66,11 +74,11 @@ async def stream_4hour_candle(self):
setting_emerald = self.get_setting_analysis(analysis_id=1, timeframe_id=3)
self.data_4hour = append(data=self.data_4hour.tail(100), symbol=self.symbol,
timeframe="4hour", candle=c_4h_data)
emerald.signal(data=self.data_4hour, gain=self.gain, cost=self.cost, coin_id=self.coin_id,
timeframe_id=3, setting=setting_emerald)
emerald(data=self.data_4hour, gain=self.gain, cost=self.cost, coin_id=self.coin_id,
timeframe_id=3, setting=setting_emerald, bot_ins=_bot_ins)
setting_diamond = self.get_setting_analysis(analysis_id=3, timeframe_id=3)
diamond.signal(data=self.data_30min, gain=self.gain, cost=self.cost, coin_id=self.coin_id,
timeframe_id=3, setting=setting_diamond)
diamond(data=self.data_30min, gain=self.gain, cost=self.cost, coin_id=self.coin_id,
timeframe_id=3, setting=setting_diamond, bot_ins=_bot_ins)
await asyncio.sleep(14390)

async def stream_1day_candle(self):
Expand All @@ -82,8 +90,8 @@ async def stream_1day_candle(self):
setting_emerald = self.get_setting_analysis(analysis_id=1, timeframe_id=4)
self.data_1day = append(data=self.data_1day.tail(100), symbol=self.symbol,
timeframe="1day", candle=c_1d_data)
emerald.signal(data=self.data_1day, gain=self.gain, cost=self.cost, coin_id=self.coin_id,
timeframe_id=4, setting=setting_emerald)
emerald(data=self.data_1day, gain=self.gain, cost=self.cost, coin_id=self.coin_id,
timeframe_id=4, setting=setting_emerald, bot_ins=_bot_ins)
await asyncio.sleep(86390)

def set_cost(self, cost: float):
Expand All @@ -93,9 +101,18 @@ def set_gain(self, gain: float):
self.gain = gain


def run_ichimoku_threads(*symbols: str):
for symbol in symbols:
ichimoku_symbol = StreamIStrategies(symbol=symbol)
btcusdt_thread = threading.Thread(target=ichimoku_symbol.run)
btcusdt_thread.daemon = True
btcusdt_thread.start()
class StrategiesThreads:
def __init__(self, *symbols: str):
self.symbols = symbols
self.threads = []
for symbol in symbols:
self.threads.append(threading.Thread(target=StreamIStrategies(symbol=symbol).run))

def start_threads(self):
for thread in self.threads:
thread.daemon = True
thread.start()

def join_threads(self):
for thread in self.threads:
thread.join()
1 change: 0 additions & 1 deletion Inc/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,6 @@ def set_recommendation(analysis_id: int, coin_id: int, timeframe_id: int, positi
cursor = connection.cursor()
cursor.execute(sql, val)
connection.commit()
print('tamam')
except Error as err:
return "Something went wrong: {}".format(err)

Expand Down
25 changes: 0 additions & 25 deletions Interfaces/strategy.py
Original file line number Diff line number Diff line change
@@ -1,25 +0,0 @@
import pandas as pd
import pandas_ta as ta
import numpy as np
from Inc import db, functions
from Telegram.Client.message import broadcast_messages


class Strategy:
def __init__(self):
print("sa")

def signal(self, *args):
raise Exception("NotImplementedException")

def cross_over(self, x, y):
return True if x[0] < y < x[1] else False

def cross_under(self, x, y):
return True if x[0] > y > x[1] else False

def broadcast_and_insert_database(self):
functions.set_recommendation(analysis_id=1, coin_id=coin_id, timeframe_id=timeframe_id, position=position,
target_price=target_price, current_price=close, cost_price=cost, risk=result[1])
broadcast_messages(coin_id=coin_id, analysis_id=1, timeframe_id=timeframe_id, position=position,
target_price=target_price, current_price=close, risk=result[1])
8 changes: 4 additions & 4 deletions Interfaces/stream.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
from binance import AsyncClient, BinanceSocketManager
from Inc import functions
from Inc.functions import get_coin_id, get_analysis_setting
import pandas as pd


Expand Down Expand Up @@ -47,7 +47,7 @@ def append(data: pd.DataFrame, symbol: str, timeframe: str, candle):
class Stream:
def __init__(self, symbol: str):
self.symbol = symbol
self.coin_id = functions.get_coin_id(symbol)
self.coin_id = get_coin_id(symbol)
self.client = None
self.socket = None
self.data_30min = pd.read_csv(f'Static/{self.symbol}-30min.csv')
Expand Down Expand Up @@ -85,8 +85,8 @@ async def stream(self):
self.stream_1day_candle())

def get_setting_analysis(self, analysis_id: int, timeframe_id: int):
settings = functions.get_analysis_setting(coin_id=self.coin_id, timeframe_id=timeframe_id,
analysis_id=analysis_id)
settings = get_analysis_setting(coin_id=self.coin_id, timeframe_id=timeframe_id,
analysis_id=analysis_id)
return settings

def run(self):
Expand Down
12 changes: 6 additions & 6 deletions Libraries/tools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pandas as pd
import pandas_ta as ta
from Inc import functions
from Inc.functions import get_recommendations, set_recommendation
from Telegram.Client.message import broadcast_messages


Expand Down Expand Up @@ -36,7 +36,7 @@ def __init__(self, analysis_id: int, timeframe_id: int, coin_id: int):
self.coin_id = coin_id

def get_last_data(self, start_position: bool = None):
query = functions.get_recommendations(analysis_id=self.analysis_id, timeframe_id=self.timeframe_id,
query = get_recommendations(analysis_id=self.analysis_id, timeframe_id=self.timeframe_id,
coin_id=self.coin_id)
try:
old_position = query[0][2]
Expand All @@ -49,11 +49,11 @@ def get_last_data(self, start_position: bool = None):

return old_position, old_price

def signal_process(self, close: float, gain: float, result: tuple, cost: float):
def signal_process(self, close: float, gain: float, result: tuple, cost: float, bot_ins):
position = 'buy' if result[0] else 'sell'
tp = target_price(close=close, gain=gain, result=result)
functions.set_recommendation(analysis_id=self.analysis_id, coin_id=self.coin_id, timeframe_id=self.timeframe_id,
set_recommendation(analysis_id=self.analysis_id, coin_id=self.coin_id, timeframe_id=self.timeframe_id,
position=position, target_price=tp, current_price=close, cost_price=cost,
risk=result[1])
broadcast_messages(analysis_id=self.analysis_id, coin_id=self.coin_id, current_price=close,
target_price=tp, risk=result[1], position=position, timeframe_id=self.timeframe_id)
broadcast_messages(coin_id=self.coin_id, analysis_id=self.analysis_id, timeframe_id=self.timeframe_id,
position=position, target_price=tp, current_price=close, risk=result[1], bot_ins=bot_ins)
9 changes: 7 additions & 2 deletions Telegram/Admin/tele.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
API_KEY = '1987746421:AAFjiQ22yuRXhzYOrRkVmeuuHM96sD4aqpA'
# test bot fro admin
# API_KEY = '1991184876:AAGfWUbxXEbnbWHeKrlh2knooi8lF1PSWKI'
# algowatch original API
API_KEY_MESSAGE = '1987308624:AAEow3hvRGt4w6ZFmz3bYaQz1J8p-OzRer0'
# test API
# API_KEY_MESSAGE = '1978536410:AAE_RMk3-4r_cLnt_nRcEnZHaSp-vIk9oVo'
_bot_ins = telebot.TeleBot(API_KEY_MESSAGE)


class AdminBot(Telegram):
Expand Down Expand Up @@ -53,7 +58,7 @@ def query_handler(call):
msg = user.temp
if str(call.data).split('_')[2] == "yes":
chat_ids = np.array(functions.get_chat_ids())
admin_broadcast(msg, chat_ids)
admin_broadcast(message=msg, chat_ids=chat_ids, bot_ins=_bot_ins)
self.bot.reply_to(call.message, "Done!\nyour message send to all")
else:
self.bot.reply_to(call.message, "Deleted, try again /broadcast")
Expand Down Expand Up @@ -98,7 +103,7 @@ def process_message_to_user_step2(message):
user = self.user_dict[message.chat.id]
try:
chat_id = functions.get_user_chat_id(user.temp)
admin_send_message(message=message.text, chat_id=chat_id)
admin_send_message(message=message.text, chat_id=chat_id, bot_ins=_bot_ins)
except Exception as e:
self.bot.reply_to(message, e)

Expand Down
36 changes: 18 additions & 18 deletions Telegram/Client/candle.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,34 @@
import pandas as pd
from Libraries.definitions import *

client = Client()
_client = Client()


def get_interval_client(timeframe: str):
return {
'1min': client.KLINE_INTERVAL_1MINUTE,
'3min': client.KLINE_INTERVAL_3MINUTE,
'5min': client.KLINE_INTERVAL_5MINUTE,
'15min': client.KLINE_INTERVAL_15MINUTE,
'30min': client.KLINE_INTERVAL_30MINUTE,
'1hour': client.KLINE_INTERVAL_1HOUR,
'2hour': client.KLINE_INTERVAL_2HOUR,
'4hour': client.KLINE_INTERVAL_4HOUR,
'6hour': client.KLINE_INTERVAL_6HOUR,
'8hour': client.KLINE_INTERVAL_8HOUR,
'12hour': client.KLINE_INTERVAL_12HOUR,
'1day': client.KLINE_INTERVAL_1DAY,
'3day': client.KLINE_INTERVAL_3DAY,
'weekly': client.KLINE_INTERVAL_1WEEK,
'monthly': client.KLINE_INTERVAL_1MONTH,
'1min': _client.KLINE_INTERVAL_1MINUTE,
'3min': _client.KLINE_INTERVAL_3MINUTE,
'5min': _client.KLINE_INTERVAL_5MINUTE,
'15min': _client.KLINE_INTERVAL_15MINUTE,
'30min': _client.KLINE_INTERVAL_30MINUTE,
'1hour': _client.KLINE_INTERVAL_1HOUR,
'2hour': _client.KLINE_INTERVAL_2HOUR,
'4hour': _client.KLINE_INTERVAL_4HOUR,
'6hour': _client.KLINE_INTERVAL_6HOUR,
'8hour': _client.KLINE_INTERVAL_8HOUR,
'12hour': _client.KLINE_INTERVAL_12HOUR,
'1day': _client.KLINE_INTERVAL_1DAY,
'3day': _client.KLINE_INTERVAL_3DAY,
'weekly': _client.KLINE_INTERVAL_1WEEK,
'monthly': _client.KLINE_INTERVAL_1MONTH,

}.get(timeframe, client.KLINE_INTERVAL_1MINUTE)
}.get(timeframe, _client.KLINE_INTERVAL_1MINUTE)


def get_candle_details(symbol: str, timeframe: str):
symbol = symbol.upper()
timeframe = get_interval_client(timeframe)
data = pd.DataFrame(client.get_klines(symbol=symbol, interval=timeframe, limit=1)).values
data = pd.DataFrame(_client.get_klines(symbol=symbol, interval=timeframe, limit=1)).values
return data


Expand Down
Loading

0 comments on commit 1e0b00b

Please sign in to comment.