Exemplo n.º 1
0
        public bool IsFirstWeek(string assetCode)
        {
            var firstweek   = new YearOfWeek(_portfolioDataset[assetCode].FirstOrDefault().Key);
            var currentweek = new YearOfWeek(_currentDate);

            return(firstweek == currentweek);
        }
        public override void OnAfterOpen(string assetCode)
        {
            double prevHigh = 0;
            double prevLow  = 0;

            if (TradeTerm == TradeTerm.Daily)
            {
                if (_simulator.IsFirstDate(assetCode))
                {
                    return;
                }

                prevHigh = _simulator.GetPrice(assetCode, PriceType.High, -1);
                prevLow  = _simulator.GetPrice(assetCode, PriceType.Low, -1);
            }
            else if (TradeTerm == TradeTerm.Weekly)
            {
                _high = Math.Max(_high, _simulator.GetPrice(assetCode, PriceType.High, 0));
                _low  = Math.Min(_low, _simulator.GetPrice(assetCode, PriceType.Low, 0));

                if (_simulator.IsFirstWeek(assetCode))
                {
                    return;
                }

                var currentWeek = _simulator.GetYearOfWeek();
                if (currentWeek <= _yearOfWeek)
                {
                    return; // 한 주가 안지남
                }

                prevHigh = _high;
                prevLow  = _low;

                _high = 0;
                _low  = double.MaxValue;
            }

            var curHigh    = _simulator.GetPrice(assetCode, PriceType.High, 0);
            var curLow     = _simulator.GetPrice(assetCode, PriceType.Low, 0);
            var openPrice  = _simulator.GetPrice(assetCode, PriceType.Open, 0);
            var closePrice = _simulator.GetPrice(assetCode, PriceType.Close, 0);

            var volPrice = openPrice + (prevHigh - prevLow) * K;
            var volume   = _simulator.GetVolume(assetCode);

            if (0 < volume)
            {
                _simulator.LimitOrder(assetCode, OrderType.Sell, openPrice, volume); // 보유물량 전량매도
            }

            // 마지막날은 매수하지 않음
            if (!_simulator.IsLastDate(assetCode))
            {
                if (volPrice > curLow && volPrice < curHigh)
                {
                    // 돌파가격에 매수
                    switch (_simulator.Property.TradeType)
                    {
                    case TradeType.Fixed:
                        _simulator.LimitOrder(assetCode, OrderType.Buy, volPrice, _simulator.GetSubject(assetCode).Volume);
                        break;

                    case TradeType.Ratio:
                        _simulator.LimitOrderPercent(assetCode, OrderType.Buy, volPrice, _simulator.GetSubject(assetCode).Ratio);
                        break;
                    }

                    // 매수한 week update
                    _yearOfWeek = _simulator.GetYearOfWeek();
                }
            }
        }
 private void _Initialize()
 {
     _yearOfWeek = new YearOfWeek();
     _high       = 0;
     _low        = double.MaxValue;
 }