Example #1
0
        public static List<MarketData> FillByPivotData(MarketData pivotData, List<MarketData> inputData)
        {
            // pivotData의 datetime에 맞추어서 나머지 데이터를 채운다.
            DateTime curDateTime = pivotData.StartDate;
            List<MarketData> outputData = new List<MarketData>();
            Dictionary<String, MarketDataTrace> trace = new Dictionary<string, MarketDataTrace>();

            // init variables
            foreach (MarketData md in inputData)
            {
                String ticker = md.Ticker;
                DOHLC firstData = md.GetByIndex(0);
                trace.Add(md.Ticker, new MarketDataTrace(ticker, firstData));
                outputData.Add(new MarketData(md));
            }

            while (curDateTime <= pivotData.EndDate)
            {
                if (pivotData.IsExistDate(curDateTime))
                {
                    // update trace
                    foreach (MarketData md in inputData)
                    {
                        String ticker = md.Ticker;
                        if (md.IsExistDate(curDateTime))
                        {
                            trace[ticker].CurData = md.GetData(curDateTime);
                        }
                    }

                    // set data
                    foreach (MarketData md in outputData)
                    {
                        String ticker = md.Ticker;
                        MarketDataTrace tr = trace[ticker];
                        // 기준 데이터보다 전 데이터이어야 한다.
                        Trace.Assert(tr.CurData.CurDate <= curDateTime, String.Format("{0}, {1}, {2}", tr.Ticker, tr.CurData.CurDate, curDateTime));
                        md.AddDatum(curDateTime, tr.CurData.OHLC);

                        if (tr.CurData.CurDate != curDateTime)
                        {
                            md.SetPrevDataUsedDate(curDateTime);
                        }
                    }
                }
                curDateTime = curDateTime.AddDays(1);
            }
            return outputData;
        }
        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;
        }
Example #3
0
        public static DateTime GetPivotTradingDate(int pivot, DateTime curDate, MarketData pivotData)
        {
            DateTime pivotDate = curDate;

            while (pivot > 0)
            {
                pivotDate = pivotDate.AddDays(1);
                if (pivotData.IsExistDate(pivotDate))
                {
                    pivot--;
                }
            }

            while (pivot < 0)
            {
                pivotDate = pivotDate.AddDays(-1);
                if (pivotData.IsExistDate(pivotDate))
                {
                    pivot++;
                }
            }

            return pivotDate;
        }