示例#1
0
        public void RawTradingData_Is_Singleton()
        {
            RawTradingData d  = RawTradingData.Instance;
            RawTradingData d2 = RawTradingData.Instance;

            Assert.AreSame(d, d2);
        }
        SinglePnLResult SingleSimulate(MarketData md)
        {
            List<RawTradingData> tradingList = new List<RawTradingData>();
            MarketDataSetKey key= (DataUtil.GetMarketDataKeyFromTicker(md.Ticker));
            DateTime curDate = _startDate;
            int index = 0;
            double curSize = 0;
            while (curDate <= _endDate)
            {

                if (md.IsExistDate(curDate))
                {
                    double curPrice = md.GetData(curDate).OHLC.Close;
                    if (index % _rebalancingPeriod == 0)
                    {
                        curSize = _baseInvest / curPrice;
                    }

                    if (curPrice < 50000)
                    {
                        curSize = Math.Round(curSize / 10, 0) * 10;
                    }

                    RawTradingData tradingData = new RawTradingData(key, curDate, curPrice, curSize);
                    tradingData.AddDelta(curSize * curPrice / _baseInvest);
                    tradingList.Add(tradingData);
                    index++;
                }
                curDate = curDate.AddDays(1);
            }

            SinglePnLResult profitAndLossResult =
                new SinglePnLResult(key, key.ToString(), TradingDirection.Long, true, tradingList);
            profitAndLossResult.CalculatePnL();

            return profitAndLossResult;
        }
        public void Calculate()
        {
            if (_bDeltaSwitchUpDown)
            {
                InitSwitchAreaMappings();
            }

            double startPrice = 0;
            int passedDays = 0;

            foreach (PriceData data in _priceDataList)
            {
                if (passedDays == 0)
                {
                    startPrice = data.Price;
                }

                double curSize = CalculateExpectedSize(startPrice, data.Price, passedDays);
                RawTradingData tradingData = new RawTradingData(_key, data.CurDate, data.Price, curSize);
                tradingData.AddDelta(CalculateDelta(startPrice, data.Price, passedDays));
                _tradingResultList.Add(tradingData);
                passedDays++;
            }
        }
示例#4
0
        double CalculteDailyPnL(RawTradingData prevTradingData, RawTradingData curTradingData)
        {
            double positionSize = prevTradingData.PositionSize;
            double prevPrice = prevTradingData.Price;
            double curPrice = curTradingData.Price;
            double pnL = _onePointAmount * positionSize * (curPrice - prevPrice);

            return pnL;
        }
示例#5
0
        double CalculteDailyCost(RawTradingData prevTradingData, RawTradingData curTradingData)
        {
            double curPrice = curTradingData.Price;

            double sellPositionSize = (prevTradingData.PositionSize - curTradingData.PositionSize) > 0 ?
                (prevTradingData.PositionSize - curTradingData.PositionSize) : 0;
            double cost = _onePointAmount * curPrice * CostRate * sellPositionSize;

            return cost;
        }
示例#6
0
        double CalculateDailyInvest(RawTradingData curTradingData)
        {
            double curPrice = curTradingData.Price;
            double curSize = curTradingData.PositionSize;
            double invest = _onePointAmount * curSize * curPrice;

            return invest;
        }
        void CreateTradingData(MarketData md)
        {
            int curPositionSize = 0;
            double curFactor = 0;
            double entryPrice = 0;
            for (int index = 0; index < md.GetTotalCount(); index++)
            {
                double prevFactor = curFactor;
                DOHLC dohlc = md.GetByIndex(index);
                DateTime curDate = dohlc.CurDate;
                double curPrice = dohlc.OHLC.Close;
                if (_bMATrendTester)
                {
                    _leadingMA.Add(curPrice);
                    _baseMA.Add(curPrice);
                }
                double curLeadingMA = _leadingMA.GetCurMA();
                double curBaseMA = _baseMA.GetCurMA();

                //curFactor = GetTradingFactorWith3Value(curPrice, curLeadingMA, curBaseMA, curFactor, entryPrice);
                curFactor = GetMASpreadSwitchedTradingFactor(curPrice, curLeadingMA, curBaseMA, curFactor);
                //curFactor = GetMASpreadTradingFactor(maSpread, curFactor);

                if (!IsAvailableTimeRange(curDate, curFactor))
                {
                    curFactor = kNeutralFactor;
                    //AddMATrendTester(10, 50);
                }

                //if (TestLossCut(prevFactor, entryPrice, curPrice))
                //{
                //    curFactor = kNeutralFactor;
                //}

                curPositionSize = (int) curFactor;

                if (prevFactor == kNeutralFactor && curFactor != kNeutralFactor)
                {
                    entryPrice = curPrice;
                }

                if (prevFactor < curFactor)
                {
                    curPrice = curPrice + 0.025;
                }
                else if (prevFactor > curFactor)
                {
                    curPrice = curPrice - 0.025;
                }

                RawTradingData tradingData =
                    new RawTradingData(MarketDataSetKey.KospiFuture, curDate, curPrice, curPositionSize);
                _tradingDataList.Add(tradingData);

            }
        }
