public Signal Run(RawMarketData rmd)
        {
            if (rmd == null)
            {
                return Signal.Stay;
            }

            if (Util.IsValidPrice(rmd.CurPrice))
            {
                _curFuturePrice = rmd.CurPrice;

                if (_curFuturePrice >= _upBarrier)
                {
                    return Signal.Rebalance;
                }
                else if (_curFuturePrice <= _downBarrier)
                {
                    return Signal.Rebalance;
                }
                return Signal.Stay;
            }
            else
            {
                return Signal.Stay;
            }
        }
Example #2
0
        public static RawMarketData GetRawMarketData()
        {
            RawMarketData rmd = new RawMarketData("test_code", Detail.ProductType.Unknown);

            rmd.AskPrice5 = 10;
            rmd.AskPrice4 = 9;
            rmd.AskPrice3 = 8;
            rmd.AskPrice2 = 7;
            rmd.AskPrice1 = 6;

            rmd.BidPrice1 = 5;
            rmd.BidPrice2 = 4;
            rmd.BidPrice3 = 3;
            rmd.BidPrice4 = 2;
            rmd.BidPrice5 = 1;

            rmd.AskCount5 = 10;
            rmd.AskCount4 = 20;
            rmd.AskCount3 = 30;
            rmd.AskCount2 = 40;
            rmd.AskCount1 = 50;

            rmd.BidCount1 = 55;
            rmd.BidCount2 = 45;
            rmd.BidCount3 = 35;
            rmd.BidCount4 = 25;
            rmd.BidCount5 = 15;

            rmd.LastUpdatedTime = DateTime.Now;

            return rmd;
        }
Example #3
0
        void KtbFutureTest()
        {
            String future10yrCode = KtbFutureUtil.Ins().KtbFuture_10yr_1.Code;
            {
                TradingDirection longShort = TradingDirection.Long;
                string code = future10yrCode;
                long reqCount = 10;
                double reqPrice = 105.10;
                Account account = AccountManager.Ins().CreateSimFOAccount();

                IOrderLimit orderLimit = new DefaultOrderLimit(account);
                orderLimit = new LimOrderLimit(orderLimit);

                account.SetOrderLimit(orderLimit);

                RawMarketData rmdClone = new RawMarketData(future10yrCode, Detail.ProductType.KtbFuture);
                rmdClone.BidPrice1 = 105.10;
                rmdClone.AskPrice1 = 105.15;

                POrder target = new POrder(longShort, code, reqCount, reqPrice, account, rmdClone);
                bool success = POrderUtil.RequestOrder(target, null);
                Assert.AreEqual(true, success);

                target.UpdateCancelMessage(target.ReqCount);

                POrder_UnittestUtil.ClearOrder(target);
            }
        }
