Author

Topic: [BOUNTY] simple .php sell-bot for mtgox and intersango (Read 2288 times)

newbie
Activity: 27
Merit: 0
change the end point to data.mtgox.com/api

YES that fixes it thanks!
BCB
vip
Activity: 1078
Merit: 1002
BCJ
change the end point to data.mtgox.com/api
newbie
Activity: 27
Merit: 0
this code is no longer working for me, but it was for a long time... is anyone else using the API with this code?

did gox make changes to their API to prevent DDOS/HFT?
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
while that is true, i laid out what i want in pseudocode pretty clearly and i also tried to make clear exactly how i want the spread to be calculated.

the last couple of things (including two programming tasks) i had made for bounties worked out pretty well.

the biggest problem is members with a limited understanding of the english language just diving head-in and doing *something* instead of asking first.

i dont want a moneymaking monster... i just need to sell close to VWAP, shouldnt be too hard.
newbie
Activity: 18
Merit: 0
bounty is 5 BTC.

Software for bounty is a great way to get low quality software from inexperienced devs who can't do any better. Not the greatest idea for automated financial software.
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
also tipped the member 4 BTC for  his troubles.
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
so it turns out that one member quit because i obviously didnt make it clear enough what i wanted and how i want it.

so, any takers?
i will provide ftp access to my server so you can test it.
i will also provide you with my API credentials (with the necessary permissions and very few BTC) so you can make sure it really does what it should.

the tricky point was the spread calculation, it seems.
Quote
so say you go to
http://bitcoin.clarkmoody.com/
and you look at the column "sum"
i want the bot to use the bid order as bid side of the spread where "sum > x"
Code:
X = 20
price  BTC sum
8.85212 1.1325 1.1
8.86147 0.0136 1.1
8.88159 1.5413 2.7
8.88234 25.0000 27.7
in this example, the ask price for the spread would be 8.88234


Code:
X = 30
price BTC  sum
8.85150 1.1325 1.1
8.85212 1.1325 2.3
8.88159 1.5413 3.8
8.88234 25.0000 28.8
8.88362 0.0192 28.8
8.89000 8.8900 37.7
in this example, the ask price for the spread would be 8.89000

since this is the price where I can trade X bitcoins.



Code:
X = 50
sum BTC  price
10.2 10.2245 8.67497
20.4 10.2249 8.67466
80.4  59.9540 8.64670
140.4 59.9524 8.64615
154.2 13.8541 8.64570
in this example the bid price for the bot would be 8.64670 since that is where I can trade 50 Bitcoins.


the intersango code
Code:
require("settings.php");
if(
INTERSANGOAPIKEY==""){
echo "You didn't supply your api key";
kill;
}
for(
$i=0$i<2$i++){
$currentfunds=getfunds();
echo 
"Coins in account ".$currentfunds."";
if(
$currentfunds MINAMOUNTOFBTC){
$ticker=json_decode(request("https://intersango.com/api/ticker.php?currency_pair_id=2"), true);
$ask=$ticker['sell'];
$bid=$ticker['buy'];
if($bid-$ask>=&& ($bid-$ask)>=SPREAD){
$size=$currentfunds*(PERCENTTOTRADE/100);
$midspread=($bid-$ask)/2;
echo "Order size ".$size."";
placeorder($size,$midspread);
echo "Placed order for ".$size." for ".$midspread."";
}else{
echo "Spread was negative";
}
}else{
cancelAllOrders();
echo "Canceled all orders "."";
}
}
function 
getAcountid(){
$data=array('api_key'=>INTERSANGOAPIKEY);
$accounts=json_decode(request('https://intersango.com/api/authenticated/v0.1/listAccounts.php',$data),true);
return $accounts[0]['id'];
}
function 
getfunds(){
$data=array('api_key'=>INTERSANGOAPIKEY);
$accounts=json_decode(request('https://intersango.com/api/authenticated/v0.1/listAccounts.php',$data),true);
for($i=0$i<sizeof($accounts); $i++){
if($accounts[$i]['currency_id']==1)
return $accounts[$i]['balance'];
}
return null;
}
function 
placeorder($quantity$rate){
$data=array('api_key'=>INTERSANGOAPIKEY);
$data['account_id']=getAcountid();
$data['selling']=true;
$data['type']='gtc';
$data['quantity']=$quantity;
$data['rate']=$rate;
$data['base_account_id']='';
$data['quote_account_id']='';
request('https://intersango.com/api/authenticated/v0.1/placeLimitOrder.php'$data);
}
function 
cancelAllOrders(){
$data=array('api_key'=>INTERSANGOAPIKEY);
$data['account_id']=getAcountid();
$orders=json_decode(request('https://intersango.com/api/authenticated/v0.1/listOrders.php'$data));
unset($data['filter']);
for($i=0$i<count($orders); $i++){
$data['order_id']=$orders[$i]['id'];
request('https://intersango.com/api/authenticated/v0.1/requestCancelOrder.php'$data);
}
}
function 
request($url$postdata=null){
$ch curl_init($url);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT30);
if(
$postdata!=null){
curl_setopt($ch,CURLOPT_POSTcount($postdata));
$fields_string;
foreach($postdata as $key=>$value) { 
$fields_string .= $key.'='.$value.'&'
}
rtrim($fields_string,'&');
curl_setopt($chCURLOPT_POSTFIELDS$fields_string);
}
$return=curl_exec($ch);
curl_close($ch);
return 
$return;
}
?>

