-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtradingAlgorithm.js
More file actions
198 lines (168 loc) · 7 KB
/
tradingAlgorithm.js
File metadata and controls
198 lines (168 loc) · 7 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
(function() {
var POSITIONS = {
LONG: 'buy',
SHORT: 'sell'
},
fs = require('fs'),
fxAPI = require('./api.js'),
POSITIONS_FOLDER = 'positions',
PIPS_FOR_TAKE_PROFIT = 0.0040,
PIPS_FOR_STOP_LOSS = 0.0010,
MAX_PIPS_SPREAD = 0.0003,
fxAPI;
function getInstrumentPosition(instrument, callback) {
fxAPI.getPositionForInstrument(instrument, callback);
}
function loadInstrumentData(instrument, callback) {
var path = POSITIONS_FOLDER + '/' + instrument;
var readFile = function() {
fs.readFile(path, function(err, data) {
if (err) throw err;
data = data.toString();
var jsonData = {};
if (data && typeof data === 'string') {
jsonData = JSON.parse(data);
}
if (typeof callback === 'function') callback(jsonData);
});
};
fs.exists(path, function(exists) {
if (exists) {
readFile();
} else {
saveInstrumentData(instrument, {}, readFile);
}
});
}
function saveInstrumentData(instrument, data, callback) {
fs.writeFile(POSITIONS_FOLDER + '/' + instrument, JSON.stringify(data), function(err) {
if (err) throw err;
if (typeof callback === 'function') callback();
});
}
function openPositionForInstrument(instrument, spread, currentPrice, positionDirection, callback) {
var units = '1000000'; //TODO: calculate this number
var stopLoss = '';
if (spread >= MAX_PIPS_SPREAD) {
console.log('spread too high to open ' + instrument + ' [' + spread + ']');
return;
}
if (positionDirection === POSITIONS.LONG) {
stopLoss = currentPrice - PIPS_FOR_STOP_LOSS;
} else if (positionDirection === POSITIONS.SHORT) {
stopLoss = currentPrice + PIPS_FOR_STOP_LOSS;
} else {
console.log('Error: positionDirection not set in openPositionForInstrument');
return;
}
console.log('opening ' + instrument + '[' + positionDirection + '] @ price: ' + currentPrice + ' with stopLoss: ' + stopLoss);
fxAPI.openPosition(instrument, units, positionDirection, stopLoss, callback);
}
function closePositionForInstrument(instrument, callback) {
fxAPI.closePosition(instrument, callback);
}
function getTransactions(callback) {
fxAPI.getTransactions(callback);
}
var publicReturn = {
updatePrice: function(instrument, ask, bid) {
console.log('--- 2. Get position: ' + instrument);
getInstrumentPosition(instrument, function(instrumentData) {
console.log(instrumentData);
var midPoint = (ask + bid) / 2;
var spread = Math.abs(ask - bid);
function decide(instrumentData) {
var currentDelta = midPoint - instrumentData.avgPrice;
console.log(instrument + ' -- currentDelta: ' + currentDelta + ' position: ' + instrumentData.side);
if (instrumentData.side === POSITIONS.LONG) {
if (currentDelta >= PIPS_FOR_TAKE_PROFIT) {
//take profit, stay long
console.log('--- 3. take profit, stay long');
closePositionForInstrument(instrument, function() {
console.log('position closed for profit');
//TODO: stop loss not exact since it's a market order it might not execute exactly at midPoint price
openPositionForInstrument(instrument, spread, midPoint, POSITIONS.LONG, function(createdPosition) {
console.log('new position:');
console.log(createdPosition);
if (createdPosition.instrument && createdPosition.instrument === instrument && createdPosition.price) {
//TODO: don't create an object to save from scratch, use existing structure
saveInstrumentData(instrument, {side: POSITIONS.LONG, price: createdPosition.price}, function() {
console.log('saved localInstrumentData.');
});
} else {
console.log('Error creating position for ' + instrument);
}
});
});
}
} else if (instrumentData.side === POSITIONS.SHORT) {
if (currentDelta <= -PIPS_FOR_TAKE_PROFIT) {
//take profit, stay short
console.log('--- 3. take profit, stay short');
closePositionForInstrument(instrument, function() {
console.log('position closed for profit');
//TODO: stop loss not exact since it's a market order it might not execute exactly at midPoint price
openPositionForInstrument(instrument, spread, midPoint, POSITIONS.SHORT, function(createdPosition) {
console.log('new position:');
console.log(createdPosition);
if (createdPosition.instrument && createdPosition.instrument === instrument && createdPosition.price) {
//TODO: don't create an object to save from scratch, use existing structure
saveInstrumentData(instrument, {side: POSITIONS.SHORT, price: createdPosition.price}, function() {
console.log('saved localInstrumentData.');
});
} else {
console.log('Error creating position for ' + instrument);
}
});
});
}
}
}
if (instrumentData.code && instrumentData.code === 14) {
console.log('No position for ' + instrument);
//No position for this instrument
loadInstrumentData(instrument, function(localInstrumentData) {
localInstrumentData = localInstrumentData || {};
console.log('localInstrumentData:');
console.log(localInstrumentData);
if (typeof localInstrumentData.side === 'undefined') {
localInstrumentData.side = localInstrumentData.side || POSITIONS.LONG;
}
if (localInstrumentData.price) {
//TODO: better detect stoploss (can use transaction history)
//somewhat detect if position doesn't exist from stoploss being triggered.
//if it was triggered from stoploss, then switch direction
if (localInstrumentData.side === POSITIONS.LONG && localInstrumentData.price - midPoint >= PIPS_FOR_STOP_LOSS) {
localInstrumentData.side = POSITIONS.SHORT;
console.log('stoploss happened, switch to ' + localInstrumentData.side);
} else if (localInstrumentData.side === POSITIONS.SHORT && localInstrumentData.price - midPoint <= -PIPS_FOR_STOP_LOSS) {
localInstrumentData.side = POSITIONS.LONG;
console.log('stoploss happened, switch to ' + localInstrumentData.side);
}
}
//TODO: stop loss not exact since it's a market order it might not execute exactly at midPoint price
openPositionForInstrument(instrument, spread, midPoint, localInstrumentData.side, function(createdPosition) {
console.log('created position:');
console.log(createdPosition);
if (createdPosition.instrument && createdPosition.instrument === instrument && createdPosition.price) {
localInstrumentData.price = createdPosition.price;
saveInstrumentData(instrument, localInstrumentData, function() {
console.log('saved localInstrumentData.');
});
} else {
console.log('Error creating position for ' + instrument);
}
});
});
} else if (instrumentData.instrument && instrumentData.instrument === instrument) {
//existing position
decide(instrumentData);
}
});
}
};
module.exports = function(apiObject) {
fxAPI = apiObject;
return publicReturn;
};
})();