Show Posts
|
Pages: « 1 2 3 4 [5] 6 »
|
Are you asking how to use an API to check balances and transfer money out of wallets, or are you asking how to do calculations in PHP?
|
|
|
Sometimes speed is not an issue.
|
|
|
What do you mean by 'validate' exactly?
|
|
|
Re: Seeking a few good men. So...women not welcome?
|
|
|
The right language is the one that is right for the implementation. It seems, to me, snobbish to criticize a language or implementation without first asking about how the code will be used. It is hilarious, to me, that anyone claiming any level of trade skills would not at least ask those obvious questions first. YMMV.
@Malavi, there is actually a good java library pointed it out in another bot trading thread somewhere, and not a chance in hell I could ever find it again. This is a project in my spare time, and PHP is easy. It is also accessible to more people, so I can easily share.
|
|
|
I broke this out into a few classes that make a little more sense, and actually provides some structure. I suppose technically at this point this is only a Bitcoin trading simulation with lofty aspirations. <?php /* * By: cclites@minzie.com 2013-04-28 * For: Minzie.com * Version: .1 Controller.php * BTC Address: 19aTxxfEm6JSRHnW9yektMbwfyxaySWfKv * * * Implements the Bitstamp api. * https://www.bitstamp.net/api/ */
/* * Controller.php - Main entry point for this testing/development framework. * (hint: run this class); * * This is a simple procedural class with a recursive function call to update * every 30 seconds. Its job is to create a single LazyI instance, process * market info, and notify the bot when the information has been updated. * * This is being tested on a LAMP stack, running from the command line. * */
require_once("Configs.php"); require_once("Utilities.php"); require_once("Lazy_I.php"); /* * These fields will eventually live in a database. They are used to represent * the market state. */ $lastClose = -1; $direction = 0; $trend = 0; $ticker; $high = 0; $low = 0; $botArray = array(); writeLog("************* LazyI Starting up. *******************"); init($ticker); writeLog("PREVIOUS LAST CLOSE: " . $lastClose); writeLog("CURRENT LAST: " . number_format($ticker->last, 8)); $last = number_format((float)$ticker->last,8); updateTrend($last, $lastClose, $direction, $trend); writeLog("DIRECTION: " . $direction . " TREND: " . $trend); writeLog("INSTANTIATE LazyI."); /* * NOTE: This Lazy_I class constructor will be expanded to pass other initialization * attributes, pulled from $_POST. For now, use these defaults */ $basePoint = 150; $startUsd = 20; $startBtc = .2; $bot = new Lazy_I($basePoint, $startUsd, $startBtc); $bot->checkTransactionRules($ticker->last, $direction, $trend); $botArray[] = $bot; mainRoutine($ticker);
/* * function: update * * This function retrieves and updates market information, and then * processes the bots. */ function update() { global $botArray, $lastClose, $usd, $btc, $direction, $trend; getTicker($ticker); writeLog("PREVIOUS LAST CLOSE: " . $lastClose); writeLog("CURRENT LAST: " . number_format($ticker->last, 8)); $last = number_format((float)$ticker->last,8); updateTrend($last, $lastClose, $direction, $trend); writeLog("DIRECTION: " . $direction . " TREND: " . $trend); $cnt = count($botArray); for ($i = 0; $i < $cnt; $i += 1) { $tmpBot = $botArray[$i]; $tmpBot->update(); $tmpBot->checkTransactionRules($ticker->last, $direction, $trend); } mainRoutine($ticker); } function mainRoutine(&$ticker) { updateLastClose($ticker); sleep(30); update(); } ?>
<?php /* * By: cclites@minzie.com 2013-04-28 * For: Minzie.com * Version: .1 Configs.php * BTC Address: 19aTxxfEm6JSRHnW9yektMbwfyxaySWfKv * * * Implements the Bitstamp api. * https://www.bitstamp.net/api/ */
define("LOG", "lazyI_v1_dev.log"); define("GET_TICKER", "https://www.bitstamp.net/api/ticker/"); define("GET_BALANCE", "https://www.bitstamp.net/api/balance/"); define("USER", "********"); define("PASSWORD","********"); ?>
<?php /* * By: cclites@minzie.com 2013-04-28 * For: Minzie.com * Version: .1 Utilities.php * BTC Address: 19aTxxfEm6JSRHnW9yektMbwfyxaySWfKv * * * Implements the Bitstamp api. * https://www.bitstamp.net/api/ */ /* * Utilities.php - main library for common functions. * * Realistically, this should be divided into market-related utilities * and bot-related utilities. The set of functions is small enough for * now that I can live with it. */ /* * Set the exchange state */ function init(&$ticker) { getTicker($ticker); } /* * Returns the Bitstamp ticker object * * Example ticker content: * * stdClass Object * ( * [high] => 121.00 * [last] => 119.96 * [bid] => 119.96 * [volume] => 8291.58742485 * [low] => 106.00 * [ask] => 120.98 * ) * * Parameters: * $ticker - represents the ticker object */ function getTicker(&$ticker) { $url = GET_TICKER; writeLog("Getting TICKER: $url"); curl($url, $ticker); if(!$ticker) { writeLog("Error retrieving ticker."); return 0; } return 1; } function writeLog($message) { $address = LOG; $timeStamp = date ("D M d H:i:s Y"); $f = fopen($address, "a"); if($message == "") { fwrite($f, "\n"); } else { fwrite( $f, $timeStamp . ": $message\n\r" ); //echo("$timeStamp: $message\n\r<br>"); } fclose($f); return 0; } /* * Wrapper to allow reuse of the curl functionality for GET requests. * * Parameters: * $result - represents the ticker object * $url - represents the REST request */ function curl($url, &$result) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = json_decode(curl_exec($ch)); curl_close($ch); return 1; } /* * Wrapper to allow reuse of the curl functionality for POST requests. * * Parameters: * $result - represents the ticker object * $str - * $url - represents the REST request */ function pCurl($url, $str, &$result) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ch, CURLOPT_POST, 1); curl_setopt ($ch, CURLOPT_POSTFIELDS, $str); $result = json_decode(curl_exec($ch)); curl_close($ch); return 1; } /* * Update the market trend and direction * * Parameters: * $last - represents latest market close in USD * $lastClose - represents previous market close in USD * * Returns: * $direction - represents market direction as an int. * $trend - represents trend as an int. */ function updateTrend($last, $lastClose, &$direction, &$trend) { if($last < $lastClose) { if( $direction == -1) //was already decreasing { $trend += 1; } else { $trend = 1; $direction = -1; } } else if ($last > $lastClose) { if( $direction == 1) //was already increasing { $trend += 1; } else { $trend = 1; $direction = 1; } } else { $trend = 0; $direction = 0; } return 1; } /* * Gets balance information for a user. Currently, testing only supports a single account. * */ function getBalanceInformation(&$balance) { global $usd, $btc, $xchangeFee, $user, $password; $url = GET_BALANCE; $str = "user=" . USER . "&password=" . PASSWORD; $account; pCurl($url, $str, $balance); } /* * Just saves the last price to the lastClose array */ function updateLastClose($ticker) { global $lastClose, $high, $low; if(!$ticker) return; // if ticker is empty, wait until next go-round. if( $ticker->last > $high) { $high = $lastClose; } else if ($ticker->last < $low) { $low = $lastClose; } // Initialize arrays if seeing this for the first time. if ($lastClose == -1) { $lastClose = number_format($ticker->last, 8); $high = $low = $lastClose; }
if( $lastClose != $ticker->last) { $lastClose = number_format($ticker->last, 8); } writeLog("********************LAST CLOSE IS $lastClose"); writeLog("HIGH $high LOW $low"); } ?>
<?php /* * By: cclites@minzie.com 2013-04-28 * For: Minzie.com * Version: .1 Lazy_I.php * BTC Address: 19aTxxfEm6JSRHnW9yektMbwfyxaySWfKv * * * Implements the Bitstamp api. * https://www.bitstamp.net/api/ */
require_once("Utilities.php"); require_once("Transaction.php"); class Lazy_I { /* * These fields will eventually be moved onto a database. */ var $spp = 0; var $ppp = 0; var $base = -1; var $usd = 0; var $btc = 0; var $exchangeFee = 0; var $increase = 5; var $decrease = 5; public function Lazy_I($basePoint, $_usd, $_btc) { writeLog("Initializing bot instance"); writeLog("Base point is $basePoint"); $this->base = $basePoint; $this->usd = $_usd; $this->btc = $_btc; $this->update(); return 1; } public function update() { writeLog("Updating"); $this->getBalance(); $this->calculatePricePoints(); } public function getBalance() { writeLog("Getting Balance"); getBalanceInformation($balance); /* * For testing, we want to use the values provided in the initialization * of the object. Uncomment for account-based testing. */ //$this->usd = $balance->usd_available; //$this->btc = $balance->btc_available; $this->exchangeFee = ($balance->fee) / 100; writeLog("WALLET: USD: " . $this->usd . " BTC: " . $this->btc); return 1; } /* * Kick off the price point calculations */ public function calculatePricePoints() { $totalFee = (1 + ($this->exchangeFee)); $this->calculateSellPoint($totalFee); $this->calculateBuyPoint(); writeLog("SPP: " . $this->spp . " PPP: " . $this->ppp); return 1; } /* * Calculate sell point. This is used to determine the point at which * a sell order would be placed. */ public function calculateSellPoint($totalFee) { writeLog("Calculating sell point."); $this->spp = (($this->base * $totalFee) + $this->increase); return 1; } /* * Calculate buy point. This is used to determine the point at which * a buy order would be placed. */ public function calculateBuyPoint() { writeLog("Calculating buy point."); $this->ppp = (($this->base - ($this->base * $this->exchangeFee)) - $this->decrease); return 1; } /* * Process business transaction rules. */ public function checkTransactionRules($lastClose, $direction, $trend) { writeLog("CHECKING TRANSACTIONS"); $this->checkSell($lastClose, $direction, $trend); $this->checkBuy($lastClose, $direction, $trend); return 1; } /* * Determine if the sell point conditions */ public function checkSell($lastClose, $direction, $trend) { //writeLog("CHECK SELL $lastClose : $direction : $trend"); //writeLog("SPP " . $this->spp . " BTC " . $this->btc); //return; writeLog("$lastClose > " . $this->spp . " && $direction == -1 && " . $this->btc ."> 0 "); if ($lastClose > $this->spp && $direction == -1 && $this->btc > 0 ) { writeLog("SELLING at " . $lastClose . " per Bitcoin. "); // STUB: create sell transaction createSellTransaction(); $this->base = $lastClose; //This goes away in production because the account info will be updated when //the bots are updated. This will be updated on the bitstamp side. $this->usd += ($this->btc * $last); $this->btc = 0; } return 1; } public function checkBuy($lastClose, $direction, $trend) { //writeLog("CHECK BUY $lastClose : $direction : $trend"); //writeLog("PPP " . $this->ppp . " USD " . $this->usd); //return; writeLog("$lastClose < " . $this->ppp . " && $direction == 1 && " . $this->usd ."> 0 "); if ($lastClose < $this->ppp && $direction == 1 && $this->usd > 0 ) { writeLog("BUYING at " . $lastClose . " per Bitcoin. "); // STUB: create buy transaction createBuyTransaction(); $this->base = $lastClose; //This goes away in production because the account info will be updated when //the bots are updated. This will be updated on the bitstamp side. $this->btc += number_format(((float)$this->usd/(float)$lastClose),8); $this->usd = 0; } return 1; } public function getLazyI() { return $this; } }
?>
<?php /* * By: cclites@minzie.com 2013-04-28 * For: Minzie.com * Version: .1 Transaction.php * BTC Address: 19aTxxfEm6JSRHnW9yektMbwfyxaySWfKv * * * Implements the Bitstamp api. * https://www.bitstamp.net/api/ */ require_once("Configs.php"); require_once("Utilities.php"); function createBuyTransaction() { writeLog("STUB: createBuyTransaction"); } function createSellTransaction() { writeLog("STUB: createSellTransaction"); } ?>
|
|
|
Here is a more simple case that may make it easier to see what is happening. <?php require_once("getInfo.php"); $getInfo = new getInfo(); $infoObject = $getInfo->getInfoObject(); echo("<pre>"); print_r($infoObject); echo("</pre><br>"); echo("Difficulty = " . $infoObject->difficulty . "<br>"); echo("Estimate = " . $infoObject->estimate . "<br>"); echo("Hash Rate = " . $infoObject->hashrate . "<br>"); echo("Interval = " . $infoObject->interval . "<br>"); echo("Hashes to Win = " . $infoObject->hashestowin . "<br>"); ?>
<?php
class getInfo { var $difficultyUrl = "http://blockexplorer.com/q/getdifficulty"; var $estimateUrl = "http://blockexplorer.com/q/estimate"; var $hashrateUrl = "http://blockchain.info/q/hashrate"; var $intervalUrl = "http://blockexplorer.com/q/interval"; var $hashesUrl = "http://blockexplorer.com/q/hashestowin"; var $difficulty; var $estimate; var $hashrate; var $interval; var $hashestowin; public function getInfo() { $this->curl($this->difficultyUrl, $result); $this->difficulty = $result;
$this->curl($this->estimateUrl, $result); $this->estimate = $result; $this->curl($this->hashrateUrl, $result); $this->hashrate = $result; $this->curl($this->intervalUrl, $result); $this->interval = $result; $this->curl($this->hashesUrl, $result); $this->hashestowin = $result; } public function curl($url, &$result) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = json_decode(curl_exec($ch)); curl_close($ch); return 1; } public function getInfoObject() { return $this; } } ?>
|
|
|
Your php code to format a number to a decimal representation is correct.
Verify that the data that you are trying to format is what you actually think it is. In your sample code, you are treating the return responses from the curl calls as single values. I am trying to ascertain whether or not the curl calls are returning simply text, or if they are returning JSON. If they are returning JSON, then you cannot format it, and the number_format call is going to fail.
Get some real data, real code, and real results so someone can help. We cannot guess based on some examples that may or may not be what you are really doing.
|
|
|
I think you are not handling the object correctly. Can you do something? Add this bit of code right before you echo out the results in b.php: echo("<pre>"); print_r($res); echo("</pre><br>");
I don't think your input is what you think it is, but if you can show me what that looks like, I may have a little better understanding of what is happening. I am a little confused though because you are showing 5 lines of output, yet your code only accounts for 3.
|
|
|
Do you have a complete and compelling business case that is going to convince me to set aside what I am working on now, and engage as a financial and developmental resource? Honestly, I have enough ideas of my own, and in fact, I just now thought of one that may be patentable.
I am not trying to give you a bad time. If you want me to PM you, then define clearly what your expectations are. "putting forth the time, money, etc" gives me no idea of scope or required level of engagement. What is my return on investment for utilizing my time and effort? What are you bringing to the table? Ideas are rarely unique; the implementation is.
Anyway, you might garner more interest if you at least say something like 'I have an idea for a Wordpress Plug-in that adds a customizable exchange monitor to the sidebar'. I have a silly little exchange monitor I made, and I might be interested in messaging you to see if what I already have may align with your goals.
You did not specifically ask for software development resources, but I assume that is part of the 'etcetera'?
|
|
|
I think we would be happy to help; It is unclear to me what you are expecting to happen, and what is really happening. Obviously you are trying to display some data of some sort, but I can't tell what format that data is in. Do you know? Is it JSON, or XML. I guess I can't see that you are instantiating your class either.
Do you see anything for output at all?
|
|
|
Sorry for the double post, and I realize I am probably helping nobody, but I figured out my issue, and I didn't want to leave this thread incomplete. I did not have my account set to allow API access.
|
|
|
I can't get it to work using curl either, which is how I am retrieving the ticker. Weird.
|
|
|
I don't understand how that solves anything.
|
|
|
I was almost very happy... I have been trying a boilerplate jQuery ajax function that always works, and it is failing: $.ajax ({ type: "POST", url: "https://www.bitstamp.net/api/balance/", data: { user:"999", password:"venomous_rat_regeneration" } }).success( function(msg) { alert(msg); }).error ( function( msg, e ) { var message; for (property in msg) { message += property + ":" + msg[property] + "\n"; } alert(message); });
I also tried: $.post("https://www.bitstamp.net/api/balance", { user: "999", password: "venomous_rat_regeneration" }) .done(function(msg) { //alert("Data Loaded: " + data); var message; for (property in msg) { message += property + ":" + msg[property] + "\n"; } alert(message); });
I expect to get a json object back that I can traverse like a normal javascript object, but I am getting an error object instead.
|
|
|
Thanks for sharing. I always learn something from other people's code.
|
|
|
I am not sure if you found your solution yet, but it is not clear to me what it is you can connect to using Google Chrome. You are using PHP, so are you trying to run the PHP script from the command line, or from a browser that is not Google Chrome?
|
|
|
A good example of how simple the APIs are to use.
|
|
|
Oh. I misunderstood. I thought it starkly obvious to anyone that knows anything about code that this isn't suitable for production. But, you know, big shocker getting a critique from someone trying to drum up business as a developer.
A monkey can write a bot to make money on bitcoin right now.
Funniest shit I've seen all day.
|
|
|
Cool. Maybe you should share some of your code......
|
|
|
|