微交易平台搭建开发-盘内实时K线对接
微盘,微交易系统是一个有争议的系统,为什么这么说呢,因为有玩家被割了韭菜,有玩家被杀了猪,这是因为遇到了不法运营者。今天我们暂且不论运营问题,单从技术的角度讨论一下微交易系统K线的对接。
微交易盘有时间盘和点位盘两种,看涨看跌,买点位,这些的基础就是你的**要对接好行情,不管你是期货,黄金白银,还是数字货币还是股票,总之你要让K线动起来,让行情跑起来。那么有很多交易所和平台都有对外提供行情接口API,所以给广大运营者提供了很大的便利,根据不同的API接口,我们按照对接文档获取到我们需要的数据然后入库就可以了。
今天就以新浪抓取黄金、白银、原油的数据为例子讲解一下微盘K线数据获取入库
一般的接口是通过post或者get或者socket的方式进行接口信息交互。市面上的微交易**大多是php版本。那么以php获取数据为例:
接口地址:http://hq.sinajs.cn/
list后面代上需要获取的行情接口产品代码,
那么返回值如下:
var hq_str_hf_CL="42.264,,42.200,42.230,42.570,41.620,05:00:00,42.240,42.330,154923.000,11,1,2020-08-15,WTI纽约原油,281945"; var hq_str_hf_SI="26.469,,26.525,26.545,27.885,25.780,04:59:59,27.718,27.620,91009.000,1,4,2020-08-15,纽约白银,220726"; var hq_str_hf_GC="1952.240,,1953.200,1953.900,1970.000,1939.100,05:00:00,1970.400,1963.800,405157.000,3,1,2020-08-15,纽约黄金,237092";
原油 ,白银和黄金,返回数据依次代表:现价,涨跌幅,买价,卖价, 最高价,最低价, ,收盘价,开盘价 持仓量,买量,卖量,名称。再根据php的正则方式切割每个参数出来入库。详细代码如下:
public function getmessage(){
if(date('H') == '5'){
$time = strtotime(date('Y-m-d',strtotime('-3 day')));
M("Api")->where(array('time'=>array('LT',$time)))->delete();
}
$url = 'http://hq.sinajs.cn/?_/&list=hf_CL,hf_SI,hf_GC';//,hf_SI,hf_GC
$data = $this->httpGet($url);
$data2 = explode(';',$data);
$data3 ="";
//echo count($data2)-1;exit;
for($index=0;$index<count($data2)-1;$index++)
{
$one = substr( $data2[$index], stripos($data2[$index], "\"")+1, -1);
$onellist = explode(',',$one);
$kine = "";
if(strpos($data2[$index], "hq_str_hf_CL"))//沥青
{
//48.62,1.8860,48.62,48.63, 48.87,48.31,22:24:42,47.72,48.76, 18043,0,0,2017-03-15,NYMEX原油
//现价,涨跌幅,买价,卖价, 最高价,最低价, ,收盘价,开盘价 持仓量,买量,卖量,名称
$kine['cid'] = 28;
$kine['code'] = "CONO";
}else if(strpos($data2[$index], "hq_str_hf_SI")){//白银
//16.870,-0.2955,16.870,16.870,16.980,16.820,22:50:23,16.920,16.890,1076,0,0,2017-03-15,COMEX白银
//现价,涨跌幅,买价,卖价, 最高价,最低价, ,收盘价,开盘价 持仓量,买量,卖量,名称
$kine['cid'] = 15;
$kine['code'] = "XAG";
}else if(strpos($data2[$index], "hq_str_hf_GC")){//黄金
//1199.0,-0.2993,1198.9,1199.0,1204.8,1196.8,22:50:53,1202.6,1198.7,10300,0,0,2017-03-15,COMEX黄金
//现价,涨跌幅,买价,卖价, 最高价,最低价, ,收盘价,开盘价 持仓量,买量,卖量,名称
$kine['cid'] = 26;
$kine['code'] = "XAU";
}
$kine['price'] = $onellist[0];
$kine['diff'] = round($onellist[0]-$onellist[7],2);
$kine['diffRate'] = round($onellist[1],2).'%';
$kine['time'] = time();
$kine['high'] = $onellist[4];
$kine['low'] = $onellist[5];
$kine['closes'] = $onellist[7];
$kine['open'] = $onellist[8];
$kine['name'] = $onellist[13];
$data3[$index] = $kine;}
//print_r($data3);exit;//F('cc'.time(),$data);
$data_list = $data3;//json_decode($data);
foreach($data_list as $k=>$v){
$acc = S('apiDataPrice'.$v['code']);
$aee = $this->catproductcode($v['code']);
$cid = $aee['cid'];
$v['cid'] = $cid;
if((($acc['price'] != $v['price']) || ((date('w') == 6 && 4 <= date("H")) || (date('w') == 0) || (date('w') == 1 && date("H") <= 8) || (4 <= date("H") && date("H") <= 8))) && strstr($v['low'],'-') === false && strstr($v['price'],'..') === false && strstr($v['price'],'-') === false && strstr($v['high'],'%') === false && strstr($v['high'],'-') === false){
S('apiDataPrice'.$v['code'],$v);
if($cid){
$zuoclose = S('zuoclose'.$v['code']);
if(!$zuoclose){
$aqq['code'] = $v['code'];
$time = strtotime(date('Y-m-d'))+4*3600;
$aqq['time'] = array('ELT',$time);
$zuoclose = M('Api')->where($aqq)->order('time desc')->field('price')->find();
S('zuoclose'.$v['code'],$zuoclose['price'],24*3600);
}
$v['close'] = $zuoclose;
S('price'.$v['code'],$v);
if($acc['high'] != $v['high']){
S('high'.$v['code'],$v['high']);
}
if($acc['low'] != $v['low']){
S('low'.$v['code'],$v['low']);
}if($acc['open'] != $v['open']){
S('open'.$v['code'],$v['open']);
}
S('close'.$v['code'],$zuoclose);
$arr['cid'] = $cid;
$arr['code'] = $v['code'];
$arr['price'] = $v['price'];
$arr['high'] = $v['high'];
$arr['low'] = $v['low'];
$arr['open'] = $v['open'];
$arr['close'] = $zuoclose;
$arr['time'] = $v['time'];
$arr['diff'] = $v['diff'];
$arr['diffRate'] = $v['diffRate'];
M('api')->add($arr);
}
}}
$config = M('Webconfig')->where('id=1')->field('day,hour,minute')->find();
$time = $config['day']*24*3600 + $config['hour']*3600 + $config['minute']*60 + F('qiangp');
//查询所有正在交易的订单
$arraynow = M('order')->where(array('ostaus'=>0))->select();
//逐个判断盈亏比例
foreach($arraynow as $k=>$v){
$product = $this->productpid($v['pid']);
if($product){
$cid = $product['cid'];
$aff = $this->catproductcid($cid);
$code = $aff['title'];
if(time() == S('CheatTime'.$code) || time() == S('CheatTimes'.$code)){
$arr = S('CheatPrice'.$code);
}else{
$arr = S('price'.$code);
}
$youjia = (float)$arr['price'];
if($youjia < 0 || !$youjia){
$where['code'] = $code;
$where['time'] = array('ELT',time());
$aee = M('Api')->where($where)->order('time desc')->field('price')->find();
$youjia = $aee['price'];
}
$order['eid'] = $v['eid'];
$order['uprice'] = $product['uprice'];
$order['onumber'] = $v['onumber'];
$order['buyprice'] = $v['buyprice'];
$order['wave'] = $product['wave'];
$order['ostyle'] = $v['ostyle'];
$order['endprofit'] = $v['endprofit'];
$order['endloss'] = $v['endloss'];
$orderid = $this->trends($youjia,$order);
//F('ww'.time(),$orderid);
//盈亏百分百
$ykbfb = round($orderid['ykbfb']*100,2);
$bdyy = $orderid['bdyy']; //本单盈余
$jiancj = $orderid['jc']; //建仓金额
$ykzj = $orderid['ykzj']; //盈亏资金$user = M('accountinfo')->where('uid='.$v['uid'])->find();
//达到客户设置止盈或止亏的百分比时强制平仓
if($time <= time()){
//平仓
$msg = $this->closeTrade($v['uid'],$user['balance'],$youjia,$v['oid'],$bdyy,$jiancj,$ykzj,$product['uprice']);
}
if($v['ostyle'] == 0){//涨
if($ykzj > 0){//盈
if($v['endprofit'] == 1){}else{
if(abs($ykbfb) >= $v['endprofit']){
//平仓
$msg = $this->closeTrade($v['uid'],$user['balance'],$youjia,$v['oid'],$bdyy,$jiancj,$ykzj,$product['uprice']);
}
}
}else{
if(abs($ykbfb) >= $v['endloss']){
//平仓
$msg = $this->closeTrade($v['uid'],$user['balance'],$youjia,$v['oid'],$bdyy,$jiancj,$ykzj,$product['uprice']);
}
}
}else{ //跌
if($ykzj > 0){ //赚
if($v['endprofit'] == 1){}else{
if(abs($ykbfb) >= $v['endprofit']){
//平仓
$msg = $this->closeTrade($v['uid'],$user['balance'],$youjia,$v['oid'],$bdyy,$jiancj,$ykzj,$product['uprice']);
}
}}else{
if(abs($ykbfb) >= $v['endloss']){
//平仓
$msg = $this->closeTrade($v['uid'],$user['balance'],$youjia,$v['oid'],$bdyy,$jiancj,$ykzj,$product['uprice']);
}}
}}
}
if($time <= time()){
F('qiangp',time());
$this->ajaxReturn($msg);
}
}
以上代码仅供交流,不足之处请多指正,如果有更佳方法或者微盘交易系统的疑惑难题,可以互相 交流。