Bitcoin Forum
May 25, 2024, 08:31:12 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
  Home Help Search Login Register More  
  Show Posts
Pages: « 1 2 3 4 5 [6]
101  Bitcoin / Project Development / The trading bot that doesn't on: April 24, 2013, 12:13:01 AM
I have been toying around with a trading bot for a little bit now, mostly as an exercise in creating a simple trading bot that is easily usable. I also wanted to start with something that was easily approachable for novice programmers, so I tried to keep everything clear and clean. It is simply one big loop that runs until the program is manually terminated. There is absolutely no trading functionality in it at all, although from the API, it looks pretty easy to implement. Regardless of one's opinion, PHP is a perfectly acceptable language that is accessible for many people.

It is always fun to put code out in public, so yeah.... this is just a proof of concept for people that may be less experienced with software development; a platform with which to start tinkering. Or maybe ask questions. Or maybe contribute some code.  Grin

I'ma just gonna paste the entire thing in here and see what happens:
Code:
<?php
                        
/*
 *  By: cclites@minzie.com 2013-04-22
 *  For: Minzie.com
 *  Version: .01
 *  BTC Donations: 19aTxxfEm6JSRHnW9yektMbwfyxaySWfKv
 *
 *
 *  Implements the Bitstamp api.
 *  https://www.bitstamp.net/api/
*/
writeLog(".");
writeLog("**************************************");
writeLog("**************************************");
writeLog("bot is starting up");
writeLog("**************************************");
writeLog("**************************************");



$usd 0; //Amount in account of user in US dollars
$btc 0.03; //Amount of Bitcoin in User account
$lastClose = array(); //Array of closing prices, with the last being the most current.
$direction 0; //Current trend
//  - incremented if increasing
//  - decremented if decreasing
$maxIncrease .05; //Percent increase from last sale
                        
$maxDecrease .05;              //Percent decrease from last sale
                        
$tradingFee  .05;    //Percent of trading fee
                        
$lastPurchase = array();         //Array of prices of purchases, with the last being the most current.
                        
$lastSale = array(); //Array of prices of sales, with the last being the most current.
$failSafe 0;                                      //This is the BTC floor. Sell nothing past this amount, whatever it may be.
$base 123.97;                         //Base price in USD for calculating buy/sell points.
$drop 5;                                          //US dollar decrease since last sale.
$increase 10;                                   //US dollar increase since last sale.
$direction 0;                                    //Direction of last from previous last
$trend 0;                                        //Number of price-points in current trend
$xchangeFee .004;                //Bitstamp exchange fee .4%
$buyFlag true;                                 //Flag indicating permission to buy
$sellFlag true;                                 //Flag indicating permission to sell

/*For transaction testing*/
$index 0;
$testUsd 100.00;
$testBtc 1;
$testPricePoints = array(100105100);
$usd $testUsd;
$btc $testBtc;


/*******************************************
* ROUTINE
********************************************/
updateState();

function updateState()
{
      global $lastClose$lastPurchase$lastSale$direction$usd$btc$base$drop
       $increase$trend$direction$xchangeFee;
       $ticker null;

                                
/*** Retrieve ticker to get the last close price. ***/
        getTicker($ticker);
/****************************************************/

/*** Once we get the ticker, we need to update lastClose ***/
updateLastClose($ticker);

$last = (float)end($lastClose);
$previousLast = (float)prev($lastClose);

/***  Update trend information  ****/
if($last $previousLast)
{
        if ($direction == 1$trend 0;
else $trend += 1;
        $direction = -1;
}
else if ($last $previousLast)
{
        if ($direction == -1$trend 0;
else $trend += 1;
$direction 1;
}
else
{
        $trend 0;
$direction 0;
}
/**********************************/

/*** Initialize base if seeing this for the first time ***/
if($base == -1$base $last;

                                
$totalFee = ($xchangeFee);
                                
$salePoint = (($base $totalFee) + $increase);
$purchasePoint = (($base - ($base $xchangeFee)) - $drop);

writeLog("($base * $totalFee) + $increase");
writeLog("($base - ($base * $xchangeFee)) - $drop");
writeLog("SPP: " $purchasePoint "    PPP: " $salePoint);

// 'direction' is used to make sure not to trigger a sell
// if the price is still rising (trend), or to trigger
// a buy if the price is still falling.
 

if ($last $salePoint && $direction == -&& $btc )
{
$direction += 1;
writeLog("SELLING at " $last " per Bitcoin. ");
$lastSale[] = $last;
$base $last;
$usd += ($btc $last);
$btc 0;
}
else if ($last $purchasePoint && $direction == && $usd 0)
{
$direction -= 1;
writeLog("BUYING at " $last " per Bitcoin. ");
$lastPurchase[] = $last;
$base $last;
$btc += ( (float)$usd/(float)$last);
$usd 0;
}

                                
writeLog("LAST: " $last "    PREVIOUS LAST: " $previousLast);
writeLog("WALLET:  USD: " $usd "    BTC: " $btc);
writeLog("DIRECTION: " $direction "  TREND: " $trend);
writeLog(".");

sleep(30);


// Debugging
/*
global $index, $testPricePoints;

if ($index == count($testPricePoints) )
{
        writeLog("<pre>");
print_r($lastClose);
writeLog("</pre><br>");
        exit;
}
*/

updateState();
}

/*
* Just saves the last price to the lastClose array
*/
function updateLastClose(&$ticker)
{
    global $lastClose$lastPurchase$lastSale;

                            if(
end($lastClose) != $ticker->last)
    {
  $lastClose[] = $ticker->last;
    }

    // Initialize arrays if seeing this for the first time.
                            
if (count($lastClose) == 1)
   {
  $lastClose[] = $ticker->last;
  $lastPurchase[] = $ticker->last;
  $lastSale[] = $ticker->last;
    }

}

/*
*  Returns the Bitstamp ticker object
*
*  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)
{
  global $lastClose$lastPurchase$lastSale;

  $url "https://www.bitstamp.net/api/ticker/";
curl($url$ticker);
}

/* 
*  Used to simulate a 3 point update with known values to do allow
*  some crude testing.
*
*  Parameters:
*  $ticker - represents the ticker object
*/

function getTestTicker(&$ticker)
{
  global $testPricePoints$index$lastClose$lastPurchase$lastSale;

writeLog("index is $index<br>");

  $ticker->last $testPricePoints[$index];
writeLog("<pre>");
print_r($ticker);
writeLog("</pre><br>");
$index += 1;
}


/*
*  Just a wrapper to allow reuse of the curl functionality.
*
*  Parameters:
*  $result - represents the ticker object
*  $url - represents the REST request
*/
function curl($url, &$result)
{
        $ch curl_init();
                                
curl_setopt($chCURLOPT_URL$url);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
$result json_decode(curl_exec($ch));
curl_close($ch);
return 0;
}

function writeLog($message
                        {

$address 'bot.log';
                        $timeStamp date ("D M d H:i:s Y");
                        $f fopen($address"a");
if ($message == "")
{
  fwrite$f" ");
}
else
{
  fwrite$f$timeStamp ": $message\n\r" );
}

                        fclose$f );
                return 0;
                        }
?>



Sorry, cut and paste munged the formatting a bit. And sorry to the first person that tries to use this and finds that it doesn't work. I only changed one thing before I posted this. I'm sure it was fine.
102  Bitcoin / Development & Technical Discussion / Re: Advanced users on: April 21, 2013, 09:39:44 PM
When asking for help it is useful to explain what you are trying to do, what you expected to happen, what really happened, how you are able to replicate it, etc.
103  Other / Beginners & Help / Re: Questions on SMF Boards/ JSON / Hi everybody! on: April 14, 2013, 10:18:38 PM
That's quite a predicament you got yourself into. What do you plan to do?
104  Other / Beginners & Help / Re: How to generate public address from private key? on: April 14, 2013, 10:06:12 PM
Double thanks. That was timely and helpful for me also.
105  Other / Beginners & Help / Re: Bitcoin Businesses and Developers, Let's Get Started! on: April 14, 2013, 12:57:56 PM
I am looking for someone that may be interested in extending a project that I have been working on. I put together a simple exchange price monitoring widget. Why is that so special? It isn't really. Other than I can select only the exchanges that those developers make available. Mine allows one to create their own list of exchanges.

Here is the most simple example:
http://www.minzie.com/?v1=true&action=pull&symbol=bitfloorUSD

The code can be embedded in any HTML using something like this:
Code:
<iframe src=”http://www.minzie.com/?v1=true&action=pull&symbol=bitfloorUSD” style=’width:100%; height:80px;border:none;’></iframe>

More information can be found here.

Now that I am done with the spamming portion of the post, I will fully admit that it is only marginally useful. And it is not very attractive. I am willing to spend the time to spiff it up. Is there someone that might be interested in building a simple (and free) android widget that makes use of the API? I would like to do it myself, but I don't have enough bandwidth to tackle that right now. If anyone is interested, let me know. Otherwise, anyone that is so inclined can feel free to use the API.

<Now I must go and wash the spamminess off of me>

Min
106  Other / Beginners & Help / Re: Newbie restrictions on: April 12, 2013, 11:00:31 AM
Freeman and slave, patrician and plebeian, lord and serf, guild-master(3) and journeyman, in a word, oppressor and oppressed, stood in constant opposition to one another, carried on an uninterrupted, now hidden, now open fight, a fight that each time ended, either in a revolutionary reconstitution of society at large, or in the common ruin of the contending classes.

*****************

If one finds themselves forced to spam a forum in order to participate, at least make it useful or interesting.
107  Other / Beginners & Help / Re: Newbie restrictions on: April 12, 2013, 10:58:03 AM
http://www.ehow.com/how_2524_remove-red-wine.html
108  Other / Beginners & Help / Re: Newbie restrictions on: April 12, 2013, 10:54:12 AM
AFTER an unequivocal experience of the inefficiency of the subsisting federal government, you are called upon to deliberate on a new Constitution for the United States of America. The subject speaks its own importance; comprehending in its consequences nothing less than the existence of the UNION, the safety and welfare of the parts of which it is composed, the fate of an empire in many respects the most interesting in the world. It has been frequently remarked that it seems to have been reserved to the people of this country, by their conduct and example, to decide the important question, whether societies of men are really capable or not of establishing good government from reflection and choice, or whether they are forever destined to depend for their political constitutions on accident and force. If there be any truth in the remark, the crisis at which we are arrived may with propriety be regarded as the era in which that decision is to be made; and a wrong election of the part we shall act may, in this view, deserve to be considered as the general misfortune of mankind.

Alexander Hamilton; Federalist Papers, Federalist #1
109  Other / Beginners & Help / Re: Newbie restrictions on: April 12, 2013, 10:51:44 AM
Autumn moonlight—

a worm digs silently

into the chestnut.
110  Other / Beginners & Help / Re: why I can't make new topic? on: April 10, 2013, 11:11:43 PM
Took me a bit to figure out too. Just dense I guess.
Pages: « 1 2 3 4 5 [6]
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!