Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
JSapp.me committed Jun 13, 2019
1 parent a192265 commit 6cafa10
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 41 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<img src="nbt_diagram.png">

<h6 align="center">Currently this open source version does not auto trade real money.</h6>
<h6 align="center">Currently this open source version is a work in progress and does not auto trade real money.</h6>

<h4 align="center">NBT is an open cryptocurrency trading bot development framework for the Binance exchange.</h4>

Expand All @@ -13,14 +13,15 @@ NBT includes 3 main scripts:
* a script to run the **server**:

* to track a selection of asset pairs and record all their binance data (candles, depths, trades) into text files
*
* if a buy or sell signal condition is detected, the server will emit a web socket signal to:
* the concurrent running trader client.
* (optional) the NBT Hub a.k.a. [Bitcoin vs. Altcoins](https://bitcoinvsaltcoins.com) to rank your signals.
* (optional) the NBT Hub a.k.a. [Bitcoin vs. Altcoins](https://bitcoinvsaltcoins.com) to monitor your strategies and signals.

* a script to run the **client / trader**:
* to follow and compute the PnL for each signals received via web socket from the server.
* to follow and compute the PnL for each strategy and signal received via web socket from the server.

* a script to **backtest** your signals on the recorded historical data.
* a script to **backtest** your strategies on the recorded historical data.

# Requirements

Expand All @@ -39,16 +40,16 @@ npm i

Execute the following commands in their own terminal.

**To start the server** to record pair data and generate the trading signals:
**To start the server** to record pair data, define strategies and emit trading signals:
```
npm run start
```
**To start the trader/client** to compute the signals received by the trader:
**To start the trader/client** to monitor strategies and signals received by the trader:

```
npm run trader
```
**To backtest** signals using the data recorded by the server:
**To backtest** strategies using the data recorded by the server:

```
npm run bt
Expand Down
2 changes: 1 addition & 1 deletion backtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const test_pair = "BTCUSDT"
const stop_loss_pnl = -0.81
const stop_profit_pnl = 1.81

const nbt_prefix = "oasis_"
const nbt_prefix = "nbt_"
const max_rows = 9000000
const plot_result = false

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-binance-trader",
"version": "0.1.0",
"version": "0.1.1",
"description": "An efficient cryptocurrency trading bot framework for Binance using Node.js",
"main": "server.js",
"keywords": [
Expand Down
55 changes: 23 additions & 32 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ const INDEX = path.join(__dirname, 'index.html')

//////////////////////////////////////////////////////////////////////////////////

const sound_alert = true // if true a sound alert will be played for each signals
const insert_into_files = true // to save pair data to txt files in the data sub-folder
const send_signal_to_bva = true // to send your signals to NBT Hub a.k.a http://bitcoinvsaltcoins.com
const sound_alert = false // if true a sound alert will be played for each signals
const insert_into_files = true // to save pair data to txt files in the data sub-folder
const send_signal_to_bva = false // to monitor your strategies and send your signals to NBT Hub a.k.a http://bitcoinvsaltcoins.com
const bva_ws_key = "" // if send_signal_to_bva true, please enter your ws key that you will find after signing up at http://bitcoinvsaltcoins.com

const tracked_max = 200 // max of pairs to be tracked (useful for testing)
const wait_time = 800 // to time out binance api calls (a lower number than 800 can result in api rstriction)
const tracked_max = 200 // max of pairs to be tracked (useful for testing)
const wait_time = 800 // to time out binance api calls (a lower number than 800 can result in api rstriction)

const stop_loss_pnl = -0.81 // to set your stop loss per trade
const stop_profit_pnl = 1.81 // to set your stop profit per trade
const stop_loss_pnl = -0.41 // to set your stop loss per trade
const stop_profit_pnl = 0.81 // to set your stop profit per trade

/////////////////////////////////////////////////////////////////////////////////

Expand All @@ -37,8 +38,9 @@ console.log("send_signal_to_bva: ", send_signal_to_bva)

let socket_client = {}
if (send_signal_to_bva) {
console.log("Connection to NBT HUB...")
// create a socket client connection to send your signals to NBT Hub (http://bitcoinvsaltcoins.com)
socket_client = io_client('https://nbt-hub.herokuapp.com')
socket_client = io_client('https://nbt-hub.herokuapp.com', { query: "key=" + bva_ws_key })
}

let alert_sound = {}
Expand All @@ -50,7 +52,7 @@ if (sound_alert) {
/////////////////////

let pairs = []
const nbt_prefix = "oasis_"
const nbt_prefix = "nbt_"
const interv_time = 10000
let sum_bids = {}
let sum_asks = {}
Expand Down Expand Up @@ -87,10 +89,6 @@ io.on('connection', (socket) => {
socket.on('message', (message) => console.log(' ...client message :: ' + message))
})

if (send_signal_to_bva) {
socket_client.on('connect', () => {})
}

//////////////////////////////////////////////////////////////////////////////////
// BINANCE API initialization //

Expand Down Expand Up @@ -230,38 +228,32 @@ async function trackPairData(pair) {

//////////////////////////////////////////////////////////////////////////////////////////

const author_name = "your_signal_author_name" // enter your name
const author_key = "your_unique_author_key" // please use an unique key for this from https://randomkeygen.com
let curr_price = new BigNumber(0)
let pnl = new BigNumber(0)
let signal_name, signal_key, description
let stratname, signal_key

//////////////////////////////// SIGNAL DECLARATION - START /////////////////////////////////

signal_name = "SIGNAL TEST" // enter the name of your signal
signal_key = signal_name.replace(/\s+/g, '') + author_key
description = signal_name + " :: " + interv_vols_sum[pair].times(first_ask_price[pair]).toFormat(2) + " " + srsi[pair].toFormat(2)
stratname = "NBT SIGNAL TEST" // enter the name of your strategy
signal_key = stratname.replace(/\s+/g, '')

//////// BUY SIGNAL DECLARATION ///////
if ( interv_vols_sum[pair].times(first_ask_price[pair]).isGreaterThan(10.0)
if ( interv_vols_sum[pair].times(first_ask_price[pair]).isGreaterThan(1.0)
&& srsi[pair].isGreaterThan(69)
&& !signaled_pairs[pair+signal_key]
) {
signaled_pairs[pair+signal_key] = true
buy_prices[pair+signal_key] = new BigNumber(first_ask_price[pair])
report.stop()
console.log(pair.green + " BUY => " + signal_name.green
console.log(pair.green + " BUY => " + stratname.green
+ " " + interv_vols_sum[pair].times(first_ask_price[pair]).toFormat(2))
+ " " + trades[pair][trades[pair].length-1]
+ " " + _.mean(trades[pair].slice(-6, trades[pair].length-1)
)
report.start()
const buy_signal = {
author_name: author_name,
author_key: author_key,
signal_name: signal_name,
signal_key: signal_key,
description: description,
key: bva_ws_key,
stratname: stratname,
pair: pair,
buy_price: first_ask_price[pair]
}
Expand All @@ -278,12 +270,11 @@ async function trackPairData(pair) {
) {
signaled_pairs[pair+signal_key] = false
report.stop()
console.log(pair.red + " SELL => " + signal_name.red)
console.log(pair.red + " SELL => " + stratname.red)
report.start()
const sell_signal = {
author_key: author_key,
signal_name: signal_name,
signal_key: signal_key,
key: bva_ws_key,
stratname: stratname,
pair: pair,
sell_price: first_bid_price[pair]
}
Expand Down Expand Up @@ -346,7 +337,7 @@ async function trackPairData(pair) {
const makers_total = new BigNumber(makers[pair].length)
const maker_ratio = makers_count > 0 ? makers_count.dividedBy(makers_total).times(100) : new BigNumber(0)

if (pair === "BTCUSDT") {
//if (pair === "BTCUSDT") {
report.text = moment().format().grey.padStart(20) +
pair.white.padStart(20) +
(prev_price.isEqualTo(prices[pair]) ? colors.grey(prices[pair]).padStart(30) : prev_price.isLessThan(prices[pair]) ? colors.green(prices[pair]).padStart(30) : colors.red(prices[pair]).padStart(30)) +
Expand All @@ -357,7 +348,7 @@ async function trackPairData(pair) {
colors.grey(last_sum_bids_bn.toFormat(2)).padStart(30) +
colors.grey(last_sum_asks_bn.toFormat(2)).padStart(30) +
colors.cyan(srsi[pair].decimalPlaces(2).toFormat(2)).padStart(20)
}
//}

if (BigNumber.isBigNumber(srsi[pair]) && prices[pair].isGreaterThan(0) && last_sum_bids_bn.isGreaterThan(0) && last_sum_asks_bn.isGreaterThan(0)) {

Expand Down

0 comments on commit 6cafa10

Please sign in to comment.