Example #4
0
 void SetData_Raw(RawMarketData rmd, String type, double value, DateTime dt)
 {
     switch (type)
     {
         case "CurPrice":
             rmd.CurPrice = value;
             break;
         case "AskCount1":
             rmd.AskCount1 = Convert.ToInt64(value);
             break;
         case "BidCount1":
             rmd.BidCount1 = Convert.ToInt64(value);
             break;
         case "AskPrice1":
             rmd.AskPrice1 = value;
             break;
         case "BidPrice1":
             rmd.BidPrice1 = value;
             break;
         case "Basis":
             break;
         case "CurRate":
             break;
         default:
             logger.Warn("Invalid {0}, {1}", type, value);
             break;
     }
     rmd.LastUpdatedTime = dt;
     LastUpdated = dt;
 }
        public RawMarketData Decode_Raw(InPacket iPacket, String targetCode, DateTime curDateTime)
        {
            try
            {
                StringPacket sp = new StringPacket(iPacket.ToString().Substring(24));
                String code = sp.Decode();

                if (code != targetCode)
                {
                    return null;
                }

                RawMarketData rmd = new RawMarketData(targetCode, Detail.ProductType.Unknown);

                String type = sp.Decode();
                double value = Convert.ToDouble(sp.Decode());
                DateTime dt = DateTime.ParseExact(sp.Decode(), "yyyyMMdd HH:mm:ss.fff", null);

                SetData_Raw(rmd, type, value, dt);

                return rmd;
            }
            catch (System.Exception ex)
            {
                logger.Warn(ex.ToString());
            }
            return null;
        }
        // RMD clone을 만드는 등 데이터를 준비하는 역할을 수행한다.
        // build ot, adjust price, adjust count 등의 작업을 진행한다.
        public void BeforeEnterSweeperChance()
        {
            _instanceRmdFut1 = _referenceRmdFut1.Clone() as RawMarketData;

            // adjust count
            POrderBidAskCountManager.Ins().AdjustRmdCountWithoutMine(ref _instanceRmdFut1);
        }
        Boolean IsValidCondition(RawMarketData rmd, RawMarketData rmdFuture)
        {
            if (!Util.IsValidRMDTimeAndPrice(rmdFuture) || !Util.IsValidRMDTimeAndPrice(rmd))
            {
                return false;
            }

            ArrayList arr = _context.TradingOrders;

            foreach (POrder o in arr)
            {
                if (o.LongShort == TradingDirection.Long)
                {
                    if (o.ReqPrice * 1.5 >= rmd.CurPrice)
                    {
                        return false;
                    }
                }
                else
                {
                    if (o.ReqPrice * 0.67 <= rmd.CurPrice)
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        public void TestPOrderLegalData()
        {
            string code = "201EC260";
            RawMarketData rmd = new RawMarketData(code, Detail.ProductType.CallOption);
            rmd.BidPrice1 = 0.81000001F;
            rmd.BidCount1 = 1000;
            rmd.AskPrice1 = 0.81999999F;
            rmd.AskCount1 = 1000;

            Account simAccount = AccountManager.Ins().CreateSimFOAccount();

            POrder o0 = new POrder(TradingDirection.Long, code, 1, 0.81, simAccount, rmd);
            POrderLegalManager.Ins().Add(o0);

            {

                POrder o2 = new POrder(TradingDirection.Long, code, 1, 0.81, simAccount, rmd);
                Boolean success = POrderLegalManager.Ins().Remove(o2);
                Assert.AreEqual(success, false);
            }

            {
                POrder o = new POrder(TradingDirection.Short, code, 1, 0.8099998, simAccount, rmd);
                POrder_Accessor oAccessor = new POrder_Accessor(o);
                oAccessor.ReqPrice = 0.810006;

                Boolean legal = POrderLegalManager.Ins().IsLegalOrder(o, false);
                Assert.AreEqual(legal, false);

                o.SetAsLegalPrice();
                legal = POrderLegalManager.Ins().IsLegalOrder(o, false);
                Assert.AreEqual(legal, true);

                Util.LogOutCriticalError("\t\t # Non exist code search 1 permitted.");
                Boolean success = POrderLegalManager.Ins().Remove(o);
                Assert.AreEqual(success, false);

                POrderLegalManager.Ins().Add(o);

                POrder o2 = new POrder(TradingDirection.Short, code, 1, 0.8099998, simAccount, rmd);
                success = POrderLegalManager.Ins().Remove(o2);
                Assert.AreEqual(success, false);

                // test reverse
                {
                    POrder o3 = new POrder(TradingDirection.Long, code, 1, 0.83, simAccount, rmd);
                    POrder_Accessor oAccessor3 = new POrder_Accessor(o3);
                    oAccessor3.ReqPrice = 0.83;

                    legal = POrderLegalManager.Ins().IsLegalOrder(o3, false);
                    Assert.AreEqual(legal, false);

                    POrderLegalManager.Ins().Remove(o3);
                }
                POrderLegalManager.Ins().Remove(o2);
                POrderLegalManager.Ins().Remove(o);
            }
            POrderLegalManager.Ins().Remove(o0);
        }
Example #9
0
 public static Boolean IsValidCurPrice(RawMarketData rmd)
 {
     if (rmd.CurPrice == double.MaxValue || rmd.CurPrice == double.MinValue || rmd.CurPrice == 0)
     {
         return false;
     }
     return true;
 }
        // 기존에 나가 있는 주문 수량을 고려해준다.
        // 하나의 기회에 대해서 순간적으로 많은 주문을 낼 수 있으므로 이렇게 해주도록 한다.
        public void AdjustRmdCountWithoutMine(ref RawMarketData rmd)
        {
            long myLongReqCount = GetRegisteredReqCount(rmd.Code, TradingDirection.Long, rmd.AskPrice1);
            rmd.AskCount1 = Math.Max(rmd.AskCount1 - myLongReqCount, 0);

            long myShortReqCount = GetRegisteredReqCount(rmd.Code, TradingDirection.Short, rmd.BidPrice1);
            rmd.BidCount1 = Math.Max(rmd.BidCount1 - myShortReqCount, 0);
        }
        public double GetOneUnitPnL(
            Position_KtbSkel position, RawMarketData spotRmdClone, RawMarketData futureRmdClone)
        {
            double spotProfit = spotRmdClone.BidPrice1 - position.SpotAvgPriceTimeAdjust;
            double futureProfit = (position.FutureAvgPrice - futureRmdClone.AskPrice1) * 100;

            double pnl = spotProfit + futureProfit;
            return pnl;
        }
        public MonitorEnter_KtbSkel(RawMarketData spotRmd, RawMarketData futureRmd, IMonitorEnterPolicy policy)
        {
            this._spotRmd = spotRmd;
            this._futureRmd = futureRmd;
            this._policy = policy;

            ValidateInputs();
            MakeInfoLog();
        }
        public void Add(DateTime dt, RawMarketData y, RawMarketData x)
        {
            MarketDataPair pair = new MarketDataPair();
            pair.CurDateTime = dt;
            pair.Y = y;
            pair.X = x;

            _data.Put(pair);
        }
Example #14
0
        public MonitorDatum_KtbSkel GetIndicator(
            RawMarketData spotRmdThisTurn, RawMarketData futureRmdThisTurn)
        {
            try
            {
                double spotMidPrice = (spotRmdThisTurn.AskPrice1 + spotRmdThisTurn.BidPrice1) / 2;
                double futureMidPrice = (futureRmdThisTurn.AskPrice1 + futureRmdThisTurn.BidPrice1) / 2;

                double spotMidRate =
                    BondPriceRateMapManager.Ins().ConvertToRate(spotRmdThisTurn.Code, (int)spotMidPrice);

                double spotBidRate = BondPriceRateMapManager.Ins().ConvertToRate(
                    spotRmdThisTurn.Code, (int)spotRmdThisTurn.BidPrice1);
                double spotAskRate = BondPriceRateMapManager.Ins().ConvertToRate(
                    spotRmdThisTurn.Code, (int)spotRmdThisTurn.AskPrice1);

                double futureMidRate =
                    BondPriceRateMapManager.Ins().ConvertToRate(futureRmdThisTurn.Code, (int)(futureMidPrice * 100));
                double futureBidRate =
                    BondPriceRateMapManager.Ins().ConvertToRate(
                    futureRmdThisTurn.Code, (int)(futureRmdThisTurn.BidPrice1 * 100));
                double futureAskRate =
                    BondPriceRateMapManager.Ins().ConvertToRate(
                    futureRmdThisTurn.Code, (int)(futureRmdThisTurn.AskPrice1 * 100));

                double curSpotRate = spotMidRate;
                double curFutureRate = futureMidRate * (-1);

                String spotLog = String.Format("S {0:n0}({1:n5}) // {2:n0}({3:n5}) // {4:n0}({5:n5})",
                    spotRmdThisTurn.AskPrice1, spotAskRate,
                    spotMidPrice, spotMidRate,
                    spotRmdThisTurn.BidPrice1, spotBidRate);

                String futureLog = String.Format("F {0:n2}({1:n5}) // {2:n2}({3:n5}) // {4:n2}({5:n5})",
                    futureRmdThisTurn.AskPrice1, futureAskRate,
                    futureMidPrice, futureMidRate,
                    futureRmdThisTurn.BidPrice1, futureBidRate);

                double curIndicator = (curSpotRate + curFutureRate) * 10000;
                DateTime lastUpdated = DateTime.Now;

                MonitorDatum_KtbSkel datum = new MonitorDatum_KtbSkel();
                datum.CurIndicator = curIndicator;
                datum.FutureLog = futureLog;
                datum.SpotLog = spotLog;
                datum.LastUpdated = lastUpdated;

                return datum;
            }
            catch (System.Exception ex)
            {
                logger.Error(ex.ToString());
                Util.KillWithNotice(ex.ToString());
            }
            return null;
        }
Example #15
0
 public Dictionary<int, double> GetIfExitPnL(RawMarketData rmd)
 {
     Dictionary<int, double> ret = new Dictionary<int, double>();
     foreach (KeyValuePair<int, ISweeper> sweeper in _liveSweepers)
     {
         double pnl = sweeper.Value.GetIfExitPnL(rmd);
         ret.Add(sweeper.Key, pnl);
     }
     return ret;
 }
 public Boolean IsExitChance(
     Position_KtbSkel position, RawMarketData spotRmdClone, RawMarketData futureRmdClone)
 {
     double pnl = GetOneUnitPnL(position, spotRmdClone, futureRmdClone);
     if (pnl >= kProfitPoint)
     {
         return true;
     }
     return false;
 }
Example #17
0
 public POrder(
     TradingDirection longShort, 
     String code, 
     long reqCount, 
     double reqPrice, 
     Account account,
     RawMarketData rmdClone)
     : this(longShort, code, reqCount, reqPrice, account, rmdClone, MarketType._3_Default, false)
 {
 }
Example #18
0
        public RawMarketData Register(String code, String name, Detail.ProductType dpt)
        {
            Trace.Assert(dpt == Detail.ProductType.KtbSpot);

            logger.Info("{0}({1}), {2} is registered.", code, name, dpt.ToString());

            RawMarketData rmd = new RawMarketData(code, name, dpt);
            _rmdDict.Add(code, rmd);
            return rmd;
        }
        public void RMDJsonTest()
        {
            RawMarketData rmd = new RawMarketData("testCode", Detail.ProductType.KospiFuture);
            RawMarketDataJson rmdJson = new RawMarketDataJson(rmd);

            Serializer serializer = new Serializer(typeof(RawMarketDataJson));
            string jsonText = serializer.Serialize(rmdJson);

            RawMarketDataJson rmdJson2 = (RawMarketDataJson)serializer.Deserialize(jsonText);
        }
Example #20
0
        public void CloneTest()
        {
            RawMarketData origin = new RawMarketData(KospiFutureUtil.Ins().KFI.Code, Detail.ProductType.KospiFuture);
            origin.BidCount1 = 10;

            RawMarketData target = origin.Clone() as RawMarketData;
            origin.BidCount1 = 20;

            Assert.AreNotEqual(origin.BidCount1, target.BidCount1);
            Assert.AreNotEqual(origin, target);
        }
        // Count = 잔량
        public Boolean IsOverAvgOptionBidAskCount(RawMarketData rmdOption)
        {
            long avgBidAskCount = Util.GetAvgBidAskCount(rmdOption);

            long enteredGoalOptionCount = (long)Math.Abs(GetUnsignedLiveGoalOptionCount());
            if (enteredGoalOptionCount > avgBidAskCount)
            {
                return true;
            }
            return false;
        }
        public IProcessState_KtbSkel Run(double indicator, RawMarketData spotRmd, RawMarketData futureRmd)
        {
            _sweepController.Run();

            if (_sweepController.IsDone())
            {
                Position_KtbSkel position = _sweepController.GetPosition();
                _enterExitPolicy.ExecuteAfterSweeperDone(_parent, position);
                logger.Info("KtbSkel State from enter/exit --> default.");
                return new ProcessState_Default(_parent);
            }
            return this;
        }
Example #23
0
        public POrder(
            TradingDirection longShort, 
            String code, 
            long reqCount, 
            double reqPrice, 
            Account account,
            RawMarketData rmdClone,
            MarketType market,
            Boolean bWeakLongBlock)
        {
            this.LongShort = longShort;
            this.Code = code;
            this.ReqCount = reqCount;
            this.ReqPrice = reqPrice;
            this.TargetAccount = account;
            this.RMDClone = rmdClone.Clone() as RawMarketData; // 밖에서도 열심히 했겠지만 최종적으로 Clone을 저장하도록 한다.
            this.Market = market;
            this.IsWeakLongBlock = bWeakLongBlock;

            this.InitReqPrice = reqPrice;

            this.ContractedCount = 0;
            this.CanceledCount = 0;
            this.OrderNumber = -1;
            this.AvgContractPrice = 0;

            this.IsAllocatedYetRequestOrFree = true;

            this.CreatedTime = DateTime.Now;
            this.NoErrorOrderIn = false;
            this.Comment = "";

            this.ShortCutTargetOfContractCallBack = null;

            this.EarlyUpdatedLongBlockCount = 0;

            _bGotLastPacket = false;

            // dependency가 있음(SetMarketType, SetcodeNoTail, SetLongBlock)
            Boolean bSuccess1 = SetMarketType();
            Boolean bSuccess2 = SetCodeNoTail(bSuccess1);

            ConvertOverPriceToInRMDPriceIfNotZero();

            SetLongBlock(bSuccess2);

            POrderManager.Ins().Add(this);

            ValidateInput();
        }
 public IProcessState_KtbSkel Run(double indicator, RawMarketData spotRmd, RawMarketData futureRmd)
 {
     // Indicator까지 다 찼는지를 확인한다.
     // 다 찬 경우라면 빼는 것을 고민한다.(Exit)
     if (_stack.IsFull(indicator))
     {
         // Exit 확인해보고 기회면 Exit 진행한다.
         return CheckAnd_Out(indicator, spotRmd, futureRmd);
     }
     else
     {
         // 비어 있다면 채우는 것 고민해본다.
         return CheckAnd_Enter(indicator, spotRmd, futureRmd);
     }
 }
Example #25
0
 public static Boolean IsValidBidAskCurPrice(RawMarketData rmd)
 {
     if (!IsValidPrice(rmd.CurPrice))
     {
         return false;
     }
     if (!IsValidPrice(rmd.BidPrice1))
     {
         return false;
     }
     if (!IsValidPrice(rmd.AskPrice1))
     {
         return false;
     }
     return true;
 }
        public Test_PB_HTS_MonitorEnter(StrategyNode node)
        {
            this.IsPermissionOK = false;

            this._node = node;
            this._monitorLimit = new MonitorLimit_LastUpdatedRmd();

            String fut1 = KospiFutureUtil.Ins().KFSI.FutureCode1;

            _referenceRmdFut1 = RmdManager.Ins().GetData(fut1);

            _referenceRmdFut1List = new List<RawMarketData>();
            _referenceRmdFut1List.Add(_referenceRmdFut1);

            _root = node.Root as STR_Test_PB_Fut_HTS;
            _futureAccount = _root.FutureAccount;
        }
        Boolean IsLimOrderLimitOK(POrder o, RawMarketData rmdReferenceOption)
        {
            String optionCode = rmdReferenceOption.Code;

            const double kBuffer = 0.5;

            double targetOneOptionValue = ElwOptionUtil.ConvertPriceToOneOptionValue(o.Code, o.ReqPrice);

            if (o.LongShort == TradingDirection.Short)
            {
                if (!Util.IsValidPrice(rmdReferenceOption.BidPrice1))
                {
                    return false;
                }

                double downBarrierPrice = rmdReferenceOption.BidPrice1 - kBuffer;

                double optionOneOptionValue = ElwOptionUtil.ConvertPriceToOneOptionValue(optionCode, downBarrierPrice);

                if (targetOneOptionValue < optionOneOptionValue)
                {
                    // 너무 싸게 팔자를 냈다.
                    return false;
                }
                return true;
            }
            else
            {
                if (!Util.IsValidPrice(rmdReferenceOption.AskPrice1))
                {
                    return false;
                }

                double upBarrierPrice = rmdReferenceOption.AskPrice1 + kBuffer;

                double optionOneOptionValue = ElwOptionUtil.ConvertPriceToOneOptionValue(optionCode, upBarrierPrice);

                if (targetOneOptionValue > optionOneOptionValue)
                {
                    // 너무 비싸게 사자를 냈다.
                    return false;
                }
                return true;
            }
        }
        void AddData(PairMarketDataRegression_Accessor acc, int multiplier, int slope)
        {
            DateTime dt = new DateTime(2011, 7, 22, 9, 0, 1 * multiplier);
            RawMarketData rmdY = new RawMarketData("test_code", Detail.ProductType.Unknown);
            rmdY.BidPrice1 = 2 * multiplier * slope;
            rmdY.CurPrice = 2 * multiplier * slope;
            rmdY.AskPrice1 = 1 * multiplier * slope;
            rmdY.LastUpdatedTime = dt;

            RawMarketData rmdX = new RawMarketData("test_code", Detail.ProductType.Unknown);
            rmdX.BidPrice1 = 2 * multiplier;
            rmdX.CurPrice = 2 * multiplier;
            rmdX.AskPrice1 = 1 * multiplier;
            rmdX.LastUpdatedTime = dt;

            // Add more
            acc.Add(dt, rmdY, rmdX);
        }
Example #29
0
        public static ElwOptionArbData GetElwOptionArbData(
            ElwInfo elw, 
            RawMarketData rmdElw, 
            KospiOptionInfo kospiOptionInfo, 
            RawMarketData rmdOption,
            long maxCountOfOptionPerOnce,
            double maxValueOfOptionPerOnce,
            Account optionAccount,
            Account elwAccount,
            Boolean bForward,
            Boolean bAgressiveReverseOn,
            long totalAttemptOptionCount)
        {
            PreData data = GetPreData(
                                elw,
                                rmdElw,
                                kospiOptionInfo,
                                rmdOption,
                                maxCountOfOptionPerOnce,
                                maxValueOfOptionPerOnce,
                                optionAccount,
                                elwAccount,
                                bForward,
                                bAgressiveReverseOn,
                                totalAttemptOptionCount);

            {
                ElwOptionArbData eoad = GetElwOptionArbData_WithProfit(data);
                if (eoad != null)
                {
                    return eoad;
                }
            }

            {
                ElwOptionArbData eoad = GetElwOptionArbData_WO_Profit(data);
                if (eoad != null)
                {
                    return eoad;
                }
            }

            return null;
        }
        public MonitorEnter_CalFut(
            StrategyNode node, 
            Detail.ProductType pt, 
            String fut1, 
            String spread, 
            String fut2, 
            ForwardBackward fb)
        {
            this._node = node;

            IMarketDataBoard board = null;

            switch (pt)
            {
                case Detail.ProductType.KospiFuture:
                    board = RmdManager.Ins().KospiFuture;
                    break;
                case Detail.ProductType.UsdFuture:
                    board = RmdManager.Ins().Future;
                    break;
                case Detail.ProductType.KtbFuture:
                    board = RmdManager.Ins().Future;
                    break;
                default:
                    String errorMsg = String.Format("{0} is not valid type", pt);
                    logger.Error(errorMsg);
                    Util.KillWithNotice(errorMsg);
                    break;
            }

            _refRmdFut1 = board.GetData(fut1);
            _refRmdSpread = board.GetData(spread);
            _refRmdFut2 = board.GetData(fut2);

            _referenceRmds.Add(_refRmdFut1);
            _referenceRmds.Add(_refRmdSpread);
            _referenceRmds.Add(_refRmdFut2);

            STR_Arb root = node.Root as STR_Arb;

            _futureAccount = root.FutureAccount;
        }