Skip to content

Commit b0ee8ca

Browse files
committed
level up
1 parent 60d4054 commit b0ee8ca

2 files changed

Lines changed: 46 additions & 23 deletions

File tree

src/kingapistream.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { STREAMING_TRADE_RECORD } from 'xapi-node'
22
import SocketApiRobot from './kingapisocket'
3-
import { takeProfits } from './profits'
3+
import { checkProfits } from './profits'
44

55
export default class StreamingApiRobot extends SocketApiRobot {
66

7-
protected takeProfits: Function
7+
protected checkProfits: Function
88

99
constructor() {
1010
super()
11-
this.takeProfits = takeProfits
11+
this.checkProfits = checkProfits
1212
this.xapi.Stream.listen.getTrades(this.tradeEvent.bind(this))
1313
}
1414

@@ -24,7 +24,7 @@ export default class StreamingApiRobot extends SocketApiRobot {
2424

2525
private async tradeEvent (data: STREAMING_TRADE_RECORD): Promise<void> {
2626
this.printTrades([data])
27-
this.takeProfits(data)
27+
this.checkProfits(data)
2828
this.log(data)
2929
}
3030

src/profits.ts

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,57 @@ import KingBot from './kingbot'
1010

1111
type UpdateOrderEvent = Partial<TRADE_TRANS_INFO>
1212

13-
function getStopLoss(data: STREAMING_TRADE_RECORD, trades: TRADE_RECORD[]) {
13+
function _isBuyOrder(cmd: number): boolean {
14+
return ['BUY', 'BUY_LIMIT', 'BUY_STOP'].includes(CMD_FIELD[cmd])
15+
}
16+
17+
function getLevel(data: STREAMING_TRADE_RECORD): number {
18+
const isBuyOrder = _isBuyOrder(data.cmd)
19+
function stoplossWorseThanEntry(): boolean {
20+
return isBuyOrder ? data.sl < data.open_price : data.sl > data.open_price
21+
}
22+
if (stoplossWorseThanEntry()) {
23+
return data.open_price
24+
}
25+
const level = (data.open_price + data.close_price) / 2
26+
console.log('LEVEL', level, '=', data.open_price, '+', data.close_price, '/', 2)
27+
return level
28+
}
29+
30+
function getStopLoss(data: STREAMING_TRADE_RECORD): number {
1431
const margin = data.open_price * 0.0003
15-
const betterment = data.cmd === CMD_FIELD.BUY ? margin : margin * -1
16-
const tps: number[] = trades.map(trade => trade.tp)
17-
console.log('getStopLoss', tps)
18-
return parseInt(`${data.open_price + betterment}`) // TODO: Don't use parseInt: Need to check trade.digits and truncate
32+
const isBuyOrder = _isBuyOrder(data.cmd)
33+
const betterment = isBuyOrder ? +margin : -margin
34+
const level = getLevel(data)
35+
const stopLoss = +(level + betterment).toFixed(data.digits)
36+
console.log('STOP LOSS:', stopLoss, '=', level, '+', betterment)
37+
return stopLoss
1938
}
2039

21-
function getTransaction(data: STREAMING_TRADE_RECORD, trades: TRADE_RECORD[]) {
22-
return {
40+
async function setFamilyStoploss( data: STREAMING_TRADE_RECORD,
41+
trades: TRADE_RECORD[],
42+
tradeTransaction: Function,
43+
error: Function
44+
): Promise<void> {
45+
console.log('Updating stop loss for', trades.length, 'orders')
46+
const transaction: UpdateOrderEvent = {
2347
type: TYPE_FIELD.MODIFY,
24-
sl: getStopLoss(data, trades),
48+
sl: getStopLoss(data),
49+
}
50+
for (const trade of trades) {
51+
transaction.order = trade.order
52+
console.log('transaction', transaction)
53+
// The transaction will fail if the take-profit is "worse" than the entry price
54+
await tradeTransaction(<TRADE_TRANS_INFO>transaction).catch(error)
2555
}
2656
}
2757

28-
export async function takeProfits (this: KingBot, data: STREAMING_TRADE_RECORD): Promise<void> {
58+
export async function checkProfits (this: KingBot, data: STREAMING_TRADE_RECORD): Promise<void> {
2959
if (data.closed && data.comment === '[T/P]') {
3060
console.log('TAKE PROFIT', data)
31-
const trades: TRADE_RECORD[] = await this.getFamilyTrades(data)
32-
const transaction: UpdateOrderEvent = getTransaction(data, trades)
33-
const tradeTransaction = this.xapi.Socket.send.tradeTransaction
34-
trades.length && console.log('Updating stop loss for', trades.length, 'orders')
35-
36-
for (const trade of trades) {
37-
transaction.order = trade.order
38-
console.log('transaction', transaction)
39-
// The transaction will fail if the take-profit is "worse" than the entry price
40-
await tradeTransaction(<TRADE_TRANS_INFO>transaction).catch(this.console.error)
61+
const trades: TRADE_RECORD[] = await this.getFamilyTrades(data) // TODO Make sure we don't include any pending orders
62+
if (trades.length > 0) {
63+
await setFamilyStoploss(data, trades, this.xapi.Socket.send.tradeTransaction, this.console.error)
4164
}
4265
await this.printAllTrades()
4366
}

0 commit comments

Comments
 (0)