- UID
- 2
- 积分
- 2874604
- 威望
- 1387331 布
- 龙e币
- 1487273 刀
- 在线时间
- 13155 小时
- 注册时间
- 2009-12-3
- 最后登录
- 2024-11-24
|
bar事件驱动模式下范例:- void test::OnBarOpen(TKVALUE t)
- {
- if (state != "run")return;
- if (t.InstrumentID != sInst)return;
- mapK[sPeriod][sInst][t.sDate + t.sDayNight + t.sTime] = t;
- ma = avg(sPeriod, sInst, jxzq);
- string s2 = " 合约 " + sInst + " 均线 " + to_string(ma);
- InsertLog(s2);
- //(1)最新价小于均线价格,无持仓进行卖出开仓模块
- if (mapMd[sInst].LastPrice < ma && fx == 0)
- {
- fx = -1;
- ss = dbfx;
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int sl = (int)(ss * it->second);
- OrderInsert(it->first, sInst, '1', '0', sl, mapMd[sInst].AskPrice1 - hd * mapInstrument[sInst].PriceTick, "", "");//卖出开仓语句
- string s = it->first + " 突破入场价格空单达到入场条件卖出开仓 " + to_string(sl) + " 手,价格 " + to_string(mapMd[sInst].AskPrice1 - hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 均线 " + to_string(ma);
- maps[num] = s;
- num++;
- }
- if (num != 0)shuchurizhi();
- tm = 0;
- xieruzhuangtai();
- }
- //(2)最新价大于均线价格,无持仓进行卖入开仓模块
- if (mapMd[sInst].LastPrice > ma && fx == 0)
- {
- fx = 1;
- ss = dbfx;
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int sl = (int)(ss * it->second); //获取根据单笔风险计算的交易数量并赋值给(sl)数量
- OrderInsert(it->first, sInst, '0', '0', sl, mapMd[sInst].BidPrice1 + hd * mapInstrument[sInst].PriceTick, "", "");//买入开仓语句
- //输出至交易界面的log里面
- string s = it->first + " 突破入场价格多单达到入场条件买入开仓 " + to_string(sl) + " 手,价格 " + to_string(mapMd[sInst].BidPrice1 + hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 均线 " + to_string(ma);
- maps[num] = s;
- num++;
- }
- if (num != 0)shuchurizhi();
- tm = 0;
- xieruzhuangtai();
- }
- //(3)最新价小于均线价格,持有多单进行平仓模块
- if (mapMd[sInst].LastPrice < ma && fx == 1)
- {
- fx = 0;
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int sl = (int)(ss * it->second);
- if (yxpc == 0)
- {
- closesell1(it->first, sInst, sl, mapMd[sInst].BidPrice1 - hd * mapInstrument[sInst].PriceTick);//卖出平仓,优先平昨仓
- }
- else if (yxpc == 1)
- {
- closesell2(it->first, sInst, sl, mapMd[sInst].BidPrice1 - hd * mapInstrument[sInst].PriceTick);//卖出平仓,优先平今仓
- }
- string s = it->first + " 多单跌破均线达到出场条件卖出平仓 " + to_string(sl) + " 手,价格 " + to_string(mapMd[sInst].BidPrice1 - hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 均线 " + to_string(ma);
- maps[num] = s;
- num++;
- }
- if (num != 0)shuchurizhi();
- tm = 0;
- xieruzhuangtai();
- }
- //(4)最新价大于均线价格,持有空单进行平仓模块
- if (mapMd[sInst].LastPrice > ma && fx == -1)
- {
- fx = 0;
- map<string, double>::iterator it;
- for (it = mapSub.begin(); it != mapSub.end(); it++)
- {
- int sl = (int)(ss * it->second);
- if (yxpc == 0)
- {
- closebuy1(it->first, sInst, sl, mapMd[sInst].AskPrice1 + hd * mapInstrument[sInst].PriceTick); //买入平仓,优先平昨仓
- }
- else if (yxpc == 1)
- {
- closebuy2(it->first, sInst, sl, mapMd[sInst].AskPrice1 + hd * mapInstrument[sInst].PriceTick); //买入平仓,优先平今仓
- }
- string s = it->first + " 空单涨破均线达到出场条件买入平仓 " + to_string(ss) + " 手,价格 " + to_string(mapMd[sInst].AskPrice1 + hd * mapInstrument[sInst].PriceTick) + " 基础手数 " + to_string(ss) + " 均线 " + to_string(ma);
- maps[num] = s;
- num++;
- }
- if (num != 0)shuchurizhi();
- tm = 0;
- xieruzhuangtai();
- }
- }
复制代码 |
|