PHP code example of timirey / xapi-php
1. Go to this page and download the library: Download timirey/xapi-php 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/ */
timirey / xapi-php example snippets
use Timirey\XApi\Client;
use Timirey\XApi\Enums\Host;
use Timirey\XApi\Responses\GetSymbolResponse;
$client = new Client(
userId: 12345678,
password: 'password',
host: Host::DEMO,
appName: 'My Test App'
);
/**
* @var $response GetSymbolResponse
*/
$response = $client->getSymbol(
symbol: 'EURUSD'
);
use Timirey\XApi\Client;
use Timirey\XApi\Enums\Host;
use Timirey\XApi\Responses\Data\TickStreamRecord;
use Timirey\XApi\Responses\FetchTickPricesResponse;
$client = new Client(
userId: 12345678,
password: 'password',
host: Host::DEMO,
appName: 'My Test App'
);
// Meant to be a daemon, run as separate process.
$client->fetchTickPrices(
symbol: 'EURUSD',
callback: static function (FetchTickPricesResponse $response): void {
/**
* @var TickStreamRecord $record
*/
$record = $response->tickStreamRecord;
},
minArrivalTime: 100,
maxLevel: 1
);
// Unreachable code.
use Timirey\XApi\Responses\LoginResponse;
use Timirey\XApi\Client;
/**
* @var LoginResponse $response
* @var Client $client
*/
$response = $client->login(
userId: 123456789,
password: 'password',
appName: 'My App'
);
use Timirey\XApi\Responses\LogoutResponse;
use Timirey\XApi\Client;
/**
* @var LogoutResponse $response
* @var Client $client
*/
$response = $client->logout();
use Timirey\XApi\Responses\Data\BalanceStreamRecord;
use Timirey\XApi\Responses\FetchBalanceResponse;
use Timirey\XApi\Client;
/**
* @var Client $client
*/
$client->fetchBalance(
callback: static function (FetchBalanceResponse $response): void {
/**
* @var BalanceStreamRecord $record
*/
$record = $response->balanceStreamRecord;
}
);
use Timirey\XApi\Responses\Data\CandleStreamRecord;
use Timirey\XApi\Responses\FetchCandlesResponse;
use Timirey\XApi\Client;
/**
* @var Client $client
*/
$client->fetchCandles(
symbol: 'EURUSD',
callback: static function (FetchCandlesResponse $response): void {
/**
* @var CandleStreamRecord $record
*/
$record = $response->candleStreamRecord;
}
);
use Timirey\XApi\Responses\Data\KeepAliveStreamRecord;
use Timirey\XApi\Responses\FetchKeepAliveResponse;
use Timirey\XApi\Client;
/**
* @var Client $client
*/
$client->fetchKeepAlive(
callback: static function (FetchKeepAliveResponse $response): void {
/**
* @var KeepAliveStreamRecord $record
*/
$record = $response->keepAliveStreamRecord;
}
);
use Timirey\XApi\Responses\Data\NewsStreamRecord;
use Timirey\XApi\Responses\FetchNewsResponse;
use Timirey\XApi\Client;
/**
* @var Client $client
*/
$client->fetchNews(
callback: static function (FetchNewsResponse $response): void {
/**
* @var NewsStreamRecord $record
*/
$record = $response->newsStreamRecord;
}
);
use Timirey\XApi\Responses\Data\ProfitStreamRecord;
use Timirey\XApi\Responses\FetchProfitsResponse;
use Timirey\XApi\Client;
/**
* @var Client $client
*/
$client->fetchProfits(
callback: static function (FetchProfitsResponse $response): void {
/**
* @var ProfitStreamRecord $record
*/
$record = $response->profitStreamRecord;
}
);
use Timirey\XApi\Responses\Data\TickStreamRecord;
use Timirey\XApi\Responses\FetchTickPricesResponse;
use Timirey\XApi\Client;
/**
* @var Client $client
*/
$client->fetchTickPrices(
symbol: 'EURUSD',
callback: static function (FetchTickPricesResponse $response): void {
/**
* @var TickStreamRecord $record
*/
$record = $response->tickStreamRecord;
},
maxLevel: 1,
minArrivalTime: 200
);
use Timirey\XApi\Responses\Data\TradeStreamRecord;
use Timirey\XApi\Responses\FetchTradesResponse;
use Timirey\XApi\Client;
/**
* @var Client $client
*/
$client->fetchTrades(
callback: static function (FetchTradesResponse $response): void {
/**
* @var TradeStreamRecord $record
*/
$record = $response->tradeStreamRecord;
}
);
use Timirey\XApi\Responses\Data\TradeStatusStreamRecord;
use Timirey\XApi\Responses\FetchTradeStatusResponse;
use Timirey\XApi\Client;
/**
* @var Client $client
*/
$client->fetchTradeStatus(
callback: static function (FetchTradeStatusResponse $response): void {
/**
* @var TradeStatusStreamRecord $record
*/
$record = $response->tradeStatusStreamRecord;
}
);
use Timirey\XApi\Client;
/**
* @var Client $client
*/
$client->pingStream();
use Timirey\XApi\Responses\GetAllSymbolsResponse;
use Timirey\XApi\Client;
/**
* @var GetAllSymbolsResponse $response
* @var Client $client
*/
$response = $client->getAllSymbols();
use Timirey\XApi\Responses\GetCalendarResponse;
use Timirey\XApi\Client;
/**
* @var GetCalendarResponse $response
* @var Client $client
*/
$response = $client->getCalendar();
use Timirey\XApi\Responses\GetChartLastRequestResponse;
use Timirey\XApi\Client;
use Timirey\XApi\Payloads\Data\ChartLastInfoRecord;
use Timirey\XApi\Enums\Period;
use DateTime;
$chartLastInfoRecord = new ChartLastInfoRecord(
period: Period::PERIOD_M1,
start: new DateTime('-1 week'),
symbol: 'EURUSD'
);
/**
* @var GetChartLastRequestResponse $response
* @var Client $client
*/
$response = $client->getChartLastRequest(
chartLastInfoRecord: $chartLastInfoRecord
);
use Timirey\XApi\Responses\GetChartRangeRequestResponse;
use Timirey\XApi\Client;
use Timirey\XApi\Payloads\Data\ChartRangeInfoRecord;
use Timirey\XApi\Enums\Period;
use DateTime;
$chartRangeInfoRecord = new ChartRangeInfoRecord(
period: Period::PERIOD_H1,
start: new DateTime('-1 month'),
end: new DateTime(),
symbol: 'EURUSD',
ticks: 1000
);
/**
* @var GetChartRangeRequestResponse $response
* @var Client $client
*/
$response = $client->getChartRangeRequest(
chartRangeInfoRecord: $chartRangeInfoRecord
);
use Timirey\XApi\Responses\GetCommissionDefResponse;
use Timirey\XApi\Client;
/**
* @var GetCommissionDefResponse $response
* @var Client $client
*/
$response = $client->getCommissionDef(
symbol: 'EURUSD',
volume: 1.0
);
use Timirey\XApi\Responses\GetCurrentUserDataResponse;
use Timirey\XApi\Client;
/**
* @var GetCurrentUserDataResponse $response
* @var Client $client
*/
$response = $client->getCurrentUserData();
use Timirey\XApi\Responses\GetIbsHistoryResponse;
use Timirey\XApi\Client;
use DateTime;
/**
* @var GetIbsHistoryResponse $response
* @var Client $client
*/
$response = $client->getIbsHistory(
start: new DateTime('-1 month'),
end: new DateTime()
);
use Timirey\XApi\Responses\GetMarginLevelResponse;
use Timirey\XApi\Client;
/**
* @var GetMarginLevelResponse $response
* @var Client $client
*/
$response = $client->getMarginLevel();
use Timirey\XApi\Responses\GetMarginTradeResponse;
use Timirey\XApi\Client;
/**
* @var GetMarginTradeResponse $response
* @var Client $client
*/
$response = $client->getMarginTrade(
symbol: 'EURPLN',
volume: 1.0
);
use Timirey\XApi\Responses\GetNewsResponse;
use Timirey\XApi\Client;
use DateTime;
/**
* @var GetNewsResponse $response
* @var Client $client
*/
$response = $client->getNews(
start: new DateTime('-1 month'),
end: new DateTime()
);
use Timirey\XApi\Responses\GetProfitCalculationResponse;
use Timirey\XApi\Client;
use Timirey\XApi\Enums\Cmd;
/**
* @var GetProfitCalculationResponse $response
* @var Client $client
*/
$response = $client->getProfitCalculation(
closePrice: 1.3000,
cmd: Cmd::BUY,
openPrice: 1.2233,
symbol: 'EURPLN',
volume: 1.0
);
use Timirey\XApi\Responses\GetServerTimeResponse;
use Timirey\XApi\Client;
/**
* @var GetServerTimeResponse $response
* @var Client $client
*/
$response = $client->getServerTime();
use Timirey\XApi\Responses\GetStepRulesResponse;
use Timirey\XApi\Client;
/**
* @var GetStepRulesResponse $response
* @var Client $client
*/
$response = $client->getStepRules();
use Timirey\XApi\Responses\GetSymbolResponse;
use Timirey\XApi\Client;
/**
* @var GetSymbolResponse $response
* @var Client $client
*/
$response = $client->getSymbol(
symbol: EURUSD
);
use Timirey\XApi\Responses\GetTickPricesResponse;
use Timirey\XApi\Enums\Level;
use Timirey\XApi\Client;
use DateTime;
/**
* @var GetTickPricesResponse $response
* @var Client $client
*/
$response = $client->getTickPrices(
level: Level::BASE,
symbols: ['EURPLN', 'AGO.PL'],
timestamp: new DateTime()
);
use Timirey\XApi\Responses\GetTradeRecordsResponse;
use Timirey\XApi\Client;
/**
* @var GetTradeRecordsResponse $response
* @var Client $client
*/
$response = $client->getTradeRecords(
orders: [7489839, 7489841]
);
use Timirey\XApi\Responses\GetTradesResponse;
use Timirey\XApi\Client;
/**
* @var GetTradesResponse $response
* @var Client $client
*/
$response = $client->getTrades(
openedOnly: true
);
use Timirey\XApi\Responses\GetTradesHistoryResponse;
use Timirey\XApi\Client;
use DateTime;
/**
* @var GetTradesHistoryResponse $response
* @var Client $client
*/
$response = $client->getTradesHistory(
start: new DateTime('last month'),
end: new DateTime()
);
use Timirey\XApi\Responses\GetTradingHoursResponse;
use Timirey\XApi\Client;
/**
* @var GetTradingHoursResponse $response
* @var Client $client
*/
$response = $client->getTradingHours(
symbols: ['EURPLN', 'AGO.PL']
);
use Timirey\XApi\Responses\GetVersionResponse;
use Timirey\XApi\Client;
/**
* @var GetVersionResponse $response
* @var Client $client
*/
$response = $client->getVersion();
use Timirey\XApi\Responses\PingResponse;
use Timirey\XApi\Client;
/**
* @var PingResponse $response
* @var Client $client
*/
$response = $client->ping();
use Timirey\XApi\Responses\TradeTransactionResponse;
use Timirey\XApi\Payloads\Data\TradeTransInfo;
use Timirey\XApi\Client;
$tradeTransInfo = new TradeTransInfo(
cmd: Cmd::BUY,
customComment: 'Test trade',
expiration: new DateTime(),
offset: 0,
order: 0,
price: 1.12345,
sl: 1.12000,
symbol: 'EURUSD',
tp: 1.12500,
type: Type::OPEN,
volume: 1.0
);
/**
* @var TradeTransactionResponse $response
* @var Client $client
*/
$response = $client->tradeTransaction(
tradeTransInfo: $tradeTransInfo
);
use Timirey\XApi\Responses\TradeTransactionStatusResponse;
use Timirey\XApi\Client;
/**
* @var TradeTransactionStatusResponse $response
* @var Client $client
*/
$response = $client->tradeTransactionStatus(
order: 123456
);
use Timirey\XApi\Exceptions\ResponseException;
use Timirey\XApi\Enums\Host;
use Timirey\XApi\Client;
/**
* @var Client $client
*/
$client = new Client(
userId: 123456789,
password: 'invalidPassword',
host: Host::DEMO
);
try {
$client->getVersion();
} catch (ErrorResponseException $e) {
echo ($e->getErrorCode()); // 'BE005'
echo ($e->getErrorDescr()); // 'userPasswordCheck: Invalid login or password.'
}
SH
composer