@@ -120,47 +120,44 @@ module.exports = function (program, conf) {
120
120
keyMap . set ( 'd' , 'dump statistical output to HTML file' . grey )
121
121
keyMap . set ( 'D' , 'toggle automatic HTML dump to file' . grey )
122
122
123
+ var pushStr = ''
124
+
123
125
function listKeys ( ) {
124
- console . log ( '\nAvailable command keys:')
126
+ printLog ( 'Available command keys:', true )
125
127
keyMap . forEach ( ( value , key ) => {
126
- console . log ( ' ' + key + ' - ' + value )
128
+ printLog ( ' ' + key + ' - ' + value )
127
129
} )
128
130
}
129
131
130
132
function listOptions ( ) {
131
- console . log ( )
132
- console . log ( s . exchange . name . toUpperCase ( ) + ' exchange active trading options:' . grey )
133
- console . log ( )
134
- process . stdout . write ( z ( 22 , 'STRATEGY' . grey , ' ' ) + '\t' + so . strategy + '\t' + ( require ( `../extensions/strategies/${ so . strategy } /strategy` ) . description ) . grey )
135
- console . log ( '\n' )
136
- process . stdout . write ( [
133
+ printLog ( s . exchange . name . toUpperCase ( ) + ' exchange active trading options:' . grey , true )
134
+ printLog ( z ( 22 , 'STRATEGY' . grey , ' ' ) + '\t' + so . strategy + '\t' + ( require ( `../extensions/strategies/${ so . strategy } /strategy` ) . description ) . grey , true )
135
+ printLog ( [
137
136
z ( 24 , ( so . mode === 'paper' ? so . mode . toUpperCase ( ) : so . mode . toUpperCase ( ) ) + ' MODE' . grey , ' ' ) ,
138
137
z ( 26 , 'PERIOD' . grey , ' ' ) ,
139
138
z ( 30 , 'ORDER TYPE' . grey , ' ' ) ,
140
139
z ( 28 , 'SLIPPAGE' . grey , ' ' ) ,
141
140
z ( 33 , 'EXCHANGE FEES' . grey , ' ' )
142
- ] . join ( '' ) + '\n' )
143
- process . stdout . write ( [
141
+ ] . join ( '' ) , true )
142
+ printLog ( [
144
143
z ( 15 , ( so . mode === 'paper' ? ' ' : ( so . mode === 'live' && ( so . manual === false || typeof so . manual === 'undefined' ) ) ? ' ' + 'AUTO' . black . bgRed + ' ' : ' ' + 'MANUAL' . black . bgGreen + ' ' ) , ' ' ) ,
145
144
z ( 13 , so . period_length , ' ' ) ,
146
145
z ( 29 , ( so . order_type === 'maker' ? so . order_type . toUpperCase ( ) . green : so . order_type . toUpperCase ( ) . red ) , ' ' ) ,
147
146
z ( 31 , ( so . mode === 'paper' ? 'avg. ' . grey + so . avg_slippage_pct + '%' : 'max ' . grey + so . max_slippage_pct + '%' ) , ' ' ) ,
148
147
z ( 20 , ( so . order_type === 'maker' ? so . order_type + ' ' + s . exchange . makerFee : so . order_type + ' ' + s . exchange . takerFee ) , ' ' )
149
- ] . join ( '' ) + '\n' )
150
- process . stdout . write ( '' )
151
- process . stdout . write ( [
148
+ ] . join ( '' ) )
149
+ printLog ( [
152
150
z ( 19 , 'BUY %' . grey , ' ' ) ,
153
151
z ( 20 , 'SELL %' . grey , ' ' ) ,
154
152
z ( 35 , 'TRAILING STOP %' . grey , ' ' ) ,
155
153
z ( 33 , 'TRAILING DISTANCE %' . grey , ' ' )
156
- ] . join ( '' ) + '\n' )
157
- process . stdout . write ( [
154
+ ] . join ( '' ) )
155
+ printLog ( [
158
156
z ( 9 , so . buy_pct + '%' , ' ' ) ,
159
157
z ( 9 , so . sell_pct + '%' , ' ' ) ,
160
158
z ( 20 , so . profit_stop_enable_pct + '%' , ' ' ) ,
161
159
z ( 20 , so . profit_stop_pct + '%' , ' ' )
162
- ] . join ( '' ) + '\n' )
163
- process . stdout . write ( '' )
160
+ ] . join ( '' ) )
164
161
}
165
162
166
163
/* Implementing statistical Exit */
@@ -226,7 +223,7 @@ module.exports = function (program, conf) {
226
223
}
227
224
if ( ! statsonly ) {
228
225
output_lines . forEach ( function ( line ) {
229
- console . log ( line )
226
+ printLog ( line )
230
227
} )
231
228
}
232
229
if ( quit || dump ) {
@@ -275,9 +272,9 @@ module.exports = function (program, conf) {
275
272
function toggleStats ( ) {
276
273
shouldSaveStats = ! shouldSaveStats
277
274
if ( shouldSaveStats )
278
- console . log ( 'Auto stats dump enabled' )
275
+ printLog ( 'Auto stats dump enabled' )
279
276
else
280
- console . log ( 'Auto stats dump disabled' )
277
+ printLog ( 'Auto stats dump disabled' )
281
278
}
282
279
283
280
function saveStatsLoop ( ) {
@@ -368,6 +365,79 @@ module.exports = function (program, conf) {
368
365
369
366
}
370
367
368
+ function printLog ( str , cr = false ) {
369
+ if ( str ) {
370
+ console . log ( ( cr ?'\n' :'' ) + str )
371
+ pushStr += str + '\n'
372
+ }
373
+ }
374
+
375
+ function executeCommand ( command ) {
376
+ var info = { ctrl : false }
377
+ if ( conf . debug ) {
378
+ console . log ( '\nCommand received: ' + command )
379
+ }
380
+ executeKey ( command , info )
381
+ }
382
+
383
+ function executeKey ( key , info ) {
384
+ if ( key === 'l' ) {
385
+ listKeys ( )
386
+ } else if ( key === 'b' && ! info . ctrl ) {
387
+ engine . executeSignal ( 'buy' )
388
+ printLog ( 'manual' . grey + ' limit ' + 'BUY' . green + ' command executed' . grey , true )
389
+ } else if ( key === 'B' && ! info . ctrl ) {
390
+ engine . executeSignal ( 'buy' , null , null , false , true )
391
+ printLog ( 'manual' . grey + ' market ' + 'BUY' . green + ' command executed' . grey , true )
392
+ } else if ( key === 's' && ! info . ctrl ) {
393
+ engine . executeSignal ( 'sell' )
394
+ printLog ( 'manual' . grey + ' limit ' + 'SELL' . red + ' command executed' . grey , true )
395
+ } else if ( key === 'S' && ! info . ctrl ) {
396
+ engine . executeSignal ( 'sell' , null , null , false , true )
397
+ printLog ( 'manual' . grey + ' market ' + 'SELL' . red + ' command executed' . grey , true )
398
+ } else if ( ( key === 'c' || key === 'C' ) && ! info . ctrl ) {
399
+ delete s . buy_order
400
+ delete s . sell_order
401
+ printLog ( 'manual' . grey + ' order cancel' + ' command executed' . grey , true )
402
+ } else if ( key === 'm' && ! info . ctrl && so . mode === 'live' ) {
403
+ so . manual = ! so . manual
404
+ printLog ( 'MANUAL trade in LIVE mode: ' + ( so . manual ? 'ON' . green . inverse : 'OFF' . red . inverse ) , true )
405
+ } else if ( key === 'T' && ! info . ctrl ) {
406
+ so . order_type = 'taker'
407
+ printLog ( 'Taker fees activated' . bgRed , true )
408
+ } else if ( key === 'M' && ! info . ctrl ) {
409
+ so . order_type = 'maker'
410
+ printLog ( 'Maker fees activated' . black . bgGreen , true )
411
+ } else if ( key === 'o' && ! info . ctrl ) {
412
+ listOptions ( )
413
+ } else if ( key === 'O' && ! info . ctrl ) {
414
+ printLog ( cliff . inspect ( so ) , true )
415
+ } else if ( key === 'P' && ! info . ctrl ) {
416
+ printLog ( 'Writing statistics...' . grey , true )
417
+ printTrade ( false )
418
+ } else if ( key === 'X' && ! info . ctrl ) {
419
+ printLog ( 'Exiting... ' + '\nWriting statistics...' . grey , true )
420
+ printTrade ( true )
421
+ } else if ( key === 'd' && ! info . ctrl ) {
422
+ printLog ( 'Dumping statistics...' . grey , true )
423
+ printTrade ( false , true )
424
+ } else if ( key === 'D' && ! info . ctrl ) {
425
+ printLog ( 'Dumping statistics...' . grey , true )
426
+ toggleStats ( )
427
+ } else if ( key === 'L' && ! info . ctrl ) {
428
+ debug . flip ( )
429
+ printLog ( 'DEBUG mode: ' + ( debug . on ? 'ON' . green . inverse : 'OFF' . red . inverse ) , true )
430
+ } else if ( info . name === 'c' && info . ctrl ) {
431
+ // @todo : cancel open orders before exit
432
+ process . exit ( )
433
+ }
434
+
435
+ if ( pushStr ) {
436
+ engine . pushMessage ( 'Reply' , colors . stripColors ( pushStr ) )
437
+ pushStr = ''
438
+ }
439
+ }
440
+
371
441
var order_types = [ 'maker' , 'taker' ]
372
442
if ( ! order_types . includes ( so . order_type ) ) {
373
443
so . order_type = 'maker'
@@ -483,62 +553,13 @@ module.exports = function (program, conf) {
483
553
484
554
forwardScan ( )
485
555
setInterval ( forwardScan , so . poll_trades )
556
+ if ( ! so . non_interactive ) {
557
+ engine . onMessage ( executeCommand )
558
+ }
486
559
readline . emitKeypressEvents ( process . stdin )
487
560
if ( ! so . non_interactive && process . stdin . setRawMode ) {
488
561
process . stdin . setRawMode ( true )
489
- process . stdin . on ( 'keypress' , function ( key , info ) {
490
- if ( key === 'l' ) {
491
- listKeys ( )
492
- } else if ( key === 'b' && ! info . ctrl ) {
493
- engine . executeSignal ( 'buy' )
494
- console . log ( '\nmanual' . grey + ' limit ' + 'BUY' . green + ' command executed' . grey )
495
- } else if ( key === 'B' && ! info . ctrl ) {
496
- engine . executeSignal ( 'buy' , null , null , false , true )
497
- console . log ( '\nmanual' . grey + ' market ' + 'BUY' . green + ' command executed' . grey )
498
- } else if ( key === 's' && ! info . ctrl ) {
499
- engine . executeSignal ( 'sell' )
500
- console . log ( '\nmanual' . grey + ' limit ' + 'SELL' . red + ' command executed' . grey )
501
- } else if ( key === 'S' && ! info . ctrl ) {
502
- engine . executeSignal ( 'sell' , null , null , false , true )
503
- console . log ( '\nmanual' . grey + ' market ' + 'SELL' . red + ' command executed' . grey )
504
- } else if ( ( key === 'c' || key === 'C' ) && ! info . ctrl ) {
505
- delete s . buy_order
506
- delete s . sell_order
507
- console . log ( '\nmanual' . grey + ' order cancel' + ' command executed' . grey )
508
- } else if ( key === 'm' && ! info . ctrl && so . mode === 'live' ) {
509
- so . manual = ! so . manual
510
- console . log ( '\nMANUAL trade in LIVE mode: ' + ( so . manual ? 'ON' . green . inverse : 'OFF' . red . inverse ) )
511
- } else if ( key === 'T' && ! info . ctrl ) {
512
- so . order_type = 'taker'
513
- console . log ( '\n' + 'Taker fees activated' . bgRed )
514
- } else if ( key === 'M' && ! info . ctrl ) {
515
- so . order_type = 'maker'
516
- console . log ( '\n' + 'Maker fees activated' . black . bgGreen )
517
- } else if ( key === 'o' && ! info . ctrl ) {
518
- listOptions ( )
519
- } else if ( key === 'O' && ! info . ctrl ) {
520
- console . log ( '\n' + cliff . inspect ( so ) )
521
- } else if ( key === 'P' && ! info . ctrl ) {
522
- console . log ( '\nWriting statistics...' . grey )
523
- printTrade ( false )
524
- } else if ( key === 'X' && ! info . ctrl ) {
525
- console . log ( '\nExiting... ' + '\nWriting statistics...' . grey )
526
- printTrade ( true )
527
- } else if ( key === 'd' && ! info . ctrl ) {
528
- console . log ( '\nDumping statistics...' . grey )
529
- printTrade ( false , true )
530
- } else if ( key === 'D' && ! info . ctrl ) {
531
- console . log ( '\nDumping statistics...' . grey )
532
- toggleStats ( )
533
- } else if ( key === 'L' && ! info . ctrl ) {
534
- debug . flip ( )
535
- console . log ( '\nDEBUG mode: ' + ( debug . on ? 'ON' . green . inverse : 'OFF' . red . inverse ) )
536
- } else if ( info . name === 'c' && info . ctrl ) {
537
- // @todo : cancel open orders before exit
538
- console . log ( )
539
- process . exit ( )
540
- }
541
- } )
562
+ process . stdin . on ( 'keypress' , executeKey )
542
563
}
543
564
} )
544
565
} )
0 commit comments