-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.sh
More file actions
1282 lines (943 loc) · 61.5 KB
/
bot.sh
File metadata and controls
1282 lines (943 loc) · 61.5 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# TO RESOLVE THE FAST BUY SITUATION!!! AT THE MOMENT, THE BOT IS BUYING BASED ON NUMBERS ONLY!
trade_type=simulate
#can be simulate or live
simulate_file=simulate/pricelist
#location of simulation file
sim_coin1=1
#if we simulate, we start with this number of coin 1
sim_coin2=0
#if we simulate, we start with this number of coin 2
market_fee=0.1
#trading fee on binance
getassetsvariable=1
strategy=buy_low_sell_high_strategy
# this can be either buy_low_sell_high_strategy. In the future, i hope to add more strategys
############################################################variables################
# add last action to menu, add info about panic selling value....
#secondlast -second last price
#getliveprice - function to get the live price
#####################################################################################
buy_price_smaller_than_sale_price=true
#######################################################################################
#high - the highest price of the coin
#low - the lowest price of the coin
#failsafe=panic sell
coin=BTCUSDT
#change here if you want a different coin
#coin - the selected pair of coins
##############################################################################
buypercent=0.2
#buy percentage
##############################################################################
sellpercent=0.2
#sell percentage
#############################################################################
failsafebuypercent=1
failsafesellpercent=1 ######will have to delete it in the future
#After 1 failsafe triggered, the default buy and sell values will change to this fallback values
#############################################################################
stoploss=true
#if true, this will not sell bellow the last buy price, other options: "false" to disable it
##############################################################################################
minimumprofitpercent=0.2
#this is the minimum profit the bot will sell for if more than 0. to disable. set this to 0
##############################################################################################
failsafe=0
#this is the failsafe of the bot. It will sell everything once the bot is reachis a loss of the set % , after it buys. to disable, set it to 0
readingerrorpercent=2 # this is the maximum allowed percent between readings.ex. if a reading is smaller or bigger than a average reading! happened in the testing!!!
################################################################################################
#change this if you want a different update time for binance price readings. it does not have any effect in simulate mode!
binanceupdateseconds=3
######################################################################################################################################################################################################
rdg=10
#change this to change the number of readings required for smooting. This is required because the price is fluctuating a lot. So the bot takes this number of reagings,
# cut the spiketop number from the top, the spikebottom number from the bottom, and creates an average of the remaining midddle numbers
#example : if the prices are 1,2,3,4,5,6,7,8,9,10 ( this is rdg number), spike bottom will remove 1 and 2, spiketop will remove 9 and 10 and the bot makes an average of this
#ALSO IMPORTANT: THIS NUMBER MUST BE BIGGER THAN spikebottom + spiketop+1
#numbers of spikes reading to cut from the top. this cannot be more than rdg
spiketop=4
#numbers of spikes reading to cut from the bottom. this cannot be more than rdg
spikebottom=4
#######################################################################################################################################################################################################
buysellretryseconds=240
#this is the time that the bot is trying to retry a buy or sell in seconds
#################################################################################################################################################################################################
sellbuyretry=10
#this is the number of retry the bot is trying to place a buy or sell order...................
######################################################################################################################################################################################################
order_type=taker
#this is the order type of the bot. it can be taker or maker..... ( taker means the price is market price, on maker, the bot sets the price
#taker is market price, on maker we set the price.....................................
####################
####################################################################################################################################################################################
zenbotpath="/home/lolren/zenbot"
#this is the path to zenbot. currently this bot requires zenbot as a buy and sell backend. WARNING , DO NOT CHANGE THIS UNLESS YOU KNOW WHAT YOU ARE DOING !!!!!!
###########################################################################################################################################################################################################
tradelog="/home/lolren/bot//logs/tradelog.txt"
# default log tradelog path
#######################################################################################END OF CONFIG FILES!!!!!!!!!!!#######################################################################################
default_email=lor3n4you200@gmail.com
send_mail=no # this will send or not send a email on each transaction!
#the trade bot sends an email from a gmail account
####### if you want to set your own gmail account sender, change the value from : /home/zenbot/.msmtprc and /home/zenbot/.msmtprc
### if you don`t care about it, just modify the above mail and use your email.
start_price_as_last_buy_price=false
#can be true or false- THIS WILL DISABLE PANIC FOR THE FIRST ACTION, IF THE COIN WILL GO DOWN, IT WON`T PROTECT THE MONEY!!!!!
#########################################################################
##################################multicoin...not enabled currently####################################
###########################################################WARNING###########################################################
######################################################BOT VARIABLES##########################################################
######################################################DO NOT CHANGE!!!!!!!!##################################################
oldbuypercent=$buypercent
oldsellpercent=$sellpercent
paniccounter=0
buyretrycount=0
sellretrycount=0
sellcheckswitch=0
buycheckswitch=0
sellswitch=0 #this should be deprecated
buyswitch=0 #this should be deprecated
panicnr=0
ramdirectory=/mnt/lpmbot
#lastbuyprice=N/A #because of this and the stoploss option, script cannot beging with a sale.
lastbuypriceswitch=1 # this will set the bot with the first read price as the buy price
lastsellprice=N/A
manualkeypressswitch=1
firsruntimer=0
allow_buy=1
runtimecounter=0 # this is the runtime counter that adds 1 at every minute of script run! ( for now, just when multicoin is enabled, but that will change in the future)
cut="#" # this ensures you don`t see all the unavailable multicoin options
#######
gainpercentswich=1
####################################these switches change to 1 after the time in their name ===
#################hack to simulate the assets understanding. this will be changed soon
#############################################################################bot start, nothing to change beyond this point###########################################################################
#############################multicoin start colours######################
#######################################variables##################################################################
#sold=1
#bought=0
readingsnr=0
tradesnr=0
getassets=1
##############################################end of hack
#clear the pricelist and sorted files at first run from previous sessions
getassetsvariable=1 ####this is the variable that change the bot from getting the assets again
path=`pwd` ######get the path of the script required for the bot to come back after it`s calling zenbot
############################################################keypres#######################
#############################################################################################
####################################Functions#########################################
#############################################################rootcheck#######################################################
#this function checks for root, create a mount directory and a mounts a ramdisk. this is necesary because the number of writes the bot makes
rootcheck () {
if [ "$EUID" -ne 0 ]
then
echo "Please run as root. As user, I will make create and update files on hdd, but i rather do it on ram!"
sleep 5
root=0
else
root=1
if [ -d $ramdirectory ]
then
echo "Directory already exists"
if mountpoint -q $ramdirectory ; then
echo "It's mounted."
else
echo "It's not mounted1."
mount -t tmpfs -o size=500m tmpfs $ramdirectory
echo $ramdirectory
fi
else
mkdir $ramdirectory
if mountpoint -q $ramdirectory ; then
echo "It's mounted."
else
echo "It's not mounted."
mount -t tmpfs -o size=500m tmpfs $ramdirectory
echo creating ramdisk
fi
fi
echo I am root
fi
if [ "$root" = "1" ] ; then
buysellcheck=/mnt/lpmbot/buysellcheck
pricelist=/mnt/lpmbot/pricelist
sorted=/mnt/lpmbot/sorted
assetlist=/mnt/lpmbot/assetlist
allpricesfile=/mnt/lpmbot/allpricesfile
truncate -s 0 $sorted
truncate -s 0 $pricelist
truncate -s 0 $assetlist
truncate -s 0 $allpricesfile
truncate -s 0 $buysellcheck
date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"
echo all files will be mounted in temp directory
if [ "$trade_type" = "live" ] ; then
loop
else
simulate_loop
fi
else
buysellcheck=buysellcheck
pricelist=pricelist
sorted=sorted
assetlist=assetlist
allpricesfile=allpricesfile
truncate -s 0 $pricelist
truncate -s 0 $sorted
truncate -s 0 $assetlist
truncate -s 0 $allpricesfile
truncate -s 0 $buysellcheck
#############################multicoin options############################################################
##################################################END OF MULTICOIN OPTIONS##############################################
echo all files will be in local directory
sleep 2
if [ "$trade_type" = "live" ] ; then
loop
else
simulate_loop
fi
fi
}
coinselect () {
############this function will get the 2 coins from the exchange name given as a variable ($1). For example it will split up ETHUSDT to : coin1=ETH, coin2=USDT
#there are for main market on binance ( BNB, ETH, USDT and BTC)
if [[ $(echo $1 |tail -c 5) = "USDT" ]] ; then
coin2main=USDT
coin1main=`echo $1 | sed "s/USDT.*//"`
else
coin2main=`echo $1 | tail -c 4 | cut -d ' ' -f4`
coin1main=`echo $1 | sed "s/$coin2main.*//"`
fi
}
coinmarketcap () {
#this function is getting the prices from coinmarketcap, in order to know the ammount of coin it has.......
# usage : call coinmarketcap ETH for example
python coinmarketcap/coinmarketcapbridge.py | grep -E -o ".{0,0}$1'.{0,175}" | grep -E -o ".{0,0}price.{0,15}" | sed 's/[^0-9.]//g'
}
findassetvalue () {
##########first, we need to get the type of the coins we own...
### the variable of the exchange coins are :
# $coin1main and $coin2main
#####################################if getassets variable is 1, the variable coin
if [ "trade_type" = "live" ] ; then
if [ "$getassets"="1" ] ; then
if [ "$coin1main" != "BCC" ] ; then pricecoin1=$(coinmarketcap $coin1main)
else
pricecoin1=$(coinmarketcap BCH)
fi
pricecoin2=$(coinmarketcap $coin2main)
echo the price of coin1 is $pricecoin1
echo the price of coin2 is $pricecoin2
#############################
#now, we will see what asset will have more , so we know if we buy first or sell first
#our asset variables are :
#$coin1mainfreeassets
#$coin1mainlockedassets
#$coin1mainfreeassets
#$coin1mainlockedassets
#the calculator function is echo " x + y " | bc -l
#total assets
#echo 1 $coin2mainfreeassets 2 $coin2mainlockedassets 3 $pricecoin2
coin1value=`echo " ( $coin1mainfreeassets + $coin1mainlockedassets) * $pricecoin1 " | bc -l | sed 's/^\./0./'`
#echo $coin1mainfreeassets , 1 $coin1mainlockedassets, 2 $pricecoin1 3
coin2value=`echo " ( $coin2mainfreeassets + $coin2mainlockedassets) * $pricecoin2 " | bc -l`
echo total value of coin1 $coin1main is $coin1value USD
echo total value of coin2 $coin2main is $coin2value USD
getassets=0
if [ "$coin1value" = "0" ] && [ "$coin2value" = "0" ] ;
then
echo You have no money in, the Bot will exit
sleep 5
exit 1
fi
if [ "$tradesnr" = "0" ] ;
then
if (( $(echo "$coin1value > $coin2value" | bc -l) )); then
echo $coin1value este valuare coin 1
echo $coin2value este valuare coin 2
if [ "$readingsnr" = "0" ] ; then
echo first action will be sell
sold=0
bought=1
fi
else
if [ "$readingsnr" = "0" ] ; then
echo first action will be buy
sold=1
bought=0
fi
fi
fi
fi
else
if [ "$tradesnr" = "0" ] ; #this will assume that first action will be sale!
then
echo first action will be sell
sold=0
bought=1
fi
fi
}
getallprices () {
python binance/binancebridge.py prices > $allpricesfile
}
getliveprice () {
#filter the live price from the binance API response from the allpricesfile . to get a specific pair of coin call getliveprice $coin
cat $allpricesfile | grep -E -o ".{0,0}$1.{0,17}" | sed 's/.*u//' | sed 's:^.\(.*\).$:\1:' | sed 's/[^0-9.]//g'
}
#####################################################################get highest value function###################################################################################################
gethigh () {
#this gets highest price from the pricelist
awk -F ":" '{print|"sort -n"}' $pricelist | sed '/^$/d' | tail -1
}
#####################################################################get lowest value function###################################################################################################
getlow () {
#this is the function to get the lowest number from pricelist
awk -F ":" '{print|"sort -n"}' $pricelist |sed '/^$/d' | sed -n '1p'
}
################################################################################################last price function###################################################################################################
lastprices () {
#this function get`s the last prices. for example if i want next to last price i give the variable 2 (e.g lastprices 2) should give me last but 1 price and so on
tail -$1 $pricelist | head -1
}
#######################################################################price function###################################################################################################
finalprice () {
tail -n $rdg $pricelist | sort > $sorted #put the last readings in a file called sorted
readingsnr=`wc -l $sorted | awk '{print $1;}'` # command to find out how many lines there are in file
if [ "$readingsnr" = "$rdg" ]
then
#now let sort the sorted file list and do an average
#set to 2 and spiketop is also set cu 3, middle file wil contain the values 4 till 8
#marker2
averageprice=`head -n -$spikebottom $sorted | tail -n +$((spiketop+1)) | awk '{ s += $1 } END { printf("%.7f\n", s/NR) }'`
errorfix2=`echo "scale=3; 100 * ($liveprice - $averageprice) / $averageprice " | bc | sed -e 's/^-\./-0./' -e 's/^\./0./' | tr --delete -`
if (( $(echo "$errorfix2 > $readingerrorpercent" | bc -l) )); then #this wont allow any panic sell if the difference between averageprice and liveprice is to big. it is necesary to remove spikes
averageprice=$liveprice
fi
lowpercentage=`echo "scale=3; 100 * ($averageprice - $low) / $low" | bc | sed -e 's/^-\./-0./' -e 's/^\./0./'`
#percentage formula is "echo "scale=2; 100 * ($y - $x) / $x" | bc | sed -e 's/^-\./-0./' -e 's/^\./0./' "
highpercentage=`echo "scale=3; 100 * ($averageprice - $high) / $high" | bc | sed -e 's/^-\./-0./' -e 's/^\./0./' | tr --delete - `
lowtohighpercentage=`echo "scale=3; 100 * ($low - $high) / $high" | bc | sed -e 's/^-\./-0./' -e 's/^\./0./' | tr --delete - `
#echo ""
#echo "$(tput setaf 1)Average price: $(tput setab 7)$averageprice$(tput sgr 0)"
#echo the average price is : $averageprice
else
return 1
fi
}
##################################################################assets function######################################################################################################
assets () {
#this is the part to find the assets own . it will display free and locked assets, at the moment, just for the selected coins
if [ $getassetsvariable = "1" ] ;
then
truncate -s 0 $assetlist #deletes previous values in the assetlist file
python binance/binancebridge.py balances > $assetlist #get all our assets from binance and put them in assetlist file
coin1mainlenght=`echo -n $coin1main | wc -c` #find the lenght of coin1main
coin2mainlenght=`echo -n $coin2main | wc -c` #find the lenght of coin1main
if [ $coin1mainlenght = "3" ]; then
coin1mainfreeassets=`cat $assetlist | egrep -o "'$coin1main.{49}" | egrep -o ".{10}$" | sed 's/[^0-9.]//g' | sed -n 2,4p`
coin1mainlockedassets=` cat $assetlist | egrep -o "'$coin1main.{49}" | sed 's/,.*//' | sed 's/[^0-9.]//g' | sed -n 2,4p`
else
coin1mainfreeassets=`cat $assetlist | egrep -o "'$coin1main.{50}" | egrep -o ".{11}$"`
coin1mainlockedassets=` cat $assetlist | egrep -o "'$coin1main.{50}" | sed 's/,.*//' | sed 's/[^0-9.]//g'`
fi
if [ $coin2mainlenght = "3" ]; then
coin2mainfreeassets=`cat $assetlist | egrep -o "'$coin2main.{49}" | egrep -o ".{10}$" | sed 's/[^0-9.]//g'`
coin2mainlockedassets=`cat $assetlist | egrep -o "'$coin2main.{49}" | sed 's/,.*//' | sed 's/[^0-9.]//g' `
else
coin2mainfreeassets=`cat $assetlist | egrep -o "'$coin2main.{50}" | egrep -o ".{11}$" | sed 's/[^0-9.]//g'`
coin2mainlockedassets=`cat $assetlist | egrep -o "'$coin2main.{50}" | sed 's/,.*//' | sed 's/[^0-9.]//g '`
fi
if [ "trade_type" = "live" ] ; then
#############this runs only after a transaction or at startup!!!!!!!!!!!!!!!!!!!!!!!!
display #this is the display function, which is the main menu of the bot
findassetvalue #######this is the function that gets the price from coinmarketcap to compare the assets value
getassetsvariable=0 # this turns off the get asset command. if you want to get assets, put this to 1
else
echo This is a simulation
simulator_calculator
display
findassetvalue
fi
else
if [ "trade_type" = "live" ] ; then
#this gets called all the time!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!
getallprices #this command get`s all prices into 1 file
liveprice=$(getliveprice $coin) # this command get`s the live price from the live price function after the bot get assets from binance account
finalprice
echo $liveprice >> $pricelist
high=$(gethigh)
low=$(getlow)
display
profit
if [ "$readingsnr" = "$rdg" ] ; then
$strategy # this should be the strategy
fi # this should be the strategy
else
liveprice=$sim_price
finalprice
echo $liveprice >> $pricelist
high=$(gethigh)
low=$(getlow)
display
profit
if [ "$readingsnr" = "$rdg" ] ; then
$strategy # this should be the strategy
fi
simulator_calculator
# echo This is a simulation!!!
fi
fi
}
simulator_calculator () {
#echo $sim_buy $sim_sale
# this is the calculator for the simulator! it will calculate the loss or wins in a simulated trade only!
#for the moment it will assume only that we start with sell operation!!
if [ "$trade_type" = "simulate" ] ; then
if [ "$tradesnr" = "0" ] ; #this will assume that first action will be sale!
then
coin1mainfreeassets=$sim_coin1
coin2mainfreeassets=$sim_coin2
fi
if [ "$sim_sale" = "1" ] ; then
echo do the simulation calculations for a selling operation
coin1afterfee=`echo "scale=10; $coin1mainfreeassets - ( $coin1mainfreeassets / 100 * $market_fee)" | bc | sed 's/^\./0./'`
coin2mainfreeassets=`echo "scale=10; $coin1afterfee * $liveprice " | bc | sed 's/^\./0./'`
coin1mainfreeassets=0
sim_sale=0
fi
if [ "$sim_buy" = "1" ] ; then
coin2afterfee=`echo "scale=10; $coin2mainfreeassets - ( $coin2mainfreeassets / 100 * $market_fee)" | bc | sed 's/^\./0./'`
coin1mainfreeassets=`echo "scale=10; $coin2afterfee / $liveprice " | bc | sed 's/^\./0./'`
echo do the simulation calculations for a buying operation
coin2mainfreeassets=0
sim_buy=0
fi
fi
getassetsvariable=0
}
#############################################deprecated sell command. will be replaced in the future###############################################################
#}
sellcheck () {
if [ $sellcheckswitch = "1" ] ; then
echo setting up time and date before sell
date -s "$(wget -qSO- --max-redirect=0 google.co.uk 2>&1 | grep Date: | cut -d' ' -f5-8)Z"
truncate -s 0 $buysellcheck
cd $zenbotpath
echo "try to sell for $buysellretryseconds with order typer : $order_type at $liveprice"
echo " " >> $tradelog
date >> $tradelog
echo try to sell for $buysellretryseconds seconds with order maker at $liveprice >> $tradelog
echo "-------------------------------------------------------------------" >> $tradelog
if [ "$trade_type" = "live" ] ; then
timeout $buysellretryseconds node zenbot sell binance.$coin1main-$coin2main --order_type $order_type 2>&1 | tee $buysellcheck
fi
echo Zenbot has sold on Binance Liveprice : $liveprice, Averageprice : $averageprice, Lastbuyprice: $lastbuyprice >> $tradelog
if [ "$trade_type" = "live" ] && [ "send_mail" = "yes"] ;
then
echo Subject: Zenbot has sold > mail.txt
echo " " >> mail.txt
echo Liveprice - $liveprice >> mail.txt
echo Averageprice - $averageprice >> mail.txt
echo Lastbuyprice - $lastbuyprice >> mail.txt
ssmtp loren.bufanu@gmail.com < mail.txt
fi
if [ "$trade_type" = "simulate" ] ; then
sim_sale=1
fi
##########################################################################################################################################################################################################################
cd $path
gainpercent=""
cat $buysellcheck | grep 'completed' &> /dev/null
if [ $? == 0 ] || [ "$trade_type" = "simulate" ]; then ########## this happens only if trade is completed succesfully
cat $buysellcheck | sed -n -e '/sell/,$p' >> $tradelog
cat $buysellcheck | sed -n -e '/sell/,$p'
echo sell has completed succesfully
lastsellprice=$liveprice
sellcheckswitch=0
aplay sounds/sold.wav
sold=1
bought=0
tradesnr=`expr $tradesnr + 1`
highpercentage=0
truncate -s 0 $pricelist
truncate -s 0 $sorted
if [ "$trade_type" = "live" ] ; then
getassetsvariable=1
fi
lowpercentage=0
oldlastbuyprice=$lastbuyprice
lastbuyprice=0
truncate -s 0 $buysellcheck
sellretrycount=0
else
sellretrycount=`expr $sellretrycount + 1`
echo the sell failed
echo try again with maker price
truncate -s 0 $buysellcheck
cd $zenbotpath
echo "RETRY to buy for $buysellretryseconds seconds with order type taker at $liveprice"
echo " " >> $tradelog
date >> $tradelog
echo "===========================================================================" >> $tradelog
echo "Retry to sell for $buysellretryseconds seconds with order taker at $liveprice" >> $tradelog
if [ "$trade_type" = "live" ] ; then
timeout $buysellretryseconds node zenbot sell binance.$coin1main-$coin2main --order_type taker 2>&1 | tee $buysellcheck
fi
if [ "$trade_type" = "simulate" ] ; then
sim_sale=1
fi
echo Subject: Zenbot has sold > mail.txt
echo " " >> mail.txt
echo Liveprice - $liveprice >> mail.txt
echo Averageprice - $averageprice >> mail.txt
echo Lastbuyprice - $lastbuyprice >> mail.txt
ssmtp loren.bufanu@gmail.com < mail.txt
echo Zenbot has sold on Binance Liveprice : $liveprice, Averageprice : $averageprice, Lastbuyprice: $lastbuyprice >> $tradelog
cd $path
cat $buysellcheck | grep 'completed' &> /dev/null
if [ $? == 0 ]; then ########## this happens only if trade is completed succesfully
echo Retry completed succesfully
cat $buysellcheck | sed -n -e '/sell/,$p' >> $tradelog
echo sell has completed succesfully
lastsellprice=$liveprice
sellcheckswitch=0
aplay sounds/sold.wav
sold=1
bought=0
tradesnr=`expr $tradesnr + 1`
highpercentage=0
truncate -s 0 $pricelist
truncate -s 0 $sorted
if [ "$trade_type" = "live" ] ; then
getassetsvariable=1
fi
lowpercentage=0
oldlastbuyprice=$lastbuyprice
lastbuyprice=0
truncate -s 0 $buysellcheck
sellretrycount=0
else
if grep -q enough "$buysellcheck"; then
echo
echo Zenbot does not have enough assets to complete the command
echo sell has completed succesfully
lastsellprice=$liveprice
sellcheckswitch=0
aplay sounds/sold.wav
sold=1
bought=0
tradesnr=`expr $tradesnr + 1`
highpercentage=0
truncate -s 0 $pricelist
truncate -s 0 $sorted
if [ "$trade_type" = "live" ] ; then
getassetsvariable=1
if ["$trade_type" != "live" ] ; then
sim_sale=1
fi
fi
lowpercentage=0
oldlastbuyprice=$lastbuyprice
lastbuyprice=0
truncate -s 0 $buysellcheck
sellretrycount=0
else
sellretrycount=`expr $sellretrycount + 1`
echo sell retry failed $sellretrycount times
if [ "$sellretrycount" -ge "$sellbuyretry" ] ; then
echo we failed to many times to try and sell the coins
echo insert action here
aplay sounds/gameover.wav
###################################################
else
echo going back and try to sell the coins. we failed $sellretrycount times
sellcheck
fi
fi
fi
fi
fi
}
buycheck () {
if [ $buycheckswitch = "1" ] ; then
echo setting up time and date before buy
date -s "$(wget -qSO- --max-redirect=0 google.co.uk 2>&1 | grep Date: | cut -d' ' -f5-8)Z"
truncate -s 0 $buysellcheck
cd $zenbotpath
echo executing Zenbot
if [ "$trade_type" = "live" ] ; then
timeout $buysellretryseconds node zenbot buy binance.$coin1main-$coin2main --order_type $order_type 2>&1 | tee $buysellcheck
fi
if [ "$trade_type" = "live" ] && [ "send_mail" = "yes"] ;
then
echo Subject: Zenbot has bought > mail.txt
echo " " >> mail.txt
echo Liveprice - $liveprice >> mail.txt
echo Averageprice - $averageprice >> mail.txt
echo Lastbuyprice - $lastbuyprice >> mail.txt
ssmtp loren.bufanu@gmail.com < mail.txt
fi
if [ "$trade_type" = "simulate" ] ; then
sim_buy=1
fi
echo " " >> $tradelog
date >> $tradelog
echo Zenbot has Bought on Binance Liveprice : $liveprice, Averageprice : $averageprice, Lastbuyprice: $lastbuyprice >> $tradelog
echo "try to buy for $buysellretryseconds with order typer : $order_type at $liveprice"
echo "-------------------------------------------------------------------" >> $tradelog
echo "try to buy for $buysellretryseconds with order maker at $liveprice" >> $tradelog
cd $path
highestgainpercent=0 # reseting highest gain percent
cat $buysellcheck | grep 'completed' &> /dev/null
##########################################################################################################################################################################################################################
if [ $? == 0 ] || [ "$trade_type" = "simulate" ]; then ########## this happens only if trade is completed succesfully
cat $buysellcheck | sed -n -e '/buy/,$p' >> $tradelog
cat $buysellcheck | sed -n -e '/buy/,$p'
echo buy has completed succesfully
lastbuyprice=$liveprice
aplay sounds/bought.wav
bought=1
sold=0
highpercentage=0
lowpercentage=0
tradesnr=`expr $tradesnr + 1`
date=`date`
truncate -s 0 $pricelist
truncate -s 0 $sorted
if [ "$trade_type" = "live" ] ; then
getassetsvariable=1
fi
truncate -s 0 $buysellcheck
buycheckswitch=0
else
buyretrycount=`expr $buyretrycount + 1`
echo the buy failed
echo try again with taker price
#######################################################################################################################################################################################################
truncate -s 0 $buysellcheck
cd $zenbotpath
echo "RETRY to buy for $buysellretryseconds with order type taker at $liveprice"
echo "" >> $tradelog
echo "===========================================================================" >> $tradelog
echo "Retry to buy for $buysellretryseconds with order taker at $liveprice" >> $tradelog
if [ "$trade_type" = "live" ] ; then
timeout $buysellretryseconds node zenbot buy binance.$coin1main-$coin2main --order_type taker 2>&1 | tee $buysellcheck
fi
if [ "$trade_type" = "live" ] && [ "send_mail" = "yes"] ;
then
echo Subject: Zenbot has bought > mail.txt
echo " " >> mail.txt
echo Liveprice - $liveprice >> mail.txt
echo Averageprice - $averageprice >> mail.txt
echo Lastbuyprice - $lastbuyprice >> mail.txt
ssmtp loren.bufanu@gmail.com < mail.txt
fi
if [ "$trade_type" = "simulate" ] ; then
sim_buy=1
fi
echo " " >> $tradelog
date >> $tradelog
echo Zenbot has Bought on Binance Liveprice : $liveprice, Averageprice : $averageprice, Lastbuyprice: $lastbuyprice >> $tradelog
cd $path
cat $buysellcheck | grep 'completed' &> /dev/null
if [ $? == 0 ] || [ "$trade_type" = "simulate" ]; then ########## this happens only if trade is completed succesfully
echo Retry completed succesfully
cat $buysellcheck | sed -n -e '/buy/,$p' >> $tradelog
echo buy has completed succesfully
lastbuyprice=$liveprice
aplay sounds/bought.wav
bought=1
sold=0
highpercentage=0
lowpercentage=0
tradesnr=`expr $tradesnr + 1`
date=`date`
truncate -s 0 $pricelist
truncate -s 0 $sorted
if [ "$trade_type" = "live" ] ; then
getassetsvariable=1
fi
truncate -s 0 $buysellcheck
buycheckswitch =0
else
if grep -q enough "$buysellcheck"; then
echo
echo Zenbot does not have enough assets to complete the command
echo buy has completed succesfully
lastbuyprice=$liveprice
aplay sounds/bought.wav
bought=1
sold=0
highpercentage=0
lowpercentage=0
tradesnr=`expr $tradesnr + 1`
date=`date`
truncate -s 0 $pricelist
truncate -s 0 $sorted
if [ "$trade_type" = "live" ] ; then
getassetsvariable=1
fi
truncate -s 0 $buysellcheck
buycheckswitch=0
else
buyretrycount=`expr $buyretrycount + 1`
echo buy retry failed $sellretrycount times
if [ "$buyretrycount" -ge "$sellbuyretry" ] ; then
echo we failed to many times to try and sell the coins
echo insert action here
aplay sounds/gameover.wav
buycheck
###################################################
else
echo going back and try to buy the coins. we failed $buyretrycount times
buycheck
fi
fi
fi
fi
fi
}
######################################################STRATEGYS########################################################################################################################
buy_low_sell_high_strategy ()
{
#this is the strategy which means ,buy low, sell high
#note : add a function when it buy`s after lowpercentage recovers a bit, maybe in a perioud of time
# important variables :
# $low - this is the lowest price so far
# $averageprice - the average price is
# $high - the highest price
# $lowpercentage % - average reading compared to lowest value in percentage
# $highpercentage -average reading compared to highest value in percentage
# $buypercent - buy value variable set by the user
# $sellpercent
# $getassetsvariable - if this is 1, it will retrieve assets from binance
# $lastbuyprice - the last price the bot bought
# $lastsellprice - the last price the bot sold at
# $allowsellminprofitswitch - switch from the minimum profit percent function (1 or 0)
if (( $(echo "$lowpercentage >= $buypercent" | bc -l) )); then
buy
fi
if (( $(echo "$highpercentage >= $sellpercent" | bc -l) )); then
sale
fi
}
buy () {
#this is the function that will buy. i`ve made it easy to simplify the strategy!
if [ "$sold" = "1" ] && [ "$allow_buy" = "1" ] ; then
echo we should buy here,calling zenbot
buyswitch=1
buycheckswitch=1
buycheck
allow_buy=0
fi
}
sale () {
#this is the function that will sale. i`ve made it easy to simplify the strategy!
if [ "$bought" = "1" ] && [ "$allowsell" = "1" ] && [ "$allowsellminprofitswitch" = "1" ] ; then
echo we should sell here, calling zenbot
sellswitch=1
sellcheckswitch=1
sellcheck
fi
}
############################################################################################END OF STRATEGYS##########################################################################################################
profit () {
if [ "$buy_price_smaller_than_sale_price" = "true" ] ; then
if (( $(echo "$lastsellprice > 0" | bc -l) )); then
if (( $(echo "$lastsellprice > $averageprice" | bc -l) )); then
allow_buy=1
else
allow_buy=0
fi
fi
else
allow_buy=1