Created
October 12, 2012 20:11
-
-
Save chrislewis/3881252 to your computer and use it in GitHub Desktop.
Andrew's market-making bot in perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use JSON; | |
use IO::Socket; | |
my $pid=$$; | |
my $user = "marketmaker"; | |
my $pass = "liquidity"; | |
my $host = "10.10.9.115"; | |
my $port = "10000"; | |
my %posit; | |
my %newposit; | |
my $cash; | |
my $numsym=0; | |
my @syms; | |
my $sock=initsock($host, $port, $user, $pass); | |
my $initpos=register ($sock); | |
my $pos = decode_json($initpos); | |
for my $sym (keys %{ $pos->{playerPositions} } ) { | |
$posit{$sym}->{position} = $pos->{playerPositions}->{$sym}; | |
$starthold += $posit{$sym}->{value}; | |
print "Own $sym, $posit{$sym}->{position} shares\n"; | |
$numsym++; | |
push @syms, $sym; | |
} | |
my $initcash = $pos->{accountBalance}; | |
print "Starting cash $initcash\n"; | |
my $lastcash=$initcash; | |
while (<$sock>) { | |
my $tick = $_; | |
my $holdings=0; | |
$pos = decode_json($tick); | |
for my $sym (keys %{ $pos->{playerPositions} } ) { | |
$newposit{$sym}->{price} = $pos->{securityPrices}->{$sym}; | |
$newposit{$sym}->{position} = $pos->{playerPositions}->{$sym}; | |
$newposit{$sym}->{value} = $newposit{$sym}->{position} * $newposit{$sym}->{price}; | |
$holdings += $newposit{$sym}->{value}; | |
my $fill = $newposit{$sym}->{position} - $posit{$sym}->{position}; | |
print "Own $sym, $newposit{$sym}->{position} shares, at $newposit{$sym}->{price}, total $newposit{$sym}->{value} Last fill $fill \n"; | |
$posit{$sym}->{position} = $newposit{$sym}->{position}; | |
} | |
my $cash = $pos->{accountBalance}; | |
if ($starting == $initcash) { | |
$starting = $initcash + $holdings; | |
print "Initializing starting\n"; | |
} | |
my $curval = $cash + $holdings; | |
my $gain = $curval - $starting; | |
print "Cash is $cash, Holdings = $holdings, starting = $starting, current = $curval, gain = $gain\n"; | |
$lastcash = $cash; | |
# Create order | |
my %order_obj= ( | |
"msgCode" => "OrderRequest", | |
"playerId" => $user, | |
"password" => $pass, | |
); | |
for my $sym (@syms) { | |
my $price=$newposit{$sym}->{price}; | |
$order_obj{market}->{$sym}->{bid}->{playerId}=$user; | |
$order_obj{market}->{$sym}->{bid}->{quantity}=100; | |
$order_obj{market}->{$sym}->{bid}->{price}=$price; | |
$order_obj{market}->{$sym}->{ask}->{playerId}=$user; | |
$order_obj{market}->{$sym}->{ask}->{quantity}=100; | |
$order_obj{market}->{$sym}->{ask}->{price}=$price; | |
print "providing 100 of $sym at $price\n"; | |
} | |
my $orderstr=encode_json(\%order_obj); | |
print $sock "$orderstr\n"; | |
} | |
sub initsock { | |
my $host = shift; my $port=shift; my $user=shift; my $pass=shift; | |
my $sock = new IO::Socket::INET ( | |
PeerAddr => $host, | |
PeerPort => $port, | |
Proto => 'tcp', | |
); | |
die "Could not create socket: $!\n" unless $sock; | |
return $sock; | |
} | |
sub register { | |
my $sock=shift; | |
my %auth_obj= ( | |
"msgCode" => "RegistrationRequest", | |
"playerId" => $user, | |
"password" => $pass, | |
); | |
my $str=encode_json(\%auth_obj); | |
print "Auth str = $str\n"; | |
print $sock "$str\n"; | |
my $retstr = <$sock>; | |
return $retstr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment