Guest User

KrakenTradeSet.php

a guest
Feb 20th, 2014
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.40 KB | None | 0 0
  1. <?php
  2.  
  3. class KrakenTradeSet{
  4.    
  5.     protected $buyRate;
  6.     protected $initialBuyRate;
  7.     protected $margin;
  8.     protected $bitcoins;
  9.     protected $buySell;
  10.    
  11.     protected $placeOrderAtAnyRate;
  12.    
  13.     protected $processing;
  14.     protected $txid;
  15.     protected $sellAt;
  16.     protected $volumeExecuted;
  17.    
  18.     protected $index;
  19.    
  20.     protected $numberOfBuys;
  21.     protected $numberOfSells;
  22.  
  23.     public function __construct($buyRate, $margin, $bitcoins, $buySell, $index, $placeOrderAtAnyRate = false){
  24.        
  25.         $this->buyRate          = $buyRate;
  26.         $this->initialBuyRate   = $buyRate;
  27.         $this->margin           = $margin;
  28.         $this->bitcoins         = $bitcoins;
  29.         $this->buySell          = $buySell;                                          
  30.        
  31.         $this->placeOrderAtAnyRate = $placeOrderAtAnyRate;
  32.        
  33.         $this->processing       = false;
  34.         $this->txid             = null;
  35.         $this->index            = $index;
  36.        
  37.         $this->numberOfBuys     = 0;
  38.         $this->numberOfSells    = 0;
  39.        
  40.        
  41.         $this->validateMargin($margin);
  42.         $this->validateBitcoins($bitcoins);
  43.         $this->validateBuySell($buySell);
  44.        
  45.     }  
  46.    
  47.     public function getBuyRate(){
  48.         return $this->buyRate;
  49.     }
  50.     public function getInitialBuyRate(){
  51.         return $this->initialBuyRate;
  52.     }
  53.     public function getMargin(){
  54.         return $this->margin;
  55.     }
  56.     public function getBitcoins(){
  57.         return $this->bitcoins;
  58.     }
  59.     public function getBuySell(){
  60.         return $this->buySell;
  61.     }
  62.     public function isProcessing(){
  63.         return $this->processing;
  64.     }
  65.     public function getTxid(){
  66.         return $this->txid;
  67.     }
  68.     public function getSellAt(){
  69.         return $this->sellAt;
  70.     }
  71.     public function getVolumeExecuted(){
  72.         return $this->volumeExecuted;
  73.     }
  74.    
  75.     public function getNumberOfBuys(){
  76.         return $this->numberOfBuys;
  77.     }
  78.    
  79.     public function getNumberOfSells(){
  80.         return $this->numberOfSells;
  81.     }
  82.    
  83.     public function getPlaceOrderAtAnyRate(){
  84.         return $this->placeOrderAtAnyRate;
  85.     }
  86.    
  87.     public function setBuyRate($buyRate){
  88.         $this->buyRate = $buyRate;
  89.     }
  90.     public function setMargin($margin){
  91.         $this->validateMargin($margin);
  92.         $this->margin = $margin;
  93.     }
  94.     public function setBitcoins($bitcoins){
  95.         $this->validateBitcoins($bitcoins);
  96.         $this->bitcoins = $bitcoins;
  97.     }
  98.     public function setBuySell($buySell){
  99.         $this->validateBuySell($buySell);
  100.         $this->buySell = $buySell;
  101.     }
  102.     public function setProcessing($processing){
  103.         $this->processing = $processing;
  104.     }
  105.     public function setTxid($txid){
  106.         $this->txid = $txid;
  107.     }
  108.    
  109.     public function setSellAt($sellAt){
  110.         $this->sellAt = $sellAt;
  111.     }
  112.    
  113.     public function setVolumeExecuted($volume){
  114.         $this->volumeExecuted = $volume;
  115.     }
  116.    
  117.     public function getIndex(){
  118.         return $this->index;
  119.     }
  120.    
  121.     public function addSell(){
  122.         $this->numberOfSells++;
  123.     }
  124.     public function addBuy(){
  125.         $this->numberOfBuys++;
  126.     }
  127.  
  128.     /**
  129.     * Validate margin
  130.     * Should be higher then double the fee
  131.     *
  132.     * @param mixed $margin   (1 = 1%, 10 = 10%, 0.5 = 0.5%)
  133.     */
  134.     protected function validateMargin($margin){
  135.         if (floatval($margin) == 0 || $margin <= 0 || $margin > 100){
  136.             throw new InvalidArgumentException('margin should be a percentage between 0 - 100');
  137.         }
  138.        
  139.         if ($margin <= (KrakenTrader::getFee(100) * 2)){
  140.             throw new InvalidArgumentException('Margin is lower then the current fee x 2. Profit will be negative.');
  141.         }
  142.         return true;
  143.     }
  144.    
  145.     /**
  146.     * Validate amount of bitcoins. 0.01 is minimum
  147.     *
  148.     * @param float $bitcoins
  149.     */
  150.     protected function validateBitcoins($bitcoins){
  151.         if ($bitcoins < 0.01){
  152.             throw new InvalidArgumentException('Not a valid trade amount (minimum = 0.01)');
  153.         }
  154.     }
  155.    
  156.     protected function validateBuySell($buySell){
  157.         if (!in_array($buySell, array(KrakenTrader::BUY, KrakenTrader::SELL))){
  158.             throw new InvalidArgumentException('buySell should be one of buy|sell');    
  159.         }
  160.     }
  161.    
  162. }
Advertisement
Add Comment
Please, Sign In to add comment