- 策略广场
- 多品种对冲
多品种对冲
Author:
量价时空, Date: 2020-07-24 11:54:42
Tags:
void main() {
Sleep(10000);
Log(exchange.GetAccount());
auto t1=0;
auto t2=0;
auto x1=0;
auto x2=0;
while (1) {
int chi1=0;
int chi2=0;
int pl1=0;
int pl2=0;
auto m1=0;
auto m2=0;
exchange.SetContractType(Symbol11);
auto ticker1 = exchange.GetTicker();
exchange.SetContractType(Symbol12);
auto ticker2 = exchange.GetTicker();
exchange.SetContractType(Symbol21);
auto ticker3 = exchange.GetTicker();
exchange.SetContractType(Symbol22);
auto ticker4 = exchange.GetTicker();
if (exchange.IO("status") == 1 && ticker1.Last > 500 && ticker2.Last >500 && ticker3.Last >500 && ticker4.Last >500) {
Sleep(1100);
//判断持仓方向和位置
auto time1 = UnixNano();
auto position = exchange.GetPosition();
if(position.size() > 0){
for( int u=0; u < (int)position.size(); u++ ){
if( position[position.size()-u].ContractType == Symbol11){
chi1=1;
pl1=position.size()-u;
}
if( position[position.size()-u].ContractType == Symbol21){
chi2=1;
pl2=position.size()-u;
}
}
}else if( !(position.size() > 0)){
chi1=-1;
chi2=-1;
}
if(chi1 != 1){ chi1=-1;}
if(chi2 != 1){ chi2=-1;}
exchange.SetContractType(Symbol11);
auto ticker11 = exchange.GetTicker();
exchange.SetContractType(Symbol12);
auto ticker21 = exchange.GetTicker();
int y1=(int)(ticker11.Last -ticker21.Last);
int w1=((int)(y1-k1))/(int)v1;
int a11=(int)(ticker11.Buy -ticker21.Sell-k1)%(int)v1;
int b11=(int)(ticker11.Buy -ticker21.Sell-k1)/(int)v1;
int a21=(int)(ticker11.Sell -ticker21.Buy-k1)%(int)v1;
int b21=(int)(ticker11.Sell -ticker21.Buy-k1)/(int)v1;
if(b11<0){b11=-b11;}
if(b21<0){b21=-b21;}
if(y1-k1 == 0){t1=0;}
if(y1-k1 < 0){w1=-w1;
t1=-1;
}else if(y1-k1 >0){ t1=1;}
if(chi1 == 1){ //1
m1=position[pl1].Amount;
}else{
if(chi1 == -1){
if (t1>0 && b11 >= 1 ) {
exchange.SetContractType(Symbol11);
exchange.SetDirection("sell");
exchange.Sell(ticker11.Last - 10, 1);
exchange.SetContractType(Symbol12);
exchange.SetDirection("buy");
exchange.Buy(ticker21.Last + 10, 1);
continue;
} else if (t1<0 && b21 >= 1 ) {
exchange.SetContractType(Symbol11);
exchange.SetDirection("buy");
exchange.Buy(ticker11.Last + 10, 1);
exchange.SetContractType(Symbol12);
exchange.SetDirection("sell");
exchange.Sell(ticker21.Last - 10, 1);
continue;
}}}
if(chi1 == 1){
if(position[pl1].ContractType == Symbol11 ){
if(position[pl1].Type == 0 || position[pl1].Type == 2){
x1=1;
}else if(position[pl1].Type == 1 || position[pl1].Type == 3){
x1=-1;
}
}}
if(chi1 == 1){
if(( ticker11.Sell -ticker21.Buy-k1) <= 0 && x1 < 0){
if(position[pl1].Type < 2){
exchange.SetContractType(Symbol11);
exchange.SetDirection("closesell_today");
auto id1=exchange.Buy(ticker11.Last + 10, 1);
exchange.SetContractType(Symbol12);
exchange.SetDirection("closebuy_today");
auto id2=exchange.Sell(ticker21.Last - 10, 1);
continue;
}
if(position[pl1].Type > 1){
exchange.SetContractType(Symbol11);
exchange.SetDirection("closesell");
exchange.Buy(ticker11.Last + 10, 1);
exchange.SetContractType(Symbol12);
exchange.SetDirection("closebuy");
exchange.Sell(ticker21.Last - 10, 1);
continue;
}
}else if(( ticker11.Buy -ticker21.Sell-k1) >= 0 && x1 > 0){
if(position[pl1].Type < 2){
exchange.SetContractType(Symbol12);
exchange.SetDirection("closesell_today");
auto id1=exchange.Buy(ticker21.Last + 10, 1);
exchange.SetContractType(Symbol11);
exchange.SetDirection("closebuy_today");
auto id2=exchange.Sell(ticker11.Last - 10, 1);
continue;
}
if(position[pl1].Type > 1){
exchange.SetContractType(Symbol12);
exchange.SetDirection("closesell");
exchange.Buy(ticker21.Last + 10, 1);
exchange.SetContractType(Symbol11);
exchange.SetDirection("closebuy");
exchange.Sell(ticker11.Last - 10, 1);
continue;
}
}
}
//开平仓1
if (m1>w1 && chi1 == 1 && m1 > 1) { //1
if( x1 > 0 && t1 < 0 && ((a11==0 && m1 > b11) || m1 > b11+1) ){
if(position[pl1].Type < 2 ){
exchange.SetContractType(Symbol12);
exchange.SetDirection("closesell_today");
auto id1=exchange.Buy(ticker21.Last + 10, 1);
exchange.SetContractType(Symbol11);
exchange.SetDirection("closebuy_today");
auto id2=exchange.Sell(ticker11.Last - 10, 1);
continue;
}
if(position[pl1].Type > 1){
exchange.SetContractType(Symbol12);
exchange.SetDirection("closesell");
exchange.Buy(ticker21.Last + 10, 1);
exchange.SetContractType(Symbol11);
exchange.SetDirection("closebuy");
exchange.Sell(ticker11.Last - 10, 1);
continue;
}
}
if( x1 < 0 && t1 > 0 && ((a21==0 && m1 > b21) || m1 > b21+1) ){
if(position[pl1].Type < 2){
exchange.SetContractType(Symbol11);
exchange.SetDirection("closesell_today");
auto id1=exchange.Buy(ticker11.Last + 10, 1);
exchange.SetContractType(Symbol12);
exchange.SetDirection("closebuy_today");
auto id2=exchange.Sell(ticker21.Last - 10, 1);
continue;
}
if(position[pl1].Type > 1){
exchange.SetContractType(Symbol11);
exchange.SetDirection("closesell");
exchange.Buy(ticker11.Last + 10, 1);
exchange.SetContractType(Symbol12);
exchange.SetDirection("closebuy");
exchange.Sell(ticker21.Last - 10, 1);
continue;
}
}
} else if( w1>m1 && chi1 == 1 ) {
exchange.SetContractType(Symbol11);
auto ticker11 = exchange.GetTicker();
exchange.SetContractType(Symbol12);
auto ticker21 = exchange.GetTicker();
int b11=(int)(ticker11.Buy -ticker21.Sell-k1)/(int)v1;
int b21=(int)(ticker11.Sell -ticker21.Buy-k1)/(int)v1;
if(b11<0){b11=-b11;}
if(b21<0){b21=-b21;}
if (t1>0 && b11 > m1) {
exchange.SetContractType(Symbol11);
exchange.SetDirection("sell");
exchange.Sell(ticker11.Last - 10, 1);
exchange.SetContractType(Symbol12);
exchange.SetDirection("buy");
exchange.Buy(ticker21.Last + 10, 1);
continue;
} else if (t1<0 && b21 >m1) {
exchange.SetContractType(Symbol11);
exchange.SetDirection("buy");
exchange.Buy(ticker11.Last + 10, 1);
exchange.SetContractType(Symbol12);
exchange.SetDirection("sell");
exchange.Sell(ticker21.Last - 10, 1);
continue;
}
}
auto time2 = UnixNano();
auto time3 = UnixNano();
exchange.SetContractType(Symbol21);
auto ticker12 = exchange.GetTicker();
exchange.SetContractType(Symbol22);
auto ticker22 = exchange.GetTicker();
int y2=(int)(ticker12.Last -ticker22.Last);
int w2=((int)(y2-k2))/(int)v2;
int a12=(int)(ticker12.Buy -ticker22.Sell-k2)%(int)v2;
int b12=(int)(ticker12.Buy -ticker22.Sell-k2)/(int)v2;
int a22=(int)(ticker12.Sell -ticker22.Buy-k2)%(int)v2;
int b22=(int)(ticker12.Sell -ticker22.Buy-k2)/(int)v2;
if(b12<0){b12=-b12;}
if(b22<0){b22=-b22;}
if(y2-k2 == 0){t2=0;}
if(y2-k2 < 0){w2=-w2;
t2=-1;
}else if(y2-k2 >0){ t2=1;}
if(chi2 == 1){ //2
m2=position[pl2].Amount;
}else{
if(chi2 == -1){
if (t2>0 && b12 >= 1 ) {
exchange.SetContractType(Symbol21);
exchange.SetDirection("sell");
exchange.Sell(ticker12.Last - 10, 1);
exchange.SetContractType(Symbol22);
exchange.SetDirection("buy");
exchange.Buy(ticker22.Last + 10, 1);
continue;
} else if (t2<0 && b22 >= 1 ) {
exchange.SetContractType(Symbol21);
exchange.SetDirection("buy");
exchange.Buy(ticker12.Last + 10, 1);
exchange.SetContractType(Symbol22);
exchange.SetDirection("sell");
exchange.Sell(ticker22.Last - 10, 1);
continue;
}}}
if(chi2 == 1){
if(position[pl2].ContractType == Symbol21 ){
if(position[pl2].Type == 0 || position[pl2].Type == 2){
x2=1;
}else if(position[pl2].Type == 1 || position[pl2].Type == 3){
x2=-1;
}
}}
if(chi2 == 1){
if((ticker12.Sell -ticker22.Buy-k2) <= 0 && x2 < 0){
if(position[pl2].Type < 2){
exchange.SetContractType(Symbol21);
exchange.SetDirection("closesell_today");
auto id1=exchange.Buy(ticker12.Last + 10, 1);
exchange.SetContractType(Symbol22);
exchange.SetDirection("closebuy_today");
auto id2=exchange.Sell(ticker22.Last - 10, 1);
continue;
}
if(position[pl2].Type > 1){
exchange.SetContractType(Symbol21);
exchange.SetDirection("closesell");
exchange.Buy(ticker12.Last + 10, 1);
exchange.SetContractType(Symbol22);
exchange.SetDirection("closebuy");
exchange.Sell(ticker22.Last - 10, 1);
continue;
}
}else if((ticker12.Buy -ticker22.Sell-k2) >= 0 && x2 > 0){
if(position[pl2].Type < 2){
exchange.SetContractType(Symbol22);
exchange.SetDirection("closesell_today");
auto id1=exchange.Buy(ticker22.Last+ 10, 1);
exchange.SetContractType(Symbol21);
exchange.SetDirection("closebuy_today");
auto id2=exchange.Sell(ticker12.Last - 10, 1);
continue;
}
if(position[pl2].Type > 1){
exchange.SetContractType(Symbol22);
exchange.SetDirection("closesell");
exchange.Buy(ticker22.Last + 10, 1);
exchange.SetContractType(Symbol21);
exchange.SetDirection("closebuy");
exchange.Sell(ticker12.Last - 10, 1);
continue;
}
}
}
//开平仓2
if (m2>w2 && chi2 == 1 && m2 > 1) { //2
if( x2 > 0 && t2 < 0 && ((a12==0 && m1 > b12) || m2 > b12+1) ){
if(position[pl2].Type < 2){
exchange.SetContractType(Symbol22);
exchange.SetDirection("closesell_today");
auto id1=exchange.Buy(ticker22.Last+ 10, 1);
exchange.SetContractType(Symbol21);
exchange.SetDirection("closebuy_today");
auto id2=exchange.Sell(ticker12.Last - 10, 1);
continue;
}
if(position[pl2].Type > 1){
exchange.SetContractType(Symbol22);
exchange.SetDirection("closesell");
exchange.Buy(ticker22.Last + 10, 1);
exchange.SetContractType(Symbol21);
exchange.SetDirection("closebuy");
exchange.Sell(ticker12.Last - 10, 1);
continue;
}
}
if( x2 < 0 && t2 > 0 && ((a22==0 && m2 > b22) || m2 > b22+1) ){
if(position[pl2].Type <2){
exchange.SetContractType(Symbol21);
exchange.SetDirection("closesell_today");
auto id1=exchange.Buy(ticker12.Last+ 10, 1);
exchange.SetContractType(Symbol22);
exchange.SetDirection("closebuy_today");
auto id2=exchange.Sell(ticker22.Last - 10, 1);
continue;
}
if(position[pl2].Type > 1){
exchange.SetContractType(Symbol21);
exchange.SetDirection("closesell");
exchange.Buy(ticker12.Last + 10, 1);
exchange.SetContractType(Symbol22);
exchange.SetDirection("closebuy");
exchange.Sell(ticker22.Last - 10, 1);
continue;
}
}
} else if( w2>m2 && chi2 == 1 ) {
exchange.SetContractType(Symbol21);
auto ticker12 = exchange.GetTicker();
exchange.SetContractType(Symbol22);
auto ticker22 = exchange.GetTicker();
int b12=(int)(ticker12.Buy -ticker22.Sell-k2)/(int)v2;
int b22=(int)(ticker12.Sell -ticker22.Buy-k2)/(int)v2;
if(b12<0){b12=-b12;}
if(b22<0){b22=-b22;}
if (t2>0 && b12 > m2) {
exchange.SetContractType(Symbol21);
exchange.SetDirection("sell");
exchange.Sell(ticker12.Last - 10, 1);
exchange.SetContractType(Symbol22);
exchange.SetDirection("buy");
exchange.Buy(ticker22.Last + 10, 1);
continue;
} else if (t2<0 && b22 >m2) {
exchange.SetContractType(Symbol21);
exchange.SetDirection("buy");
exchange.Buy(ticker12.Last + 10, 1);
exchange.SetContractType(Symbol22);
exchange.SetDirection("sell");
exchange.Sell(ticker22.Last - 10, 1);
continue;
}
}
auto time4 = UnixNano();
Log("第一段时间: " ,(time2-time1)/1000000,"/第二段时间:",(time4-time3)/1000000);
Log("chi1=",chi1,"/ chi2=",chi2," /m1=",m1,"/ m2=",m2 ,"/ w1=",w1," /w2=",w2," /t1=",t1," /t2=",t2," /a11=" ,a11," /a21=",a21," /b12=",b12,"/ b22=",b22 );
if(chi1 == 1){
Log("position[pl1].Type=",position[pl1].Type,"/position[pl1].ContractType= " ,position[pl1].ContractType);
}
if(chi2 == 1){
Log("position[pl2].Type=",position[pl2].Type,"/position[pl2].ContractType= " ,position[pl2].ContractType);
}
// Log(m1);
} else {
LogStatus(_D(), "未连接CTP !");
Sleep(1000);
}
}
}
更多内容