コード例 #1
0
        public void Update(int barNumber)
        {
            security.BarNumber = barNumber;
            if (!flagToDebugLog)
            {
                var message = string.Format("ГО на покупку: {0}; ГО на продажу: {1}; Шаг цены: {2}", security.BuyDeposit, security.SellDeposit, security.StepPrice);
                logger.Log(message);
                flagToDebugLog = true;
            }

            var le      = sec.Positions.GetLastActiveForSignal("LE", barNumber);
            var subBars = security.GetBars(barNumber);

            var lastBar = security.LastBar;
            var lows    = pivotPointsIndicator.GetLows(barNumber);

            logger.Log("lows.Count = " + lows.Count.ToString());

            var lowsValues = new List <double>();

            foreach (var low in lows)
            {
                lowsValues.Add(low.Value);
            }

            var compressedSec   = sec.CompressTo(new Interval(30, DataIntervals.MINUTE));
            var filterBarNumber = compressedSecurity.GetBarCompressedNumberFromBarBaseNumber(barNumber);
            var lowsFilter      = pivotPointsIndicatorFilter.GetLows(filterBarNumber - 1);

            var valuesFilterLows = new List <double>();

            foreach (var low in lowsFilter)
            {
                valuesFilterLows.Add(low.Value);
            }

            var highsFilter = pivotPointsIndicatorFilter.GetHighs(filterBarNumber - 1);

            var valuesFilterHighs = new List <double>();

            foreach (var high in highsFilter)
            {
                valuesFilterHighs.Add(high.Value);
            }

            #region Long

            if (le == null)
            {
                if (!IsAboutEndOfSession(lastBar.Date))
                {
                    var message = string.Format("Номер бара = {0}; patternPivotPoints_1g2.Check(lowsValues) = {1}; patternPivotPoints_1g2g3.Check(valuesFilterLows) = {2}; " +
                                                "!patternPivotPoints_1l2.Check(valuesFilterHighs) = {3}", barNumber, patternPivotPoints_1g2.Check(lowsValues), patternPivotPoints_1g2g3.Check(valuesFilterLows),
                                                !patternPivotPoints_1l2.Check(valuesFilterHighs));
                    logger.Log(message);

                    message = string.Format("lowsValues.Count = {0}", lowsValues.Count);
                    logger.Log(message);

                    if (patternPivotPoints_1g2.Check(lowsValues) && patternPivotPoints_1g2g3.Check(valuesFilterLows) && !patternPivotPoints_1l2.Check(valuesFilterHighs))
                    {
                        logger.Log("Номер бара = " + barNumber.ToString() + "; Условие входа выполнено!");
                        if (barNumber == lows.Last().BarNumber + 3)
                        {
                            var lowLast   = lows.Last();
                            var stopPrice = lowLast.Value - 1;
                            var lastPrice = lastBar.Close;
                            if (lastPrice > stopPrice)
                            {
                                var contracts = localMoneyManager.GetQntContracts(lastPrice, stopPrice, PositionSide.Long);
                                //var contracts = 1;
                                sec.Positions.BuyAtMarket(barNumber + 1, contracts, "LE");
                                takeProfitLong = 0;
                            }
                        }
                    }
                }
            }
            else
            {
                if (lows.Count == 0)
                {
                    return;
                }
                var low = lows.Last();
                stopLossLong = low.Value - 1;
                var riskValue = le.EntryPrice - stopLossLong;

                if (takeProfitLong == 0)
                {
                    takeProfitLong = le.EntryPrice + riskValue * 2;
                }

                if (IsAboutEndOfSession(lastBar.Date))
                {
                    le.CloseAtMarket(barNumber + 1, "LXT");
                }

                le.CloseAtStop(barNumber + 1, stopLossLong, 100, "LXS");
                le.CloseAtProfit(barNumber + 1, takeProfitLong, 100, "LXP");
            }

            #endregion

            #region Short

            var se = sec.Positions.GetLastActiveForSignal("SE", barNumber);

            var highs       = pivotPointsIndicator.GetHighs(barNumber);
            var highsValues = new List <double>();
            foreach (var high in highs)
            {
                highsValues.Add(high.Value);
            }

            if (se == null)
            {
                if (!IsAboutEndOfSession(lastBar.Date))
                {
                    if (patternPivotPoints_1l2.Check(highsValues) && patternPivotPoints_1l2l3.Check(valuesFilterHighs) && !patternPivotPoints_1g2.Check(valuesFilterLows))
                    {
                        if (barNumber == highs.Last().BarNumber + 3)
                        {
                            var highLast  = highs.Last();
                            var stopPrice = highLast.Value + 1;
                            var lastPrice = lastBar.Close;
                            if (lastPrice < stopPrice)
                            {
                                var contracts = localMoneyManager.GetQntContracts(lastPrice, stopPrice, PositionSide.Short);
                                //var contracts = 1;
                                sec.Positions.SellAtMarket(barNumber + 1, contracts, "SE");
                                takeProfitShort = 0;
                            }
                        }
                    }
                }
            }
            else
            {
                if (highs.Count == 0)
                {
                    return;
                }

                var high = highs.Last();

                stopLossShort = high.Value + 1;
                var riskValue = stopLossShort - se.EntryPrice;

                if (takeProfitShort == 0)
                {
                    takeProfitShort = se.EntryPrice - riskValue * 2;
                }

                if (IsAboutEndOfSession(lastBar.Date))
                {
                    se.CloseAtMarket(barNumber + 1, "SXT");
                }

                se.CloseAtStop(barNumber + 1, stopLossShort, 100, "SXS");
                se.CloseAtProfit(barNumber + 1, takeProfitShort, 100, "SXP");
            }

            #endregion
        }
