public static TrdCommon.TrdHeader MakeTrdHeader(TrdCommon.TrdEnv trdEnv, ulong accID, TrdCommon.TrdMarket trdMarket) { TrdCommon.TrdHeader header = TrdCommon.TrdHeader.CreateBuilder() .SetTrdEnv((int)trdEnv) .SetAccID(accID) .SetTrdMarket((int)trdMarket) .Build(); return(header); }
void Buy(QotCommon.Security sec, ulong trdAcc, TrdCommon.TrdMarket trdMarket, TrdCommon.TrdEnv trdEnv) { TrdCommon.TrdSecMarket secMarket = TrdCommon.TrdSecMarket.TrdSecMarket_Unknown; if (sec.Market == (int)QotCommon.QotMarket.QotMarket_HK_Security) { secMarket = TrdCommon.TrdSecMarket.TrdSecMarket_HK; } else { secMarket = TrdCommon.TrdSecMarket.TrdSecMarket_US; } TrdGetFunds.Response getFundsRsp = GetFundsSync(trdAcc, trdMarket, trdEnv, false, TrdCommon.Currency.Currency_Unknown); if (getFundsRsp.RetType != (int)Common.RetType.RetType_Succeed) { Console.Error.Write("getFundsSync err; retType={0} msg={1}\n", getFundsRsp.RetType, getFundsRsp.RetMsg); return; } QotGetSecuritySnapshot.Response snapshotRsp = GetSecuritySnapshotSync(new List <QotCommon.Security>() { sec }); if (snapshotRsp.RetType != (int)Common.RetType.RetType_Succeed) { Console.Error.Write("getSecuritySnapshotSync err; retType={0} msg={1}\n", snapshotRsp.RetType, snapshotRsp.RetMsg); return; } int lotSize = snapshotRsp.S2C.SnapshotListList[0].Basic.LotSize; double curPrice = snapshotRsp.S2C.SnapshotListList[0].Basic.CurPrice; double cash = getFundsRsp.S2C.Funds.Cash; int qty = (int)Math.Floor(cash / curPrice); qty = qty / lotSize * lotSize; TrdCommon.TrdHeader trdHeader = MakeTrdHeader(trdEnv, trdAcc, trdMarket); TrdPlaceOrder.C2S c2s = TrdPlaceOrder.C2S.CreateBuilder() .SetPacketID(trd.NextPacketID()) .SetHeader(trdHeader) .SetTrdSide((int)TrdCommon.TrdSide.TrdSide_Buy) .SetOrderType((int)TrdCommon.OrderType.OrderType_Normal) .SetCode(sec.Code) .SetQty(qty) .SetPrice(curPrice) .SetAdjustPrice(true) .SetSecMarket((int)secMarket) .Build(); TrdPlaceOrder.Response placeOrderRsp = PlaceOrderSync(c2s); Console.WriteLine(placeOrderRsp); }
void Sell(QotCommon.Security sec, TrdCommon.Position pstn, ulong trdAcc, TrdCommon.TrdMarket trdMarket, TrdCommon.TrdEnv trdEnv) { QotGetSecuritySnapshot.Response snapshotRsp = GetSecuritySnapshotSync(new List <QotCommon.Security>() { sec }); if (snapshotRsp.RetType != (int)Common.RetType.RetType_Succeed) { Console.Error.Write("getSecuritySnapshotSync err; retType={0} msg={1}\n", snapshotRsp.RetType, snapshotRsp.RetMsg); return; } double price = snapshotRsp.S2C.SnapshotListList[0].Basic.CurPrice; TrdCommon.TrdSecMarket secMarket = TrdCommon.TrdSecMarket.TrdSecMarket_Unknown; if (sec.Market == (int)QotCommon.QotMarket.QotMarket_HK_Security) { secMarket = TrdCommon.TrdSecMarket.TrdSecMarket_HK; } else { secMarket = TrdCommon.TrdSecMarket.TrdSecMarket_US; } TrdCommon.TrdHeader trdHeader = MakeTrdHeader(trdEnv, trdAcc, trdMarket); TrdPlaceOrder.C2S c2s = TrdPlaceOrder.C2S.CreateBuilder() .SetPacketID(trd.NextPacketID()) .SetHeader(trdHeader) .SetTrdSide((int)TrdCommon.TrdSide.TrdSide_Sell) .SetOrderType((int)TrdCommon.OrderType.OrderType_Normal) .SetCode(sec.Code) .SetQty(pstn.CanSellQty) .SetPrice(price) .SetAdjustPrice(true) .SetSecMarket((int)secMarket) .Build(); TrdPlaceOrder.Response placeOrderRsp = PlaceOrderSync(c2s); Console.WriteLine(placeOrderRsp); }
void Run(ulong trdAcc, String unlockTrdPwdMD5, TrdCommon.TrdMarket trdMarket, TrdCommon.TrdEnv trdEnv, QotCommon.Security sec) { if (sec.Market != (int)QotCommon.QotMarket.QotMarket_HK_Security && sec.Market != (int)QotCommon.QotMarket.QotMarket_US_Security) { Console.Error.WriteLine("unsupported stock market"); return; } bool ret = InitConnectTrdSync(Config.OpendIP, Config.OpendPort); if (!ret) { Console.Error.WriteLine("fail to connect trd"); return; } ret = InitConnectQotSync(Config.OpendIP, Config.OpendPort); if (!ret) { Console.Error.WriteLine("fail to connect qot"); return; } TrdUnlockTrade.Response unlockTrdRsp = UnlockTradeSync(unlockTrdPwdMD5, true); if (unlockTrdRsp.RetType != (int)Common.RetType.RetType_Succeed) { Console.Error.Write("fail to unlock trade; retType={0} msg={1}\n", unlockTrdRsp.RetType, unlockTrdRsp.RetMsg); return; } DateTime now = DateTime.Now; DateTime startDate = now.Subtract(new TimeSpan(100, 0, 0, 0)); QotRequestHistoryKL.Response historyKLRsp = RequestHistoryKLSync(sec, QotCommon.KLType.KLType_Day, QotCommon.RehabType.RehabType_Forward, startDate.ToString("yyyy-MM-dd"), now.ToString("yyyy-MM-dd"), 1000, null, new byte[] {}, false); List <double> klCloseList = new List <double>(); List <double> difList = new List <double>(); List <double> deaList = new List <double>(); List <double> macdList = new List <double>(); foreach (QotCommon.KLine kl in historyKLRsp.S2C.KlListList) { klCloseList.Add(kl.ClosePrice); } MACDUtil.CalcMACD(klCloseList, 12, 26, 9, difList, deaList, macdList); int difCount = difList.Count; int deaCount = deaList.Count; if (difCount > 0 && deaCount > 0) { if (difList[difCount - 1] < deaList[deaCount - 1] && difList[difCount - 2] > deaList[deaCount - 2]) { TrdCommon.TrdFilterConditions filterConditions = TrdCommon.TrdFilterConditions.CreateBuilder() .AddCodeList(sec.Code) .Build(); TrdGetPositionList.Response getPositionListRsp = GetPositionListSync(trdAcc, trdMarket, trdEnv, filterConditions, null, null, false); if (getPositionListRsp.RetType != (int)Common.RetType.RetType_Succeed) { Console.Error.Write("getPositionListSync err; retType={0} msg={1}\n", getPositionListRsp.RetType, getPositionListRsp.RetMsg); return; } foreach (TrdCommon.Position pstn in getPositionListRsp.S2C.PositionListList) { if (pstn.CanSellQty > 0) { Sell(sec, pstn, trdAcc, trdMarket, trdEnv); } } } else if (difList[difCount - 1] > deaList[deaCount - 1] && difList[difCount - 2] < deaList[deaCount - 2]) { Buy(sec, trdAcc, trdMarket, trdEnv); } } }
public TrdGetPositionList.Response GetPositionListSync(ulong accID, TrdCommon.TrdMarket trdMarket, TrdCommon.TrdEnv trdEnv, TrdCommon.TrdFilterConditions filterConditions, double?filterPLRatioMin, double?filterPLRatioMax, bool isRefreshCache) { ReqInfo reqInfo = null; Object syncEvent = new Object(); lock (syncEvent) { lock (trdLock) { if (trdConnStatus != ConnStatus.READY) { return(null); } TrdCommon.TrdHeader trdHeader = MakeTrdHeader(trdEnv, accID, trdMarket); TrdGetPositionList.C2S.Builder c2s = TrdGetPositionList.C2S.CreateBuilder() .SetHeader(trdHeader); if (filterConditions != null) { c2s.SetFilterConditions(filterConditions); } if (filterPLRatioMin.HasValue) { c2s.SetFilterPLRatioMin(filterPLRatioMin.Value); } if (filterPLRatioMax.HasValue) { c2s.SetFilterPLRatioMax(filterPLRatioMax.Value); } c2s.SetRefreshCache(isRefreshCache); TrdGetPositionList.Request req = TrdGetPositionList.Request.CreateBuilder().SetC2S(c2s).Build(); uint sn = trd.GetPositionList(req); if (sn == 0) { return(null); } reqInfo = new ReqInfo(ProtoID.TrdGetPositionList, syncEvent); trdReqInfoMap.Add(sn, reqInfo); } Monitor.Wait(syncEvent); return((TrdGetPositionList.Response)reqInfo.Rsp); } }
public TrdGetHistoryOrderFillList.Response GetHistoryOrderFillListSync(ulong accID, TrdCommon.TrdMarket trdMarket, TrdCommon.TrdEnv trdEnv, TrdCommon.TrdFilterConditions filterConditions) { ReqInfo reqInfo = null; Object syncEvent = new Object(); lock (syncEvent) { lock (trdLock) { if (trdConnStatus != ConnStatus.READY) { return(null); } TrdCommon.TrdHeader trdHeader = MakeTrdHeader(trdEnv, accID, trdMarket); TrdGetHistoryOrderFillList.C2S.Builder c2s = TrdGetHistoryOrderFillList.C2S.CreateBuilder() .SetHeader(trdHeader); if (filterConditions != null) { c2s.SetFilterConditions(filterConditions); } TrdGetHistoryOrderFillList.Request req = TrdGetHistoryOrderFillList.Request.CreateBuilder().SetC2S(c2s).Build(); uint sn = trd.GetHistoryOrderFillList(req); if (sn == 0) { return(null); } reqInfo = new ReqInfo(ProtoID.TrdGetHistoryOrderFillList, syncEvent); trdReqInfoMap.Add(sn, reqInfo); } Monitor.Wait(syncEvent); return((TrdGetHistoryOrderFillList.Response)reqInfo.Rsp); } }
public TrdGetOrderList.Response GetOrderListSync(ulong accID, TrdCommon.TrdMarket trdMarket, TrdCommon.TrdEnv trdEnv, bool isRefreshCache, TrdCommon.TrdFilterConditions filterConditions, List <TrdCommon.OrderStatus> filterStatusList) { ReqInfo reqInfo = null; Object syncEvent = new Object(); lock (syncEvent) { lock (trdLock) { if (trdConnStatus != ConnStatus.READY) { return(null); } TrdCommon.TrdHeader trdHeader = MakeTrdHeader(trdEnv, accID, trdMarket); TrdGetOrderList.C2S.Builder c2s = TrdGetOrderList.C2S.CreateBuilder() .SetHeader(trdHeader) .SetRefreshCache(isRefreshCache); if (filterConditions != null) { c2s.SetFilterConditions(filterConditions); } if (filterStatusList.Count > 0) { foreach (TrdCommon.OrderStatus status in filterStatusList) { c2s.AddFilterStatusList((int)status); } } TrdGetOrderList.Request req = TrdGetOrderList.Request.CreateBuilder().SetC2S(c2s).Build(); uint sn = trd.GetOrderList(req); if (sn == 0) { return(null); } reqInfo = new ReqInfo(ProtoID.TrdGetOrderList, syncEvent); trdReqInfoMap.Add(sn, reqInfo); } Monitor.Wait(syncEvent); return((TrdGetOrderList.Response)reqInfo.Rsp); } }
public TrdGetFunds.Response GetFundsSync(ulong accID, TrdCommon.TrdMarket trdMarket, TrdCommon.TrdEnv trdEnv, bool isRefreshCache, TrdCommon.Currency currency) { ReqInfo reqInfo = null; Object syncEvent = new Object(); lock (syncEvent) { lock (trdLock) { if (trdConnStatus != ConnStatus.READY) { return(null); } TrdCommon.TrdHeader trdHeader = MakeTrdHeader(trdEnv, accID, trdMarket); TrdGetFunds.C2S c2s = TrdGetFunds.C2S.CreateBuilder() .SetHeader(trdHeader) .SetCurrency((int)currency) .SetRefreshCache(isRefreshCache) .Build(); TrdGetFunds.Request req = TrdGetFunds.Request.CreateBuilder().SetC2S(c2s).Build(); uint sn = trd.GetFunds(req); if (sn == 0) { return(null); } reqInfo = new ReqInfo(ProtoID.TrdGetFunds, syncEvent); trdReqInfoMap.Add(sn, reqInfo); } Monitor.Wait(syncEvent); return((TrdGetFunds.Response)reqInfo.Rsp); } }
void smartSell(String code, QotCommon.QotMarket qotMarket, TrdCommon.TrdSecMarket secMarket, int volume, TrdCommon.OrderType orderType, TrdCommon.TrdEnv trdEnv, ulong accID, TrdCommon.TrdMarket trdMarket, String trdPwdMD5) { int lotSize = 0; QotCommon.Security sec = MakeSec(qotMarket, code); while (true) { Thread.Sleep(1000); if (lotSize == 0) { List <QotCommon.Security> secList = new List <QotCommon.Security>(); secList.Add(sec); QotGetSecuritySnapshot.Response rsp = GetSecuritySnapshotSync(secList); if (rsp.RetType != (int)Common.RetType.RetType_Succeed) { Console.Error.Write("getSecuritySnapshotSync err; retType={} msg={1}\n", rsp.RetType, rsp.RetMsg); return; } lotSize = rsp.S2C.SnapshotListList[0].Basic.LotSize; if (lotSize <= 0) { Console.Error.Write("invalid lot size; code={0} lotSize={1}\n", code, lotSize); return; } } int qty = (volume / lotSize) * lotSize; // 将数量调整为整手的股数 QotSub.Response subRsp = SubSync(new List <QotCommon.Security>() { sec }, new List <QotCommon.SubType>() { QotCommon.SubType.SubType_OrderBook }, true, false); if (subRsp.RetType != (int)Common.RetType.RetType_Succeed) { Console.Error.Write("subSync er; retType={0}; msg={1}\n", subRsp.RetType, subRsp.RetMsg); return; } QotGetOrderBook.Response getOrderBookRsp = GetOrderBookSync(MakeSec(qotMarket, code), 1); if (getOrderBookRsp.RetType != (int)Common.RetType.RetType_Succeed) { Console.Error.Write("getOrderBookSync er; retType={0}; msg={1}\n", subRsp.RetType, subRsp.RetMsg); return; } double bid1Price = getOrderBookRsp.S2C.OrderBookBidListList[0].Price; TrdCommon.TrdHeader trdHeader = MakeTrdHeader(trdEnv, accID, trdMarket); TrdPlaceOrder.C2S c2s = TrdPlaceOrder.C2S.CreateBuilder() .SetHeader(trdHeader) .SetPacketID(trd.NextPacketID()) .SetTrdSide((int)TrdCommon.TrdSide.TrdSide_Sell) .SetOrderType((int)orderType) .SetCode(code) .SetQty(qty) .SetPrice(bid1Price) .SetAdjustPrice(true) .SetSecMarket((int)secMarket) .Build(); TrdPlaceOrder.Response placeOrderRsp = PlaceOrderSync(c2s); if (placeOrderRsp.RetType != (int)Common.RetType.RetType_Succeed) { Console.Error.Write("placeOrderSync err; retType={0} msg={1}\n", placeOrderRsp.RetType, placeOrderRsp.RetMsg); return; } else { Console.Write("下单成功: {0}\n", placeOrderRsp.S2C); } } }
void simpleSell(String code, QotCommon.QotMarket qotMarket, TrdCommon.TrdSecMarket secMarket, double price, int volume, TrdCommon.OrderType orderType, TrdCommon.TrdEnv trdEnv, ulong accID, TrdCommon.TrdMarket trdMarket, String trdPwdMD5) { bool ret = InitConnectQotSync(Config.OpendIP, Config.OpendPort); if (!ret) { Console.Error.WriteLine("Fail to connect opend"); return; } ret = InitConnectTrdSync(Config.OpendIP, Config.OpendPort); if (!ret) { Console.Error.WriteLine("Fail to connect opend"); return; } int lotSize = 0; QotCommon.Security sec = MakeSec(qotMarket, code); while (true) { Thread.Sleep(1000); if (lotSize == 0) { List <QotCommon.Security> secList = new List <QotCommon.Security>(); secList.Add(sec); QotGetSecuritySnapshot.Response rsp = GetSecuritySnapshotSync(secList); if (rsp.RetType != (int)Common.RetType.RetType_Succeed) { Console.Error.Write("getSecuritySnapshotSync err; retType={0} msg={1}\n", rsp.RetType, rsp.RetMsg); return; } lotSize = rsp.S2C.SnapshotListList[0].Basic.LotSize; if (lotSize <= 0) { Console.Error.Write("invalid lot size; code={0} lotSize={1}\n", code, lotSize); return; } } int qty = (volume / lotSize) * lotSize; // 将数量调整为整手的股数 TrdCommon.TrdHeader trdHeader = MakeTrdHeader(trdEnv, accID, trdMarket); TrdPlaceOrder.C2S c2s = TrdPlaceOrder.C2S.CreateBuilder() .SetHeader(trdHeader) .SetPacketID(trd.NextPacketID()) .SetTrdSide((int)TrdCommon.TrdSide.TrdSide_Sell) .SetOrderType((int)orderType) .SetCode(code) .SetQty(qty) .SetPrice(price) .SetAdjustPrice(true) .SetSecMarket((int)secMarket) .Build(); TrdPlaceOrder.Response placeOrderRsp = PlaceOrderSync(c2s); if (placeOrderRsp.RetType != (int)Common.RetType.RetType_Succeed) { Console.Error.Write("placeOrderSync err; retType={0} msg={1}\n", placeOrderRsp.RetType, placeOrderRsp.RetMsg); return; } else { Console.Write("下单成功: {0}\n", placeOrderRsp.S2C); } } }