<?php
$Price = "0.00050159";
$stepsize = "0.0001";
    $stepsize1 = fmod($stepsize, 1);
$total_no = str_replace('.', '', $stepsize1);
$count = strlen($total_no) - 1;
$pos_qty = number_format($position_quantity,$count);
echo"<pre>";
print_r($pos_qty);
echo"</pre>";
die;
$fsym = "USDT";
$tsyms = "BTC";
$getfilecompare = file_get_contents("https://min-api.cryptocompare.com/data/price?fsym=".strtoupper($fsym)."&tsyms=".strtoupper($tsyms)."");
                            $getval  = json_decode($getfilecompare,TRUE);
                            $calbtc  = $getval['BTC'];
            
echo"<pre>";
print_r($res);
echo"</pre>";
die;
$date = "2019-03-05T09:56:55.728933+00:00";
echo"<pre>";
print_r(date('Y-m-d H:i:s',strtotime($date)));
echo"</pre>";
die;

/**
 * @author lin <465382251@qq.com>
 *
 * Fill in your key and secret and pass can be directly run
 *
 * Most of them are unfinished and need your help
 * https://github.com/zhouaini528/okex-php.git
 * */
use Lin\Okex\OkexV5;

$key = 'd5c2840d-af58-4d3b-b2f6-949314f6c1b8';
$secret = '9B24E5B3E49CB6F501EA53BDA93FB8BC';
$passphrase = 'Yanceyker89@';

$okex=new OkexV5($key,$secret,$passphrase);

$t = microtime(true);
$micro = sprintf("%06d",($t - floor($t)) * 1000000);
$d = new DateTime( date('Y-m-d H:i:s.'.$micro, $t) );

//You can set special needs
$okex->setOptions([
    //Set the request timeout to 60 seconds by default
    'timeout'=>60,

    //If you are developing locally and need an agent, you can set this
    //'proxy'=>true,
    //More flexible Settings
    /* 'proxy'=>[
     'http'  => 'http://127.0.0.1:12333',
     'https' => 'http://127.0.0.1:12333',
     'no'    =>  ['.cn']
     ], */
    //Close the certificate
    //'verify'=>false,

    //Set Demo Trading
    'headers'=>['x-simulated-trading'=>1]
]);


$symbol = "BTCUSDT";

$new = substr_replace( $symbol, '-', -4, 0 );


