PHP code example of james.rus52 / tinkoffinvest

1. Go to this page and download the library: Download james.rus52/tinkoffinvest library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

james.rus52 / tinkoffinvest example snippets




use \jamesRUS52\TinkoffInvest\TIClient;
use \jamesRUS52\TinkoffInvest\TISiteEnum;
use \jamesRUS52\TinkoffInvest\TICurrencyEnum;
use \jamesRUS52\TinkoffInvest\TIInstrument;
use \jamesRUS52\TinkoffInvest\TIPortfolio;
use \jamesRUS52\TinkoffInvest\TIOperationEnum;
use \jamesRUS52\TinkoffInvest\TIIntervalEnum;
use \jamesRUS52\TinkoffInvest\TICandleIntervalEnum;
use \jamesRUS52\TinkoffInvest\TICandle;
use \jamesRUS52\TinkoffInvest\TIOrderBook;
use \jamesRUS52\TinkoffInvest\TIInstrumentInfo;


$client = new TIClient("TOKEN",TISiteEnum::SANDBOX);

$client = new TIClient("TOKEN",TISiteEnum::EXCHANGE);

$client->sbCurrencyBalance(500,TICurrencyEnum::USD);

$client->sbRegister();

$client->sbRemove();

$client->sbPositionBalance(10.4,"BBG000BR37X2");

$client->sbClear();

$stockes = $client->getStocks();
$instr = $client->getBonds();
$instr = $client->getEtfs();
$instr = $client->getCurrencies();

$stockes = $client->getStocks(["V","LKOH"]);
$instr = $client->getBonds(["RU000A0JX3X7"]);
$instr = $client->getEtfs(["FXRU"]);
$instr = $client->getCurrencies(["USD000UTSTOM"]);

$instr = $client->getInstrumentByTicker("AMZN");

$instr = $client->getInstrumentByFigi("BBG000BR37X2");

$book = $client->getHistoryOrderBook("BBG000BR37X2", 1); 

$from = new \DateTime();
$from->sub(new \DateInterval("P7D"));
$to = new \DateTime();
$candles = $client->getHistoryCandles("BBG000BR37X2", $from, $to, TIIntervalEnum::MIN15);

$accounts = $client->getAccounts(); 

$port = $client->getPortfolio(TIAccount $account = null);

print $port->getCurrencyBalance(TICurrencyEnum::RUB);

print $port->getinstrumentLots("PGR");

$order = $client->sendOrder("BBG000BVPV84", 1, TIOperationEnum::BUY, 1.2);
print $order->getOrderId();

$order = $client->sendOrder("BBG000BVPV84", 1, TIOperationEnum::BUY);
print $order->getOrderId();

$client->cancelOrder($order->getOrderId());

$from = new \DateTime();
$from->sub(new \DateInterval("P7D"));
$to = new \DateTime();
$operations = $client->getOperations($from, $to);
foreach ($operations as $operation)
  print $operation->getId ().' '.$operation->getFigi (). ' '.$operation->getPrice ().' '.$operation->getOperationType().' '.$operation->getDate()->format('d.m.Y H:i')."\n";


$status = $client->getInstrumentInfo($sber->getFigi());
print 'Instrument status: '. $status->getTrade_status()."\n";

if ($status->getTrade_status()=="normal_trading")
{
        $candle = $client->getCandle($sber->getFigi(), TICandleIntervalEnum::DAY);
        print 'Low: '.$candle->getLow(). ' High: '.$candle->getHigh().' Open: '.$candle->getOpen().' Close: '.$candle->getClose().' Volume: '.$candle->getVolume()."\n";

        $orderbook = $client->getOrderBook($sber->getFigi(),2);
        print 'Price to buy: '.$orderbook->getBestPriceToBuy().' Available lots: '.$orderbook->getBestPriceToBuyLotCount().' Price to Sell: '.$orderbook->getBestPriceToSell().' Available lots: '.$orderbook->getBestPriceToSellLotCount()."\n";
}

function action($obj)
{
        print "action\n";
        if ($obj instanceof TICandle)
            print 'Time: '.$obj->getTime ()->format('d.m.Y H:i:s').' Volume: '.$obj->getVolume ()."\n";
        if ($obj instanceof TIOrderBook)
            print 'Price to Buy: '.$obj->getBestPriceToBuy().' Price to Sell: '.$obj->getBestPriceToSell()."\n";
}

$client->subscribeGettingCandle($sber->getFigi(), TICandleIntervalEnum::MIN1);
$client->subscribeGettingOrderBook($sber->getFigi(), 2);

$client->startGetting("action",20,60);

$client->startGetting("action");
$client->startGetting("action",null,600);
$client->startGetting("action",1000,null);

$client->stopGetting();