コード例 #2
0
        public void Update(int barNumber)
        {
            security.BarNumber = barNumber;
            if (!flagToDebugLog)
            {
                var message = string.Format("ГО на покупку: {0}; ГО на продажу: {1}; Шаг цены: {2}", security.BuyDeposit, security.SellDeposit, security.StepPrice);
                Logger.Log(message);
                flagToDebugLog = true;
            }

            var le = sec.Positions.GetLastActiveForSignal("LE", barNumber);

            var lastBar = security.LastBar;
            var lows    = pivotPointsIndicator.GetLows(barNumber);
            //Logger.Log("lows.Count = " + lows.Count.ToString());

            var lowsValues = new List <double>();

            foreach (var low in lows)
            {
                lowsValues.Add(low.Value);
            }

            var lastPrice = lastBar.Close;

            var lastLowValue = 0d;

            if (lows.Count != 0)
            {
                lastLowValue = lowsValues.Last();
            }


            #region Long
            if (le == null)
            {
                if (lastLowForOpenLongPosition != 0)
                {
                    lastLowCaseLongClose = lastLowForOpenLongPosition;
                }
                lastLowForOpenLongPosition = 0;
                if (patternPivotPoints_1g2.Check(lowsValues) && (lastPrice > ema[barNumber]) && (lastLowValue != lastLowCaseLongClose))
                {
                    lastLowForOpenLongPosition = lastLowValue;
                    Logger.Log("Номер бара = " + barNumber.ToString() + "; Условие входа в лонг выполнено!");
                    var lowLast   = lows.Last();
                    var stopPrice = lowLast.Value - atr[barNumber];
                    if (lastPrice > stopPrice)
                    {
                        var contracts = localMoneyManager.GetQntContracts(lastPrice, stopPrice, PositionSide.Long);
                        //var contracts = 1;
                        sec.Positions.BuyAtMarket(barNumber + 1, contracts, "LE");//174: 78583
                        lastPriceOpenLongPosition = lastPrice;
                        stopLossLong = 0;
                    }
                }
            }
            else
            {
                if (patternPivotPoints_1g2.Check(lowsValues) && (lastPrice > ema[barNumber]) && (lastLowForOpenLongPosition != lastLowValue))
                {
                    lastLowForOpenLongPosition = lastLowValue; //минимум для открытия (в том числе и потенциального)
                    if (lastPrice > lastPriceOpenLongPosition)
                    {
                        var lowLast   = lows.Last();
                        var stopPrice = lowLast.Value - atr[barNumber];
                        if (lastPrice > stopPrice)
                        {
                            Logger.Log("Номер бара = " + barNumber.ToString() + "; Условие наращивания позиции лонг выполнено!");
                            var contracts = localMoneyManager.GetQntContracts(lastPrice, stopPrice, PositionSide.Long);
                            var shares    = le.Shares + contracts;
                            le.ChangeAtMarket(barNumber + 1, shares, "LE");
                            lastPriceOpenLongPosition = lastPrice;
                        }
                    }
                }

                else
                {
                    if (lows.Count == 0)
                    {
                        return;
                    }

                    var low      = lows.Last();
                    var stopLoss = low.Value - atr[barNumber];
                    if (stopLoss > stopLossLong)
                    {
                        stopLossLong = stopLoss;
                    }
                    le.CloseAtStop(barNumber + 1, stopLossLong, 100, "LXS");
                }
            }

            #endregion

            #region Short

            var se = sec.Positions.GetLastActiveForSignal("SE", barNumber);

            var highs       = pivotPointsIndicator.GetHighs(barNumber);
            var highsValues = new List <double>();
            foreach (var high in highs)
            {
                highsValues.Add(high.Value);
            }

            var lastHighValue = 0d;
            if (highs.Count != 0)
            {
                lastHighValue = highsValues.Last();
            }

            if (se == null)
            {
                if (lastHighForOpenShortPosition != 0)
                {
                    lastHighCaseShortClose = lastHighForOpenShortPosition;
                }
                lastHighForOpenShortPosition = 0;
                if (patternPivotPoints_1l2.Check(highsValues) && (lastPrice < ema[barNumber]) && (lastHighValue != lastHighCaseShortClose))
                {
                    lastHighForOpenShortPosition = lastHighValue;
                    Logger.Log("Номер бара = " + barNumber.ToString() + "; Условие входа в шорт выполнено!");
                    var highLast  = highs.Last();
                    var stopPrice = highLast.Value + atr[barNumber];
                    if (lastPrice < stopPrice)
                    {
                        Logger.Log("Номер бара = " + barNumber.ToString() + "; Условие наращивания позиции шорт выполнено!");
                        var contracts = localMoneyManager.GetQntContracts(lastPrice, stopPrice, PositionSide.Short);
                        //var contracts = 1;
                        sec.Positions.SellAtMarket(barNumber + 1, contracts, "SE");
                        lastPriceOpenShortPosition = lastPrice;
                        stopLossShort = double.MaxValue;
                    }
                }
            }
            else
            {
                if (patternPivotPoints_1l2.Check(highsValues) && (lastPrice < ema[barNumber]) && (lastHighForOpenShortPosition != lastHighValue))
                {
                    lastHighForOpenShortPosition = lastHighValue; //максимум для открытия (в том числе и потенциального)
                    if (lastPrice < lastPriceOpenShortPosition)
                    {
                        var highLast  = highs.Last();
                        var stopPrice = highLast.Value + atr[barNumber];
                        if (lastPrice < stopPrice)
                        {
                            var contracts = localMoneyManager.GetQntContracts(lastPrice, stopPrice, PositionSide.Short);
                            //var contracts = 1;
                            var shares = se.Shares + contracts;
                            se.ChangeAtMarket(barNumber + 1, -shares, "SE");
                            lastPriceOpenShortPosition = lastPrice;
                        }
                    }
                }

                else
                {
                    if (highs.Count == 0)
                    {
                        return;
                    }

                    var high     = highs.Last();
                    var stopLoss = high.Value + atr[barNumber];
                    if (stopLoss < stopLossShort)
                    {
                        stopLossShort = stopLoss;
                    }
                    se.CloseAtStop(barNumber + 1, stopLossShort, 100, "SXS");
                }
            }

            #endregion
        }