composer require james.rus52/tinkoffinvest
or add to your compose.json
{
"require": {
"james.rus52/tinkoffinvest": "^1.*"
}
}
and then
composer install
Include classes via autoloader
require_once 'vendor/autoload.php';
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;
create token to use tinkoff invest on Tinkoff invest setting page
Create client instance for sandbox
$client = new TIClient("TOKEN",TISiteEnum::SANDBOX);
or real exchange
$client = new TIClient("TOKEN",TISiteEnum::EXCHANGE);
Put money to your sandbox account (sandbox only)
$client->sbCurrencyBalance(500,TICurrencyEnum::USD);
Client register on sandbox (sandbox only)
$client->sbRegister();
Client remove account on sandbox (sandbox only)
$client->sbRemove();
Put stocks to your sandbox account (sandbox only)
$client->sbPositionBalance(10.4,"BBG000BR37X2");
Clear all positions on sandbox (sandbox only)
$client->sbClear();
Get all stocks/bonds/etfs/currencies from market
$stockes = $client->getStocks();
$instr = $client->getBonds();
$instr = $client->getEtfs();
$instr = $client->getCurrencies();
or with filter
$stockes = $client->getStocks(["V","LKOH"]);
$instr = $client->getBonds(["RU000A0JX3X7"]);
$instr = $client->getEtfs(["FXRU"]);
$instr = $client->getCurrencies(["USD000UTSTOM"]);
Get instrument by ticker
$instr = $client->getInstrumentByTicker("AMZN");
or by figi
$instr = $client->getInstrumentByFigi("BBG000BR37X2");
Get history OrderBook
$book = $client->getHistoryOrderBook("BBG000BR37X2", 1);
Get historical Candles
$from = new \DateTime();
$from->sub(new \DateInterval("P7D"));
$to = new \DateTime();
$candles = $client->getHistoryCandles("BBG000BR37X2", $from, $to, TIIntervalEnum::MIN15);
Get accounts
$accounts = $client->getAccounts();
Get portfolio (if null, used default Tinkoff account)
$port = $client->getPortfolio(TIAccount $account = null);
Get portfolio balance
print $port->getCurrencyBalance(TICurrencyEnum::RUB);
Get instrument lots count
print $port->getinstrumentLots("PGR");
Send limit order (default brokerAccountId = Tinkoff)
$order = $client->sendOrder("BBG000BVPV84", 1, TIOperationEnum::BUY, 1.2);
print $order->getOrderId();
Send market order (default brokerAccountId = Tinkoff)
$order = $client->sendOrder("BBG000BVPV84", 1, TIOperationEnum::BUY);
print $order->getOrderId();
Cancel order
$client->cancelOrder($order->getOrderId());
List of operations from 10 days ago to 30 days period
$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";
Getting instrument status
$status = $client->getInstrumentInfo($sber->getFigi());
print 'Instrument status: '. $status->getTrade_status()."\n";
Get Candles and Order books
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";
}
You can also to subscribe on changes order books, candles or instrument info: First of all, make a callback function to manage events:
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";
}
Then subscribe to events
$client->subscribeGettingCandle($sber->getFigi(), TICandleIntervalEnum::MIN1);
$client->subscribeGettingOrderBook($sber->getFigi(), 2);
and finaly start listening new events
$client->startGetting("action",20,60);
in this example we awaiting max 20 respnse and max for 60 seconds if you want no limits, you should make
$client->startGetting("action");
$client->startGetting("action",null,600);
$client->startGetting("action",1000,null);
to stop listening do
$client->stopGetting();
If you use subscriptions you should check figi on response, because you getting all subscribed instruments in one queue
Please support my project
MIT