Repository: EasyAI/Simple-Binance-Trader
Branch: master
Commit: 3ad6ad40e259
Files: 14
Total size: 87.6 KB
Directory structure:
gitextract_jeifm_e2/
├── LICENSE
├── README.md
├── core/
│ ├── botCore.py
│ ├── static/
│ │ ├── css/
│ │ │ └── style.css
│ │ └── js/
│ │ ├── charts.js
│ │ └── script.js
│ ├── templates/
│ │ ├── jinja_templates.html
│ │ ├── main_page.html
│ │ └── page_template.html
│ └── trader.py
├── patterns.py
├── requirements.txt
├── run.py
└── trader_configuration.py
================================================
FILE CONTENTS
================================================
================================================
FILE: LICENSE
================================================
MIT License
Copyright (c) 2021 John P Lennie
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
# Simple Binance Trader
# Disclaimer
I am not responsible for the trades you make with the script, this script has not been exstensivly tested on live trades.
## Please check the following:
- Make sure your account uses BNB for the trade fees and that you have plenty of BNB for the trader to use for trades as if not there will be issues with the trader.
- Please clear any cache files when updating the trader as there may be issues if not.
- Please if any updates are also available for binance_api or the technical indicators update those also.
NOTE: The current strtergy is also very weak and will mostlikley return only losses therefore I recomend you create your work or use some which would work. However the trader currently also does not support simulated trades and only support live trading, simulated trades should be added in the future so you may need to wait until then to use this to test stratergies.
NOTE: Trader now supports MARGIN trades allowing the placement of short and long positions. If SPOT trading put your conditions within the "long_entry/long_exit" sections within the trader_configuration.py file.
## Description
This is a simple binance trader that uses the REST api and sockets to allow automation or manipulation of account detail/data. The script which in the default configuration uses a basic MACD trading setup to trade. The script however is very customisable and you'll be able to configure it as you with via the help of the document in the usage section.
### Repository Contains:
- run.py : This is used to start/setup the bot.
- trader_configuration.py : Here is where you write your conditions using python logic.
- patterns.py : Can be used to trade based on specific patterns.
- Core
- botCore.py : Is used to manage the socket and trader as well as pull data to be displayed.
- trader.py : The main trader inchage or updating and watching orders.
- static : Folder for static files for the website (js/css).
- templates : Folder for HTML page templates.
### Setting File:
- PUBLIC_KEY - Your public binanace api key
- PRIVATE_KEY - Your private binanace api key
- IS_TEST - Allow trader to be run in test mode (True/False)
- MARKET_TYPE - Market type to be traded (SPOT/MARGIN)
- UPDATE_BNB_BALANCE - Automatically update the BNB balance when low (for trading fees, only applicable to real trading)
- TRADER_INTERVAL - Interval used for the trader (1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d).
- TRADING_CURRENCY - The currency max the trader will use (in the quote key) also note this scales up with the number of markets i.e. 2 pairs each market will have 0.0015 as their trading currency pair.
- TRADING_MARKETS - The markets that are being traded and seperate with ',' (BTC-ETH,BTC-NEO)
- HOST_IP - The host IP for the web UI (if left blank default is 127.0.0.1)
- HOST_PORT - The host port for the web UI (if left blank default is 5000)
- MAX_CANDLES - Max candles the trader will use (if left brank default is 500)
- MAX_DEPTH - Max market depth the trader will use (if left brank default is 50)
## Usage
I recommend setting this all up within a virtual python enviornment:
First get the base modules:
- To quickly install all the required modules use 'pip3 install -r requirements'.
Secondly get the required techinal indicators module adn binance api.
- https://github.com/EasyAI/binance_api, This is the binance API that the trader uses.
- https://github.com/EasyAI/Python-Charting-Indicators, This contains the logic to calculate technical indicators. (only the file technical_indicators.py is needed)
Move them into the site-packages folder. NOTE: If you get an error saying that either the technical_indicators or binance_api is not found you can move them in to the same directory as the run.py file for the trader.
Finally navigate to the trader directory.
To set up the bot and for any further detail please refer to the google doc link below:
https://docs.google.com/document/d/1VUx_1O5kQQxk0HfqqA8WyQpk6EbbnXcezAdqXkOMklo/edit?usp=sharing
### Contact
Please if you find any bugs or issues contact me so I can improve.
EMAIL: jlennie1996@gmail.com
================================================
FILE: core/botCore.py
================================================
#! /usr/bin/env python3
import os
import sys
import time
import json
import os.path
import hashlib
import logging
import threading
from decimal import Decimal
from flask_socketio import SocketIO
from flask import Flask, render_template, url_for, request
from binance_api import api_master_rest_caller
from binance_api import api_master_socket_caller
from . import trader
MULTI_DEPTH_INDICATORS = ['ema', 'sma', 'rma', 'order']
# Initilize globals.
## Setup flask app/socket
APP = Flask(__name__)
SOCKET_IO = SocketIO(APP)
## Initilize base core object.
core_object = None
started_updater = False
## Initilize IP/port pair globals.
host_ip = ''
host_port = ''
## Set traders cache file name.
CAHCE_FILES = 'traders.json'
@APP.context_processor
def override_url_for():
return(dict(url_for=dated_url_for))
def dated_url_for(endpoint, **values):
# Override to prevent cached assets being used.
if endpoint == 'static':
filename = values.get('filename', None)
if filename:
file_path = os.path.join(APP.root_path,
endpoint,
filename)
values['q'] = int(os.stat(file_path).st_mtime)
return url_for(endpoint, **values)
@APP.route('/', methods=['GET'])
def control_panel():
# Base control panel configuration.
global started_updater
## Web updater used for live updating.
if not(started_updater):
started_updater = True
web_updater_thread = threading.Thread(target=web_updater)
web_updater_thread.start()
## Set socket ip/port.
start_up_data = {
'host':{'IP': host_ip, 'Port': host_port},
'market_symbols': core_object.trading_markets
}
return(render_template('main_page.html', data=start_up_data))
@APP.route('/rest-api/v1/trader_update', methods=['POST'])
def update_trader():
# Base API for managing trader interaction.
data = request.get_json()
## Check if specified bot exists.
current_trader = api_error_check(data)
if current_trader == None:
## No trader therefore return false.
return(json.dumps({'call':False, 'message':'INVALID_TRADER'}))
elif data['action'] == 'start':
## Updating trader status to running.
if current_trader.state_data['runtime_state'] == 'FORCE_PAUSE':
current_trader.state_data['runtime_state'] = 'RUN'
elif data['action'] == 'pause':
## Updating trader status to paused.
if current_trader.state_data['runtime_state'] == 'RUN':
current_trader.state_data['runtime_state'] = 'FORCE_PAUSE'
else:
## If action was not found return false
return(json.dumps({'call':False, 'message':'INVALID_ACTION'}))
return(json.dumps({'call':True}))
@APP.route('/rest-api/v1/get_trader_charting', methods=['GET'])
def get_trader_charting():
# Endpoint to pass trader indicator data.
market = request.args.get('market')
limit = int(request.args.get('limit'))
data = {'market':market}
## Check if specified bot exists.
current_trader = api_error_check(data)
if current_trader == None:
## No trader therefore return false.
return(json.dumps({'call':False, 'message':'INVALID_TRADER'}))
candle_data = core_object.get_trader_candles(current_trader.print_pair)[:limit]
indicator_data = core_object.get_trader_indicators(current_trader.print_pair)
short_indicator_data = shorten_indicators(indicator_data, candle_data[-1][0])
return(json.dumps({'call':True, 'data':{'market':market, 'indicators':short_indicator_data, 'candles':candle_data}}))
@APP.route('/rest-api/v1/get_trader_indicators', methods=['GET'])
def get_trader_indicators():
# Endpoint to pass trader indicator data.
market = request.args.get('market')
limit = int(request.args.get('limit'))
data = {'market':market}
## Check if specified bot exists.
current_trader = api_error_check(data)
if current_trader == None:
## No trader therefore return false.
return(json.dumps({'call':False, 'message':'INVALID_TRADER'}))
indicator_data = core_object.get_trader_indicators(current_trader.print_pair)
return(json.dumps({'call':True, 'data':{'market':market, 'indicators':indicator_data}}))
@APP.route('/rest-api/v1/get_trader_candles', methods=['GET'])
def get_trader_candles():
# Endpoint to pass trader candles.
market = request.args.get('market')
limit = int(request.args.get('limit'))
data = {'market':market}
## Check if specified bot exists.
current_trader = api_error_check(data)
if current_trader == None:
## No trader therefore return false.
return(json.dumps({'call':False, 'message':'INVALID_TRADER'}))
candle_data = core_object.get_trader_candles(current_trader.print_pair)[:limit]
return(json.dumps({'call':True, 'data':{'market':market, 'candles':candle_data}}))
@APP.route('/rest-api/v1/test', methods=['GET'])
def test_rest_call():
# API endpoint test
return(json.dumps({'call':True, 'message':'HELLO WORLD!'}))
def shorten_indicators(indicators, end_time):
base_indicators = {}
for ind in indicators:
if ind in MULTI_DEPTH_INDICATORS:
base_indicators.update({ind:{}})
for sub_ind in indicators[ind]:
base_indicators[ind].update({sub_ind:[ [val[0] if ind != 'order' else val[0]*1000,val[1]] for val in indicators[ind][sub_ind] if (val[0] if ind != 'order' else val[0]*1000) > end_time ]})
else:
base_indicators.update({ind:[ [val[0],val[1]] for val in indicators[ind] if val[0] > end_time]})
return(base_indicators)
def api_error_check(data):
## Check if specified bot exists.
current_trader = None
for trader in core_object.trader_objects:
if trader.print_pair == data['market']:
current_trader = trader
break
return(current_trader)
def web_updater():
# Web updater use to update live via socket.
lastHash = None
while True:
if core_object.coreState == 'RUN':
## Get trader data and hash it to find out if there have been any changes.
traderData = core_object.get_trader_data()
currHash = hashlib.md5(str(traderData).encode())
if lastHash != currHash:
## Update any new changes via socket.
lastHash = currHash
total_bulk_data = []
for trader in traderData:
bulk_data = {}
bulk_data.update({'market':trader['market']})
bulk_data.update({'trade_recorder':trader['trade_recorder']})
bulk_data.update({'wallet_pair':trader['wallet_pair']})
bulk_data.update(trader['custom_conditions'])
bulk_data.update(trader['market_activity'])
bulk_data.update(trader['market_prices'])
bulk_data.update(trader['state_data'])
total_bulk_data.append(bulk_data)
SOCKET_IO.emit('current_traders_data', {'data':total_bulk_data})
time.sleep(.8)
class BotCore():
def __init__(self, settings, logs_dir, cache_dir):
# Initilization for the bot core managment object.
logging.info('[BotCore] Initilizing the BotCore object.')
## Setup binance REST and socket API.
self.rest_api = api_master_rest_caller.Binance_REST(settings['public_key'], settings['private_key'])
self.socket_api = api_master_socket_caller.Binance_SOCK()
## Setup the logs/cache dir locations.
self.logs_dir = logs_dir
self.cache_dir = cache_dir
## Setup run type, market type, and update bnb balance.
self.run_type = settings['run_type']
self.market_type = settings['market_type']
self.update_bnb_balance = settings['update_bnb_balance']
## Setup max candle/depth setting.
self.max_candles = settings['max_candles']
self.max_depth = settings['max_depth']
## Get base quote pair (This prevents multiple different pairs from conflicting.)
pair_one = settings['trading_markets'][0]
self.quote_asset = pair_one[:pair_one.index('-')]
self.base_currency = settings['trading_currency']
self.candle_Interval = settings['trader_interval']
## Initilize base trader settings.
self.trader_objects = []
self.trading_markets = settings['trading_markets']
## Initilize core state
self.coreState = 'READY'
def start(self):
# Start the core object.
logging.info('[BotCore] Starting the BotCore object.')
self.coreState = 'SETUP'
## check markets
found_markets = []
not_supported = []
for market in self.rest_api.get_exchangeInfo()['symbols']:
fmtMarket = '{0}-{1}'.format(market['quoteAsset'], market['baseAsset'])
# If the current market is not in the trading markets list then skip.
if not fmtMarket in self.trading_markets:
continue
found_markets.append(fmtMarket)
if (self.market_type == 'MARGIN' and market['isMarginTradingAllowed'] == False) or (self.market_type == 'SPOT' and market['isSpotTradingAllowed'] == False):
not_supported.append(fmtMarket)
continue
# This is used to setup min quantity.
if float(market['filters'][2]['minQty']) < 1.0:
minQuantBase = (Decimal(market['filters'][2]['minQty'])).as_tuple()
lS = abs(int(len(minQuantBase.digits)+minQuantBase.exponent))+1
else: lS = 0
# This is used to set up the price precision for the market.
tickSizeBase = (Decimal(market['filters'][0]['tickSize'])).as_tuple()
tS = abs(int(len(tickSizeBase.digits)+tickSizeBase.exponent))+1
# This is used to get the markets minimal notation.
mN = float(market['filters'][3]['minNotional'])
# Put all rules into a json object to pass to the trader.
market_rules = {'LOT_SIZE':lS, 'TICK_SIZE':tS, 'MINIMUM_NOTATION':mN}
# Initilize trader objecta dn also set-up its inital required data.
traderObject = trader.BaseTrader(market['quoteAsset'], market['baseAsset'], self.rest_api, socket_api=self.socket_api)
traderObject.setup_initial_values(self.market_type, self.run_type, market_rules)
self.trader_objects.append(traderObject)
## Show markets that dont exist on the binance exchange.
if len(self.trading_markets) != len(found_markets):
no_market_text = ''
for market in [market for market in self.trading_markets if market not in found_markets]:
no_market_text+=str(market)+', '
logging.warning('Following pairs dont exist: {}'.format(no_market_text[:-2]))
## Show markets that dont support the market type.
if len(not_supported) > 0:
not_support_text = ''
for market in not_supported:
not_support_text += ' '+str(market)
logging.warning('[BotCore] Following market pairs are not supported for {}: {}'.format(self.market_type, not_support_text))
valid_tading_markets = [market for market in found_markets if market not in not_supported]
## setup the binance socket.
for market in valid_tading_markets:
self.socket_api.set_candle_stream(symbol=market, interval=self.candle_Interval)
self.socket_api.set_manual_depth_stream(symbol=market, update_speed='1000ms')
if self.run_type == 'REAL':
self.socket_api.set_userDataStream(self.rest_api, self.market_type)
self.socket_api.BASE_CANDLE_LIMIT = self.max_candles
self.socket_api.BASE_DEPTH_LIMIT = self.max_depth
self.socket_api.build_query()
self.socket_api.set_live_and_historic_combo(self.rest_api)
self.socket_api.start()
# Load the wallets.
if self.run_type == 'REAL':
user_info = self.rest_api.get_account(self.market_type)
if self.market_type == 'SPOT':
wallet_balances = user_info['balances']
elif self.market_type == 'MARGIN':
wallet_balances = user_info['userAssets']
current_tokens = {}
for balance in wallet_balances:
total_balance = (float(balance['free']) + float(balance['locked']))
if total_balance > 0:
current_tokens.update({balance['asset']:[
float(balance['free']),
float(balance['locked'])]})
else:
current_tokens = {self.quote_asset:[float(self.base_currency), 0.0]}
# Load cached data
cached_traders_data = None
if os.path.exists(self.cache_dir+CAHCE_FILES):
with open(self.cache_dir+CAHCE_FILES, 'r') as f:
cached_traders_data = json.load(f)['data']
## Setup the trader objects and start them.
logging.info('[BotCore] Starting the trader objects.')
for trader_ in self.trader_objects:
currSymbol = "{0}{1}".format(trader_.base_asset, trader_.quote_asset)
# Update trader with cached data (to resume trades/keep records of trades.)
if cached_traders_data != '' and cached_traders_data:
for cached_trader in cached_traders_data:
m_split = cached_trader['market'].split('-')
if (m_split[1]+m_split[0]) == currSymbol:
trader_.configuration = cached_trader['configuration']
trader_.custom_conditional_data = cached_trader['custom_conditions']
trader_.market_activity = cached_trader['market_activity']
trader_.trade_recorder = cached_trader['trade_recorder']
trader_.state_data = cached_trader['state_data']
wallet_pair = {}
if trader_.quote_asset in current_tokens:
wallet_pair.update({trader_.quote_asset:current_tokens[trader_.quote_asset]})
if trader_.base_asset in current_tokens:
wallet_pair.update({trader_.base_asset:current_tokens[trader_.base_asset]})
trader_.start(self.base_currency, wallet_pair)
logging.debug('[BotCore] Starting trader manager')
TM_thread = threading.Thread(target=self._trader_manager)
TM_thread.start()
if self.update_bnb_balance:
logging.debug('[BotCore] Starting BNB manager')
BNB_thread = threading.Thread(target=self._bnb_manager)
BNB_thread.start()
logging.debug('[BotCore] Starting connection manager thread.')
CM_thread = threading.Thread(target=self._connection_manager)
CM_thread.start()
logging.debug('[BotCore] Starting file manager thread.')
FM_thread = threading.Thread(target=self._file_manager)
FM_thread.start()
logging.info('[BotCore] BotCore successfully started.')
self.coreState = 'RUN'
def _trader_manager(self):
''' '''
while self.coreState != 'STOP':
pass
def _bnb_manager(self):
''' This will manage BNB balance and update if there is low BNB in account. '''
last_wallet_update_time = 0
while self.coreState != 'STOP':
socket_buffer_global = self.socket_api.socketBuffer
# If outbound postion is seen then wallet has updated.
if 'outboundAccountPosition' in socket_buffer_global:
if last_wallet_update_time != socket_buffer_global['outboundAccountPosition']['E']:
last_wallet_update_time = socket_buffer_global['outboundAccountPosition']['E']
for wallet in socket_buffer_global['outboundAccountPosition']['B']:
if wallet['a'] == 'BNB':
if float(wallet['f']) < 0.01:
bnb_order = self.rest_api.place_order(self.market_type, symbol='BNBBTC', side='BUY', type='MARKET', quantity=0.1)
time.sleep(2)
def _file_manager(self):
''' This section is responsible for activly updating the traders cache files. '''
while self.coreState != 'STOP':
time.sleep(15)
traders_data = self.get_trader_data()
if os.path.exists(self.cache_dir):
file_path = '{0}{1}'.format(self.cache_dir,CAHCE_FILES)
with open(file_path, 'w') as f:
json.dump({'lastUpdateTime':time.time() ,'data':traders_data}, f)
def _connection_manager(self):
''' This section is responsible for re-testing connectiongs in the event of a disconnect. '''
update_time = 0
retryCounter = 1
time.sleep(20)
while self.coreState != 'STOP':
time.sleep(1)
if self.coreState != 'RUN':
continue
if self.socket_api.last_data_recv_time != update_time:
update_time = self.socket_api.last_data_recv_time
else:
if (update_time + (15*retryCounter)) < time.time():
retryCounter += 1
try:
print(self.rest_api.test_ping())
except Exception as e:
logging.warning('[BotCore] Connection issue: {0}.'.format(e))
continue
logging.info('[BotCore] Connection issue resolved.')
if not(self.socket_api.socketRunning):
logging.info('[BotCore] Attempting socket restart.')
self.socket_api.start()
def get_trader_data(self):
''' This can be called to return data for each of the active traders. '''
rData = [ _trader.get_trader_data() for _trader in self.trader_objects ]
return(rData)
def get_trader_indicators(self, market):
''' This can be called to return the indicators that are used by the traders (Will be used to display web UI activity.) '''
for _trader in self.trader_objects:
if _trader.print_pair == market:
indicator_data = _trader.indicators
indicator_data.update({'order':{'buy':[], 'sell':[]}})
indicator_data['order']['buy'] = [ [order[0],order[1]] for order in _trader.trade_recorder if order[4] == 'BUY']
indicator_data['order']['sell'] = [ [order[0],order[1]] for order in _trader.trade_recorder if order[4] == 'SELL']
return(indicator_data)
def get_trader_candles(self, market):
''' This can be called to return the candle data for the traders (Will be used to display web UI activity.) '''
for _trader in self.trader_objects:
if _trader.print_pair == market:
sock_symbol = str(_trader.base_asset)+str(_trader.quote_asset)
return(self.socket_api.get_live_candles(sock_symbol))
def start(settings, logs_dir, cache_dir):
global core_object, host_ip, host_port
if core_object == None:
core_object = BotCore(settings, logs_dir, cache_dir)
core_object.start()
logging.info('[BotCore] Starting traders in {0} mode, market type is {1}.'.format(settings['run_type'], settings['market_type']))
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
host_ip = settings['host_ip']
host_port = settings['host_port']
SOCKET_IO.run(APP,
host=settings['host_ip'],
port=settings['host_port'],
debug=True,
use_reloader=False)
================================================
FILE: core/static/css/style.css
================================================
@import url('https://fonts.googleapis.com/css?family=Josefin+Sans&display=swap');
*{
margin: 0;
padding: 0;
box-sizing: border-box;
list-style: none;
text-decoration: none;
font-family: arial;
}
body{
background-color: #f3f5f9;
display: flex;
position: relative;
}
.sidebar{
width: 200px;
height: 100%;
background: #4b4276;
padding: 30px 0px;
position: fixed;
color: #bdb8d7;
}
.sidebar ul li{
padding: 15px;
}
.sidebar ul li a{
display: block;
color: #bdb8d7;
}
.sidebar ul li:hover{
background-color: #594f8d;
}
.sidebar ul li:hover a{
color: #fff;
}
main {
width: 100%;
margin-left: 200px;
background-color: #F5F5F0;
padding-left: 20px;
padding-top: 10px;
}
.trader-panel {
display: none;
}
.small-button {
padding: 5px;
font-size: 12px;
display: block;
display: inline-block;
border: none;
border-radius: 4px;
cursor: pointer;
color: white;
}
.green-button {
background-color: #00802b;
}
.green-button:hover {
background-color: #33ff77;
}
.amber-button {
background-color: #e68a00
}
.amber-button:hover {
background-color: #ffa31a;
}
================================================
FILE: core/static/js/charts.js
================================================
// Indicator meta details
const indicator_chart_types_mapping = {'patterns_data_lines':'line', 'patterns_data_points':'scatter', 'tops_bottoms':'scatter', 'data_lines':'line', 'cps':'line', 'ichi':'line', 'boll':'line', 'adx':'line', 'stock':'line', 'order':'scatter', 'ema':'line', 'sma':'line', 'rma':'line', 'rsi':'line', 'mfi':'line', 'cci':'line', 'zerolagmacd':'macd', 'macd':'macd'}
const indicator_home_type_mapping = {'patterns_data_lines':'MAIN', 'patterns_data_points':'MAIN', 'tops_bottoms':'MAIN', 'data_lines':'MAIN', 'ichi':'MAIN', 'cps':'MAIN', 'boll':'MAIN', 'adx':'OWN', 'stock':'OWN', 'order':'MAIN', 'ema':'MAIN', 'sma':'MAIN', 'rma':'MAIN', 'rsi':'OWN', 'mfi':'OWN', 'cci':'OWN', 'zerolagmacd':'OWN', 'macd':'OWN'}
const indicator_is_single_mapping = ['patterns_data_lines', 'patterns_data_points', 'tops_bottoms', 'data_lines', 'order', 'ema', 'sma', 'rma', 'rsi', 'mfi', 'cci', 'cps']
const double_depth_indicators = ['ema', 'sma', 'order', 'patterns_data_points', 'patterns_data_lines'];
// Base Apex chart configuration.
window.Apex = {
chart: {
animations: {
enabled: false
}
},
autoScaleYaxis: false
};
// Chart template for main chart.
var base_candle_chart_configuration = {
series: [],
chart: {
height: 800,
id: 'main_Chart',
type: 'line'
},
fill: {
type:'solid',
},
markers: {
size: []
},
//colors: [],
stroke: {
width: []
},
tooltip: {
shared: true,
custom: []
},
xaxis: {
type: 'datetime',
},
yaxis: {
labels: {
minWidth: 40,
formatter: function (value) { return Math.round(value); }
},
}
};
let loaded_candles_chart = null;
function initial_build(target_element, charting_data) {
// Add main chandle chart position.
var candle_data = charting_data['candles'];
var indicator_data = charting_data['indicators'];
var main_chart_series = [];
loaded_candles_chart = JSON.parse(JSON.stringify(base_candle_chart_configuration));
populate_chart(indicator_data);
var built_data = build_candle_data(candle_data);
var built_candle_data = built_data[0];
// Finally add the candle to the displayed chart.
loaded_candles_chart["series"].push({
name: 'candle',
type: 'candlestick',
data: built_candle_data
});
loaded_candles_chart["stroke"]["width"].push(1);
loaded_candles_chart["markers"]["size"].push(0);
loaded_candles_chart["tooltip"]["custom"].push(function({seriesIndex, dataPointIndex, w}) {
var o = w.globals.seriesCandleO[seriesIndex][dataPointIndex]
var h = w.globals.seriesCandleH[seriesIndex][dataPointIndex]
var l = w.globals.seriesCandleL[seriesIndex][dataPointIndex]
var c = w.globals.seriesCandleC[seriesIndex][dataPointIndex]
return (`Open:${o}
Total PL: | Total Trades:
State: | Trades: | Overall: | Last Update: | Last Price:
High:${h}
Low:${l}
Close:${c}`)
});
candle_chart = new ApexCharts(target_element, loaded_candles_chart);
candle_chart.render();
}
function build_candle_data(candle_data) {
var built_candle_data = [];
var built_volume_data = [];
for (var i=0; i < candle_data.length; i++) {
var candle = candle_data[i];
built_candle_data.push({
x: new Date(parseInt(candle[0])),
y: [
candle[1],
candle[2],
candle[3],
candle[4]
]
});
built_volume_data.push({
x: new Date(parseInt(candle[0])),
y: Math.round(candle[5])
});
}
return([built_candle_data, built_volume_data]);
}
function build_timeseries(ind_obj) {
var indicator_lines = [];
var keys = []
// Use sorted timestamp to print out.
for (var ind in ind_obj) {
var current_set = ind_obj[ind]
if (typeof current_set[0] == 'number') {
indicator_lines.push({
x: new Date(parseInt(current_set[0])),
y: current_set[1].toFixed(8)
});
} else {
for (var sub_ind in current_set) {
if (!keys.includes(sub_ind)) {
keys.push(sub_ind)
indicator_lines[sub_ind] = []
}
indicator_lines[sub_ind].push({
x: new Date(parseInt(current_set[sub_ind][0])),
y: current_set[sub_ind][1].toFixed(8)
});
}
}
}
return(indicator_lines);
}
function build_basic_indicator(chart_obj, ind_obj, chart_type, line_name=null, ind_name=null) {
var indicator_lines = build_timeseries(ind_obj);
if (!(line_name == null)) {
chart_obj["series"].push({
name: line_name,
type: chart_type,
data: indicator_lines
});
if (chart_type == "scatter") {
chart_obj["stroke"]["width"].push(2);
chart_obj["markers"]["size"].push(8);
} else {
chart_obj["stroke"]["width"].push(2);
chart_obj["markers"]["size"].push(0);
}
} else {
for (var sub_ind_name in indicator_lines) {
chart_obj["series"].push({
name: sub_ind_name,
type: chart_type,
data: indicator_lines[sub_ind_name]});
if (chart_type == "scatter") {
chart_obj["stroke"]["width"].push(2);
chart_obj["markers"]["size"].push(8);
} else {
chart_obj["stroke"]["width"].push(2);
chart_obj["markers"]["size"].push(0);
}
}
}
if ('custom' in chart_obj["tooltip"]) {
if (!(line_name == null)) {
chart_obj["tooltip"]["custom"].push(
function({seriesIndex, dataPointIndex, w}) {
return w.globals.series[seriesIndex][dataPointIndex]
});
} else {
for (var ind in indicator_lines) {
chart_obj["tooltip"]["custom"].push(
function({seriesIndex, dataPointIndex, w}) {
return w.globals.series[seriesIndex][dataPointIndex]
});
}
}
}
}
function populate_chart(indicator_data) {
for (var raw_indicator in indicator_data) {
var patt = /[^\d]+/i;
var indicator = raw_indicator.match(patt)[0];
var current_ind = indicator_data[raw_indicator];
var chart_type = indicator_chart_types_mapping[indicator];
var home_chart = indicator_home_type_mapping[indicator];
var line_name = null;
var ind_name = null;
if (home_chart == "MAIN") {
target_chart = loaded_candles_chart;
if (double_depth_indicators.includes(indicator)) {
for (var sub_ind in current_ind) {
line_name = sub_ind;
var built_chart = build_basic_indicator(target_chart, current_ind[sub_ind], chart_type, line_name, ind_name);
}
} else {
if (indicator_is_single_mapping.includes(indicator)) {
line_name = raw_indicator;
}
var built_chart = build_basic_indicator(target_chart, current_ind, chart_type, line_name, ind_name);
}
}
}
}
================================================
FILE: core/static/js/script.js
================================================
//
//
const socket = io('http://'+ip+':'+port);
const class_data_mapping = {
'trader-state':'runtime_state',
'trader-lastupdate':'last_update_time',
'trader-lastprice':'lastPrice',
'trader-markettype':'order_market_type',
'trader-orderside':'order_side',
'trader-ordertype':'order_type',
'trader-orderstatus':'order_status',
'trader-buyprice':'price',
'trader-sellprice':'price',
'trader-orderpoint':'order_point'
};
var current_chart = '';
var update_chart = false;
$(document).ready(function() {
socket.on('current_traders_data', function(data) {
update_trader_results(data);
});
});
function update_trader_results(data) {
//
var currentTraders = data['data'];
var overall_total_trades = 0;
var overall_total_pl = 0;
for (x = 0; x < (currentTraders.length); x++){
var current = currentTraders[x];
var trade_recorder = current['trade_recorder'];
var update_targets = [`trader_${current['market']}`, `overview_${current['market']}`]
for (i = 0; i < (update_targets.length); i++){
var trader_panel = document.getElementById(update_targets[i]);
var target_el = null;
for (var key in class_data_mapping) {
target_el = trader_panel.getElementsByClassName(key);
if (target_el.length != 0) {
if (current['order_side'] == 'SELL' && key == 'trader-buyprice') {
show_val = trade_recorder[trade_recorder.length-1][1];
} else {
show_val = current[class_data_mapping[key]];
}
if (show_val === null) {
show_val = 'Null';
}
target_el[0].innerText = show_val;
}
}
target_el = trader_panel.getElementsByClassName('show-sellaction');
if (target_el.length != 0) {
if (current['order_side'] == 'SELL') {
target_el[0].style.display = 'block';
} else {
target_el[0].style.display = 'none';
}
}
var outcome = 0;
var total_trades = 0;
if (trade_recorder.length >= 2) {
range = trade_recorder.length/2
for (y = 0; y < (range); y++) {
buy_order = trade_recorder[(range*2)-2]
sell_order = trade_recorder[range*2-1]
buy_value = buy_order[1]*buy_order[2]
sell_value = sell_order[1]*buy_order[2]
if (buy_order[3].includes("SHORT")) {
outcome += buy_value-sell_value;
} else {
outcome += sell_value-buy_value;
}
total_trades += 1
}
}
var r_outcome = Math.round(outcome*100000000)/100000000;
target_el = trader_panel.getElementsByClassName('trader-trades')[0];
target_el.innerText = total_trades;
target_el = trader_panel.getElementsByClassName('trader-overall')[0];
target_el.innerText = r_outcome;
if (update_targets[i] != `overview_${current['market']}`) {
overall_total_trades += total_trades;
overall_total_pl += r_outcome;
}
if ((current_chart == update_targets[i]) && (update_chart == true) && current_chart != 'trader_Overview') {
console.log(currentTraders);
update_chart = false;
target_el = trader_panel.getElementsByClassName('trader_charts')[0];
build_chart(current['market'], target_el);
}
}
}
var overview_section = document.getElementById('trader_Overview');
target_el = overview_section.getElementsByClassName('overview-totalpl')[0];
target_el.innerText = overall_total_pl;
target_el = overview_section.getElementsByClassName('overview-totaltrades')[0];
target_el.innerText = overall_total_trades;
}
function hide_section(e, section_id){
var section_el = document.getElementsByTagName('section');
for (i = 0; i < (section_el.length); i++){
if (section_el[i].id == `trader_${section_id}`) {
current_chart = `trader_${section_id}`;
update_chart = true;
section_el[i].style.display = "block";
} else {
section_el[i].style.display = "none";
}
}
}
function start_trader(e, market_pair){
e.preventDefault();
rest_api('POST', 'trader_update', {'action':'start', 'market':market_pair});
}
function pause_trader(e, market_pair){
e.preventDefault();
rest_api('POST', 'trader_update', {'action':'pause', 'market':market_pair});
}
function build_chart(market_pair, element){
rest_api('GET', `get_trader_charting?market=${market_pair}&limit=200`, null, initial_build, element);
}
function rest_api(method, endpoint, data=null, target_function=null, target_element=null){
// if either the user has requested a force update on bot data or the user has added a new market to trade then send an update to the backend.
console.log(`'M: ${method}, ULR: /rest-api/v1/${endpoint}, D:${data}`);
let request = new XMLHttpRequest();
request.open(method, '/rest-api/v1/'+endpoint, true);
request.onload = function() {
if (this.status == 200){
var resp_data = JSON.parse(request.responseText);
console.log(resp_data);
if (target_function != null && target_element == null) {
target_function(resp_data);
} else if (target_function != null && target_element != null) {
target_function(target_element, resp_data['data']);
}
} else {
console.log(`error ${request.status} ${request.statusText}`);
}
}
if (data == null){
request.send();
} else {
request.setRequestHeader('content-type', 'application/json');
request.send(JSON.stringify(data));
}
}
================================================
FILE: core/templates/jinja_templates.html
================================================
{% macro overview_data_panel(market_symbols) %}
Trader Overview
{{ market_symbol }}
Start
Pause
| Type: | Status: | Buy Price: | Sell Price: | OP: