void IBClient::updateAccountValue(const IBString &key, const IBString &val, const IBString ¤cy, const IBString &accountName) { receiveData("updateAccountValue", knk(4, kp((S)key.c_str()), kp((S)val.c_str()), ks((S)currency.c_str()), ks((S)accountName.c_str()) )); }
void IBClient::position(const IBString &account, const Contract &contract, int position, double avgCost) { receiveData("position", knk(4, ks((S)account.c_str()), kj(contract.conId), ki(position), kf(avgCost))); }
void IBClient::accountSummary(int reqId, const IBString &account, const IBString &tag, const IBString &value, const IBString &curency) { receiveData("accountSummary", knk(5, ki(reqId), ks((S)account.c_str()), ks((S)tag.c_str()), kp((S)value.c_str()), ks((S)curency.c_str()))); }
//create ,pass and recieve a simple table. int eg7() { K cc,d,e,v,tab; K flip,result,columnNames,columnData; int row,col,nCols,nRows; cc=ktn(KS,2);kS(cc)[0]=ss("pid");kS(cc)[1]=ss("uid"); d=ktn(KS,3);kS(d)[0]=ss("ibm");kS(d)[1]=ss("gte");kS(d)[2]=ss("kvm"); e=ktn(KI,3);kI(e)[0]=1;kI(e)[1]=2;kI(e)[2]=3; v=knk(2,d,e); tab=xT(xD(cc,v)); flip=k(c,"{[x]a:update t:.z.t,y:.z.d from x;.tst.t:a;a}",tab,(K)0); //Turn into a dictionary. flip->k [transpose?] //Display table. [Borrowed from code.kx.com: // https://code.kx.com/trac/attachment/wiki/Cookbook/InterfacingWithC/csv.c ] columnNames=kK(flip->k)[0]; columnData=kK(flip->k)[1]; nCols=columnNames->n; nRows=kK(columnData)[0]->n; for(row=0;row<nRows;row++) { if(0==row) { for(col=0;col<nCols;col++) { if(col>0)printf(","); printf("%s",kS(columnNames)[col]); } printf("\n"); } for(col=0;col<nCols;col++) { K obj=kK(columnData)[col]; if(col>0)printf(","); switch(obj->t) { case(1):{printf("%d",kG(obj)[row]);}break; case(4):{printf("%d",kG(obj)[row]);}break; case(5):{printf("%d",kH(obj)[row]);}break; case(6):{printf("%d",kI(obj)[row]);}break; case(7):{printf("%lld",kJ(obj)[row]);}break; case(8):{printf("%f",kE(obj)[row]);}break; case(9):{printf("%f",kF(obj)[row]);}break; case(11):{printf("%s",kS(obj)[row]);}break; case(19):{printf("%i",kI(obj)[row]);}break; case(14):{printf("%i",kI(obj)[row]);}break; default:{printf("unknown type");}break; } } printf("\n"); } return 1; }
void IBClient::updateMktDepth(TickerId id, int position, int operation, int side, double price, int size) { receiveData("updateMktDepth", knk(6, kj(id), ki(position), ki(operation), ki(side), kf(price), ki(size))); }
void IBClient::updateMktDepthL2(TickerId id, int position, IBString marketMaker, int operation, int side, double price, int size) { receiveData("updateMktDepthL2", knk(7, kj(id), ki(position), ks((S)marketMaker.c_str()), ki(operation), ki(side), kf(price), ki(size))); }
K create_trade_schema() { return knk(7, col("sym",KS), col("exg",KS), col("size",KI), col("volume",KI), col("sequence",KI), col("price",KF), col("cond", 0)); }
K create_quote_schema() { return knk(8, col("sym",KS), col("exg",KS), col("asksize",KI), col("bidsize",KI), col("askprice",KF), col("bidprice",KF), col("sequence", KI), col("cond", 0)); }
K eval(K x,K y,K z){K*k;S*b,s;SQLULEN w;SQLLEN*nb;SQLINTEGER*wb;H*tb,u,t,j=0,p,m;F f;C c[128];I n=xj<0;D d=d1(n?-xj:xj);U(d)x=y;Q(z->t!=-KJ||xt!=-KS&&xt!=KC,"type") if(z->j)SQLSetStmtAttr(d,SQL_ATTR_QUERY_TIMEOUT,(SQLPOINTER)(SQLULEN)z->j,0); if(xt==-KS)Q1(SQLColumns(d,(S)0,0,(S)0,0,xs,S0,(S)0,0))else{I e;K q=kpn(xG,xn);ja(&q,"\0");e=SQLExecDirect(d,q->G0,xn);r0(q);Q1(e)} SQLNumResultCols(d,&j);P(!j,(d0(d),knk(0))) b=malloc(j*SZ),tb=malloc(j*2),wb=malloc(j*SZ),nb=malloc(j*SZ),x=ktn(KS,j),y=ktn(0,j);// sqlserver: no bind past nonbind DO(j,Q1(SQLDescribeCol(d,(H)(i+1),c,128,&u,&t,&w,&p,&m))xS[i]=sn(c,u); if(t>90)t-=82; Q(t<-11||t>12,xS[i])wb[i]=ut[tb[i]=t=t>0?t:12-t]==KS&&w?w+1:wt[t];if(ut[t]==KS&&(n||!wb[i]||wb[i]>9))tb[i]=13) DO(j,kK(y)[i]=ktn(ut[t=tb[i]],0);if(w=wb[i])Q1(SQLBindCol(d,(H)(i+1),ct[t],b[i]=malloc(w),w,nb+i))) for(;SQL_SUCCEEDED(SQLFetch(d));)DO(j,k=kK(y)+i;u=ut[t=tb[i]];s=b[i];n=SQL_NULL_DATA==(int)nb[i]; if(!u)jk(k,n?ktn(ct[t]?KC:KG,0):wb[i]?kp(s):gb(d,(H)(i+1),t)); else ja(k,n?nu(u):u==KH&&wb[i]==1?(t=(H)*s,(S)&t):u==KS?(s=dtb(s,nb[i]),(S)&s):u<KD?s:u==KZ?(f=ds(s)+(vs(s+6)+*(I*)(s+12)/1e9)/8.64e4,(S)&f):(w=u==KD?ds(s):vs(s),(S)&w))) if(!SQLMoreResults(d))O("more\n");DO(j,if(wb[i])free(b[i]))R free(b),free(tb),free(wb),free(nb),d0(d),xT(xD(x,y));}
K fa2q(double *data,long *dims,long d,long size){ int length=dims[0],elesize=size/length,i; K L; if(1==d){ L=ktn(KF,length); for(i=0;i<length;i++)kF(L)[i]=*(data+i); R L; }else if(1<d){ L=knk(0); for(i=0;i<length;i++)jk(&L,fa2q(data+i*elesize,dims+1,d-1,elesize)); R L; } R (K)0; }
static K run_test(void (*execute_test)(void(s1)(void),void(s2)(void)), int testCount) { int i; K result, kffc[3], kpmc[4]; for (i = 0 ; i < PMC_COUNT ; i++) pmc_fixed[i] = 0; for (i = 0 ; i < FFC_COUNT ; i++) ffc_fixed[i] = 0; ioctl(fd, IOCTL_MSR_CMDS, (long long)pmc_reset); execute_baseline(testCount, &start_baseline, &stop_baseline); pmc_fixed[0] = pmc_read[1].value / testCount; pmc_fixed[1] = pmc_read[2].value / testCount; pmc_fixed[2] = pmc_read[3].value / testCount; pmc_fixed[3] = pmc_read[4].value / testCount; ffc_fixed[0] = pmc_read[5].value / testCount; ffc_fixed[1] = pmc_read[6].value / testCount; ffc_fixed[2] = pmc_read[7].value / testCount; for (i = 0 ; i < PMC_COUNT ; i++) kpmc[i] = ktn(KJ, testCount); for (i = 0 ; i < FFC_COUNT ; i++) kffc[i] = ktn(KJ, testCount); for (i = 1 ; i < 1 + PMC_COUNT + FFC_COUNT ; i++) pmc_read[i].value = 0; for (i = 0 ; i < testCount ; i++) { ioctl(fd, IOCTL_MSR_CMDS, (long long)pmc_reset); execute_test(&start_counters, &stop_counters); kJ(kpmc[0])[i] = pmc_read[1].value - pmc_fixed[0]; kJ(kpmc[1])[i] = pmc_read[2].value - pmc_fixed[1]; kJ(kpmc[2])[i] = pmc_read[3].value - pmc_fixed[2]; kJ(kpmc[3])[i] = pmc_read[4].value - pmc_fixed[3]; kJ(kffc[0])[i] = pmc_read[5].value - ffc_fixed[0]; kJ(kffc[1])[i] = pmc_read[6].value - ffc_fixed[1]; kJ(kffc[2])[i] = pmc_read[7].value - ffc_fixed[2]; } result = knk(7, kffc[0], kffc[1], kffc[2], kpmc[0], kpmc[1], kpmc[2], kpmc[3]); return result; }
void IBClient::execDetails(int reqId, const Contract &contract, const Execution &execution) { auto exec = createDictionary(std::map<std::string, K> { { "execId", ks((S)execution.execId.c_str()) }, { "time", kp((S)execution.time.c_str()) }, // TODO: Convert { "acctNumber", ks((S)execution.acctNumber.c_str()) }, { "exchange", ks((S)execution.exchange.c_str()) }, { "side", ks((S)execution.side.c_str()) }, { "shares", ki(execution.shares) }, { "price", kf(execution.price) }, { "permId", ki(execution.permId) }, { "clientId", kj(execution.clientId) }, { "orderId", kj(execution.orderId) }, { "liquidation", ki(execution.liquidation) }, { "cumQty", ki(execution.cumQty) }, { "avgPrice", kf(execution.avgPrice) }, { "evRule", kp((S)execution.evRule.c_str()) }, { "evMultiplier", kf(execution.evMultiplier) } }); receiveData("execDetails", knk(3, ki(reqId), kj(contract.conId), exec)); }
void IBClient::error(const int id, const int errorCode, const IBString errorString) { std::string type; if (1100 <= errorCode && errorCode <= 1300) { type = "system"; } else if (2100 <= errorCode && errorCode <= 2110) { type = "warning"; } else { type = "error"; } receiveData(type.c_str(), knk(3, kj(id), kj(errorCode), kp((S)errorString.c_str()))); // "Connectivity between IB and TWS has been lost" if (id == -1 && errorCode == 1100) { disconnect(); } // Exception caught while reading socket - Connection reset by peer if (id == -1 && errorCode == 509) { disconnect(); } }
void IBClient::tickSize(TickerId tickerId, TickType field, int size) { receiveData("tickSize", knk(3, kj(tickerId), ki(field), ki(size))); }
K threads_async(K x,K y) { async_job *job=threads_create_job(knk(1,r1(x)),r1(y)); threadpool_add(threadpool,threads_thread_callback,(V*)job,0);R(K)0;}
static K threads_q_callback(int fd) { C b;async_job* job;I rd=read(fd,&job,sizeof(async_job*));K deserialized=knk(1,d9(job->job_result)); K retval=dot(job->callback,deserialized);if(write(job->free_pipe[1],&b,1)==-1){}; threads_free_job(job);r0(deserialized);R retval;}
void IBClient::tickGeneric(TickerId tickerId, TickType tickType, double value) { receiveData("tickGeneric", knk(3, kj(tickerId), ki(tickType), kf(value))); }
void IBClient::fundamentalData(TickerId reqId, const IBString& data) { receiveData("fundamentalData", knk(2, kj(reqId), kp((S)data.c_str()))); }
void IBClient::tickPrice(TickerId tickerId, TickType field, double price, int canAutoExecute) { receiveData("tickPrice", knk(4, kj(tickerId), ki(field), kf(price), kb(canAutoExecute))); }
void IBClient::tickString(TickerId tickerId, TickType tickType, const IBString &value) { receiveData("tickString", knk(3, kj(tickerId), ki(tickType), kp((S)value.c_str()))); }