示例#8
0
        void CreateHedgingTradingData(MarketData md)
        {
            int curPositionSize = 0;
            double curGrossDelta = 0;
            double curHedgingFactor = EquityVolPredefinedVariables.FullHedgingFactor;
            double prevHedgingFactor = 0;
            double prevPrice = 0;
            int lastHedgedIndex = 0;
            for (int index = 0; index < md.GetTotalCount(); index++)
            {
                bool bHegingTest = false;
                DOHLC dohlc = md.GetByIndex(index);

                DateTime curDate = dohlc.CurDate;

                if (curDate.DayOfWeek  == DayOfWeek.Saturday
                    || curDate.DayOfWeek == DayOfWeek.Sunday)
                {
                    continue;
                }

                double curPrice = dohlc.OHLC.Close;
                if (prevPrice != 0 && curPrice != 0)
                {
                    double upDownRate = MathUtil.GetUpDownRate(prevPrice, curPrice);
                    if (_upDownRates.Count == _volN)
                    {
                        _upDownRates.Dequeue();
                    }
                    _upDownRates.Enqueue(upDownRate);
                }

                if (_bMATrendTester)
                {
                    _leadingMA.Add(curPrice);
                    _baseMA.Add(curPrice);

                    //if(prevHedgingFactor == EquityVolPredefinedVariables.OverHedgingFactor
                    //    && MathUtil.GetUpDownRate(_nonNeutralHedgingEntryPrice, prevPrice) > 0.02)
                    //{
                    //    curHedgingFactor = EquityVolPredefinedVariables.FullHedgingFactor;
                    //}
                    //if (prevHedgingFactor == EquityVolPredefinedVariables.UnderHedgingFactor
                    //    && MathUtil.GetUpDownRate(_nonNeutralHedgingEntryPrice, prevPrice) < -0.02)
                    //{
                    //    curHedgingFactor = EquityVolPredefinedVariables.FullHedgingFactor;
                    //}

                    if (MathUtil.GetUpDownRate(prevPrice, curPrice) > -0.02)
                    {
                        curHedgingFactor = GetCurTrendHedgingFactor();
                    }

                    //bHegingTest = (curHedgingFactor != prevHedgingFactor) ||
                    //     ((lastHedgedIndex + (int)(EquityVolPredefinedVariables.RebalancingPeriod / 2) <= index)
                    //         && index % (_hedingInterval) == 0);
                    bHegingTest = (curHedgingFactor != prevHedgingFactor) ||
                        (index % (_hedingInterval) == 0 && (lastHedgedIndex + (2 * _hedingInterval) <= index));
                    //bHegingTest = (curHedgingFactor != prevHedgingFactor);
                    //bHegingTest = (curHedgingFactor != prevHedgingFactor) ||
                    //   (lastHedgedIndex + (int)(EquityVolPredefinedVariables.RebalancingPeriod / 2) <= index);
                }
                else
                {
                    bHegingTest = index % (2 * _hedingInterval) == 0;
                }

                if (bHegingTest)
                {
                    if (_grossDeltas == null)
                    {
                        curGrossDelta = curHedgingFactor * 1.0;
                        curPositionSize = GetGHedgingPositonSize(curGrossDelta, curPrice, _baseInvest);
                        prevHedgingFactor = curHedgingFactor;
                        lastHedgedIndex = index;
                    }
                    else if (_grossDeltas.ContainsKey(curDate))
                    {
                        curGrossDelta = curHedgingFactor * _grossDeltas[curDate];
                        curPositionSize = GetGHedgingPositonSize(curGrossDelta, curPrice, _baseInvest);
                        prevHedgingFactor = curHedgingFactor;
                        lastHedgedIndex = index;
                    }
                }
                if (curDate >= _startDate)
                {
                    RawTradingData tradingData =
                        new RawTradingData(MarketDataSetKey.KospiFuture, curDate, curPrice, curPositionSize);
                    tradingData.AddDelta(curGrossDelta);
                    _tradingDataList.Add(tradingData);
                }
                prevPrice = curPrice;
            }
        }
        List<RawTradingData> SelectTradingDataList(
            MarketDataSetKey marketKey, 
            List<RawTradingData> tradingList, 
            Dictionary<DateTime, List<MarketDataSetKey>> dateKeys)
        {
            List<RawTradingData> selectedTradingList = new List<RawTradingData>();
            double prevSize = 0;
            bool bDataExist = false;
            foreach (RawTradingData data in tradingList)
            {
                RawTradingData newData;
                if (dateKeys.ContainsKey(data.CurDate))
                {
                    List<MarketDataSetKey> keys = dateKeys[data.CurDate];
                    if (keys.Contains(marketKey))
                    {
                        newData = data;
                        prevSize = data.PositionSize;
                        bDataExist = true;
                    }
                    else
                    {
                        newData = new RawTradingData(marketKey, data.CurDate, data.Price, 0);
                        prevSize = 0;
                    }
                }
                else
                {
                    newData = new RawTradingData(marketKey, data.CurDate, data.Price, prevSize);
                }

                selectedTradingList.Add(newData);
            }

            if (bDataExist)
            {
                return selectedTradingList;
            }
            else
            {
                return new List<RawTradingData>();
            }
        }
