Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

Commit 7442365

Browse files
committed
Move Bot.init logic outside the loop
1 parent 3553d26 commit 7442365

File tree

12 files changed

+149
-109
lines changed

12 files changed

+149
-109
lines changed

cli-examples/speed-test.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ var token = 'Replace with your own token';
22

33
console.log('Starting bot...');
44

5+
Bot.init(token, {
6+
symbol: 'R_100',
7+
candleInterval: 60,
8+
contractTypes: ['CALL', 'PUT'],
9+
});
10+
511
while (true) {
6-
Bot.start(token, {
7-
symbol: 'R_100',
12+
Bot.start({
813
amount: 1,
9-
basis: 'stake',
10-
candleInterval: 60,
11-
contractTypes: ['CALL', 'PUT'],
1214
currency: 'USD',
1315
duration: 2,
1416
duration_unit: 'h',

src/botPage/bot/Interface/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default class Interface extends ToolsInterface(TicksInterface(class {}))
3030
const getDetail = i => createDetails(this.get('contract'))[i]
3131

3232
return {
33+
init: (...args) => this.tradeEngine.init(...args),
3334
start: (...args) => this.tradeEngine.start(...args),
3435
stop: (...args) => this.tradeEngine.stop(...args),
3536
purchase: contractType => this.tradeEngine.purchase(contractType),

src/botPage/bot/TradeEngine/index.js

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Map } from 'immutable'
22
import { observer as globalObserver } from 'binary-common-utils/lib/observer'
3+
import { translate } from '../../..//common/i18n'
4+
import { createError } from '../../common/error'
35
import { doUntilDone } from '../tools'
4-
import { expectStartArg } from '../sanitize'
6+
import { expectInitArg } from '../sanitize'
57
import Proposal from './Proposal'
68
import Broadcast from './Broadcast'
79
import Total from './Total'
@@ -30,22 +32,30 @@ export default class TradeEngine extends Balance(Purchase(Sell(
3032
this.watches = new Map()
3133
this.signals = new Map()
3234
}
33-
start(...args) {
34-
const [token, tradeOption] = expectStartArg(args)
35+
init(...args) {
3536
this.watches = new Map()
3637
this.signals = new Map()
3738

38-
const { symbol } = tradeOption
39+
const [token, options] = expectInitArg(args)
3940

40-
this.checkLimits(tradeOption)
41+
const { symbol } = options
4142

42-
globalObserver.emit('bot.start', symbol)
43-
44-
this.makeProposals(tradeOption)
43+
this.options = options
4544

4645
this.startPromise = this.loginAndGetBalance(token)
4746

4847
this.waitBeforePurchase(symbol)
48+
49+
globalObserver.emit('bot.init', symbol)
50+
}
51+
start(tradeOptions) {
52+
if (!this.options) {
53+
throw createError('NotInitialized', translate('Bot.init is not called'))
54+
}
55+
56+
this.checkLimits(tradeOptions)
57+
58+
this.makeProposals({ ...this.options, ...tradeOptions })
4959
}
5060
loginAndGetBalance(token) {
5161
if (this.token === token) {

src/botPage/bot/__tests__/BotApi.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ describe('Interface', () => {
2222
let stay
2323

2424
beforeAll((done) => {
25-
Bot.start('Xkq6oGFEHh6hJH8', {
26-
amount: 1,
27-
basis: 'stake',
25+
Bot.init('Xkq6oGFEHh6hJH8', {
2826
candleInterval: 60,
2927
contractTypes: ['DIGITEVEN', 'DIGITODD'],
28+
symbol: 'R_100',
29+
})
30+
31+
Bot.start({
32+
amount: 1,
3033
currency: 'USD',
3134
duration: 5,
3235
duration_unit: 't',
33-
symbol: 'R_100',
3436
})
3537

3638
watch('before').then(c => {

src/botPage/bot/__tests__/UI.js

+67-54
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,83 @@ import { expectReturnTrue } from './tools'
22

33

44
expectReturnTrue('UI Generated Code', `
5-
(function(){
6-
var trade, before_purchase, during_purchase, after_purchase;
5+
(function(){
6+
var trade, before_purchase, during_purchase, after_purchase;
77
8-
function run(f) {
9-
if (f) return f();
10-
return false;
11-
}
8+
var tick_analysis_list = [];
129
13-
var limitations = {}
10+
function run(f, arg) {
11+
if (f) return f(arg);
12+
return false;
13+
}
1414
15-
var getTradeOptions;
16-
getTradeOptions = function getTradeOptions() {
17-
var tradeOptions = {}
18-
tradeOptions = {
19-
contractTypes: ['CALL', 'PUT'],
20-
candleInterval: '60',
21-
duration: 2,
22-
duration_unit: 'h',
23-
basis: 'stake',
24-
currency: 'USD',
25-
amount: 1,
26-
restartOnError: false,
27-
}
28-
tradeOptions.symbol = 'R_100'
29-
return tradeOptions
30-
}
15+
function tick_analysis() {
16+
for (var i = 0; i < tick_analysis_list.length; i++) {
17+
run(tick_analysis_list[i]);
18+
}
19+
}
20+
21+
var limitations = {};
22+
23+
var start;
24+
25+
start = function start() {
26+
Bot.start({
27+
limitations: limitations,
28+
duration: 2,
29+
duration_unit: 'h',
30+
currency: 'USD',
31+
amount: 1,
32+
prediction: undefined,
33+
barrierOffset: undefined,
34+
secondBarrierOffset: undefined,
35+
});
36+
}
3137
trade = function trade(){
32-
if (getTradeOptions !== undefined) {
33-
Bot.start('Xkq6oGFEHh6hJH8', getTradeOptions(), limitations);
34-
}
38+
Bot.init('Xkq6oGFEHh6hJH8', {
39+
symbol: 'R_100',
40+
contractTypes: ["CALL","PUT"],
41+
candleInterval: '60',
42+
});
3543
};
3644
37-
before_purchase = function before_purchase(){
38-
return Bot.purchase('CALL');
39-
};
45+
before_purchase = function before_purchase(){
46+
Bot.purchase('CALL');
4047
41-
during_purchase = function during_purchase(){
42-
if (Bot.isSellAvailable()) {
43-
Bot.sellAtMarket();
44-
}
4548
};
4649
47-
var count = 2;
48-
after_purchase = function after_purchase(){
49-
if (--count !== 0)
50-
return true;
51-
return false;
50+
during_purchase = function during_purchase(){
51+
if (Bot.isSellAvailable()) {
52+
Bot.sellAtMarket();
53+
}
54+
5255
};
5356
54-
while(true) {
55-
run(trade)
56-
while(watch('before')) {
57-
if (run(before_purchase)) {
58-
break
59-
}
60-
}
61-
while(watch('during')) {
62-
run(during_purchase)
57+
var count = 3;
58+
after_purchase = function after_purchase(){
59+
if (--count === 0) {
60+
return false;
6361
}
64-
if(!run(after_purchase)) {
65-
break;
66-
}
67-
}
68-
return count === 0;
69-
})()
70-
`)
62+
return true;
63+
};
64+
65+
66+
run(trade)
67+
while(true) {
68+
run(start)
69+
while(watch('before')) {
70+
tick_analysis();
71+
run(before_purchase);
72+
}
73+
while(watch('during')) {
74+
tick_analysis();
75+
run(during_purchase);
76+
}
77+
tick_analysis();
78+
if(!run(after_purchase)) {
79+
break;
80+
}
81+
}
82+
return count === 0
83+
})();`)
7184

src/botPage/bot/__tests__/block-tests/sanitize-test/Trade.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('Trade Definition Blocks', () => {
77

88
describe('Start arguments', () => {
99
beforeAll(done =>
10-
run('Bot.start("");').catch(e => {
10+
run('Bot.init("");').catch(e => {
1111
error = e
1212
done()
1313
}))
@@ -19,7 +19,10 @@ describe('Trade Definition Blocks', () => {
1919

2020
describe('Trade Option must be checked', () => {
2121
beforeAll(done =>
22-
run('Bot.start("SomeToken", { amount: "hello" });').catch(e => {
22+
run(`
23+
Bot.init('Sametoken');
24+
Bot.start({ amount: "hello" })
25+
`).catch(e => {
2326
error = e
2427
done()
2528
}))

src/botPage/bot/__tests__/tools.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import { expect } from 'chai'
22
import { createInterpreter } from '../cli'
33

4+
const init = `
5+
Bot.init('Xkq6oGFEHh6hJH8', {
6+
candleInterval: 60,
7+
contractTypes: ['CALL', 'PUT'],
8+
symbol: 'R_100',
9+
})
10+
`
11+
412
const start = duration => `
5-
Bot.start('Xkq6oGFEHh6hJH8', {
6-
amount: 1, basis: 'stake', candleInterval: 60,
7-
contractTypes: ['CALL', 'PUT'],
8-
currency: 'USD', ${duration}, symbol: 'R_100',
13+
${init}
14+
Bot.start({
15+
amount: 1,
16+
currency: 'USD', ${duration},
917
});
1018
`
1119

@@ -14,6 +22,7 @@ export const parts = {
1422
(function (){
1523
var result = {};
1624
`,
25+
init,
1726
timeTrade: start('duration: 2, duration_unit: "h"'),
1827
tickTrade: start('duration: 5, duration_unit: "t"'),
1928
waitToPurchase: `

src/botPage/bot/sanitize.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ export const expectPositiveNumber = (num, msg) => {
2424
return num
2525
}
2626

27-
const expectTradeOption = tradeOption => {
28-
const { symbol, contractTypes, amount, duration } = tradeOption
27+
const expectOptions = options => {
28+
const { symbol, contractTypes } = options
2929

3030
if (!symbol) {
3131
const error = createError('OptionError', translate('Underlying market is not selected'))
@@ -38,26 +38,32 @@ const expectTradeOption = tradeOption => {
3838
globalObserver.emit('Error', error)
3939
throw error
4040
}
41-
42-
expectPositiveInteger(duration, translate('Duration must be an integer'))
43-
44-
expectPositiveNumber(amount, translate('Amount must be a positive number'))
4541
}
4642

47-
export const expectStartArg = args => {
48-
const [token, tradeOption] = args
43+
export const expectInitArg = args => {
44+
const [token, options] = args
4945

5046
if (!token) {
5147
const error = createError('LoginError', translate('Please login'))
5248
globalObserver.emit('Error', error)
5349
throw error
5450
}
5551

56-
expectTradeOption(tradeOption)
52+
expectOptions(options)
5753

5854
return args
5955
}
6056

57+
export const expectTradeOptions = tradeOptions => {
58+
const { amount, duration } = tradeOptions
59+
60+
expectPositiveInteger(duration, translate('Duration must be an integer'))
61+
62+
expectPositiveNumber(amount, translate('Amount must be a positive number'))
63+
64+
return tradeOptions
65+
}
66+
6167
const isCandle = candle => candle instanceof Object &&
6268
(['open', 'high', 'low', 'close'])
6369
.every(key => isPositiveNumber(candle[key])) &&

src/botPage/view/View.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ export default class View {
490490
}
491491
})
492492

493-
globalObserver.register('bot.start', s => {
493+
globalObserver.register('bot.init', s => {
494494
if (symbol !== s) {
495495
stopTickListeners()
496496
symbol = s

src/botPage/view/blockly/blocks/trade/index.js

+6-18
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,13 @@ Blockly.JavaScript.trade = (block) => {
9696
[contractTypeSelector]
9797
// TODO: Assemble JavaScript into code variable.
9898
const code = `
99-
var getTradeOptions;
10099
${initialization.trim()}
101-
trade = function trade(limitations){
102-
if (getTradeOptions !== undefined) {
103-
var tradeOptions = getTradeOptions()
104-
Bot.start('${account.trim()}', {
105-
limitations: limitations,
106-
duration: tradeOptions.duration,
107-
duration_unit: tradeOptions.duration_unit,
108-
currency: tradeOptions.currency,
109-
amount: tradeOptions.amount,
110-
prediction: tradeOptions.prediction,
111-
barrierOffset: tradeOptions.barrierOffset,
112-
secondBarrierOffset: tradeOptions.secondBarrierOffset,
113-
symbol: '${block.getFieldValue('SYMBOL_LIST')}',
114-
contractTypes: ${JSON.stringify(contractTypeList)},
115-
candleInterval: '${candleIntervalValue}',
116-
});
117-
}
100+
trade = function trade(){
101+
Bot.init('${account.trim()}', {
102+
symbol: '${block.getFieldValue('SYMBOL_LIST')}',
103+
contractTypes: ${JSON.stringify(contractTypeList)},
104+
candleInterval: '${candleIntervalValue}',
105+
});
118106
};
119107
`
120108
return code

0 commit comments

Comments
 (0)