Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

Commit

Permalink
Rest API Support for zenbot (#797)
Browse files Browse the repository at this point in the history
* Added Binance to Zenbot

* small change in the engine to make binance cancel order work

* removed the node-binance-api

* fixed feedback for pull request.

* Added API support to zenbot

* Added output handler in the lib

* Fixed feedback added readme
  • Loading branch information
abduegal authored and DeviaVir committed Dec 4, 2017
1 parent d24a9de commit 4aadfd1
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,17 @@ https://www.prowlapp.com/
Supply zenbot with your TextBelt API key and zenbot will send SMS notifications to your cell phone.
https://www.textbelt.com/

## Rest API

You can enable a Rest API for Zenbot by enabling the following configuration
```
c.output.api = {}
c.output.api.on = true
c.output.api.port = 0 // 0 = random port
```
You can choose a port, or pick 0 for a random port.

Once you did that, you can call the API on: http://\<hostname\>:\<port\>/trades

## Manual trade tools

Expand Down
3 changes: 2 additions & 1 deletion commands/trade.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ module.exports = function container (get, set, clear) {
process.exit(1)
}
var engine = get('lib.engine')(s)

get('lib.output').initializeOutput(s)

const keyMap = new Map()
keyMap.set('b', 'limit'.grey + ' BUY'.green)
keyMap.set('B', 'market'.grey + ' BUY'.green)
Expand Down
7 changes: 7 additions & 0 deletions conf-sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,10 @@ c.notifiers.textbelt.phone = '3121234567'
c.notifiers.textbelt.key = 'textbelt'
// end textbelt configs

// output
c.output = {}

// REST API
c.output.api = {}
c.output.api.on = true
c.output.api.port = 0 // 0 = random port
5 changes: 5 additions & 0 deletions extensions/output/_codemap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
_ns: 'zenbot',

'output.api': require('./api')
}
34 changes: 34 additions & 0 deletions extensions/output/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@


module.exports = function container (get) {
let c = get('conf')
let express = require('express')
let app = express()
let random_port = require('random-port')

let run = function(reporter, tradeObject) {
if (!reporter.port || reporter.port === 0) {
random_port({from: 20000}, function(port) {
startServer(port, tradeObject)
})
} else {
startServer(reporter.port, tradeObject)
}
}

let startServer = function(port, tradeObject) {
tradeObject.port = port

app.get('/trades', function (req, res) {
res.send(tradeObject)
})

app.listen(port)
tradeObject.url = require('ip').address() + ':' + port + '/trades'
console.log('api running on ' + tradeObject.url)
}

return {
run: run
}
}
1 change: 1 addition & 0 deletions lib/_codemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
'srsi': require('./srsi'),
'stddev': require('./stddev'),
'notify': require('./notify'),
'output': require('./output'),
'tcf': require('./tcf'),
'vma': require('./vma'),
'lrc': require('./lrc'),
Expand Down
18 changes: 18 additions & 0 deletions lib/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = function container (get) {
var c = get('conf')

var initializeOutput = function(tradeObject) {
for (var output in c.output) {
if (c.output[output].on) {
if (c.debug) {
console.log(`initializing output ${output}`)
}
get(`output.${output}`).run(c.output[output], tradeObject)
}
}
}

return {
initializeOutput: initializeOutput
}
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
"commander": "^2.9.0",
"convnetjs": "0.3.0",
"ccxt": "^1.10.171",
"express": "^4.16.2",
"forex.analytics": "mkmarek/forex.analytics#7bc278987700d4204e959af17de61495941d1a14",
"gdax": "^0.4.2",
"gemini-api": "^2.0.4",
"glob": "^7.1.1",
"har-validator": "^5.0.3",
"idgen": "^2.0.2",
"ip": "~1.1.5",
"kraken-api": "^0.1.7",
"mathjs": "3.16.5",
"micro-request": "^666.0.10",
Expand All @@ -48,6 +50,7 @@
"pushbullet": "2.0.0",
"pusher-js": "^4.1.0",
"quadrigacx": "0.0.7",
"random-port": "^0.1.0",
"run-parallel": "^1.1.6",
"run-series": "^1.1.4",
"regression": "^2.0.0",
Expand Down

1 comment on commit 4aadfd1

@albabosh
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I put c.output.api.port (not 0) into config zenbot crashes at startup. port=0 works ok. It seems that REST API uses 2 ports. Could you give a correct example of api config ? Thanks

Please sign in to comment.