示例#10
0
        void CreateHedgingTradingData(MarketData md)
        {
            int curPositionSize = 0;
            double curGrossDelta = 0;
            double curHedgingFactor = EquityVolPredefinedVariables.FullHedgingFactor;
            double prevHedgingFactor = 0;
            double prevPrice = 0;
            int lastHedgedIndex = 0;
            for (int index = 0; index < md.GetTotalCount(); index++)
            {
                DOHLC dohlc = md.GetByIndex(index);

                DateTime curDate = dohlc.CurDate;

                if (curDate.DayOfWeek  == DayOfWeek.Saturday
                    || curDate.DayOfWeek == DayOfWeek.Sunday)
                {
                    continue;
                }

                double curPrice = dohlc.OHLC.Close;
                if (prevPrice != 0 && curPrice != 0)
                {
                    _leadingMA.Add(curPrice);
                    _baseMA.Add(curPrice);

                    double upDownRate = MathUtil.GetUpDownRate(prevPrice, curPrice);
                    if (_upDownRates.Count == _volN)
                    {
                        _upDownRates.Dequeue();
                    }
                    _upDownRates.Enqueue(upDownRate);
                }

                //if (MathUtil.GetUpDownRate(prevPrice, curPrice) > -0.02)
                //{
                    curHedgingFactor = GetCurTrendHedgingFactor();
                //}

                if (curHedgingFactor != prevHedgingFactor)
                {
                    if (_grossDeltas == null)
                    {
                        curGrossDelta = curHedgingFactor * 1.0;
                        curPositionSize = GetGHedgingPositonSize(curGrossDelta, curPrice, _baseInvest);
                        prevHedgingFactor = curHedgingFactor;
                        lastHedgedIndex = index;
                    }
                    else if (_grossDeltas.ContainsKey(curDate))
                    {
                        curGrossDelta = curHedgingFactor * _grossDeltas[curDate];
                        curPositionSize = GetGHedgingPositonSize(curGrossDelta, curPrice, _baseInvest);
                        prevHedgingFactor = curHedgingFactor;
                        lastHedgedIndex = index;
                    }
                }
                if (curDate >= _startDate)
                {
                    RawTradingData tradingData =
                        new RawTradingData(MarketDataSetKey.KospiFuture, curDate, curPrice, curPositionSize);
                    tradingData.AddDelta(curGrossDelta);
                    _tradingDataList.Add(tradingData);
                }
                prevPrice = curPrice;
            }
        }