def make_buy(request): payload = {} for item in request.query_string.split('&'): payload[item.split('=')[0]] = item.split('=')[1] print('PAYLOAD: ', payload) currency = payload['currency'] amount = float(payload['amount']) user_id = payload['user_id'] timeframe = int(payload['timeframe']) porog = float(payload['porog']) ma_small_value = int(payload['ma_small_value']) ma_medium_value = int(payload['ma_medium_value']) ma_porog_srabativaniya = float(payload['ma_porog_srabativaniya']) bitrex_connector = trader_stakan.create_personalized_bittrex_connection( _process_data_to_db.Db_writer(PG_PARAMS), user_id) buy_result = trader_stakan.web_version_buy(bitrex_connector, currency, amount, user_id, timeframe, porog, \ ma_small_value, ma_medium_value, ma_porog_srabativaniya) #print(currency, amount) print('BUY RESULT', buy_result) html_string = '<h3><a href = /show_monitor?user_id={}>Страница покупок</a></h3>\ <h3><a href = /choose_buy?user_id={}>Купить еще что нибудь</a></h3>\ <h2>Результат покупки</h2>'.format(user_id, user_id) for param in buy_result['order_details']['result']: html_string += '{}{}{}{}{}'.format( '<p>', param, ': ', buy_result['order_details']['result'][param], '</p>') #print(html_string) #return web.json_response(buy_result) return web.Response(text=html_string, content_type='text/html')
def make_db_conn(conn_params): try: db_conn = _process_data_to_db.Db_writer(conn_params) return db_conn except Exception as error: print('Error conectiong to DB', error) return None
def sell_all(request): payload = request.query_string.split('&') print('request', payload) user_id = payload[0].split('=')[1] bitrex_connector = trader_stakan.create_personalized_bittrex_connection( _process_data_to_db.Db_writer(PG_PARAMS), user_id) sell_result = trader_stakan.web_version_sell_all(bitrex_connector, user_id) # print(currency, amount) return web.json_response(sell_result)
def sell_one(request): payload = request.query_string.split('&') #print('request', payload) print('WEB INTERFACE SELL INITIATED===============================') order_id = float(payload[0].split('=')[1]) user_id = float(payload[1].split('=')[1]) bitrex_connector = trader_stakan.create_personalized_bittrex_connection( _process_data_to_db.Db_writer(PG_PARAMS), user_id) sell_one_result = trader_stakan.web_version_sell_one( bitrex_connector, user_id, order_id) return web.json_response(sell_one_result)
def web_get_open_ordres(user_id): order_dict = {} db_conn = _process_data_to_db.Db_writer() db_request = db_conn.execute_request("SELECT id, market_name, open_price, \ open_time, current_price, update_time, amount, user_id, timeframe, porog \ from monitor where user_id = {};".format(user_id)) db_conn.commit() db_conn.close() if db_request.rowcount > 0: for record in db_request: # print('record', record) order_id = record[0] currency = record[1] open_price = record[2] start_time = record[3] current_price = record[4] update_time = record[5] amount = record[6] user_id = record[7] timeframe = record[8] porog = record[8] order_dict[currency] = { 'order_id': order_id, 'market_name': currency, 'open_price': open_price, 'current_price': current_price, 'open_time': start_time, 'current_time': update_time, 'status': 'open', 'amount': amount, 'user_id': user_id, 'profit': 0, 'srednee': 0, 'counter': 0, 'timeframe': timeframe, 'porog': porog } # for currency in order_dict: # print(currency, order_dict[currency]) return order_dict
def show_monitor(request): payload = request.query_string.split('&') user_id = payload[0].split('=')[1] total_profit = 0 fmt = '%d %b %H:%M' fmt_update = '%H:%M' db_conn = _process_data_to_db.Db_writer(PG_PARAMS) open_orders_sql_request_ss = "SELECT market_name, amount, open_time, open_price, sl, \ update_time, profit, timeframe, porog, status, id, btc_amount, algo, \ ma_small_value, ma_medium_value, ma_porog_srabativaniya, current_price \ FROM monitor WHERE status = 'open' AND user_id = '{}' ORDER BY id DESC;".format( user_id) open_orders = db_conn.execute_request(open_orders_sql_request_ss) db_conn.commit() active_orders = '' history = '' if open_orders.rowcount > 0: print('Open rowcount ', open_orders.rowcount) sql_response_open = open_orders.fetchall() active_orders = '' for record in sql_response_open: currency = record[0] amount = record[1] open_time = record[2] open_price = record[3] stop_loss = record[4] update_time = record[5] profit = record[6] timeframe = record[7] porog = record[8] status = record[9] order_id = int(record[10]) btc_amount = round(float(record[11]), 6) algo = record[12] ma_small_value = record[13] ma_medium_value = record[14] ma_porog_srabativaniya = record[15] current_price = record[16] # print('Open record: ', record) market = '<a href = sell_one?order_id={}&user_id={}>{}</a>'.format( order_id, user_id, currency) active_orders += "<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>".format( \ market, round(amount, 6), btc_amount, open_time, open_price, round(stop_loss, 10), round(current_price, 10), update_time, round(profit, 3), algo, ma_small_value, ma_medium_value, ma_porog_srabativaniya, timeframe, porog) total_profit += round(record[6], 3) #ОРДЕРА С ОШИБКОЙ db_conn = _process_data_to_db.Db_writer(PG_PARAMS) error_orders_sql_request_ss = "SELECT market_name, amount, open_time, open_price, update_time, sl, profit, timeframe, \ status, id, comment FROM monitor WHERE\ status !='open' AND status !='closed' AND user_id = '{}' ORDER BY id DESC;".format( user_id) error_orders = db_conn.execute_request(error_orders_sql_request_ss) db_conn.commit() error_table = '' error_html = '' if error_orders.rowcount > 0: print('Error rowcount ', error_orders.rowcount) sql_response_error = error_orders.fetchall() for record in sql_response_error: currency = record[0] amount = record[1] open_time = record[2] open_price = record[3] update_time = record[4] #sl = record[5] profit = record[6] timeframe = record[7] status = record[8] order_id = int(record[9]) comment = record[10] # market = '<a href = sell_one?order_id={}&user_id={}>{}</a>'.format(order_id, user_id, currency) error_table += "<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>".format( \ order_id, currency, amount, open_time.strftime(fmt), open_price, update_time.strftime(fmt_update), round(profit, 3), timeframe, status, comment) error_html = ''' <h2>Ошибки:</h2> <table style="width:100%" border="1"> <tr> <th>ID</th> <th>Валюта</th> <th>Количество</th> <th>Время покупки</th> <th>Цена покупки</th> <th>Обновление</th> <th>Профит, %</th> <th>Интервал, </br>мин.</th> <th>Статус</th> <th>Инфо по </br>статусу</th> </tr> {} </table>'''.format(error_table) closed_orders_sql_request = "SELECT market_name, amount, open_time, open_price, sl, \ update_time, profit, timeframe, porog, algo, id, btc_amount FROM monitor WHERE status = 'closed' \ AND user_id = '{}' ORDER BY id DESC;".format(user_id) closed_orders = db_conn.execute_request(closed_orders_sql_request) db_conn.commit() if closed_orders.rowcount > 0: sql_response_closed = closed_orders.fetchall() history = '' for record in sql_response_closed: currency = record[0] amount = record[1] open_time = record[2] open_price = record[3] stop_loss = record[4] update_time = record[5] profit = record[6] timeframe = record[7] porog = record[8] algo = record[9] order_id = int(record[10]) btc_amount = round(float(record[11]), 6) # print('Closed record: ', record) history += "<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>".format( \ currency, amount, btc_amount, open_time, open_price, round(stop_loss, 9), update_time, round(profit, 3), timeframe, porog, algo) text = '''<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Робот. Монитор</title> </head> <body> <h3>Активные позиции</h3> <table style="width:100%" border="1"> <tr> <th>Валюта</th> <th>Коли-</br>чество</th> <th>Количес-</br>тво BTC</th> <th>Время </br>покупки</th> <th>Цена </br>покупки</th> <th>Стоп </br>лосс</th> <th>Тек. ср.</br>цена</th> <th>Обнов-</br>ление</th> <th>Профит, </br>%</th> <th>Алго</th> <th>МА </br>мал</th> <th>Ма </br>сред</th> <th>Ма </br>порог%</th> <th>Интер-</br>вал, мин</th> <th>Прода-</br>жа </br>% </th> </tr> {} </table> <h3>Профит по открытым позициям: {}%</h3> <h3><a href = sell_all?user_id={}>Продать все!</a></h3> <h3><a href = /choose_buy?user_id={}>Купить еще что-нибудь</a></h3> {} <h3><a href = /choose_log>Смотреть логи робота</a></h3> <h2>История</h2> <table style="width:100%" border="1"> <tr> <th>Валюта</th> <th>Количество</th> <th>Количество BTC</th> <th>Время покупки</th> <th>Цена покупки</th> <th>Стоп лосс</th> <th>Обновление</th> <th>Профит, %</th> <th>Интервал, </br>мин.</th> <th>Продажа % </th> <th>Алго</th> </tr> {} </table> </body> </html>'''.format(active_orders, total_profit, user_id, user_id, error_html, history) db_conn.close() return web.Response(text=text, content_type='text/html')
from aiohttp import web from modules_robot import trader_stakan from datetime import datetime import time from modules_robot import _process_data_to_db #import aroon_v1_classes PG_PARAMS = { 'dbname': 'postgres', 'host': 'localhost', 'password': '', 'user': '******' } DB_CONNECTOR = _process_data_to_db.Db_writer(PG_PARAMS) markets = ['USDT-BTC', 'USDT-BCC', 'USDT-BTG', 'USDT-DASH', 'USDT-ETC', 'USDT-ETH', 'USDT-LTC', 'USDT-NEO', 'USDT-OMG',\ 'USDT-XMR', 'USDT-XRP', 'USDT-ZEC'] #USER_ID = 1 # def prepare_aroon_data(market_name, interval): # # request_time - если запускается первый раз, то нужно наполнить данными # # все МА. Для этого нужно выбрать согласно лимиту назад от момента request_time, # # являющегося временем создания либо последнего обновления ордера # limit = interval#сбор данных 2 раза в минуту # db_updated_data_request = "SELECT last from crypto where marketname = '{}' ORDER BY id DESC LIMIT {};".format \ # (market_name, limit) # #print(db_updated_data_request) # data = DB_CONNECTOR.execute_request(db_updated_data_request).fetchall() #нужен ли commit??? # return data async def pokupay(request):