-
Notifications
You must be signed in to change notification settings - Fork 2
/
trade1.js
106 lines (85 loc) · 2.77 KB
/
trade1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Test subscribing to new trades from the Push API
// and processing them, namely for BTC_ETH
var colors = require('colors');
var autobahn = require('autobahn');
//var bus = require('statebus-server');
var wsuri = "wss://api.poloniex.com";
var Poloniex = require('./lib/poloniex'),
// When using as an NPM module, use `require('poloniex.js')`
const configJson = require('config.json')
config = configJson(configFile)
apiKey = config['apiKey']
secret = config['secret']
// Create a new instance, with optional API key and secret
poloniex = new Poloniex(apiKey, secret);
// * IMPORTANT *
// The line below is temporary, to avoid API server certficiate failure `Error: CERT_UNTRUSTED`
// This is presumably a temporary issue and has been reported to Poloniex.
// Do not include the line below once the issue is resolved.
Poloniex.STRICT_SSL = false;
var connection = new autobahn.Connection({
url: wsuri,
realm: "realm1"
});
function sendMessage(msg) {
var child;
child = exec("/home/ubuntu/venv/slack/bin/python slacktest.py '" + msg + "'",
function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
}
});
}
var CandleManager = require('./candleManager');
var Direction = require('./direction');
var StateMachine = require('./stateMachine');
var m = StateMachine();
var direction = Direction(3, m);
var candleManager = new CandleManager(direction);
m.onTopOut(function() {
console.log("Topping out, sell at ");
console.log(JSON.stringify(candleManager));
console.log(candleManager.lastPrice);
});
m.onBottomOut(function() {
console.log("Bottoming out, buy at ");
console.log(JSON.stringify(candleManager));
console.log(candleManager.lastPrice);
});
m.onRising(function() {
console.log("Rising");
});
m.onFalling(function() {
console.log("Falling");
});
m.onNeutral(function() {
console.log("Neutral");
});
var btcEthEvent = function(args, kwargs) {
//console.log(args);
for (var i in args) {
var item = args[i];
var data = item['data'];
var eventType = item['type'];
// We assume all event types have a subtype in their data
var type = data['type'];
if (eventType === "newTrade") {
tradeID = data['tradeID'];
price = parseFloat(data['rate']);
amount = parseFloat(data['amount']);
date = data['date'];
total = data['total'];
try {
candleManager.minuteHandler(price, amount);
} catch (e) {
console.error("candleManager.minuteHandler " + e);
}
console.log("New Trade ID: " + tradeID + " " + type + " of " + amount + " @ " + rate + " for a total of " + total + " on " + date);
}
}
};
connection.onopen = function (session) {
console.log("Started up");
session.subscribe("BTC_ETH", btcEthEvent);
};
connection.open();