try {
    $result=$okex->market()->getTicker([
        'instId'=>'BTC-USD-SWAP',
    ]);
    echo "<pre>";
    print_r($result);
    echo "<pre>";
    die;
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

die;
// try {
//     $result=$okex->trade()->postOrder([
//         'instId'=>'BTC-USDT',
//         'tdMode'=>'cross',
//         'clOrdId'=>'xxxxxxxxxxx',
//         'side'=>'buy',
//         'ordType'=>'limit',
//         'sz'=>'0.01',
//         'px'=>'10000',
//     ]);
//     print_r($result);
// }catch (\Exception $e){
//     print_r(json_decode($e->getMessage(),true));
// }

// try {
//     $result=$okex->trade()->postCancelOrder([
//         'instId'=>'BTC-USDT',
//         'ordId'=>'xxxxxxxxx',
//         //'clOrdId'=>'xxxxxxxxxxx',
//     ]);
//     print_r($result);
// }catch (\Exception $e){
//     print_r(json_decode($e->getMessage(),true));
// }


// try {
//     $result=$okex->trade()->postAmendOrder([
//         'instId'=>'BTC-USDT',
//         'ordId'=>'xxxxxxxxx',
//         //'clOrdId'=>'xxxxxxxxxxx',
//         'newSz'=>'0.012',
//         'newPx'=>'11000',
//     ]);
//     print_r($result);
// }catch (\Exception $e){
//     print_r(json_decode($e->getMessage(),true));
// }

// try {
//     $result=$okex->trade()->getOrder([
//         'instId'=>'BTC-USDT',
//         'ordId'=>'472166915117555712',
//         //'clOrdId'=>'xxxxxxxxxxx',
//     ]);
   
// }catch (\Exception $e){
//     print_r(json_decode($e->getMessage(),true));
// }
// echo"<pre>";
// print_r($result);
// echo"</pre>";
// die;

// try {
//     $result=$okex->trade()->postOrderAlgo([
//         'instId'=>'BTC-USDT',
//         'tdMode'=>'cross',
//         'clOrdId'=>'xxxxxxxxxxx',
//         'side'=>'buy',
//         'ordType'=>'trigger',
//         'sz'=>'0.01',
//     ]);
//     print_r($result);
// }catch (\Exception $e){
//     print_r(json_decode($e->getMessage(),true));
// }



// ftx api key : lUsmd83bvErGkf4q0ctifaK8kaT1-uf2ZATn4uaH
// ftx secret key : W8tKzcC420jUJPX_6Bl6MeNv5Dyg266X0rtIF33q

/**
 * @author lin <465382251@qq.com>
 *
 * Fill in your key and secret and pass can be directly run
 *
 * Most of them are unfinished and need your help
 * https://github.com/zhouaini528/okex-php.git
 * */


$key = 'd5c2840d-af58-4d3b-b2f6-949314f6c1b8';
$secret = '9B24E5B3E49CB6F501EA53BDA93FB8BC';
$passphrase = 'Yanceyker89@';

$okex=new OkexV5($key,$secret,$passphrase);

//You can set special needs
$okex->setOptions([
    //Set the request timeout to 60 seconds by default
    'timeout'=>60,


    //If you are developing locally and need an agent, you can set this
    //'proxy'=>true,
    //More flexible Settings
    /* 'proxy'=>[
     'http'  => 'http://127.0.0.1:12333',
     'https' => 'http://127.0.0.1:12333',
     'no'    =>  ['.cn']
     ], */
    //Close the certificate
    //'verify'=>false,

    //Set Demo Trading
    'headers'=>['x-simulated-trading'=>1,'expTime'=> time() - 1000]
]);

$result = $okex->trade()->getOrder([
        'instId'=>'BTC-USDT',
        'ordId'=>'472166915117555712',
        //'clOrdId'=>'xxxxxxxxxxx',
]);

echo"<pre>";
print_r($result);
echo"</pre>";
die;



// try {
//     $result=$okex->market()->getIndexCandles([
//         'instId'=>'BTC-USD',
//         'limit' => 1
//     ]);
//     echo "<pre>";
//     print_r($result);
//     echo "<pre>";
//     die;
// }catch (\Exception $e){
//     print_r(json_decode($e->getMessage(),true));
// }


try {
    $result=$okex->market()->getTickers([
        'instType'=>'SPOT',
    ]);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}


 $i=0;
$required_pairs = ['1INCH','ADA','AAVE','ATOM','ALGO','AION','AVAX','AKRO','ANT','APE','BAT','BTC','BCH','BNB','BTCST','BAKE','CRV','COMP','CAKE','COS','DOGE','DASH','DOT','ETH','EOS','ETC','FTM','FTT','FIL','GALA','GRT','GMT','HBAR','HOT','IOTA','ICP','IOST','JST','KAVA','KSM','LTC','LINK','LIT','LUNA','MANA','MATIC','MDX','NEO','OMG','ONT','QTUM','REEF','RSR','SUN','SHIB','SLP','SOL','SUSHI','SXP','TRX','THETA','TFUEL','UNI','VET','WRX','XRP','XMR','XEM','XLM','XTZ','YFI','YFII','ZEC','ZEN'];
if(!empty($result['data'])) { 
foreach ($result['data'] as $key => $value) {
  if(substr($value['instId'], -5) == "-USDT"){
    if(in_array(substr_replace($value['instId'] ,"",-5), $required_pairs)){
      $output[$i]['symbol'] = str_replace('-','',$value['instId']);
      $output[$i]['price'] = $value['last'];
      $output[$i]['change'] = (($value['open24h'] - $value['last'])/$value['open24h']) * 100;
    }
      $i++;
  }
}
}
echo "<pre>";
print_r($output);
echo "</pre>";
die;

// try {
//     $result=$okex->market()->getTickers([
//         'instType'=>'SPOT',
//     ]);
//     echo "<pre>";
//     print_r($result);
//     echo "<pre>";
// }catch (\Exception $e){
//     print_r(json_decode($e->getMessage(),true));
// }

// try {
//     $result=$okex->market()->getTicker([
//         'instId'=>'BTC-USDT-SWAP',
//     ]);
//     echo "<pre>";
//     print_r($result);
//     echo "<pre>";
// }catch (\Exception $e){
//     print_r(json_decode($e->getMessage(),true));
// }

echo"<pre>";
print_r($okex->account()->getBalance());
echo"</pre>";

// try {
//     $result=$okex->trade()->postOrder([
//         'instId'=>'BTC-USDT',
//         'tdMode'=>'cash',
//         'side'=>'sell',
//         'ordType'=>'market',
//         'sz'=>'0.00476823699',
//     ]);
//    echo"<pre>";
//    print_r($result);
//    echo"</pre>";
// }catch (\Exception $e){
//     print_r(json_decode($e->getMessage(),true));
// }

//result array 

// Array
// (
//     [code] => 0
//     [data] => Array
//         (
//             [0] => Array
//                 (
//                     [clOrdId] => 
//                     [ordId] => 472159876899282944
//                     [sCode] => 0
//                     [sMsg] => 
//                     [tag] => 
//                 )

// )

//     [msg] => 
// )


echo"<pre>";
print_r($okex->account()->getBalance());
echo"</pre>";
// die;

// try {
//     $result=$okex->trade()->postCancelOrder([
//         'instId'=>'BTC-USDT',
//         'ordId'=>'xxxxxxxxx',
//         //'clOrdId'=>'xxxxxxxxxxx',
//     ]);
//     print_r($result);
// }catch (\Exception $e){
//     print_r(json_decode($e->getMessage(),true));
// }


// try {
//     $result=$okex->trade()->postAmendOrder([
//         'instId'=>'BTC-USDT',
//         'ordId'=>'xxxxxxxxx',
//         //'clOrdId'=>'xxxxxxxxxxx',
//         'newSz'=>'0.012',
//         'newPx'=>'11000',
//     ]);
//     print_r($result);
// }catch (\Exception $e){
//     print_r(json_decode($e->getMessage(),true));
// }

try {
    $result=$okex->trade()->getOrder([
        'instId'=>'BTC-USDT',
        'ordId'=>'472166915117555712',
        //'clOrdId'=>'xxxxxxxxxxx',
    ]);
    echo "<pre>";
    print_r($result);
    echo "<pre>";
    die;
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}


try {
    $result=$okex->trade()->postOrderAlgo([
        'instId'=>'BTC-USDT',
        'tdMode'=>'cross',
        'clOrdId'=>'xxxxxxxxxxx',
        'side'=>'buy',
        'ordType'=>'trigger',
        'sz'=>'0.01',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}


