This repository has been archived by the owner on Mar 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathautoseller.pl
executable file
·220 lines (189 loc) · 7.43 KB
/
autoseller.pl
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
#!/usr/bin/perl
use Getopt::Long; # command-line options
use POSIX qw( pow );
use warnings;
use strict;
# dependencies
require Log::Log4perl; # logging
use Try::Tiny; # error handling
# our modules
use Autosell::API::CoinEx; # CoinEx API
use Autosell::Config qw( load ); # our config loader
# init
my $config = undef;
my $configFile = undef;
Log::Log4perl->init( "log.conf" );
my $log = Log::Log4perl->get_logger( "autoseller" );
$log->info;
$log->info( "Autoseller started." );
# catch interrupt
$SIG{INT} = sub { $log->info( "sigint '$!' received, quitting." ); exit 0; };
GetOptions( "usage|help|h|u" => \&usage ,
"config|file|configfile|cfg:s" => \$configFile );
# init and load config
$config = Autosell::Config->new( $configFile );
$config->load();
# load exchanges
my $exchanges = []; # array of exchanges(each a hash)
for my $exchange ( keys % { $config->{ apikeys } } )
{
&loadExchange( $exchange );
}
# error check, shouldn't ever happen as config checks for this
$log->error_die( "No exchanges configured!" ) unless ( @$exchanges );
# poll loop
while ( 1 )
{
foreach my $exchange ( @$exchanges )
{
$log->trace( "Querying balances on $exchange->{ name }..." );
try
{
my $balances = $exchange->{ exchange }->balances(
keys % { $exchange->{ currencies } } );
# request delay
$log->trace( "Sleeping for $config->{ request }s.");
sleep $config->{ request };
# try to trade balances
TRADE: foreach my $currencyID ( keys % { $balances } )
{
# adjust to real balance(/10^8)
my $realBal = $balances->{ $currencyID } / pow( 10 , 8 );
# ignore currencies we don't have a market for
unless ( $exchange->{ markets }->{ $currencyID } )
{
$log->trace(
sprintf( "No trade pair for %s, ignoring %.8f %s." ,
$exchange->{ currencies }->{ $currencyID } ,
$realBal ,
$exchange->{ currencies }->{ $currencyID } ) );
next TRADE;
}
if ( $exchange->{ currencies }->{ $currencyID } eq $config->{ target } )
{
$log->trace(
sprintf( "Ignoring target currency. Bal: %.8f %s" ,
$realBal ,
$exchange->{ currencies }->{ $currencyID } ) );
}
elsif ( $realBal >=
( $config->{ coinmins }->{ $exchange->{ currencies }->{ $currencyID } } || 0 ) )
{
$log->trace(
sprintf( "Attempting to sell %.8f %s" ,
$realBal ,
$exchange->{ currencies }->{ $currencyID } ) );
# try to sell
try
{
my $order = $exchange->{ exchange }->sellOrder(
$exchange->{ markets }->{ $currencyID } ,
$balances->{ $currencyID } ,
$config->{ strategy } );
$log->info(
sprintf( "Created sell order ID %d for %.8f %s @ %.8f %s on %s!" ,
$order->{ id } , # order ID
$realBal , # coin balance
$exchange->{ currencies }->{ $currencyID } , # currency name
$order->{ rate } / pow( 10 , 8 ) , # price/rate of order
$config->{ target } , # target currency name
$exchange->{ name } ) ); # exchange name
}
catch
{
$log->error(
sprintf( "Unable to create sell order for %.8f %s on %s." ,
$realBal , # formatted balance
$exchange->{ currencies }->{ $currencyID } , # currency name
$exchange->{ name } ) ); # exchange name
};
# request delay
$log->trace( "Sleeping for $config->{ request }s.");
sleep $config->{ request };
}
else
{
$log->trace( sprintf( "Ignoring balance of %.8f %s, below min amount." ,
$realBal ,
$exchange->{ currencies }->{ $currencyID } ) );
}
}
}
catch
{
# sleep if we errored out on a request
$log->trace( "Sleeping for $config->{ request }s.");
sleep $config->{ request };
};
}
# sleep until it's time for next poll
$log->trace( "Sleeping for $config->{ poll }s.");
sleep $config->{ poll };
}
####################################################################################################
# Load exchange from config
#
# Params:
# exchange: name of exchange we're loading
####################################################################################################
sub loadExchange
{
my $exchange = shift || '';
$log->debug( "Loading $exchange..." );
my $exchangeref = { name => $exchange };
# load coinex API
if ( $exchange =~ /^(coinex)/i )
{
# load exchange
$exchangeref->{ exchange } =
Autosell::API::CoinEx->new(
$exchange ,
$config->{ apikeys }->{ $exchange }->{ key } ,
$config->{ apikeys }->{ $exchange }->{ secret } );
}
# TODO other exchanges
else
{
$log->error( "Unsupported exchange: $exchange" );
return;
}
# attempt to grab currencies from exchange
$exchangeref->{ currencies } =
$exchangeref->{ exchange }->currencies( @ { $config->{ excludes } } );
# attempt to grab markets exchange
$exchangeref->{ markets } =
$exchangeref->{ exchange }->markets(
$config->{ target } , $exchangeref->{ currencies } );
# log what we've found
$log->debug( "Found " . keys( % { $exchangeref->{ currencies } } ) .
" relevant currencies and " .
keys( % { $exchangeref->{ markets } } ) . " markets for them." );
push(@$exchanges, $exchangeref );
$log->info( "Monitoring $exchange." );
# sleep for request delay so we don't get a "too many requests" error
$log->trace( "Sleeping for $config->{ request }s.");
sleep $config->{ request };
}
####################################################################################################
# usage
#
# Print script usage
####################################################################################################
sub usage
{
print "\n" .
"$0\n" .
"Autosell coins on an exchange\n" .
"\n" .
"Options:\n" .
" -usage: Prints usage\n" .
" -config=<filename>: Config file(optional)\n" .
"\n" .
"Usage:\n" .
" $0\n" .
" $0 -usage\n" .
" $0 -config=<filename>\n" .
"\n";
# done
exit 0;
}