作者 karlet

feat:k线获取

@@ -771,6 +771,52 @@ class CmBroker @@ -771,6 +771,52 @@ class CmBroker
771 } 771 }
772 return []; 772 return [];
773 } 773 }
  774 +
  775 + /**
  776 + * 获取k线数据 根据开始结束时间
  777 + * @param string $symbol 交易对
  778 + * @param string $peroid 周期
  779 + * @param int $startTs 开始时间
  780 + * @param int $endTs 结束时间
  781 + * @return Kline[] 返回K线数组
  782 + */
  783 + public function getKlinesBt($symbol, $peroid, $startTs, $endTs)
  784 + {
  785 + $symbolOri = $this->getSymbolOri($symbol);
  786 + $peroidOri = $this->getPeriodOri($this->plat, $peroid);
  787 + if ($this->plat == self::PLAT_OKX) {
  788 + $limit = 100;
  789 + $res = $this->exBroker->getKlines($symbolOri, $peroidOri, $limit);
  790 + if ($res['code'] != '0') {
  791 + return [];
  792 + }
  793 + $newKlines = [];
  794 + foreach ($res['data'] as $key => $value) {
  795 + $kline = Kline::transferOkx($value);
  796 + $newKlines[] = $kline;
  797 + }
  798 + //把 $newKlines 倒序
  799 + $newKlines = array_reverse($newKlines);
  800 + return $newKlines;
  801 + }
  802 + if ($this->plat == self::PLAT_BINANCE) {
  803 + // $res = $this->exBroker->getKlines($symbolOri, $peroidOri, $limit);
  804 + $res = $this->exBroker->getKlines($symbolOri, $peroidOri, 1500, $startTs, $endTs);
  805 + if (count($res) == 0) {
  806 + return [];
  807 + }
  808 + $newKlines = [];
  809 + foreach ($res as $key => $value) {
  810 + $kline = Kline::transferBinanceRest($value);
  811 + $newKlines[] = $kline;
  812 + }
  813 + //把 $newKlines 倒序
  814 + $newKlines = array_reverse($newKlines);
  815 + return $newKlines;
  816 + }
  817 + return [];
  818 + }
  819 +
774 public function initPositions() 820 public function initPositions()
775 { 821 {
776 $this->positions = $this->getAllPos(); 822 $this->positions = $this->getAllPos();