the mtgox code
Code:
require("settings.php");
if(
MTGOXSECRET=="" || MTGOXAPIKEY==""){
echo "You didn't supply your api key";
kill;
}
for(
$i=0$i<2$i++){
$currentfunds=getfunds();
echo 
"Coins in account ".$currentfunds."";
if(
$currentfunds MINAMOUNTOFBTC){
$ticker=mtgox_query('1/BTCEUR/ticker');
$ask=$ticker['return']['sell']['value'];
$bid=$ticker['return']['buy']['value'];
if($bid-$ask>=&& ($bid-$ask)>=SPREAD){
$size=$currentfunds*(PERCENTTOTRADE/100);
$midspread=($bid-$ask)/2;
echo "Order size ".$size."";
submitorder('ask'$midspread$size);
echo "Placed order for ".$size." for ".$midspread."";
}else{
echo "Spread was negative";
}
}else{
cancelAllOrders();
echo "Canceled all orders "."";
}
}

function 
getfunds(){
$temp=mtgox_query('0/getFunds.php');
return $temp['btcs'];
}
function 
cancelAllOrders(){
$orders=mtgox_query('1/generic/private/orders', array('type'=>'1''status'=>'1'));
for($i=0$i<count($orders['return']); $i++){
mtgox_query('0/cancelOrder.php', array('oid'=>$order['return'][$i]['oid'], 'type'=>'1'));
}
}
function 
submitorder($type$price$amount){
mtgox_query("1/BTCEUR/private/order/add", array('type'=>$type,'amount_int'=>$amount,'price_int'=>$price));
}
function 
mtgox_query($path, array $req = array()) {

 
// generate a nonce as microtime, with as-string handling to avoid problems with 32bits systems
$mt explode(' 'microtime());
$req['nonce'] = $mt[1].substr($mt[0], 26);
 
// generate the POST data string
$post_data http_build_query($req'''&');
 
// generate the extra headers
$headers = array(
'Rest-Key: '.MTGOXAPIKEY,
'Rest-Sign: '.base64_encode(hash_hmac('sha512'$post_database64_decode(MTGOXSECRET), true)),
);
 
// our curl handle (initialize if required)
static $ch null;
if (is_null($ch)) {
$ch curl_init();
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_USERAGENT'Mozilla/4.0 (compatible; MtGox PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
}
curl_setopt($chCURLOPT_URL'https://mtgox.com/api/'.$path);
curl_setopt($chCURLOPT_POSTFIELDS$post_data);
curl_setopt($chCURLOPT_HTTPHEADER$headers);
curl_setopt($chCURLOPT_SSL_VERIFYPEERFALSE);
 
// run the query
$res curl_exec($ch);
if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
$dec json_decode($restrue);
if (!$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists');
return $dec;
}
?>


the api-settings code
Code:
define("SPREAD",10); // THIS DETAIL IS WHAT MAKES IT DIFFICULT
define("PERCENTTOTRADE",1); //Percent of the current ammount to trade
define("MINAMOUNTOFBTC",1); //this would be the minimum amount of btc in the acount before a trade could take place
// THIS SHOULD BE: "THE MINIMUM SIZE OF EACH ORDER"

//mtgox login information
define("MTGOXAPIKEY","--");
define("MTGOXSECRET","--");

//interango api key
define("INTERSANGOAPIKEY","--");

?>
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
yes....

the one member i have workin on it seems very confident he will be able to deliver by monday.

i do, however, still accept submissions. the first (other than the member workin on it) to submit a working (!) bot for both sites receives a bounty.

bounty is 5 BTC.

newbie
Activity: 25
Merit: 0
are you still looking for people? I thought you said you had a guy working on it already and was almost done?
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
okay, one member of the forums is working on this and almost has it finished, but if you want to go ahead and send me your ideas, that's cool - if you send me a working php script then the 5 BTC bounty is yours.

this has no influence on the reward fow user gweedo, who is already nearly done.

I want to call this script via cron to sell my BTC if I should need to.

This is what it should do, this pseudocode SHOULD be understandeable, please post general questions in this thread.



Quote
variables
$tradeable_amount // desired size at orderbook depth
$btc_in_account // number of btc in account
$percentage // which percentage of all btc in account should each order be
$midspread // the calculated midspread
$mintrade //minimum order size


calculate_the_spread (this is to disregard the little bitty orders that influence the order book)
take the bid price at which a cumulated amount of  bid size exceeds $tradeable_amount
take the ask price at which a cumulated amount of ask size exceeds $tradeable_amount
$midspread = (bid+ask)/2

sell
calculate_the_spread;
if $btc_in_account < $mintrade then $ordersize = $btc_in_account
else $ordersize = roundup(3rd decimal;$btc_in_account*$percentage/100)
place_order($ordersize;$midspread)
exit script;


check_account

are there bitcoins in the account?
yes -> sell;
no ->  if there are any orders present, cancel_all_orders then
exit script;



main
check_account;


legendary
Activity: 3318
Merit: 2008
First Exclusion Ever
Does it NEED to be in PHP? I can build one in Ruby for 200 BTC

Sure you just don't want a car instead?
member
Activity: 98
Merit: 10
(:firstbits => "1mantis")
Does it NEED to be in PHP? I can build one in Ruby for 200 BTC
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
already have someone working on it.
how much would you want for your bot and what are the features?
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
thinking about it, it should support  mtgox (€ only) too.

it should be in a php file that i can call via cron every x minutes.

simple algo:

  • calculate spread (bid and ask should be where X bitcoins are tradeable cumulatively, so I can ignore the 0.14471 and 0.4675 BTC orders)
  • check "is there BTC balance in the account"
  • yes --> create sell order for 1% of balance, put it in midspread
  • no --> cancel all orders


think that should be doable for a reasonable amount of BTC.
any takers?[/list]
BCB
vip
Activity: 1078
Merit: 1002
BCJ
What would you like it to do?
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
i need a very simple intersango bot, preferrably on php basis which i can run from my webserver.

this is what it needs to do in my own little pseudocode.

Quote
variables
$tradeable_amount // desired size at orderbook depth
$btc_in_account // number of btc in account
$percentage // which percentage of all btc in account should each order be
$midspread // the calculated midspread
$mintrade //minimum order size


calculate_the_spread (this is to disregard the little bitty orders that influence the order book)
take the bid price at which the cumulated amount of bid sizes exceeds $tradeable_amount
take the ask price at which the cumulated amount of ask sizes exceeds $tradeable_amount
$midspread = (bid+ask)/2

sell
calculate_the_spread;
if $btc_in_account < $mintrade then $ordersize = $btc_in_account
else $ordersize = roundup(3rd decimal;$btc_in_account*$percentage/100)
place_order($ordersize;$midspread)
exit script;


check_account

are there bitcoins in the account?
yes -> sell;
no ->  if there are any orders present, cancel_all_orders then
exit script;



main
check_account;
Jump to: