Exemplo n.º 1
0
//=============================================================================
        protected override void NewBar()
        {
            DTime = Bars[Bars.Range.To - 1].Time;
            ci    = Bars.Range.To - 1;
//===  ќ–≈ ÷»я ================================================================
            if (posGuidBuy != Guid.Empty &&
                Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty;
            }
            if (posGuidSell != Guid.Empty &&
                Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty;
            }
//=== “релинг  =================================================================
            if (k >= 7)
            {
                TrailActiveOrders();
            }
//=====  –исуем вертикальную линию в 7:00 ======================================
            if (DTime.Hour == 6 && DTime.Minute == 45)
            {
                k         = 0; var vl1 = Tools.Create <VerticalLine>();
                vl1.Time  = Bars[Bars.Range.To - 1].Time;
                vl1.Color = Color.Aqua; vl1.Width = 2;
            }
//=====  ƒл¤ определении паузи перед стартом трелинга ==========================
            if (DTime.Hour >= 7)
            {
                k++;
            }
//====== “орги  ================================================================
            if (DTime.Hour == 7)
            {
                if (posGuidBuy == Guid.Empty && Buy1)
                {
                    var result10 = Trade.OpenMarketPosition(Instrument.Id,
                                                            ExecutionRule.Buy, 0.1, Instrument.Bid, -1,
                                                            Stops.InPips(SL1, null), null, null);
                    if (result10.IsSuccessful)
                    {
                        posGuidBuy = result10.Position.Id;
                    }
                }

                if (posGuidSell == Guid.Empty && Sell1)
                {
                    var result20 = Trade.OpenMarketPosition(Instrument.Id,
                                                            ExecutionRule.Sell, 0.1, Instrument.Ask, -1,
                                                            Stops.InPips(SL1, null), null, null);
                    if (result20.IsSuccessful)
                    {
                        posGuidSell = result20.Position.Id;
                    }
                }
            }
        }
Exemplo n.º 2
0
//===============================================================================================================================
        protected override void NewBar()
        {
            DTime = Bars[Bars.Range.To - 1].Time;
            ci    = Bars.Range.To - 1;

            var posActiveMineB = Trade.GetActivePositions(mgB, true);

            if (posActiveMineB != null && posActiveMineB.Length > 0 && posGuidBuy != posActiveMineB[0].Id)
            {
                posGuidBuy = posActiveMineB[0].Id;
            }
            var posActiveMineS = Trade.GetActivePositions(mgS, true);

            if (posActiveMineS != null && posActiveMineS.Length > 0 && posGuidSell != posActiveMineS[0].Id)
            {
                posGuidSell = posActiveMineS[0].Id;
            }
//=== КОРЕКЦИЯ ===========================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty; k7 = 0;
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty; k7 = 0;
            }

//=== Трелинг  ===========================================================================================================
            k++; if (k > kf)
            {
                TrailActiveOrders();
            }
//====================================================================================================================================

            if (posGuidBuy == Guid.Empty && Buy7)
            {
                k = 0;
                var result107 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1,
                                                         Stops.InPips(SL1, null), null, mgB);
                if (result107.IsSuccessful)
                {
                    posGuidBuy = result107.Position.Id;
                }
            }

            if (posGuidSell == Guid.Empty && Sell7)
            {
                k = 0;
                var result207 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1,
                                                         Stops.InPips(SL1, null), null, mgS);
                if (result207.IsSuccessful)
                {
                    posGuidSell = result207.Position.Id;
                }
            }
        }
Exemplo n.º 3
0
//===============================================================================================================================
        protected override void NewBar()
        {
            DTime = Bars[Bars.Range.To - 1].Time;
            ci    = Bars.Range.To - 1;

//=== КОРЕКЦИЯ =======================================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty;
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty;
            }
//=========  Рисуем линию  начала торгов Европы =============================================================================
            if (DTime.Hour == 3 && DTime.Minute == 00)
            {
                var vl1 = Tools.Create <VerticalLine>(); vl1.Time = Bars[Bars.Range.To - 1].Time.AddHours(4); vl1.Color = Color.Aqua;
                zAm = Bars[TimeToIndex(Bars[ci].Time.AddHours(-6), Timeframe)].Open;
                var vl2          = Tools.Create <VerticalLine>(); vl2.Time = Bars[ci].Time.AddHours(-6); vl2.Color = Color.DarkCyan;
                var toolPolyLine = Tools.Create <PolyLine>();
                toolPolyLine.Color = Color.DarkGoldenrod;
                toolPolyLine.AddPoint(new ChartPoint(vl2.Time = Bars[ci].Time.AddHours(-6), zAm));
                toolPolyLine.AddPoint(new ChartPoint(Bars[Bars.Range.To - 1].Time.AddHours(14), zAm));
            }

//=== Трелинг  =======================================================================================================================
            TrailActiveOrders();
//==== Торги =========================================================================================================================

            if (posGuidBuy == Guid.Empty && Buy7 && torg)
            {
                torg = false;
                var result107 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1,
                                                         Stops.InPips(SL1, null), null, mgB);
                if (result107.IsSuccessful)
                {
                    posGuidBuy = result107.Position.Id;
                }
                // var vl1 = Tools.Create<VerticalLine>(); vl1.Time=Bars[Bars.Range.To-1].Time; vl1.Color=Color.Red;
            }

            if (posGuidSell == Guid.Empty && Sell7 && torg)
            {
                torg = false;
                var result207 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1,
                                                         Stops.InPips(SL1, null), null, mgS);
                if (result207.IsSuccessful)
                {
                    posGuidSell = result207.Position.Id;
                }
                // var vl1 = Tools.Create<VerticalLine>(); vl1.Time=Bars[Bars.Range.To-1].Time; vl1.Color=Color.Blue;
            }
        }
Exemplo n.º 4
0
 protected void Sell1()
 {
     if (posGuidSell == Guid.Empty)
     {
         var result2 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1,
                                                Stops.InPips(300, null), null, magicNumber);
         if (result2.IsSuccessful)
         {
             posGuidSell = result2.Position.Id;
         }
     }
 }
Exemplo n.º 5
0
 protected void Buy1()
 {
     if (posGuidBuy == Guid.Empty)
     {
         var result1 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1,
                                                Stops.InPips(300, null), null, magicNumber);
         if (result1.IsSuccessful)
         {
             posGuidBuy = result1.Position.Id;
         }
     }
 }
Exemplo n.º 6
0
 protected void Sell1()
 {
     if (posGuidSell == Guid.Empty)
     {
         var result2 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1,
                                                //Stops.InPrice(max3+0.0003,null), null, null);
                                                Stops.InPips(null, null), null, null);
         if (result2.IsSuccessful)
         {
             posGuidSell = result2.Position.Id;
         }
     }
 }
Exemplo n.º 7
0
        protected void Buy1()
        {
            if (posGuidBuy == Guid.Empty)
            {
                var result1 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1,
                                                       //Stops.InPrice(min3-0.0003,null), null, null);
                                                       Stops.InPips(null, null), null, null);

                if (result1.IsSuccessful)
                {
                    posGuidBuy = result1.Position.Id;
                }
            }
        }
Exemplo n.º 8
0
 protected void Sell1()
 {
     if (posGuidSell == Guid.Empty)
     {
         var result2 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1,
                                                Stops.InPips(200, null), null, magicNumber);
         if (result2.IsSuccessful)
         {
             posGuidSell = result2.Position.Id;
             rz1         = Math.Round((max1 - min1) * 100000, 0);
             rz2         = Math.Round((max1 - Bars[Bars.Range.To - 1].Low) * 100000, 0);
             cn          = Bars[Bars.Range.To - 1].Close;
         }
     }
 }
Exemplo n.º 9
0
// ===================================================================================================
        private void Sell(SignalType signal)
        {
            if ((_positionGuid != Guid.Empty) && IsDoCloseOnReverseSignal)
            {
                return;
            }

            var result = Trade.Sell(Instrument.Id, LotVolume, Stops.InPips(StopLoss, TakeProfit));

            if (result.IsSuccessful)
            {
                _positionGuid            = result.Position.Id;
                _positionOpendSignalType = signal;
            }
        }
Exemplo n.º 10
0
        protected void Buy1()
        {
            if (posGuidBuy == Guid.Empty)
            {
                var result1 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1,
                                                       Stops.InPips(200, null), null, magicNumber);

                if (result1.IsSuccessful)
                {
                    posGuidBuy = result1.Position.Id;
                    rz1        = Math.Round((max1 - min1) * 100000, 0);
                    rz2        = Math.Round((Bars[Bars.Range.To - 1].High - min1) * 100000, 0);
                    cn         = Bars[Bars.Range.To - 1].Close;
                }
            }
        }
Exemplo n.º 11
0
        protected override void NewBar()
        {
            ci = Bars.Range.To - 1;
            // Event occurs on every new bar
//=== КОРЕКЦИЯ ===========================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty;
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty;
            }

            aoUp1   = _awoInd.SeriesUp[Bars.Range.To - 1];             // Зелені лінії - Вверху>0  Внизу<0
            aoDown1 = _awoInd.SeriesDown[Bars.Range.To - 1];           // Червоні лінії
            aoUp2   = _awoInd.SeriesUp[Bars.Range.To - 2];             // Зелені лінії - Вверху>0  Внизу<0
            aoDown2 = _awoInd.SeriesDown[Bars.Range.To - 2];
            aoUp3   = _awoInd.SeriesUp[Bars.Range.To - 3];             // Зелені лінії - Вверху>0  Внизу<0
            aoDown3 = _awoInd.SeriesDown[Bars.Range.To - 3];
            aoUp4   = _awoInd.SeriesUp[Bars.Range.To - 4];             // Зелені лінії - Вверху>0  Внизу<0
            aoDown4 = _awoInd.SeriesDown[Bars.Range.To - 4];

            // // Вверху ВПАДИНА Зеленый на Красний
            if (aoDown4 > 0.0002 && aoDown3 > 0.0002 && aoUp2 > 0.0002 && aoUp1 > 0.0002)
            {
                LineR(ci - 1);
                if (posGuidBuy == Guid.Empty)
                {
                    var result107 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1,
                                                             Stops.InPips(100, 100), null, null);
                    if (result107.IsSuccessful)
                    {
                        posGuidBuy = result107.Position.Id;
                    }
                }
            }
        }
Exemplo n.º 12
0
        protected override void NewBar()
        {
            _wprInd.ReInit();
            if (_wprInd.MainIndicatorSeries[Bars.Range.To - 2] > 0)
            {             // Всі точки перегину зиззага
                zz3  = zz2;        zz2 = zz1;    zz1 = _wprInd.MainIndicatorSeries[Bars.Range.To - 2];
                zzi3 = zzi2;      zzi2 = zzi1;  zzi1 = Bars.Range.To - 2;
                Print("{0} {1} {2} -- {3}", zz3, zz2, zz1, Bars[Bars.Range.To - 2].Time);
                if (zz3 > zz2 && zz2 < zz1)
                {
                    zzd3 = zzd2; zzd2 = zzd1; zzd1 = zzi2;
                }
                if (zz3 < zz2 && zz2 > zz1)
                {
                    zzd3 = zzd2; zzd2 = zzd1; zzd1 = zzi2;
                }

                if (Bars[zzd2].Low > Bars[zzi1].Low && Bars[zzd2].High < Bars[zzd1].High && Bars[zzi1].High < Bars[zzd1].High)
                {
                    Print("Ввверху Максимум");
                    vr.Time = Bars[zzd2].Time;
                    vb.Time = Bars[zzd1].Time;
                    vy.Time = Bars[zzi1].Time;
                    if (Bars[zzd1].Low != zzU)
                    {
                        zzU = Bars[zzd1].Low;
                        var result1 = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.SellLimit, 0.1, Bars[zzd1].High - Instrument.Spread - kor, 0, Stops.InPips(SL, TP), null, null, null);
                    }
                }


                if (Bars[zzd2].High < Bars[zzi1].High && Bars[zzd1].Low < Bars[zzd2].Low && Bars[zzd1].Low < Bars[zzi1].Low)
                {
                    Print("Внизу Максимум");
                    vr.Time = Bars[zzd2].Time;
                    vb.Time = Bars[zzd1].Time;
                    vy.Time = Bars[zzi1].Time;
                    if (Bars[zzd1].Low != zzD)
                    {
                        zzD = Bars[zzd1].Low;
                        var result = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.BuyLimit, 0.1, Bars[zzd1].Low + Instrument.Spread + kor, 0, Stops.InPips(SL, TP), null, null, null);
                    }
                }
            }
        }
Exemplo n.º 13
0
        protected override void NewBar()
        {
            //=== КОРЕКЦИЯ ===========================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty;
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty;
            }

            // Event occurs on every new bar
            //Print("|{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}|{10}|{11}|{12}| - {13}", _wprInd.MainIndicatorSeries[Bars.Range.To-1],_wprInd.MainIndicatorSeries[Bars.Range.To-2],_wprInd.MainIndicatorSeries[Bars.Range.To-3],_wprInd.MainIndicatorSeries[Bars.Range.To-4],_wprInd.MainIndicatorSeries[Bars.Range.To-5],_wprInd.MainIndicatorSeries[Bars.Range.To-6],_wprInd.MainIndicatorSeries[Bars.Range.To-7],_wprInd.MainIndicatorSeries[Bars.Range.To-8],_wprInd.MainIndicatorSeries[Bars.Range.To-9],_wprInd.MainIndicatorSeries[Bars.Range.To-10],_wprInd.MainIndicatorSeries[Bars.Range.To-11],_wprInd.MainIndicatorSeries[Bars.Range.To-12],_wprInd.MainIndicatorSeries[Bars.Range.To-13], Bars[Bars.Range.To-1].Time);
            //Print("ZigZag : {0} - {1} -  {2}", _wprInd.MainIndicatorSeries[Bars.Range.To-2],Bars[Bars.Range.To-2].Time,Bars.Range.To-2);
            _wprInd.ReInit();
            if (_wprInd.MainIndicatorSeries[Bars.Range.To - 2] > 0)
            {             // Всі точки перегину зиззага
                zz3  = zz2;        zz2 = zz1;    zz1 = _wprInd.MainIndicatorSeries[Bars.Range.To - 2];
                zzi3 = zzi2;      zzi2 = zzi1;  zzi1 = Bars.Range.To - 2;
                Print("{0} {1} {2} -- {3}", zz3, zz2, zz1, Bars[Bars.Range.To - 2].Time);

                /*
                 * var toolVerticalLine=Tools.Create<VerticalLine>();
                 * toolVerticalLine.Time = Bars[Bars.Range.To-2].Time;
                 *      toolVerticalLine.Color=Color.Yellow;
                 */

                if (Bars[zzd2].Low > Bars[zzi1].Low && Bars[zzd2].High < Bars[zzd1].High && Bars[zzi1].High < Bars[zzd1].High)
                {
                    var toolVerticalLine = Tools.Create <VerticalLine>();
                    toolVerticalLine.Time  = Bars[zzd1].Time;
                    toolVerticalLine.Color = Color.Red;
                    if (posGuidSell == Guid.Empty)
                    {
                        var result1 = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.SellLimit, 0.1, Bars[zzd1].High - Instrument.Spread - kor, 0, Stops.InPips(SL, TP), null, null, null);
                        if (result1.IsSuccessful)
                        {
                            posGuidSell = result1.Position.Id;
                        }
                    }
                    if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Pending)
                    {
                        var res = Trade.CancelPendingPosition(posGuidSell);
                        if (res.IsSuccessful)
                        {
                            posGuidSell = Guid.Empty;
                            var result1 = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.SellLimit, 0.1, Bars[zzd1].High - Instrument.Spread - kor, 0, Stops.InPips(SL, TP), null, null, null);
                            if (result1.IsSuccessful)
                            {
                                posGuidSell = result1.Position.Id;
                            }
                        }
                    }
                }

                if (Bars[zzd2].High <Bars[zzi1].High && Bars[zzd2].Low> Bars[zzd1].Low && Bars[zzi1].Low > Bars[zzd1].Low)
                {
                    var toolVerticalLine = Tools.Create <VerticalLine>();
                    toolVerticalLine.Time  = Bars[zzd2].Time;
                    toolVerticalLine.Color = Color.Blue;
                    if (posGuidBuy == Guid.Empty)
                    {
                        var result = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.BuyLimit, 0.1, Bars[zzd1].Low + Instrument.Spread + kor, 0, Stops.InPips(SL, TP), null, null, null);
                        if (result.IsSuccessful)
                        {
                            posGuidBuy = result.Position.Id;
                        }
                    }
                    if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Pending)
                    {
                        var res = Trade.CancelPendingPosition(posGuidBuy);
                        if (res.IsSuccessful)
                        {
                            posGuidBuy = Guid.Empty;
                            var result1 = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.BuyLimit, 0.1, Bars[zzd1].Low + Instrument.Spread + kor, 0, Stops.InPips(SL, TP), null, null, null);
                            if (result1.IsSuccessful)
                            {
                                posGuidBuy = result1.Position.Id;
                            }
                        }
                    }
                }

//if(Bars[zzd1].Close>_wprInd.MainIndicatorSeries[zzd1] && zz3>zz2 && zz2<zz1 && up)  {
                if (zz3 > zz2 && zz2 < zz1 && up)
                {
                    up = false; zzd3 = zzd2; zzd2 = zzd1; zzd1 = zzi2; zzU = _wprInd.MainIndicatorSeries[zzd1];

/*				    var toolVerticalLine=Tools.Create<VerticalLine>();
 *                              toolVerticalLine.Time = Bars[zzi2].Time;
 *                                      toolVerticalLine.Color=Color.Red;
 */
//Print("zzU={0} Bars[zzd1]={1} -- {2} -- {3}",zzU,Bars[zzd1].Close,Bars[zzd1].Time,Bars[zzd1].Close<zzU);
                }

//if(Bars[zzd1].Close<_wprInd.MainIndicatorSeries[zzd1] && zz3<zz2 && zz2>zz1 && !up) {
                if (zz3 < zz2 && zz2 > zz1 && !up)
                {
                    up = true;  zzd3 = zzd2; zzd2 = zzd1; zzd1 = zzi2; zzD = _wprInd.MainIndicatorSeries[zzd1];

/*					var toolVerticalLine=Tools.Create<VerticalLine>();
 *                              toolVerticalLine.Time = Bars[zzi2].Time;
 *                                      toolVerticalLine.Color=Color.Blue;
 */
//Print("zzD={0} Bars[zzd1]={1} -- {2} -- {3}",zzD,Bars[zzd1].Close,Bars[zzd1].Time,Bars[zzd1].Close>zzD);
                }
            }
            //Print("|{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}|{10}|{11}|{12}| - {13}", _wprInd.MainIndicatorSeries[Bars.Range.To-1],_wprInd.MainIndicatorSeries[Bars.Range.To-2],_wprInd.MainIndicatorSeries[Bars.Range.To-3],_wprInd.MainIndicatorSeries[Bars.Range.To-4],_wprInd.MainIndicatorSeries[Bars.Range.To-5],_wprInd.MainIndicatorSeries[Bars.Range.To-6],_wprInd.MainIndicatorSeries[Bars.Range.To-7],_wprInd.MainIndicatorSeries[Bars.Range.To-8],_wprInd.MainIndicatorSeries[Bars.Range.To-9],_wprInd.MainIndicatorSeries[Bars.Range.To-10],_wprInd.MainIndicatorSeries[Bars.Range.To-11],_wprInd.MainIndicatorSeries[Bars.Range.To-12],_wprInd.MainIndicatorSeries[Bars.Range.To-13], Bars[Bars.Range.To-1].Time);
        }
Exemplo n.º 14
0
//===============================================================================================================================
        protected override void NewBar()
        {
            DTime = Bars[Bars.Range.To - 1].Time;
            ci    = Bars.Range.To - 1;
            dlt   = dl * Instrument.Point;
//=========  Рисуем линию  начала торгов Европы =============================================================================
            if (DTime.Hour == 7 && DTime.Minute == 00)
            {
                var vl1 = Tools.Create <VerticalLine>(); vl1.Time = Bars[Bars.Range.To - 1].Time; vl1.Color = Color.Aqua; vl1.Width = 3;
            }
            m5F = _ftoInd.FisherSeries[Bars.Range.To - 1];
//======================================================================================================================================
            //Значение счетчика прохода фишера через 0  FU - вверх  FD - вниз
            if (_ftoInd.FisherSeries[Bars.Range.To - 2] < 0 && _ftoInd.FisherSeries[Bars.Range.To - 1] > 0)
            {
                isFU = true;
            }
            else
            {
                isFU = false;
            }
            if (_ftoInd.FisherSeries[Bars.Range.To - 2] > 0 && _ftoInd.FisherSeries[Bars.Range.To - 1] < 0)
            {
                isFD = true;
            }
            else
            {
                isFD = false;
            }
            // isFMU isFMD - синяя линия пересекла линию фишера
            if (_ftoInd.FisherSeries[Bars.Range.To - 2] < _ftoInd.Ma1Series[Bars.Range.To - 2] && _ftoInd.FisherSeries[Bars.Range.To - 1] > _ftoInd.Ma1Series[Bars.Range.To - 1])
            {
                isFMU = true;
            }
            else
            {
                isFMU = false;
            }
            if (_ftoInd.FisherSeries[Bars.Range.To - 2] > _ftoInd.Ma1Series[Bars.Range.To - 2] && _ftoInd.FisherSeries[Bars.Range.To - 1] < _ftoInd.Ma1Series[Bars.Range.To - 1])
            {
                isFMD = true;
            }
            else
            {
                isFMD = false;
            }
            // isFM2U isFM2D - красная линия пересекла линию фишера
            if (_ftoInd.FisherSeries[Bars.Range.To - 2] < _ftoInd.Ma2Series[Bars.Range.To - 2] && _ftoInd.FisherSeries[Bars.Range.To - 1] > _ftoInd.Ma2Series[Bars.Range.To - 1])
            {
                isFM2U = true;
            }
            else
            {
                isFM2U = false;
            }
            if (_ftoInd.FisherSeries[Bars.Range.To - 2] > _ftoInd.Ma2Series[Bars.Range.To - 2] && _ftoInd.FisherSeries[Bars.Range.To - 1] < _ftoInd.Ma2Series[Bars.Range.To - 1])
            {
                isFM2D = true;
            }
            else
            {
                isFM2D = false;
            }

//====================================================================================================================================
            posActiveMineB = Trade.GetActivePositions(mgB, true);
            if (posActiveMineB != null && posActiveMineB.Length > 0 && posGuidBuy != posActiveMineB[0].Id)
            {
                posGuidBuy = posActiveMineB[0].Id;
            }
            posActiveMineS = Trade.GetActivePositions(mgS, true);
            if (posActiveMineS != null && posActiveMineS.Length > 0 && posGuidSell != posActiveMineS[0].Id)
            {
                posGuidSell = posActiveMineS[0].Id;
            }
//=== КОРЕКЦИЯ =======================================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                OP  = Trade.GetPosition(posGuidBuy).OpenPrice;
                CP  = Trade.GetPosition(posGuidBuy).ClosePrice.Value;
                TPB = Math.Round((CP - OP) * Instrument.LotSize - Instrument.Spread, 0) - 16;
                Print("Корекция {0} - Open={1} Close={2} TPB={3}", DTime, OP, CP, TPB);
                posGuidBuy = Guid.Empty;
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                OP  = Trade.GetPosition(posGuidSell).OpenPrice;
                CP  = Trade.GetPosition(posGuidSell).ClosePrice.Value;
                TPS = Math.Round((OP - CP) * Instrument.LotSize + Instrument.Spread, 0) - 16;
                Print("Корекция {0} - Open={1} Close={2} TPS={3}", DTime, OP, CP, TPS);
                posGuidSell = Guid.Empty;
            }
//==== Торги =========================================================================================================================
            if (DTime.Hour < 6)
            {
                torg = true; k = 0; tr = false; TPB = 0.0; TPS = 0.0;
            }
//==== Торги =========================================================================================================================
            if (DTime.Hour == 7 && DTime.Minute == 5)
            {
                Print("{0} - {1}  {2} - {3}", DTime, m5F, tmM1, m1F);
                if (posGuidBuy == Guid.Empty && m5F < 0 && m1F < 0)
                {
                    var result107 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1,
                                                             Stops.InPips(SL1, null), null, null);
                    if (result107.IsSuccessful)
                    {
                        posGuidBuy = result107.Position.Id;
                    }
                }

                if (posGuidSell == Guid.Empty && m5F > 0 && m1F > 0)
                {
                    var result207 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1,
                                                             Stops.InPips(SL1, null), null, null);
                    if (result207.IsSuccessful)
                    {
                        posGuidSell = result207.Position.Id;
                    }
                }
            }
//===================================================================================================================
// Если 15:00 по Киеву - закрыть все ордера
            if (DTime.Hour > 10 && DTime.Minute > 30)
            {
                if (posGuidBuy != Guid.Empty)
                {
                    OP  = Trade.GetPosition(posGuidBuy).OpenPrice;
                    CP  = Trade.GetPosition(posGuidBuy).ClosePrice.Value;
                    TPB = Math.Round((CP - OP) * Instrument.LotSize - Instrument.Spread, 0) - 16;
                    Print("12:00 {0} - Open={1} Close={2} TPB={3}", DTime, OP, CP, TPB);

                    var res1 = Trade.CloseMarketPosition(posGuidBuy); if (res1.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }

                if (posGuidSell != Guid.Empty)
                {
                    OP  = Trade.GetPosition(posGuidSell).OpenPrice;
                    CP  = Trade.GetPosition(posGuidSell).ClosePrice.Value;
                    TPS = Math.Round((OP - CP) * Instrument.LotSize + Instrument.Spread, 0) - 16;
                    Print("12:00 {0} - Open={1} Close={2} TPS={3}", DTime, OP, CP, TPS);
                    var res2 = Trade.CloseMarketPosition(posGuidSell); if (res2.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
            }
//===================================================================================================================
            if (DTime.Hour == 12)
            {
                Print("12:05 {0} - TPB={1} TPS={2} {3} {4}", DTime, TPB, TPS, posGuidBuy, posGuidSell);
                if (posGuidBuy == Guid.Empty && TPB < -100)
                {
                    var result107 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1,
                                                             Stops.InPips(SL1, null), null, null);
                    if (result107.IsSuccessful)
                    {
                        posGuidBuy = result107.Position.Id;
                    }
                }
                if (posGuidSell == Guid.Empty && TPS < -100)
                {
                    var result207 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1,
                                                             Stops.InPips(SL1, null), null, null);
                    if (result207.IsSuccessful)
                    {
                        posGuidSell = result207.Position.Id;
                    }
                }
            }
//===================================================================================================================
// Если 15:00 по Киеву - закрыть все ордера
            if (DTime.Hour == 20)
            {
                if (posGuidBuy != Guid.Empty)
                {
                    var res1 = Trade.CloseMarketPosition(posGuidBuy); if (res1.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }

                if (posGuidSell != Guid.Empty)
                {
                    var res2 = Trade.CloseMarketPosition(posGuidSell); if (res2.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
            }
//===================================================================================================================
// Если фишер пересек красную линию - закрыть все ордера
            if (FU)
            {
                if (posGuidBuy != Guid.Empty && isFM2D)
                {
                    TPB = Trade.GetPosition(posGuidBuy).Pips; Print("Fisher {0} - TPB={2}", DTime, TPB);
                    var res1 = Trade.CloseMarketPosition(posGuidBuy); if (res1.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
                if (posGuidSell != Guid.Empty && isFM2U)
                {
                    TPS = Trade.GetPosition(posGuidSell).Pips; Print("Fisher {0} - TPS={2}", DTime, TPS);
                    var res2 = Trade.CloseMarketPosition(posGuidSell); if (res2.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
            }

//===================================================================================================================

            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).Pips > 50)
            {
                tr = true;
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).Pips > 50)
            {
                tr = true;
            }
            if (tr)
            {
                TrailActiveOrders();
            }
        }
Exemplo n.º 15
0
//===============================================================================================================================
        protected override void NewBar()
        {
            _wprInd.ReInit();
//=== КОРЕКЦИЯ ===========================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty;
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty;
            }

//=== Закрытие всех ордеров если пятница 16:00 (19:00 Kiev) ===========================================================================
            if (Bars[Bars.Range.To - 1].Time.DayOfWeek == DayOfWeek.Friday && Bars[Bars.Range.To - 1].Time.Hour == 16)
            {
                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Active)
                {
                    var res = Trade.CloseMarketPosition(posGuidBuy); if (res.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Active)
                {
                    var res = Trade.CloseMarketPosition(posGuidSell); if (res.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }

                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Pending)
                {
                    var res = Trade.CancelPendingPosition(posGuidBuy); if (res.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Pending)
                {
                    var res = Trade.CancelPendingPosition(posGuidSell); if (res.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
            }
//======================================================================================================================================
            if (_wprInd.MainIndicatorSeries[Bars.Range.To - 1] > 0)
            {
                zz3  = zz2;       zz2 = zz1;   zz1 = _wprInd.MainIndicatorSeries[Bars.Range.To - 1];
                zzi3 = zzi2;      zzi2 = zzi1;  zzi1 = Bars.Range.To - 1;

                if (zz3 < zz2 && zz2 > zz1)
                {                 // ВВЕРХУ
                    zzd4 = zzd3; zzd3 = zzd2; zzd2 = zzd1; zzd1 = zzi2;
                    zzu4 = zzu3; zzu3 = zzu2; zzu2 = zzu1; zzu1 = zz2;
                    Print("ВВЕРХ {0} 1={1} 2={2} 3={3} 4={4}", Bars[zzd1].Time, zzu1, zzu2, zzu3, zzu4);
                    if (zzu1 > zzu2 && zzu3 > zzu2 && zzu1 > zzu3 && zzu2 > zzu4 && zzu3 > zzu4)          // ВВЕРХУ
                    {
                        var vr = Tools.Create <VerticalLine>(); vr.Color = Color.Red; vr.Time = Bars[zzd1].Time;
                        if (posGuidSell == Guid.Empty)
                        {
                            var result1 = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.SellLimit, 0.1, zzu1 + Instrument.Spread - 0.0002, 0, Stops.InPips(200, 100), null, null, null);
                            if (result1.IsSuccessful)
                            {
                                posGuidSell = result1.Position.Id; zzts2 = zzu2;
                            }
                        }
                    }
                }

                if (zz3 > zz2 && zz2 < zz1)
                {                 // ВНИЗУ
                    zzd4 = zzd3; zzd3 = zzd2; zzd2 = zzd1; zzd1 = zzi2;
                    zzu4 = zzu3; zzu3 = zzu2; zzu2 = zzu1; zzu1 = zz2;
                    Print("НИЗ {0} 1={1} 2={2} 3={3} 4={4}", Bars[zzd1].Time, zzu1, zzu2, zzu3, zzu4);
                    if (zzu4 > zzu2 && zzu3 > zzu1 && zzu2 > zzu3 && zzu4 > zzu3)            //  ВНИЗУ
                    {
                        var vr = Tools.Create <VerticalLine>(); vr.Color = Color.Blue; vr.Time = Bars[zzd1].Time;
                        if (posGuidBuy == Guid.Empty)
                        {
                            var result = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.BuyLimit, 0.1, zzu1 - Instrument.Spread + 0.0002, 0, Stops.InPips(200, 100), null, null, null);
                            if (result.IsSuccessful)
                            {
                                posGuidBuy = result.Position.Id; zztb2 = zzu2;
                            }
                        }
                    }
                }

                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Pending && Bars[Bars.Range.To - 1].Low < zzts2)
                {
                    var res = Trade.CancelPendingPosition(posGuidSell);
                    if (res.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Pending && Bars[Bars.Range.To - 1].High > zztb2)
                {
                    var res = Trade.CancelPendingPosition(posGuidBuy);
                    if (res.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
            }
        }
Exemplo n.º 16
0
//===============================================================================================================================
        protected override void NewBar()
        {
            _wprInd.ReInit();


//=== КОРЕКЦИЯ ===========================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty;
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty;
            }

// Значение счетчика прохода фишера через 0  FU - вверх  FD - вниз
//		    if ( _ftoInd.FisherSeries[Bars.Range.To-2]<0  &&  _ftoInd.FisherSeries[Bars.Range.To-1]>0)  isFU=true;  else isFU=false;
//			if ( _ftoInd.FisherSeries[Bars.Range.To-2]>0  &&  _ftoInd.FisherSeries[Bars.Range.To-1]<0)   isFD=true;  else isFD=false;

//========  isFM2U isFM2D - красная линия пересекла линию фишера
            // Sell stop <0
            if (_ftoInd.FisherSeries[Bars.Range.To - 2] < _ftoInd.Ma2Series[Bars.Range.To - 2] && _ftoInd.FisherSeries[Bars.Range.To - 1] > _ftoInd.Ma2Series[Bars.Range.To - 1])
            {
                isFU = true;
            }
            else
            {
                isFU = false;
            }
            //  Buy stop >0
            if (_ftoInd.FisherSeries[Bars.Range.To - 2] > _ftoInd.Ma2Series[Bars.Range.To - 2] && _ftoInd.FisherSeries[Bars.Range.To - 1] < _ftoInd.Ma2Series[Bars.Range.To - 1])
            {
                isFD = true;
            }
            else
            {
                isFD = false;
            }

//=== Закрытие всех ордеров если пятница 16:00 (19:00 Kiev) ===========================================================================
            if (Bars[Bars.Range.To - 1].Time.DayOfWeek == DayOfWeek.Friday && Bars[Bars.Range.To - 1].Time.Hour == 16)
            {
                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Active)
                {
                    var res = Trade.CloseMarketPosition(posGuidBuy); if (res.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Active)
                {
                    var res = Trade.CloseMarketPosition(posGuidSell); if (res.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }

                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Pending)
                {
                    var res = Trade.CancelPendingPosition(posGuidBuy); if (res.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Pending)
                {
                    var res = Trade.CancelPendingPosition(posGuidSell); if (res.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
            }
//=== Если фишер пересек линию - закрыть все ордера  =================================================================================
            if (isFD)
            {
                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Active)
                {
                    var res1 = Trade.CloseMarketPosition(posGuidBuy); if (res1.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Pending)
                {
                    var res2 = Trade.CancelPendingPosition(posGuidBuy); if (res2.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
            }
            if (isFU)
            {
                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Active)
                {
                    var res3 = Trade.CloseMarketPosition(posGuidSell); if (res3.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Pending)
                {
                    var res4 = Trade.CancelPendingPosition(posGuidSell); if (res4.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
            }
//======================================================================================================================================
            if (_wprInd.MainIndicatorSeries[Bars.Range.To - 1] > 0)
            {
                zz3  = zz2;       zz2 = zz1;   zz1 = _wprInd.MainIndicatorSeries[Bars.Range.To - 1];
                zzi3 = zzi2;      zzi2 = zzi1;  zzi1 = Bars.Range.To - 1;

                if (zz3 < zz2 && zz2 > zz1)
                {                 // ВВЕРХУ
                    zzd4 = zzd3; zzd3 = zzd2; zzd2 = zzd1; zzd1 = zzi2;
                    zzu4 = zzu3; zzu3 = zzu2; zzu2 = zzu1; zzu1 = zz2;
                    Print("ВВЕРХ {0} 1={1} 2={2} 3={3} 4={4}", Bars[zzd1].Time, zzu1, zzu2, zzu3, zzu4);
                    if (zzu1 > zzu2 && zzu3 > zzu2 && zzu1 > zzu3 && zzu2 > zzu4 && zzu3 > zzu4)          // ВВЕРХУ
                    {
                        var vr = Tools.Create <VerticalLine>(); vr.Color = Color.Red; vr.Time = Bars[zzd1].Time;
                        if (posGuidSell == Guid.Empty)
                        {
                            var result1 = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.SellLimit, 0.1, zzu1 + Instrument.Spread - 0.0002, 0, Stops.InPips(200, 100), null, null, null);
                            // var result1 = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.SellLimit, 0.1,  zzu1+Instrument.Spread-0.0002, 0, Stops.InPips(200,null), null, null, null);
                            if (result1.IsSuccessful)
                            {
                                posGuidSell = result1.Position.Id; zzts2 = zzu2;
                            }
                        }
                    }
                }

                if (zz3 > zz2 && zz2 < zz1)
                {                 // ВНИЗУ
                    zzd4 = zzd3; zzd3 = zzd2; zzd2 = zzd1; zzd1 = zzi2;
                    zzu4 = zzu3; zzu3 = zzu2; zzu2 = zzu1; zzu1 = zz2;
                    Print("НИЗ {0} 1={1} 2={2} 3={3} 4={4}", Bars[zzd1].Time, zzu1, zzu2, zzu3, zzu4);
                    if (zzu4 > zzu2 && zzu3 > zzu1 && zzu2 > zzu3 && zzu4 > zzu3)            //  ВНИЗУ
                    {
                        var vr = Tools.Create <VerticalLine>(); vr.Color = Color.Blue; vr.Time = Bars[zzd1].Time;
                        if (posGuidBuy == Guid.Empty)
                        {
                            var result = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.BuyLimit, 0.1, zzu1 - Instrument.Spread + 0.0002, 0, Stops.InPips(200, 100), null, null, null);
                            // var result = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.BuyLimit, 0.1, zzu1-Instrument.Spread+0.0002, 0, Stops.InPips(200,null), null, null, null);
                            if (result.IsSuccessful)
                            {
                                posGuidBuy = result.Position.Id; zztb2 = zzu2;
                            }
                        }
                    }
                }

                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Pending && Bars[Bars.Range.To - 1].Low < zzts2)
                {
                    var res = Trade.CancelPendingPosition(posGuidSell);
                    if (res.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Pending && Bars[Bars.Range.To - 1].High > zztb2)
                {
                    var res = Trade.CancelPendingPosition(posGuidBuy);
                    if (res.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
            }
        }
Exemplo n.º 17
0
//===========================================================================
        protected override void NewBar()
        {
            DTime = Bars[ci].Time;
            ci    = Bars.Range.To - 1;

            if (DTime.Day == 22 && DTime.Month == 6 && DTime.Hour == 0 && DTime.Minute == 00)
            {
                zmax = 1.16335; tu = true; td = false; nu = false; nd = false; frU1 = 0; frU2 = 0; frU3 = 0; Print("{0} UP", DTime);
            }
            if (DTime.Day == 12 && DTime.Month == 7 && DTime.Hour == 0 && DTime.Minute == 00)
            {
                zmin = 1.16651; td = true; tu = false; nu = false; nd = false; frD1 = 0; frD2 = 0; frD3 = 0; Print("{0} DW", DTime);
            }
            if (DTime.Day == 20 && DTime.Month == 7 && DTime.Hour == 0 && DTime.Minute == 00)
            {
                zmax = 1.16782; tu = true; td = false; nu = false; nd = false; frU1 = 0; frU2 = 0; frU3 = 0; Print("{0} UP", DTime);
            }
            if (DTime.Day == 27 && DTime.Month == 7 && DTime.Hour == 0 && DTime.Minute == 00)
            {
                zmin = 1.16378; td = true; tu = false; nu = false; nd = false; frD1 = 0; frD2 = 0; frD3 = 0; Print("{0} DW", DTime);
            }
            if (DTime.Day == 20 && DTime.Month == 8 && DTime.Hour == 0 && DTime.Minute == 00)
            {
                zmax = 1.14444; tu = true; td = false; nu = false; nd = false; frU1 = 0; frU2 = 0; frU3 = 0; Print("{0} UP", DTime);
            }
            if (DTime.Day == 3 && DTime.Month == 9 && DTime.Hour == 0 && DTime.Minute == 00)
            {
                zmin = 1.15842; td = true; tu = false; nu = false; nd = false; frD1 = 0; frD2 = 0; frD3 = 0; Print("{0} DW", DTime);
            }
            if (DTime.Day == 11 && DTime.Month == 9 && DTime.Hour == 0 && DTime.Minute == 00)
            {
                zmax = 1.16163; tu = true; td = false; nu = false; nd = false; frU1 = 0; frU2 = 0; frU3 = 0; Print("{0} UP", DTime);
            }
            if (DTime.Day == 27 && DTime.Month == 9 && DTime.Hour == 0 && DTime.Minute == 00)
            {
                zmin = 1.17092; td = true; tu = false; nu = false; nd = false; frD1 = 0; frD2 = 0; frD3 = 0; Print("{0} DW", DTime);
            }
            if (DTime.Day == 10 && DTime.Month == 10 && DTime.Hour == 0 && DTime.Minute == 00)
            {
                zmax = 1.15092; tu = true; td = false; nu = false; nd = false; frU1 = 0; frU2 = 0; frU3 = 0; Print("{0} UP", DTime);
            }

//=== КОРЕКЦИЯ =======================================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty;
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty;
            }
            if (posGuidBuy2 != Guid.Empty && Trade.GetPosition(posGuidBuy2).State == PositionState.Closed)
            {
                posGuidBuy2 = Guid.Empty;
            }
            if (posGuidSell2 != Guid.Empty && Trade.GetPosition(posGuidSell2).State == PositionState.Closed)
            {
                posGuidSell2 = Guid.Empty;
            }
//====  Fractal вверх ================================================================================================================
            if (_frInd.TopSeries[Bars.Range.To - frac] > 0)
            {
                if (frU)
                {
                    frU  = false;
                    frU3 = frU2; frU2 = frU1; frU1 = _frInd.TopSeries[Bars.Range.To - frac];
                    tmU3 = tmU2; tmU2 = tmU1; tmU1 = Bars[Bars.Range.To - frac].Time;
                }
                else
                {
                    if (_frInd.TopSeries[Bars.Range.To - frac] > frU1)
                    {
                        frU1 = _frInd.TopSeries[Bars.Range.To - frac];
                        tmU1 = Bars[Bars.Range.To - frac].Time;
                    }
                }
            }
//====  Fractal вниз ====================================================================================
            if (_frInd.BottomSeries[Bars.Range.To - frac] > 0)
            {
                if (!frU)
                {
                    frU  = true;
                    frD3 = frD2; frD2 = frD1; frD1 = _frInd.BottomSeries[Bars.Range.To - frac];
                    tmD3 = tmD2; tmD2 = tmD1; tmD1 = Bars[Bars.Range.To - frac].Time;
                }
                else
                {
                    if (_frInd.BottomSeries[Bars.Range.To - frac] < frD1)
                    {
                        frD1 = _frInd.BottomSeries[Bars.Range.To - frac];
                        tmD1 = Bars[Bars.Range.To - frac].Time;
                    }
                }
            }
//====================================================================================================================

            if (tu && Bars[ci].High > zmax)
            {
                zmax  = Bars[ci].High;
                tmax  = Bars[ci].Time;
                nkz4  = zmax - ((NKZ - 5 - (NKZ * kf)) * Instrument.Point);
                nkz2  = zmax - (((NKZ * 2) - (NKZ * 2 * kf)) * Instrument.Point);
                nkz4v = zmax - ((NKZ - 5) * Instrument.Point);
                nkz2v = zmax - ((NKZ * 2) * Instrument.Point);
                nkz4  = Math.Round(nkz4, Instrument.PriceScale);
                nkz2  = Math.Round(nkz2, Instrument.PriceScale);
                nkz4v = Math.Round(nkz4v, Instrument.PriceScale);
                nkz2v = Math.Round(nkz2v, Instrument.PriceScale);
                nd    = false; nu = false;
            }

            if (td && Bars[ci].Low < zmin)
            {
                zmin  = Bars[ci].Low;
                tmin  = Bars[ci].Time;
                nkz4  = zmin + ((NKZ - 5 - (NKZ * kf)) * Instrument.Point);
                nkz2  = zmin + (((NKZ * 2) - (NKZ * 2 * kf)) * Instrument.Point);
                nkz4v = zmin + ((NKZ - 5) * Instrument.Point);
                nkz2v = zmin + (((NKZ * 2)) * Instrument.Point);
                nkz4  = Math.Round(nkz4, Instrument.PriceScale);
                nkz2  = Math.Round(nkz2, Instrument.PriceScale);
                nkz4v = Math.Round(nkz4v, Instrument.PriceScale);
                nkz2v = Math.Round(nkz2v, Instrument.PriceScale);
                nd    = false; nu = false;
            }
//== Касание зоны/1\2 =============================================================================================
            if (n4)
            {
                if (tu && Bars[ci].Low < nkz4 && !nu)
                {
                    nu = true;
                }
                if (td && Bars[ci].High > nkz4 && !nd)
                {
                    nd = true;
                }
            }
            else
            {
                if (tu && Bars[ci].Low < nkz2 && !nu)
                {
                    nu = true;
                }
                if (td && Bars[ci].High > nkz2 && !nd)
                {
                    nd = true;
                }
            }
// Если пересечение зоны было (nu) и торгуем вверх (tu) ===========================================================
            if (nu && tu)
            {
// Определение патерна   ПИК ПОСЛЕДНЕГО ФРАКТАЛА ПАТЕРНА ВНИЗУ
                if (_frInd.TopSeries[Bars.Range.To - frac] > 0)
                {
                    if (frU3 > frU2 && frU1 > frU2)
                    {
                        TPU  = zmax;
                        SLU  = frD2 - Instrument.Spread - dlt; SLU = Math.Round(SLU, Instrument.PriceScale);
                        PRU  = Math.Round((TPU - Bars[ci].Close) * Math.Pow(10, Instrument.PriceScale), 0);
                        PRU2 = Math.Round((TP2 - Bars[ci].Close) * Math.Pow(10, Instrument.PriceScale), 0);
                        UBU  = Math.Round((Bars[ci].Close - SLU) * Math.Pow(10, Instrument.PriceScale), 0);
                        PCU  = Math.Round(PRU / UBU, 2);
                        PCU2 = Math.Round(PRU2 / UBU, 2);
                        Ot   = Math.Round((frU3 - frU2) / (frU1 - frU2), 2);
                        PRUi = (int)PRU;
                        UBUi = (int)UBU;
                        //UBUi=(int)SL1;
                        if (UBU > 40 && UBU < SL1 && Bars[ci].Close > nkz2 && PCU > 2)
                        {
                            if (posGuidBuy != Guid.Empty && posGuidBuy2 == Guid.Empty)
                            {
                                if (TP2 != 0)
                                {
                                    PRUi = PRUi2;
                                }
                                var res4 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1, Stops.InPips(UBUi, PRUi), null, null);
                                if (res4.IsSuccessful)
                                {
                                    posGuidBuy2 = res4.Position.Id;
                                }
                            }
                            if (posGuidBuy == Guid.Empty)
                            {
                                var res1 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1, Stops.InPips(UBUi, PRUi), null, null);
                                if (res1.IsSuccessful)
                                {
                                    posGuidBuy = res1.Position.Id;
                                }
                            }
                            frD = false;
                        }
                    }
                }
            }
// Если пересечение зоны было (nd) и торгуем вниз (td) ===========================================================
            if (nd && td)
            {
// Определение патерна   ПИК ПОСЛЕДНЕГО ФРАКТАЛА ПАТЕРНА ВВЕРХУ
                if (_frInd.BottomSeries[Bars.Range.To - frac] > 0)
                {
                    if (frD2 > frD3 && frD2 > frD1)
                    {
                        TPD  = Math.Round(zmin, Instrument.PriceScale);
                        SLD  = frU2 + Instrument.Spread + dlt; SLD = Math.Round(SLD, Instrument.PriceScale);
                        PRD  = Math.Round((Bars[ci].Close - TPD) * Math.Pow(10, Instrument.PriceScale), 0);
                        PRD2 = Math.Round((Bars[ci].Close - TP2) * Math.Pow(10, Instrument.PriceScale), 0);
                        Ot   = Math.Round((frD2 - frD3) / (frD2 - frD1), 2);
                        UBD  = Math.Round((SLD - Bars[ci].Close) * Math.Pow(10, Instrument.PriceScale), 0);

                        PRDi2 = (int)PRD2;
                        PRDi  = (int)PRD;
                        UBDi  = (int)UBD;
                        //UBDi=(int)SL1;
                        PCD  = Math.Round(PRD / UBD, 2);
                        PCD2 = Math.Round(PRD2 / UBD, 2);
                        if (UBD > 40 && UBD < SL1 && Bars[ci].Close < nkz2 && PCD > 2)
                        {
                            if (posGuidSell != Guid.Empty && posGuidSell2 == Guid.Empty)
                            {
                                if (TP2 != 0)
                                {
                                    PRDi = PRDi2;
                                }
                                var res3 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1, Stops.InPips(UBDi, PRDi), null, null);
                                if (res3.IsSuccessful)
                                {
                                    posGuidSell2 = res3.Position.Id;
                                }
                            }
                            if (posGuidSell == Guid.Empty)
                            {
                                var res2 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1, Stops.InPips(UBDi, PRDi), null, null);
                                if (res2.IsSuccessful)
                                {
                                    posGuidSell = res2.Position.Id;
                                }
                            }
                            frU = false;
                        }
                    }     // if( frD2>frD3 && frD2>frD1 )
                } // if(_frInd.BottomSeries[Bars.Range.To-frac]>0)
            } //if(nd && td)
//===============================================================================================================================
            st = "";
            if (_frInd.TopSeries[Bars.Range.To - frac] > 0)
            {
                if (tu)        //Торгуем в Buy
                {
                    st = st + " tu --";
                    if (nu)            // Есть пересечение 1/4
                    {
                        st = st + "++";
                        if (frU3 > frU2 && frU1 > frU2)              //Патерн
                        {
                            if (UBU > 20 && UBU < SL1 && Bars[ci].Close > nkz2 && PCU > 2)
                            {
                                st = st + "@@ ";
                                if (posGuidBuy != Guid.Empty)
                                {
                                    st = st + "Buy1 ";
                                }
                                if (posGuidBuy2 != Guid.Empty)
                                {
                                    st = st + "Buy2 ";
                                }
                            }
                            else
                            {
                                st = st + "!! ";
                            }

                            st = st + "| SL=" + UBU.ToString() + " TP=" + PRU.ToString() + " %=" + PCU.ToString();
                        }
                    }
                    XXPrint("{1} | {0} | frU= {2}:{3}({4}) {5}:{6}({7}) {8}:{8}({10}) -- max={11}({13}) nkz4={12}", Bars[Bars.Range.To - 1].Time, st,
                            tmU3.Hour, tmU3.Minute, frU3, tmU2.Hour, tmU2.Minute, frU2, tmU1.Hour, tmU1.Minute, frU1, zmax, nkz4, tmax);
                }
            }
            if (_frInd.BottomSeries[Bars.Range.To - frac] > 0)
            {
                if (td)        //Торгуем в Buy
                {
                    st = st + " td --";
                    if (nd)            // Есть пересечение 1/4
                    {
                        st = st + "++";
                        if (frD2 > frD3 && frD2 > frD1)              //Патерн
                        {
                            if (UBD > 20 && UBD < SL1 && Bars[ci].Close < nkz2 && PCD > 2)
                            {
                                st = st + "@@ ";
                                if (posGuidSell != Guid.Empty)
                                {
                                    st = st + "Sell1 ";
                                }
                                if (posGuidSell2 != Guid.Empty)
                                {
                                    st = st + "Sell2 ";
                                }
                            }
                            else
                            {
                                st = st + "!! ";
                            }
                            st = st + "| SL=" + UBD.ToString() + " TP=" + PRD.ToString() + " %=" + PCD.ToString();
                        }
                    }
                    XXPrint("{1} | {0} | frD= {2}:{3}({4}) {5}:{6}({7}) {8}:{8}({10}) | min={11}({13}) nkz4={12}", Bars[Bars.Range.To - 1].Time, st,
                            tmD3.Hour, tmD3.Minute, frD3, tmD2.Hour, tmD2.Minute, frD2, tmD1.Hour, tmD1.Minute, frD1, zmin, nkz4, tmin);
                }
            }
        } // NewBar
Exemplo n.º 18
0
//===============================================================================================================================
        protected override void NewBar()
        {
//=== КОРЕКЦИЯ ===========================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty;
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty;
            }

//=== Закрытие всех ордеров если пятница 16:00 (19:00 Kiev) ===========================================================================
            if (Bars[Bars.Range.To - 1].Time.DayOfWeek == DayOfWeek.Friday && Bars[Bars.Range.To - 1].Time.Hour == 16)
            {
                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Active)
                {
                    var res = Trade.CloseMarketPosition(posGuidBuy); if (res.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Active)
                {
                    var res = Trade.CloseMarketPosition(posGuidSell); if (res.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }

                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Pending)
                {
                    var res = Trade.CancelPendingPosition(posGuidBuy); if (res.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Pending)
                {
                    var res = Trade.CancelPendingPosition(posGuidSell); if (res.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
            }
//======================================================================================================================================
            //Значение счетчика прохода фишера через 0  FU - вверх  FD - вниз
            if (_ftoInd.FisherSeries[Bars.Range.To - 2] < 0 && _ftoInd.FisherSeries[Bars.Range.To - 1] > 0)
            {
                FU = Bars.Range.To; isFU = true;
            }
            else
            {
                isFU = false;
            }
            if (_ftoInd.FisherSeries[Bars.Range.To - 2] > 0 && _ftoInd.FisherSeries[Bars.Range.To - 1] < 0)
            {
                FD = Bars.Range.To; isFD = true;
            }
            else
            {
                isFD = false;
            }
            // isFMU isFMD - синяя линия пересекла линию фишера
            if (_ftoInd.FisherSeries[Bars.Range.To - 2] < _ftoInd.Ma1Series[Bars.Range.To - 2] && _ftoInd.FisherSeries[Bars.Range.To - 1] > _ftoInd.Ma1Series[Bars.Range.To - 1])
            {
                isFMU = true;
            }
            else
            {
                isFMU = false;
            }
            if (_ftoInd.FisherSeries[Bars.Range.To - 2] > _ftoInd.Ma1Series[Bars.Range.To - 2] && _ftoInd.FisherSeries[Bars.Range.To - 1] < _ftoInd.Ma1Series[Bars.Range.To - 1])
            {
                isFMD = true;
            }
            else
            {
                isFMD = false;
            }
            // isFM2U isFM2D - красная линия пересекла линию фишера
            if (_ftoInd.FisherSeries[Bars.Range.To - 2] < _ftoInd.Ma2Series[Bars.Range.To - 2] && _ftoInd.FisherSeries[Bars.Range.To - 1] > _ftoInd.Ma2Series[Bars.Range.To - 1])
            {
                isFM2U = true;
            }
            else
            {
                isFM2U = false;
            }
            if (_ftoInd.FisherSeries[Bars.Range.To - 2] > _ftoInd.Ma2Series[Bars.Range.To - 2] && _ftoInd.FisherSeries[Bars.Range.To - 1] < _ftoInd.Ma2Series[Bars.Range.To - 1])
            {
                isFM2D = true;
            }
            else
            {
                isFM2D = false;
            }

            // Если красная пересекла линию - закрыть все активные ордера
            if (isFM2U)
            {
                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Active)
                {
                    var res3 = Trade.CloseMarketPosition(posGuidSell); if (res3.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
            }
            if (isFM2D)
            {
                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Active)
                {
                    var res1 = Trade.CloseMarketPosition(posGuidBuy); if (res1.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
            }

            // Если фишер пересек 0 линию - закрыть все ордера
            if (isFD)
            {
                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Active)
                {
                    var res1 = Trade.CloseMarketPosition(posGuidBuy); if (res1.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Pending)
                {
                    var res2 = Trade.CancelPendingPosition(posGuidBuy); if (res2.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
            }
            if (isFU)
            {
                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Active)
                {
                    var res3 = Trade.CloseMarketPosition(posGuidSell); if (res3.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Pending)
                {
                    var res4 = Trade.CancelPendingPosition(posGuidSell); if (res4.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
            }

//======================================================================================================================================

            if (F15U)
            {
                if (_ftoInd.FisherSeries[Bars.Range.To - 1] > 0 && _ftoInd.FisherSeries[Bars.Range.To - 2] < 0)
                {
                    var vline = Tools.Create <VerticalLine>();
                    vline.Color = Color.Blue;
                    vline.Time  = Bars[Bars.Range.To - 1].Time;
                    vline.Width = 4;
                    if (posGuidBuy == Guid.Empty)
                    {
                        var result3 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Ask, -1, Stops.InPips(200, 600), null, null);
                        if (result3.IsSuccessful)
                        {
                            posGuidBuy = result3.Position.Id;
                        }
                    }
                }
            }
            if (!F15U)
            {
                if (_ftoInd.FisherSeries[Bars.Range.To - 1] < 0 && _ftoInd.FisherSeries[Bars.Range.To - 2] > 0)
                {
                    var vline = Tools.Create <VerticalLine>();
                    vline.Color = Color.Red;
                    vline.Time  = Bars[Bars.Range.To - 1].Time;
                    vline.Width = 4;
                    if (posGuidSell == Guid.Empty)
                    {
                        var result2 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Bid, -1, Stops.InPips(200, 600), null, null);
                        if (result2.IsSuccessful)
                        {
                            posGuidSell = result2.Position.Id;
                        }
                    }
                }
            }
//===============================================================================================================================
        }
Exemplo n.º 19
0
        protected override void Init()
        {
            k = 0;
            var result = Trade.OpenMarketPosition(Instrument.Id,
                                                  ExecutionRule.Buy, 0.1,
                                                  Instrument.Bid, -1,
                                                  Stops.InPips(800, 800), "Buy", magicNumber);

            Print("1 Покупка !!! - {0} - {1}", result.Position.Comment, result.Position.MagicNumber);



            if (Instrument.Name == "EURUSD")
            {
                NKZ = 462; mgB = 101; mgS = 201;
            }
            if (Instrument.Name == "GBPUSD")
            {
                NKZ = 792; mgB = 102; mgS = 202;
            }
            if (Instrument.Name == "AUDUSD")
            {
                NKZ = 343; mgB = 103; mgS = 203;
            }
            if (Instrument.Name == "NZDUSD")
            {
                NKZ = 357; mgB = 104; mgS = 204;
            }
            if (Instrument.Name == "USDJPY")
            {
                NKZ = 501; mgB = 105; mgS = 205;
            }
            if (Instrument.Name == "USDCAD")
            {
                NKZ = 500; mgB = 106; mgS = 206;
            }
            if (Instrument.Name == "USDCHF")
            {
                NKZ = 557; mgB = 107; mgS = 207;
            }
            if (Instrument.Name == "AUDJPY")
            {
                NKZ = 550; mgB = 108; mgS = 208;
            }
            if (Instrument.Name == "AUDNZD")
            {
                NKZ = 412; mgB = 109; mgS = 209;
            }
            if (Instrument.Name == "CHFJPY")
            {
                NKZ = 1430; mgB = 110; mgS = 210;
            }
            if (Instrument.Name == "EURAUD")
            {
                NKZ = 682; mgB = 111; mgS = 211;
            }
            if (Instrument.Name == "AUDCAD")
            {
                NKZ = 357; mgB = 112; mgS = 212;
            }
            if (Instrument.Name == "EURCAD")
            {
                NKZ = 762; mgB = 113; mgS = 213;
            }
            if (Instrument.Name == "EURCHF")
            {
                NKZ = 539; mgB = 114; mgS = 214;
            }
            if (Instrument.Name == "EURGBP")
            {
                NKZ = 484; mgB = 115; mgS = 215;
            }
            if (Instrument.Name == "EURJPY")
            {
                NKZ = 715; mgB = 116; mgS = 216;
            }
            if (Instrument.Name == "GBPCHF")
            {
                NKZ = 924; mgB = 117; mgS = 217;
            }
            if (Instrument.Name == "GBPJPY")
            {
                NKZ = 1045; mgB = 118; mgS = 218;
            }



            var posActiveMine = Trade.GetActivePositions(7, true);

            if (posActiveMine != null)
            {
                for (int p = posActiveMine.Length - 1; p >= 0; p--)
                {
                    if ((int)posActiveMine[p].Type == (int)ExecutionRule.Buy)
                    {
                        ActiveBuyCount++;
                        posGuidBuy = posActiveMine[p].Id;
                    }
                    if ((int)posActiveMine[p].Type == (int)ExecutionRule.Sell)
                    {
                        ActiveSellCount++;
                        posGuidSell = posActiveMine[p].Id;
                    }
                }
            }



            //if(posActiveMine.Length>0 && posActiveMine[0].Comment.EndsWith("Buy")) posGuidBuy=posActiveMine[0].Id;
            //if(posActiveMine.Length>0 && posActiveMine[0].Comment.EndsWith("Sell")) posGuidSell=posActiveMine[0].Id;
            //if(posActiveMine.Length>1 && posActiveMine[1].Comment.EndsWith("Buy")) posGuidBuy=posActiveMine[1].Id;
            //if(posActiveMine.Length>1 && posActiveMine[1].Comment.EndsWith("Sell")) posGuidSell=posActiveMine[1].Id;
            // Print("OK!!!! - Buy={0} Sell={1}",ActiveBuyCount,ActiveSellCount);
            // Print("Результат - {0} - {1}",posActiveMine[0].InstrumentId,posActiveMine[0].Id);
            //if(posActiveMine[0].Comment.Equals("Buy")) Print("OK!!!!!");
        }
Exemplo n.º 20
0
        protected override void NewBar()
        {
            // =================================================================================================================================
            DTime = Bars[Bars.Range.To - 1].Time;
            // Event occurs on every new bar
            if (DTime.Hour == 18 && DTime.Minute == 0)
            { // 1. Измеряем высоту диапазона в промежутке с 9 до 18 часов по GMT.
                var highestIndex = Series.Highest(Bars.Range.To, 9, PriceMode.High);
                var highestPrice = Bars[highestIndex].High;
                var lowestIndex  = Series.Lowest(Bars.Range.To, 9, PriceMode.Low);
                var lowestPrice  = Bars[lowestIndex].Low;

                // 2. После 18:00 выставляем 2 ордера: Buy Limit и Sell Limit на расстоянии Х пунктов от текущей цены.
                x  = Math.Round(((highestPrice - lowestPrice) / 2), 5);
                xp = (int)(x * 100000);
                // 5. Если диапазон с 9 до 18 часов равен или превышает 600 пунктов, — НЕ ТОРГУЕМ.
                if (xp < 300)
                {
                    // 3. Тейк-профит каждого ордера равен 2Х, стоп-лосс = X+5 пунктов.
                    var result = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.BuyLimit, 0.1, Instrument.Bid - x, 0, Stops.InPips(xp + 50, 2 * xp), null, null, null);
                    if (result.IsSuccessful)
                    {
                        posGuidBuy = result.Position.Id;
                    }
                    var result1 = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.SellLimit, 0.1, Instrument.Ask + x, 0, Stops.InPips(xp + 50, 2 * xp), null, null, null);
                    if (result1.IsSuccessful)
                    {
                        posGuidSell = result1.Position.Id;
                    }
                }
            }

            //=== КОРЕКЦИЯ ===========================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty; Print("09 - Buy - Закрыто по StopLoss (Корекция) - {0} ", DTime);
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty;  Print("10 -Sell - Закрыто по StopLoss (Корекция) - {0} ", DTime);
            }

            // 4. На следующий день около 9:00 по Гринвичу удаляем и закрываем все ордера независимо от прибыли / убытка.
            if (DTime.Hour == 9 && DTime.Minute == 00)
            {
                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Pending)
                {
                    var res = Trade.CancelPendingPosition(posGuidBuy);
                    if (res.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Active)
                {
                    var result2 = Trade.CloseMarketPosition(posGuidBuy);
                    if (result2.IsSuccessful)
                    {
                        posGuidBuy = result2.Position.Id;
                    }
                }

                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Pending)
                {
                    var res1 = Trade.CancelPendingPosition(posGuidSell);
                    if (res1.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Active)
                {
                    var result3 = Trade.CloseMarketPosition(posGuidSell);
                    if (result3.IsSuccessful)
                    {
                        posGuidBuy = result3.Position.Id;
                    }
                }
            }



// 6. Важно ! После активации одного ордера, необходимо удалить второй.
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Active)
            {
                var res = Trade.CancelPendingPosition(posGuidBuy);
                if (res.IsSuccessful)
                {
                    posGuidBuy = Guid.Empty;
                }
            }

            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Active)
            {
                var res = Trade.CancelPendingPosition(posGuidSell);
                if (res.IsSuccessful)
                {
                    posGuidSell = Guid.Empty;
                }
            }
        }
Exemplo n.º 21
0
//===============================================================================================================================
        protected override void NewBar()
        {
            _wprInd.ReInit();
//=== КОРЕКЦИЯ ===========================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty;
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty;
            }

//=== Закрытие всех ордеров если пятница 16:00 (19:00 Kiev) ===========================================================================
            if (Bars[Bars.Range.To - 1].Time.DayOfWeek == DayOfWeek.Friday && Bars[Bars.Range.To - 1].Time.Hour == 16)
            {
                zzu5 = 0; zzu4 = 0; zzu3 = 0; zzu2 = 0; zzu1 = 0; zzd5 = 0; zzd4 = 0; zzd3 = 0; zzd2 = 0; zzd1 = 0;
                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Active)
                {
                    var res = Trade.CloseMarketPosition(posGuidBuy); if (res.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Active)
                {
                    var res = Trade.CloseMarketPosition(posGuidSell); if (res.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }

                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Pending)
                {
                    var res = Trade.CancelPendingPosition(posGuidBuy); if (res.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Pending)
                {
                    var res = Trade.CancelPendingPosition(posGuidSell); if (res.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
            }
//======================================================================================================================================
            if (_wprInd.MainIndicatorSeries[Bars.Range.To - 1] > 0)
            {
                if (zzd5 != 0 && zzd4 != 0 && zzd3 != 0 && zzd2 != 0 && zzd1 != 0)
                {
                    torg = true;
                }
                else
                {
                    torg = false;
                }
                zz3  = zz2;       zz2 = zz1;   zz1 = _wprInd.MainIndicatorSeries[Bars.Range.To - 1];
                zzi3 = zzi2;      zzi2 = zzi1;  zzi1 = Bars.Range.To - 1;
//====== ВВЕРХУ ПИК =====================================================================================================================
                if (zz3 < zz2 && zz2 > zz1)
                {                 // ВВЕРХУ
                    zzd5 = zzd4; zzd4 = zzd3; zzd3 = zzd2; zzd2 = zzd1; zzd1 = zzi2;
                    zzu5 = zzu4; zzu4 = zzu3; zzu3 = zzu2; zzu2 = zzu1; zzu1 = zz2;
                    Print("ВВЕРХ {0} 1={1} 2={2} 3={3} 4={4}", Bars[zzd1].Time, zzu1, zzu2, zzu3, zzu4);
                    vy.Time = Bars[Bars.Range.To - 111111].Time;
                    sv      = 0;
                    for (int i = zzd1; i > zzd2; i--)
                    {
                        sv = sv + Bars[i].TickCount;
                    }
                    av5 = av4; av4 = av3; av3 = av2; av2 = av1; av1 = Math.Round(sv / (zzd1 - zzd2), 0);

                    var toolText1 = Tools.Create <Text>();
                    toolText1.Point    = new ChartPoint(Bars[zzd1 - 1].Time, Bars[zzd1].High + 0.001);
                    toolText1.FontSize = 10;
                    toolText1.Caption  = string.Format("{0}", av1);

                    if (zzu1 > zzu2 &&
                        zzu3 > zzu2 &&
                        zzu3 > zzu1 &&
                        zzu3 > zzu4 &&
                        zzu4 > zzu1
                        )                         // ВВЕРХУ
                    {
                        var toolPolyLine = Tools.Create <PolyLine>();
                        toolPolyLine.Color = Color.Red;
                        toolPolyLine.Width = 4;
                        toolPolyLine.AddPoint(new ChartPoint(Bars[zzd5].Time, Bars[zzd5].High));
                        toolPolyLine.AddPoint(new ChartPoint(Bars[zzd4].Time, Bars[zzd4].Low));
                        toolPolyLine.AddPoint(new ChartPoint(Bars[zzd3].Time, Bars[zzd3].High));
                        toolPolyLine.AddPoint(new ChartPoint(Bars[zzd2].Time, Bars[zzd2].Low));
                        toolPolyLine.AddPoint(new ChartPoint(Bars[zzd1].Time, Bars[zzd1].High));

                        if (av1 > av3 && av2 > av4)
                        {
                            if (torg && posGuidSell == Guid.Empty)
                            {
                                var result2 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1, Stops.InPips(SL, TP), null, null);
                                if (result2.IsSuccessful)
                                {
                                    posGuidSell = result2.Position.Id;
                                }
                            }
                        }
                        else
                        {
                            if (torg && posGuidBuy == Guid.Empty)
                            {
                                var result1 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1, Stops.InPips(SL, TP), null, null);
                                if (result1.IsSuccessful)
                                {
                                    posGuidBuy = result1.Position.Id;
                                }
                            }
                        }
                    }
                }
//==== ВНИЗУ ПИК ======================================================================================================================
                if (zz3 > zz2 && zz2 < zz1)
                {                 // ВНИЗУ
                    zzd5 = zzd4; zzd4 = zzd3; zzd3 = zzd2; zzd2 = zzd1; zzd1 = zzi2;
                    zzu5 = zzu4; zzu4 = zzu3; zzu3 = zzu2; zzu2 = zzu1; zzu1 = zz2;
                    Print("НИЗ {0} 1={1} 2={2} 3={3} 4={4}", Bars[zzd1].Time, zzu1, zzu2, zzu3, zzu4);
                    vy.Time = Bars[Bars.Range.To - 1].Time;

                    sv = 0;
                    for (int i = zzd1; i > zzd2; i--)
                    {
                        sv = sv + Bars[i].TickCount;
                    }
                    av5 = av4; av4 = av3; av3 = av2; av2 = av1; av1 = Math.Round(sv / (zzd1 - zzd2), 0);

                    var toolText2 = Tools.Create <Text>();
                    toolText2.Point    = new ChartPoint(Bars[zzd1 - 1].Time, Bars[zzd1 - 1].Low);
                    toolText2.FontSize = 10;
                    toolText2.Caption  = string.Format("{0}", av1);

                    if (zzu1 > zzu3 &&
                        zzu2 > zzu1 &&
                        zzu2 > zzu3 &&
                        zzu4 > zzu3 &&
                        zzu2 > zzu4 &&
                        zzu1 > zzu4
                        )                  //  ВНИЗУ
                    {
                        var toolPolyLine = Tools.Create <PolyLine>();
                        toolPolyLine.Color = Color.Blue;
                        toolPolyLine.Width = 4;
                        toolPolyLine.AddPoint(new ChartPoint(Bars[zzd5].Time, Bars[zzd5].Low));
                        toolPolyLine.AddPoint(new ChartPoint(Bars[zzd4].Time, Bars[zzd4].High));
                        toolPolyLine.AddPoint(new ChartPoint(Bars[zzd3].Time, Bars[zzd3].Low));
                        toolPolyLine.AddPoint(new ChartPoint(Bars[zzd2].Time, Bars[zzd2].High));
                        toolPolyLine.AddPoint(new ChartPoint(Bars[zzd1].Time, Bars[zzd1].Low));

                        if (av1 > av3 && av2 > av4)
                        {
                            if (torg && posGuidBuy == Guid.Empty)
                            {
                                var result3 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1, Stops.InPips(SL, TP), null, null);
                                if (result3.IsSuccessful)
                                {
                                    posGuidBuy = result3.Position.Id;
                                }
                            }
                        }
                        else
                        {
                            if (torg && posGuidSell == Guid.Empty)
                            {
                                var result4 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1, Stops.InPips(SL, TP), null, null);
                                if (result4.IsSuccessful)
                                {
                                    posGuidSell = result4.Position.Id;
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 22
0
//===========================================================================
        protected override void NewBar()
        {
            DTime = Bars[ci].Time;
            ci    = Bars.Range.To - 1;

//=== КОРЕКЦИЯ =======================================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty;   bu1 = true;
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty;  bu1 = true;
            }
            if (posGuidBuy2 != Guid.Empty && Trade.GetPosition(posGuidBuy2).State == PositionState.Closed)
            {
                posGuidBuy2 = Guid.Empty;  bu2 = true;
            }
            if (posGuidSell2 != Guid.Empty && Trade.GetPosition(posGuidSell2).State == PositionState.Closed)
            {
                posGuidSell2 = Guid.Empty; bu2 = true;
            }

//========  начальная инициализация	==================================================================
            if (k == 0)
            {
                InitFr();
            }
            if (k == 1)
            {
                if (tu)
                {
                    vl.Time = tmax; vl.Color = Color.Red;
                    //nkz4 = zmax-((NKZ-5-(NKZ*kf))*Instrument.Point);
                    nkz4 = zmax - (NKZ * Instrument.Point);
                    nkz2 = zmax - ((NKZ * 2) * Instrument.Point);

                    nkz4v = zmax - ((NKZ * 0.9) * Instrument.Point);
                    nkz2v = zmax - (((NKZ * 2) * 0.9) * Instrument.Point);

                    nkz4  = Math.Round(nkz4, Instrument.PriceScale);
                    nkz2  = Math.Round(nkz2, Instrument.PriceScale);
                    nkz4v = Math.Round(nkz4v, Instrument.PriceScale);
                    nkz2v = Math.Round(nkz2v, Instrument.PriceScale);

                    XXPrint(" Init - {0} - frU1={1}({4}) frU2={2}({5}) frU3={3}({6}) zmax={7}({8})",
                            Bars[Bars.Range.To - 1].Time, frU1, frU2, frU3, tmU1, tmU2, tmU3, zmax, tmax);
                    hl.Price = nkz4v; hl.Text = Math.Round((nkz4 - zmin) * Math.Pow(10, Instrument.PriceScale), 0).ToString();
                }
                if (td)
                {
                    vl.Time = tmin; vl.Color = Color.Blue;
                    nkz4    = zmin + (NKZ * Instrument.Point);
                    nkz2    = zmin + ((NKZ * 2) * Instrument.Point);

                    nkz4v = zmin + ((NKZ * 0.9) * Instrument.Point);
                    nkz2v = zmin + (((NKZ * 2) * 0.9) * Instrument.Point);

                    nkz4  = Math.Round(nkz4, Instrument.PriceScale);
                    nkz2  = Math.Round(nkz2, Instrument.PriceScale);
                    nkz4v = Math.Round(nkz4v, Instrument.PriceScale);
                    nkz2v = Math.Round(nkz2v, Instrument.PriceScale);
                    XXPrint(" Init - {0} - frD1={4}({1}) frD2={5}({2}) frD3={6}({3})  zmin={7}({8})",
                            Bars[Bars.Range.To - 1].Time, tmD1, tmD2, tmD3, frD1, frD2, frD3, zmin, tmin);
                    hl.Price = nkz4v; hl.Text = Math.Round((nkz4 - zmin) * Math.Pow(10, Instrument.PriceScale), 0).ToString();
                }
            }
            k++;

//====  Fractal вверх ================================================================================================================
            if (_frInd.TopSeries[Bars.Range.To - frac] > 0)
            {
                if (frU)
                {
                    frU  = false;
                    frU3 = frU2; frU2 = frU1; frU1 = _frInd.TopSeries[Bars.Range.To - frac];
                    tmU3 = tmU2; tmU2 = tmU1; tmU1 = Bars[Bars.Range.To - frac].Time;
                    fsU3 = fsU2; fsU2 = fsU1; fsU1 = _ftoInd.FisherSeries[Bars.Range.To - frac];
                }
                else
                {
                    if (_frInd.TopSeries[Bars.Range.To - frac] > frU1)
                    {
                        frU1 = _frInd.TopSeries[Bars.Range.To - frac];
                        fsU1 = _ftoInd.FisherSeries[Bars.Range.To - frac];
                        tmU1 = Bars[Bars.Range.To - frac].Time;
                    }
                }
                Print("{2} - Top {0} {1}", Bars[Bars.Range.To - frac].Time, _frInd.TopSeries[Bars.Range.To - frac], Instrument.Name);
            }
//====  Fractal вниз ====================================================================================
            if (_frInd.BottomSeries[Bars.Range.To - frac] > 0)
            {
                if (!frU)
                {
                    frU  = true;
                    frD3 = frD2; frD2 = frD1; frD1 = _frInd.BottomSeries[Bars.Range.To - frac];
                    tmD3 = tmD2; tmD2 = tmD1; tmD1 = Bars[Bars.Range.To - frac].Time;
                    fsD3 = fsD2; fsD2 = fsD1; fsD1 = _ftoInd.FisherSeries[Bars.Range.To - frac];
                }
                else
                {
                    if (_frInd.BottomSeries[Bars.Range.To - frac] < frD1)
                    {
                        frD1 = _frInd.BottomSeries[Bars.Range.To - frac];
                        fsD1 = _ftoInd.FisherSeries[Bars.Range.To - frac];
                        tmD1 = Bars[Bars.Range.To - frac].Time;
                    }
                }
                Print("{2} - Bootom {0} {1}", Bars[Bars.Range.To - frac].Time, _frInd.BottomSeries[Bars.Range.To - frac], Instrument.Name);
            }
//====================================================================================================================

            if (tu && Bars[ci].High > zmax)
            {
                zmax    = Bars[ci].High;
                tmax    = Bars[ci].Time;
                vl.Time = tmax; vl.Color = Color.Red;
                nkz4    = zmax - (NKZ * Instrument.Point);
                nkz2    = zmax - ((NKZ * 2) * Instrument.Point);

                nkz4v = zmax - ((NKZ * 0.9) * Instrument.Point);
                nkz2v = zmax - (((NKZ * 2) * 0.9) * Instrument.Point);

                nkz4     = Math.Round(nkz4, Instrument.PriceScale);
                nkz2     = Math.Round(nkz2, Instrument.PriceScale);
                nkz4v    = Math.Round(nkz4v, Instrument.PriceScale);
                nkz2v    = Math.Round(nkz2v, Instrument.PriceScale);
                nd       = false; nu = false;
                hl.Price = nkz4v; hl.Text = Math.Round((nkz4 - zmin) * Math.Pow(10, Instrument.PriceScale), 0).ToString();
            }

            if (td && Bars[ci].Low < zmin)
            {
                zmin    = Bars[ci].Low;
                tmin    = Bars[ci].Time;
                vl.Time = tmin; vl.Color = Color.Blue;

                nkz4 = zmin + (NKZ * Instrument.Point);
                nkz2 = zmin + ((NKZ * 2) * Instrument.Point);

                nkz4v = zmin + ((NKZ * 0.9) * Instrument.Point);
                nkz2v = zmin + (((NKZ * 2) * 0.9) * Instrument.Point);


                nkz4     = Math.Round(nkz4, Instrument.PriceScale);
                nkz2     = Math.Round(nkz2, Instrument.PriceScale);
                nkz4v    = Math.Round(nkz4v, Instrument.PriceScale);
                nkz2v    = Math.Round(nkz2v, Instrument.PriceScale);
                nd       = false; nu = false;
                hl.Price = nkz4v; hl.Text = Math.Round((nkz4 - zmin) * Math.Pow(10, Instrument.PriceScale), 0).ToString();
            }
//== Касание зоны/1\2 ======================================================================================================
            if (n2)
            {
                if (tu && Bars[ci].Low < nkz2v && !nu)
                {
                    nu = true;
                }
                if (td && Bars[ci].High > nkz2v && !nd)
                {
                    nd = true;
                }
            }
            else
            {
                if (tu && Bars[ci].Low < nkz4v && !nu)
                {
                    nu = true;
                }
                if (td && Bars[ci].High > nkz4v && !nd)
                {
                    nd = true;
                }
            }
// Если пересечение зоны было (nu) и торгуем вверх (tu) ===========================================================
            if (nu && tu)
            {
//================================ Определение патерна   ПИК ПОСЛЕДНЕГО ФРАКТАЛА ПАТЕРНА ВНИЗУ =====================
//         Появился фрактал
                if (_frInd.TopSeries[Bars.Range.To - frac] > 0)
                {
                    Print("{2} - PATERN Top {0} {1}", Bars[Bars.Range.To - frac].Time, _frInd.TopSeries[Bars.Range.To - frac], Instrument.Name);
                    if (frU3 > frU2 && frU1 > frU2 && frU1 > 0 && frU2 > 0 && frU3 > 0)
                    {
                        TPU   = zmax;
                        SLU   = frD2 - Instrument.Spread - dlt; SLU = Math.Round(SLU, Instrument.PriceScale);
                        PRU   = Math.Round((TPU - Bars[ci].Close) * Math.Pow(10, Instrument.PriceScale), 0);
                        PRU2  = Math.Round((TP2 - Bars[ci].Close) * Math.Pow(10, Instrument.PriceScale), 0);
                        UBU   = Math.Round((Bars[ci].Close - SLU) * Math.Pow(10, Instrument.PriceScale), 0);
                        PCU   = Math.Round(PRU / UBU, 2);
                        PCU2  = Math.Round(PRU2 / UBU, 2);
                        Ot    = Math.Round((frU3 - frU2) / (frU1 - frU2), 2);
                        PRUi  = (int)PRU;
                        PRUi2 = (int)PRU2;
                        UBUi  = (int)UBU;
                        //UBUi=(int)SL1;
// Основное условие : STOP от 40 до 200, Ниже 1/2 , и проход фишера через 0
                        if (UBU > 40 && UBU < SL1 && Bars[ci].Close > nkz2 && PCU > kof && fsU1 > 0 && fsU2 < 0)
                        {
                            if (posGuidBuy != Guid.Empty && posGuidBuy2 == Guid.Empty)
                            {
                                if (TP2 != 0)
                                {
                                    PRUi = PRUi2;
                                }
                                var res4 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, lot, Instrument.Bid, -1, Stops.InPips(UBUi, PRUi), null, null);
                                if (res4.IsSuccessful)
                                {
                                    posGuidBuy2 = res4.Position.Id;
                                }
                            }
                            if (posGuidBuy == Guid.Empty)
                            {
                                var res1 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, lot, Instrument.Bid, -1, Stops.InPips(UBUi, PRUi), null, null);
                                if (res1.IsSuccessful)
                                {
                                    posGuidBuy = res1.Position.Id;
                                }
                            }
                            frD = false;
                        }
                    }
                }
            }
// Если пересечение зоны было (nd) и торгуем вниз (td) ===========================================================
            if (nd && td)
            {
//=============== Определение патерна   ПИК ПОСЛЕДНЕГО ФРАКТАЛА ПАТЕРНА ВВЕРХУ =====================================
                if (_frInd.BottomSeries[Bars.Range.To - frac] > 0)
                {
                    Print("{2} - PATERN Bootom {0} {1}", Bars[Bars.Range.To - frac].Time, _frInd.BottomSeries[Bars.Range.To - frac], Instrument.Name);
                    if (frD2 > frD3 && frD2 > frD1 && frD1 > 0 && frD2 > 0 && frD3 > 0)
                    {
                        TPD  = Math.Round(zmin, Instrument.PriceScale);
                        SLD  = frU2 + Instrument.Spread + dlt; SLD = Math.Round(SLD, Instrument.PriceScale);
                        PRD  = Math.Round((Bars[ci].Close - TPD) * Math.Pow(10, Instrument.PriceScale), 0);
                        PRD2 = Math.Round((Bars[ci].Close - TP2) * Math.Pow(10, Instrument.PriceScale), 0);
                        Ot   = Math.Round((frD2 - frD3) / (frD2 - frD1), 2);
                        UBD  = Math.Round((SLD - Bars[ci].Close) * Math.Pow(10, Instrument.PriceScale), 0);

                        PRDi2 = (int)PRD2;
                        PRDi  = (int)PRD;
                        PRDi2 = (int)PRD2;
                        UBDi  = (int)UBD;
                        //UBDi=(int)SL1;
                        PCD  = Math.Round(PRD / UBD, 2);
                        PCD2 = Math.Round(PRD2 / UBD, 2);
                        if (UBD > 40 && UBD < SL1 && Bars[ci].Close <nkz2 && PCD> kof && fsD1 < 0 && fsD2 > 0)
                        {
                            if (posGuidSell != Guid.Empty && posGuidSell2 == Guid.Empty)
                            {
                                if (TP2 != 0)
                                {
                                    PRDi = PRDi2;
                                }
                                var res3 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, lot, Instrument.Ask, -1, Stops.InPips(UBDi, PRDi), null, null);
                                if (res3.IsSuccessful)
                                {
                                    posGuidSell2 = res3.Position.Id;
                                }
                            }
                            if (posGuidSell == Guid.Empty)
                            {
                                var res2 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, lot, Instrument.Ask, -1, Stops.InPips(UBDi, PRDi), null, null);
                                if (res2.IsSuccessful)
                                {
                                    posGuidSell = res2.Position.Id;
                                }
                            }
                            frU = false;
                        }
                    }     // if( frD2>frD3 && frD2>frD1 )
                } // if(_frInd.BottomSeries[Bars.Range.To-frac]>0)
            } //if(nd && td)
//======  Логи  =====================================================================================================================
            st = ""; st2 = "";
            if (_frInd.TopSeries[Bars.Range.To - frac] > 0)
            {
                if (frU3 > frU2 && frU1 > frU2 && frU1 > 0 && frU2 > 0 && frU3 > 0 && tu) //Патерн
                {
                    var toolPolyLine = Tools.Create <PolyLine>();
                    toolPolyLine.Color = Color.Red;
                    if (nu)
                    {
                        toolPolyLine.Width = 1;
                    }
                    else
                    {
                        toolPolyLine.Width = 3;
                    }
                    toolPolyLine.AddPoint(new ChartPoint(tmU1, frU1));
                    toolPolyLine.AddPoint(new ChartPoint(tmU2, frU2));
                    toolPolyLine.AddPoint(new ChartPoint(tmU3, frU3));

                    var toolText = Tools.Create <Text>();
                    toolText.Point    = new ChartPoint(tmU1, frU1);
                    toolText.FontSize = 6;
                    toolText.Caption  = string.Format("{0} SL={1} PR={2}", PCU, UBUi, PRUi);
                }
                if (tu)        //Торгуем в Buy
                {
                    st = st + " tu --";
                    if (nu)            // Есть пересечение 1/4
                    {
                        st = st + "++";
                        if (frU3 > frU2 && frU1 > frU2)              //Патерн
                        {
                            if (UBU > 20 && UBU < SL1 && Bars[ci].Close > nkz2 && PCU > kof)
                            {
                                st = st + "@@ ";
                                if (posGuidBuy != Guid.Empty)
                                {
                                    st  = st + "Buy1 ";
                                    st2 = Trade.GetPosition(posGuidBuy).Number.ToString() + " " + Instrument.Name + " Buy1 ";
                                }
                                if (posGuidBuy2 != Guid.Empty)
                                {
                                    st  = st + "Buy2 ";
                                    st2 = Trade.GetPosition(posGuidBuy2).Number.ToString() + " " + Instrument.Name + " Buy2 ";
                                }
                                if (posGuidBuy == Guid.Empty && posGuidBuy2 == Guid.Empty)
                                {
                                    st = st + "!! ";
                                }
                                st  = st + "| SL=" + UBU.ToString() + " TP=" + PRU.ToString() + " %=" + PCU.ToString();
                                st2 = st2 + "| SL=" + UBU.ToString() + " TP=" + PRU.ToString() + " %=" + PCU.ToString();
                            }
                        }
                    }
                    XXPrint("{1} | {0} | frU1={2}:{3}({4}) frU2={5}:{6}({7}) frU3={8}:{9}({10}) -- zmax={11}({13}) nkz4={12}", Bars[Bars.Range.To - 1].Time, st,
                            tmU3.Hour, tmU3.Minute, frU3, tmU2.Hour, tmU2.Minute, frU2, tmU1.Hour, tmU1.Minute, frU1, zmax, nkz4, tmax);
                    XPrint("{1} | {0} | frU= {2}:{3}({4}) {5}:{6}({7}) {8}:{9}({10}) | max={11}({13}) nkz4={12}",
                           Bars[Bars.Range.To - 1].Time, st2, tmU3.Hour, tmU3.Minute, frU3, tmU2.Hour, tmU2.Minute, frU2, tmU1.Hour, tmU1.Minute, frU1, zmax, nkz4, tmax);
                }
            }
            if (_frInd.BottomSeries[Bars.Range.To - frac] > 0)
            {
                if (frD2 > frD3 && frD2 > frD1 && frD1 > 0 && frD2 > 0 && frD3 > 0 && td) //Патерн
                {
                    var toolPolyLine = Tools.Create <PolyLine>();
                    toolPolyLine.Color = Color.Blue;
                    if (nd)
                    {
                        toolPolyLine.Width = 1;
                    }
                    else
                    {
                        toolPolyLine.Width = 3;
                    }
                    toolPolyLine.AddPoint(new ChartPoint(tmD1, frD1));
                    toolPolyLine.AddPoint(new ChartPoint(tmD2, frD2));
                    toolPolyLine.AddPoint(new ChartPoint(tmD3, frD3));
                    var toolText = Tools.Create <Text>();
                    toolText.Point    = new ChartPoint(tmD1, frD1);
                    toolText.FontSize = 6;
                    toolText.Caption  = string.Format("{0} SL={1} PR={2}", PCD, UBDi, PRDi);
                }
                if (td)        //Торгуем в Buy
                {
                    st = st + " td --";
                    if (nd)            // Есть пересечение 1/4
                    {
                        st = st + "++";
                        if (frD2 > frD3 && frD2 > frD1)              //Патерн
                        {
                            if (UBD > 20 && UBD < SL1 && Bars[ci].Close < nkz2 && PCD > 2)
                            {
                                st = st + "@@ ";
                                if (posGuidSell != Guid.Empty)
                                {
                                    st  = st + "Sell1 ";
                                    st2 = Trade.GetPosition(posGuidSell).Number.ToString() + " " + Instrument.Name + " Sell1 ";
                                }
                                if (posGuidSell2 != Guid.Empty)
                                {
                                    st  = st + "Sell2 ";
                                    st2 = Trade.GetPosition(posGuidSell2).Number.ToString() + " " + Instrument.Name + " Sell2 ";
                                }
                                if (posGuidSell == Guid.Empty && posGuidSell2 == Guid.Empty)
                                {
                                    st = st + "!! ";
                                }
                                st  = st + "| SL=" + UBD.ToString() + " TP=" + PRD.ToString() + " %=" + PCD.ToString();
                                st2 = st2 + "| SL=" + UBD.ToString() + " TP=" + PRD.ToString() + " %=" + PCD.ToString();
                            }
                        }
                    }
                    XXPrint("{1} | {0} | frD1={2}:{3}({4}) frD2={5}:{6}({7}) frD3={8}:{9}({10}) | zmin={11}({13}) nkz4={12}", Bars[Bars.Range.To - 1].Time, st,
                            tmD3.Hour, tmD3.Minute, frD3, tmD2.Hour, tmD2.Minute, frD2, tmD1.Hour, tmD1.Minute, frD1, zmin, nkz4, tmin);
                    XPrint("{1} | {0} | frD= {2}:{3}({4}) {5}:{6}({7}) {8}:{9}({10}) | min={11}({13}) nkz4={12}",
                           Bars[Bars.Range.To - 1].Time, st2, tmD3.Hour, tmD3.Minute, frD3, tmD2.Hour, tmD2.Minute, frD2, tmD1.Hour, tmD1.Minute, frD1, zmin, nkz4, tmin);
                }
            }

//=============   LOG2  ========================================================================================================
            if (tu)
            {
                XXPrint("--- tu --- {0} | frU3={2}:{3}({4}) frU2={5}:{6}({7}) frU1={8}:{9}({10}) | max={11}({13}) | nkz4={12} nkz2={14}  nkz4v={15} nkz2v={16} nu={17}", Bars[Bars.Range.To - 1].Time, st,
                        tmU3.Hour, tmU3.Minute, frU3, tmU2.Hour, tmU2.Minute, frU2, tmU1.Hour, tmU1.Minute, frU1, zmax, nkz4, tmax, nkz2, nkz4v, nkz2v, nu);
            }
            if (td)
            {
                XXPrint("--- td --- {0} | frD3={2}:{3}({4}) frD2={5}:{6}({7}) frD1={8}:{9}({10}) | min={11}({13}) | nkz4={12} nkz2={14}  nkz4v={15} nkz2v={16} nd={17}", Bars[Bars.Range.To - 1].Time, st,
                        tmD3.Hour, tmD3.Minute, frD3, tmD2.Hour, tmD2.Minute, frD2, tmD1.Hour, tmD1.Minute, frD1, zmin, nkz4, tmin, nkz2, nkz4v, nkz2v, nd);
            }
//========== STOP ==========================================
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).Pips > SL1 && bu1)
            {
                bu1 = false; var res5 = Trade.UpdateMarketPosition(posGuidSell, Trade.GetPosition(posGuidSell).OpenPrice - Instrument.Spread - dlt, Trade.GetPosition(posGuidSell).TakeProfit, null);
                if (res5.IsSuccessful)
                {
                    posGuidSell = res5.Position.Id;
                }
            }

            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).Pips > SL1 && bu1)
            {
                bu1 = false; var res6 = Trade.UpdateMarketPosition(posGuidBuy, Trade.GetPosition(posGuidBuy).OpenPrice + Instrument.Spread + dlt, Trade.GetPosition(posGuidBuy).TakeProfit, null);
                if (res6.IsSuccessful)
                {
                    posGuidBuy = res6.Position.Id;
                }
            }

            if (posGuidSell2 != Guid.Empty && Trade.GetPosition(posGuidSell2).Pips > SL1 && bu2)
            {
                bu2 = false; var res7 = Trade.UpdateMarketPosition(posGuidSell2, Trade.GetPosition(posGuidSell2).OpenPrice - Instrument.Spread - dlt, Trade.GetPosition(posGuidSell2).TakeProfit, null);
                if (res7.IsSuccessful)
                {
                    posGuidSell = res7.Position.Id;
                }
            }

            if (posGuidBuy2 != Guid.Empty && Trade.GetPosition(posGuidBuy2).Pips > SL1 && bu2)
            {
                bu2 = false; var res8 = Trade.UpdateMarketPosition(posGuidBuy2, Trade.GetPosition(posGuidBuy2).OpenPrice + Instrument.Spread + dlt, Trade.GetPosition(posGuidBuy2).TakeProfit, null);
                if (res8.IsSuccessful)
                {
                    posGuidBuy2 = res8.Position.Id;
                }
            }
        } // NewBar
Exemplo n.º 23
0
//===============================================================================================================================
        protected override void NewBar()
        {
            _wprInd.ReInit();

            //if(posGuidBuy!=Guid.Empty && posGuidBuy)
            //=== КОРЕКЦИЯ ===========================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty;
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty;
            }

            //=== Закрытие всех ордеров если пятница 16:00 (19:00 Kiev) ===========================================================================
            if (Bars[Bars.Range.To - 1].Time.DayOfWeek == DayOfWeek.Friday && Bars[Bars.Range.To - 1].Time.Hour == 16)
            {
                Torg = false;
                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Active)
                {
                    var res = Trade.CloseMarketPosition(posGuidBuy); if (res.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Active)
                {
                    var res = Trade.CloseMarketPosition(posGuidSell); if (res.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
            }
            if (Bars[Bars.Range.To - 1].Time.DayOfWeek == DayOfWeek.Monday && Bars[Bars.Range.To - 1].Time.Hour == 6)
            {
                Torg = true;
            }

//======================================================================================================================================
            if (_wprInd.MainIndicatorSeries[Bars.Range.To - 1] > 0)
            {
                zz3  = zz2;       zz2 = zz1;   zz1 = _wprInd.MainIndicatorSeries[Bars.Range.To - 1];
                zzi3 = zzi2;      zzi2 = zzi1;  zzi1 = Bars.Range.To - 1;

                if (zz3 < zz2 && zz2 > zz1)
                {                 // 1 - ВВЕРХУ
                    zzd6 = zzd5; zzd5 = zzd4; zzd4 = zzd3; zzd3 = zzd2; zzd2 = zzd1; zzd1 = zzi2;
                    zzu6 = zzu5; zzu5 = zzu4; zzu4 = zzu3; zzu3 = zzu2; zzu2 = zzu1; zzu1 = zz2;
                    Print("ВВЕРХ {0} 1={1} 2={2} 3={3} 4={4}", Bars[zzd1].Time, zzu1, zzu2, zzu3, zzu4);

// Патерн 1-2-3  zzu1-пик вверху
//
                    if (zzu3 > zzu1 && zzu5 > zzu3 && zzu4 > zzu2)
                    {
                        var vr = Tools.Create <VerticalLine>(); vr.Color = Color.Red; vr.Time = Bars[zzd3].Time; vr.Width = 4;

                        var toolPolyLine = Tools.Create <PolyLine>();
                        toolPolyLine.Color = Color.Blue;
                        toolPolyLine.Width = 4;
                        toolPolyLine.AddPoint(new ChartPoint(Bars[zzd4].Time, Bars[zzd4].Low));
                        toolPolyLine.AddPoint(new ChartPoint(Bars[zzd3].Time, Bars[zzd3].High));
                        toolPolyLine.AddPoint(new ChartPoint(Bars[zzd2].Time, Bars[zzd2].Low));
                        toolPolyLine.AddPoint(new ChartPoint(Bars[zzd1].Time, Bars[zzd1].High));
                        toolPolyLine.AddPoint(new ChartPoint(Bars[Bars.Range.To].Time, Bars[Bars.Range.To].Low));

                        if (Torg && posGuidBuy == Guid.Empty)
                        {
                            var result2 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1, Stops.InPips(200, 150), null, null);
//var result1 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1,Instrument.Ask, -1,Stops.InPrice(Bars[zzd1].High,null), null, null);
                            if (result2.IsSuccessful)
                            {
                                posGuidBuy = result2.Position.Id;                                          //Stops.InPips(200,100) Stops.InPrice(Bars[zzd1].High,Bars[Bars.Range.To-1].Low-0.001)
                            }
                        }
                    }
                }

                if (zz3 > zz2 && zz2 < zz1)
                {                 // ВНИЗУ
                    zzd6 = zzd5; zzd5 = zzd4; zzd4 = zzd3; zzd3 = zzd2; zzd2 = zzd1; zzd1 = zzi2;
                    zzu6 = zzu5; zzu5 = zzu4; zzu4 = zzu3; zzu3 = zzu2; zzu2 = zzu1; zzu1 = zz2;
                    Print("НИЗ {0} 1={1} 2={2} 3={3} 4={4}", Bars[zzd1].Time, zzu1, zzu2, zzu3, zzu4);

//			if (posGuidSell!=Guid.Empty && Trade.GetPosition(posGuidSell).State==PositionState.Active)
//			{var res = Trade.CloseMarketPosition(posGuidSell); if (res.IsSuccessful) posGuidSell = Guid.Empty;}

// Патерн 1-2-3  zzu1-пик ВНИЗУ
//
                    if (zzu2 > zzu4 && zzu1 > zzu3 && zzu3 > zzu5)
                    {
                        var vr           = Tools.Create <VerticalLine>(); vr.Color = Color.Blue; vr.Time = Bars[zzd3].Time; vr.Width = 4;
                        var toolPolyLine = Tools.Create <PolyLine>();
                        toolPolyLine.Color = Color.Blue;
                        toolPolyLine.Width = 4;
                        toolPolyLine.AddPoint(new ChartPoint(Bars[zzd4].Time, Bars[zzd4].Low));
                        toolPolyLine.AddPoint(new ChartPoint(Bars[zzd3].Time, Bars[zzd3].High));
                        toolPolyLine.AddPoint(new ChartPoint(Bars[zzd2].Time, Bars[zzd2].Low));
                        toolPolyLine.AddPoint(new ChartPoint(Bars[zzd1].Time, Bars[zzd1].High));
                        toolPolyLine.AddPoint(new ChartPoint(Bars[Bars.Range.To].Time, Bars[Bars.Range.To].Low));

                        if (Torg && posGuidSell == Guid.Empty)
                        {
                            var result1 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1, Stops.InPips(200, 150), null, null);
//var result2 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1,Instrument.Bid, -1, Stops.InPrice(Bars[zzd1].Low,null), null, null);
                            if (result1.IsSuccessful)
                            {
                                posGuidSell = result1.Position.Id;                                         //Stops.InPips(200,100)  Stops.InPrice(Bars[zzd1].Low,Bars[Bars.Range.To-1].High+0.001),
                            }
                        }
                        var toolPolyLine1 = Tools.Create <PolyLine>();
                        toolPolyLine1.Color = Color.Black;
                        toolPolyLine1.Width = 4;
                        //toolPolyLine.AddPoint(new ChartPoint(Bars[zzd6].Time, Bars[zzd6].Low));
                        toolPolyLine1.AddPoint(new ChartPoint(Bars[zzd5].Time, Bars[zzd5].Low));
                        toolPolyLine1.AddPoint(new ChartPoint(Bars[zzd4].Time, Bars[zzd4].High));
                        toolPolyLine1.AddPoint(new ChartPoint(Bars[zzd3].Time, Bars[zzd3].Low));
                        toolPolyLine1.AddPoint(new ChartPoint(Bars[zzd2].Time, Bars[zzd2].High));
                        toolPolyLine1.AddPoint(new ChartPoint(Bars[zzd1].Time, Bars[zzd1].Low));
                        toolPolyLine1.AddPoint(new ChartPoint(Bars[Bars.Range.To].Time, Bars[Bars.Range.To].High));
                    }
                }
            }
        }
Exemplo n.º 24
0
//===============================================================================================================================
        protected override void NewBar()
        {
            DTime = Bars[Bars.Range.To - 1].Time;
            ci    = Bars.Range.To - 1;
//=========  Рисуем линию  начала торгов Европы =============================================================================
            if (DTime.Hour == 7 && DTime.Minute == 00)
            {
                var vl1 = Tools.Create <VerticalLine>(); vl1.Time = Bars[Bars.Range.To - 1].Time; vl1.Color = Color.Aqua; vl1.Width = 3;
            }
            if (DTime.Hour == 12 && DTime.Minute == 00)
            {
                var vl1 = Tools.Create <VerticalLine>(); vl1.Time = Bars[Bars.Range.To - 1].Time; vl1.Color = Color.DarkCyan; vl1.Width = 3;
            }
//=======  Определение разворота ====================================================================================================
// Выход синей линии
            if (_ftoInd.FisherSeries[Bars.Range.To - 1] > _ftoInd.Ma1Series[Bars.Range.To - 1])
            {
                FsU1 = true;
            }
            else
            {
                FsU1 = false;
            }
            if (_ftoInd.FisherSeries[Bars.Range.To - 1] < _ftoInd.Ma1Series[Bars.Range.To - 1])
            {
                FsD1 = true;
            }
            else
            {
                FsD1 = false;
            }

// Проход через 0
            if (_ftoInd.FisherSeries[Bars.Range.To - 2] < 0 && _ftoInd.FisherSeries[Bars.Range.To - 1] > 0)
            {
                FsU = true;
            }
            else
            {
                FsU = false;
            }
            if (_ftoInd.FisherSeries[Bars.Range.To - 2] > 0 && _ftoInd.FisherSeries[Bars.Range.To - 1] < 0)
            {
                FsD = true;
            }
            else
            {
                FsD = false;
            }

//====================================================================================================================================
            posActiveMineB = Trade.GetActivePositions(mgB, true);
            if (posActiveMineB != null && posActiveMineB.Length > 0 && posGuidBuy != posActiveMineB[0].Id)
            {
                posGuidBuy = posActiveMineB[0].Id;
            }
            posActiveMineS = Trade.GetActivePositions(mgS, true);
            if (posActiveMineS != null && posActiveMineS.Length > 0 && posGuidSell != posActiveMineS[0].Id)
            {
                posGuidSell = posActiveMineS[0].Id;
            }

//=== КОРЕКЦИЯ =======================================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty;
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty;
            }


//====================================================================================================================================
            if (DTime.Hour > 7)
            {
                if (posGuidBuy == Guid.Empty && Buy7 && torg && FsU)
                {
                    torg = false;
                    var result107 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1,
                                                             Stops.InPips(SL1, null), null, mgB);
                    if (result107.IsSuccessful)
                    {
                        posGuidBuy = result107.Position.Id;
                    }
                }

                if (posGuidSell == Guid.Empty && Sell7 && torg && FsD)
                {
                    torg = false;
                    var result207 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1,
                                                             Stops.InPips(SL1, null), null, mgS);
                    if (result207.IsSuccessful)
                    {
                        posGuidSell = result207.Position.Id;
                    }
                }
            }
//====================================================================================================================================
            if (posGuidBuy != Guid.Empty && FsD1)
            {
                var res1 = Trade.CloseMarketPosition(posGuidBuy); if (res1.IsSuccessful)
                {
                    posGuidBuy = Guid.Empty;
                }
            }
            if (posGuidSell != Guid.Empty && FsU1)
            {
                var res3 = Trade.CloseMarketPosition(posGuidSell); if (res3.IsSuccessful)
                {
                    posGuidSell = Guid.Empty;
                }
            }
        }
Exemplo n.º 25
0
//===========================================================================
        protected override void NewBar()
        {
            DTime = Bars[ci].Time;
            ci    = Bars.Range.To - 1;

//====  ИСТОРИЯ Fractal ====================================================================================

            i = 0;
            while (!(frU1 > 0 && frU2 > 0 && frU3 > 0 && frD1 > 0 && frD2 > 0 && frD3 > 0) && i < 200)
            {
                ki++;
                Print("{0} - {1}  {2}", i, _frInd.TopSeries[Bars.Range.To - frac - i], _frInd.BottomSeries[Bars.Range.To - frac - i]);
                // XXPrint("{0} - U1={1} U2={2} U3={3} D1={4} D2={5} D3={6}",i ,frU1>0 ,frU2>0 , frU3>0, frD1>0, frD2>0, frD3>0);
                if (_frInd.TopSeries[Bars.Range.To - frac - i] > 0)
                {
                    if (frU)
                    {
                        frU  = false;
                        frU1 = frU2; frU2 = frU3; frU3 = _frInd.TopSeries[Bars.Range.To - frac - i];
                        tmU1 = tmU2; tmU2 = tmU3; tmU3 = Bars[Bars.Range.To - frac - i].Time;
                    }
                    else
                    {
                        if (_frInd.TopSeries[Bars.Range.To - frac - i] > frU1)
                        {
                            frU3 = _frInd.TopSeries[Bars.Range.To - frac - i];
                            tmU3 = Bars[Bars.Range.To - frac - i].Time;
                        }
                    }
                    XXPrint("U ИТОРИЯ- {0} - frU1={1} frU2={2} frU3={3} frD1={4} frD2={5} frD3={6} i={7}", Bars[Bars.Range.To - frac - i].Time, frU1, frU2, frU3, frD1, frD2, frD3, i);
                }
                if (_frInd.BottomSeries[Bars.Range.To - frac - i] > 0)
                {
                    if (!frU)
                    {
                        frU  = true;
                        frD1 = frD2; frD2 = frD3; frD3 = _frInd.BottomSeries[Bars.Range.To - frac - i];
                        tmD1 = tmD2; tmD2 = tmD3; tmD3 = Bars[Bars.Range.To - frac - i].Time;
                    }
                    else
                    {
                        if (_frInd.BottomSeries[Bars.Range.To - frac - i] < frD1)
                        {
                            frD3 = _frInd.BottomSeries[Bars.Range.To - frac - i];
                            tmD3 = Bars[Bars.Range.To - frac - i].Time;
                        }
                    }
                    XXPrint("D ИСТОРИЯ - {0} - frU1={1} frU2={2} frU3={3} frD1={4} frD2={5} frD3={6} i={7}", Bars[Bars.Range.To - frac - i].Time, frU1, frU2, frU3, frD1, frD2, frD3, i);
                }
                i++;
            }

//====  Fractal ====================================================================================
            if (_frInd.TopSeries[Bars.Range.To - frac] > 0)
            {
                if (frU)
                {
                    frU  = false;
                    frU3 = frU2; frU2 = frU1; frU1 = _frInd.TopSeries[Bars.Range.To - frac];
                    tmU3 = tmU2; tmU2 = tmU1; tmU1 = Bars[Bars.Range.To - frac].Time;
                }
                else
                {
                    if (_frInd.TopSeries[Bars.Range.To - frac] > frU1)
                    {
                        frU1 = _frInd.TopSeries[Bars.Range.To - frac];
                        tmU1 = Bars[Bars.Range.To - frac].Time;
                    }
                }
                XXPrint("{0} - frU1={1} frU2={2} frU3={3} frD1={4} frD2={5} frD3={6} nkz4={7} nkz2={8} tu={9} td={10} dl={11} frac={12}", Bars[Bars.Range.To - frac].Time, frU1, frU2, frU3, frD1, frD2, frD3, nkz4, nkz2, tu, td, dl, frac);
            }
            if (_frInd.BottomSeries[Bars.Range.To - frac] > 0)
            {
                if (!frU)
                {
                    frU  = true;
                    frD3 = frD2; frD2 = frD1; frD1 = _frInd.BottomSeries[Bars.Range.To - frac];
                    tmD3 = tmD2; tmD2 = tmD1; tmD1 = Bars[Bars.Range.To - frac].Time;
                }
                else
                {
                    if (_frInd.BottomSeries[Bars.Range.To - frac] < frD1)
                    {
                        frD1 = _frInd.BottomSeries[Bars.Range.To - frac];
                        tmD1 = Bars[Bars.Range.To - frac].Time;
                    }
                }
                XXPrint("{0} - frU1={1} frU2={2} frU3={3} frD1={4} frD2={5} frD3={6} nkz4={7} nkz2={8} tu={9} td={10} dl={11} frac={12}", Bars[Bars.Range.To - frac].Time, frU1, frU2, frU3, frD1, frD2, frD3, nkz4, nkz2, tu, td, dl, frac);
            }
// Первое начиртание зон	=====================================================================================
            if (k == 0)
            {                            // Индекс свечипоследнего
                nu = false; nd = false;

                if (tu)
                {
                    zmax  = dt1 * Instrument.Point;
                    nkz4  = zmax - ((NKZ - 5 - (NKZ * kf)) * Instrument.Point);
                    nkz2  = zmax - (((NKZ * 2) - (NKZ * 2 * kf)) * Instrument.Point);
                    nkz4v = zmax - ((NKZ - 5) * Instrument.Point);
                    nkz2v = zmax - ((NKZ * 2) * Instrument.Point);
                    nkz4  = Math.Round(nkz4, Instrument.PriceScale);
                    nkz2  = Math.Round(nkz2, Instrument.PriceScale);
                    nkz4v = Math.Round(nkz4v, Instrument.PriceScale);
                    nkz2v = Math.Round(nkz2v, Instrument.PriceScale);
                    XXPrint("Первая прорисовка ВВЕРХ - {0} - nkz2={1} nkz4={2} - zmax={3}", Bars[ci].Time, nkz2, nkz4, zmax);
                }

                if (td)
                {
                    zmin  = dt1 * Instrument.Point;
                    nkz4  = zmin + ((NKZ - 5 - (NKZ * kf)) * Instrument.Point);
                    nkz2  = zmin + (((NKZ * 2) - (NKZ * 2 * kf)) * Instrument.Point);
                    nkz4v = zmin + ((NKZ - 5) * Instrument.Point);
                    nkz2v = zmin + (((NKZ * 2)) * Instrument.Point);
                    nkz4  = Math.Round(nkz4, Instrument.PriceScale);
                    nkz2  = Math.Round(nkz2, Instrument.PriceScale);
                    nkz4v = Math.Round(nkz4v, Instrument.PriceScale);
                    nkz2v = Math.Round(nkz2v, Instrument.PriceScale);
                    XXPrint("Первая прорисовка ВНИЗ - {0} - nkz2={1} nkz4={2} - zmin={3}", Bars[ci].Time, nkz2, nkz4, zmin);
                }

                toolRectangle.Point1 = new ChartPoint(Bars[ci].Time, nkz4);
                toolRectangle.Point2 = new ChartPoint(Bars[ci].Time.AddHours(24), nkz4v);

                toolRectangle1.Point1 = new ChartPoint(Bars[ci].Time, nkz2);
                toolRectangle1.Point2 = new ChartPoint(Bars[ci].Time.AddHours(24), nkz2v);
            }
//====================================================================================================================
            if (k > 0)
            {
                if (tu && Bars[ci].High > zmax)
                {
                    zmax  = Bars[ci].High;
                    nkz4  = zmax - ((NKZ - 5 - (NKZ * kf)) * Instrument.Point);
                    nkz2  = zmax - (((NKZ * 2) - (NKZ * 2 * kf)) * Instrument.Point);
                    nkz4v = zmax - ((NKZ - 5) * Instrument.Point);
                    nkz2v = zmax - ((NKZ * 2) * Instrument.Point);
                    nkz4  = Math.Round(nkz4, Instrument.PriceScale);
                    nkz2  = Math.Round(nkz2, Instrument.PriceScale);
                    nkz4v = Math.Round(nkz4v, Instrument.PriceScale);
                    nkz2v = Math.Round(nkz2v, Instrument.PriceScale);
                    XXPrint("ПЕРЕРИСОВКА ВВЕРХ - {0} - nkz2={1} nkz4={2} - 1U", Bars[ci].Time, nkz2, nkz4);
                    toolRectangle.Point1 = new ChartPoint(Bars[ci].Time, nkz4);
                    toolRectangle.Point2 = new ChartPoint(Bars[ci].Time.AddHours(48), nkz4v);
                    nd = false; nu = false;
                    toolRectangle1.Point1 = new ChartPoint(Bars[ci].Time, nkz2);
                    toolRectangle1.Point2 = new ChartPoint(Bars[ci].Time.AddHours(48), nkz2v);
                }

                if (td && Bars[ci].Low < zmin)
                {
                    zmin  = Bars[ci].Low;
                    nkz4  = zmin + ((NKZ - 5 - (NKZ * kf)) * Instrument.Point);
                    nkz2  = zmin + (((NKZ * 2) - (NKZ * 2 * kf)) * Instrument.Point);
                    nkz4v = zmin + ((NKZ - 5) * Instrument.Point);
                    nkz2v = zmin + (((NKZ * 2)) * Instrument.Point);
                    nkz4  = Math.Round(nkz4, Instrument.PriceScale);
                    nkz2  = Math.Round(nkz2, Instrument.PriceScale);
                    nkz4v = Math.Round(nkz4v, Instrument.PriceScale);
                    nkz2v = Math.Round(nkz2v, Instrument.PriceScale);
                    XXPrint("ПЕРЕРИСОВКА ВНИЗ - {0} - nkz2={1} nkz4={2} - 1U", Bars[ci].Time, nkz2, nkz4);
                    toolRectangle.Point1 = new ChartPoint(Bars[ci].Time, nkz4);
                    toolRectangle.Point2 = new ChartPoint(Bars[ci].Time.AddHours(48), nkz4v);
                    nd = false; nu = false;
                    toolRectangle1.Point1 = new ChartPoint(Bars[ci].Time, nkz2);
                    toolRectangle1.Point2 = new ChartPoint(Bars[ci].Time.AddHours(48), nkz2v);
                }


//== Касание зоны/1\4 ======================================================================================================
                if (n4)
                {
                    if (tu && Bars[ci].Low < nkz4 && !nu)
                    {
                        nu = true; XXPrint("ПЕРЕСЕЧЕНИЕ 1/4 ВНИЗ - {0} - {1}", Bars[ci].Time, nkz4);
                    }
                    if (td && Bars[ci].High > nkz4 && !nd)
                    {
                        nd = true; XXPrint("ПЕРЕСЕЧЕНИЕ 1/4 ВВЕРХ - {0} - {1}", Bars[ci].Time, nkz4);
                    }
                }
                if (tu && Bars[ci].Low < nkz2 && !nu)
                {
                    nu = true; XXPrint("ПЕРЕСЕЧЕНИЕ 1/2 ВНИЗ - {0} - {1}", Bars[ci].Time, nkz2);
                }
                if (td && Bars[ci].High > nkz2 && !nd)
                {
                    nd = true; XXPrint("ПЕРЕСЕЧЕНИЕ 1/2 ВВЕРХ - {0} - {1}", Bars[ci].Time, nkz2);
                }
// Если пересечение зоны было (nu) и торгуем вверх (tu) ===========================================================
                if (nu && tu)
                {
// Определение патерна   ПИК ПОСЛЕДНЕГО ФРАКТАЛА ПАТЕРНА ВНИЗУ
                    if (_frInd.TopSeries[Bars.Range.To - frac] > 0)
                    {
                        if (frU3 > frU2 && frU1 > frU2)
                        {
                            var vl1          = Tools.Create <VerticalLine>(); vl1.Time = Bars[ci].Time; vl1.Color = Color.Aqua; vl1.Width = 2;
                            var toolPolyLine = Tools.Create <PolyLine>(); toolPolyLine.Color = Color.Aqua; toolPolyLine.Width = 2;
                            toolPolyLine.AddPoint(new ChartPoint(tmU3, frU3));
                            toolPolyLine.AddPoint(new ChartPoint(tmU2, frU2));
                            toolPolyLine.AddPoint(new ChartPoint(tmU1, frU1));

                            TPU  = zmax;
                            SLU  = frD2 - Instrument.Spread - dlt; SLU = Math.Round(SLU, Instrument.PriceScale);
                            PRU  = Math.Round((TPU - Bars[ci].Close) * Math.Pow(10, Instrument.PriceScale), 0);
                            UBU  = Math.Round((Bars[ci].Close - SLU) * Math.Pow(10, Instrument.PriceScale), 0);
                            PCU  = Math.Round(PRU / UBU, 2);
                            Ot   = Math.Round((frU3 - frU2) / (frU1 - frU2), 2);
                            PRUi = (int)PRU;
                            UBUi = (int)UBU;
                            Print("{8} PATERN BUY -- {0} -- TP={1} SL={2} TP/SL={3} Fract 1-{4} 2-{5} 3-{6} Ot={7}", Bars[ci].Time, PRUi, UBUi, PCU, tmU1, tmU2, tmU3, Ot, Instrument.Name);
                            XXPrint("PATERN BUY -- {0} -- TP={1} SL={2} TP/SL={3} Fract 1-{4} 2-{5} 3-{6} Ot={7}", Bars[ci].Time, PRUi, UBUi, PCU, tmU1, tmU2, tmU3, Ot);
                            if (UBU > 20 && UBU < SL1 && PCU > 2 && Bars[ci].Close > nkz2)
                            {
                                XXPrint("BUY -- {0} -- TP={1} SL={2} TP/SL={3} Fract 1-{4} 2-{5} 3-{6} Ot={7}", Bars[ci].Time, PRU, UBU, PCU, tmU1, tmU2, tmU3, Ot);
                                Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1, Stops.InPips(UBUi, PRUi), null, null);
                                frD = false;
                            }
                        }
                    }
                }
// Если пересечение зоны было (nd) и торгуем вниз (td) ===========================================================
                if (nd && td)
                {
// Определение патерна   ПИК ПОСЛЕДНЕГО ФРАКТАЛА ПАТЕРНА ВВЕРХУ
                    if (_frInd.BottomSeries[Bars.Range.To - frac] > 0)
                    {
                        if (frD2 > frD3 && frD2 > frD1)
                        {
                            var vl1          = Tools.Create <VerticalLine>(); vl1.Time = Bars[ci].Time; vl1.Color = Color.Aqua; vl1.Width = 2;
                            var toolPolyLine = Tools.Create <PolyLine>(); toolPolyLine.Color = Color.Aqua; toolPolyLine.Width = 2;
                            toolPolyLine.AddPoint(new ChartPoint(tmD3, frD3));
                            toolPolyLine.AddPoint(new ChartPoint(tmD2, frD2));
                            toolPolyLine.AddPoint(new ChartPoint(tmD1, frD1));

                            TPD = zmin; TPD = Math.Round(TPD, Instrument.PriceScale);
                            SLD = frU2 + Instrument.Spread + dlt; SLD = Math.Round(SLD, Instrument.PriceScale);
                            PRD = Math.Round((Bars[ci].Close - TPD) * Math.Pow(10, Instrument.PriceScale), 0);

                            Ot  = Math.Round((frD2 - frD3) / (frD2 - frD1), 2);
                            UBD = Math.Round((SLD - Bars[ci].Close) * Math.Pow(10, Instrument.PriceScale), 0);

                            PRDi = (int)PRD;
                            UBDi = (int)UBD;
                            PCD  = Math.Round(PRD / UBD, 2);
                            Print("{8}  PATERN SELL -- {0} -- TP(п)={1} SL(p)={2} TP/SL={3} Fract 1-{4} 2-{5} 3-{6}  Ot={7}", Bars[ci].Time, PRDi, UBDi, PCD, tmD1, tmD2, tmD3, Ot, Instrument.Name);
                            XXPrint("PATERN SELL -- {0} -- TP(п)={1} SL(p)={2} TP/SL={3} Fract 1-{4} 2-{5} 3-{6}  Ot={7}", Bars[ci].Time, PRDi, UBDi, PCD, tmD1, tmD2, tmD3, Ot);
                            if (UBD > 20 && UBD < SL1 && PCD > 2 && Bars[ci].Close < nkz2)
                            {
                                XXPrint("SELL -- {0} -- TP={1} SL={2} TP/SL={3} Fract 1-{4} 2-{5} 3-{6} Ot={7}", Bars[ci].Time, PRDi, UBDi, PCD, tmD1, tmD2, tmD3, Ot);
                                Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1, Stops.InPips(UBDi, PRDi), null, null);
                                frU = false;
                            }
                        }
                    }
                }
            }
            k++;
        }
Exemplo n.º 26
0
        protected override void NewBar()
        {
            DTime = Bars[Bars.Range.To - 1].Time;
            SL    = SL_ / 100;     TP = TP_ / 100;
            //Print("TP_={0} SL_={1}",TP_,SL_);
            // Event occurs on every new bar
            // Значения Alligator около фрактала
            aGuba5    = _allInd.LipsSeries[Bars.Range.To - 5];
            aZub5     = _allInd.TeethSeries[Bars.Range.To - 5];
            aChelust5 = _allInd.JawsSeries[Bars.Range.To - 5];

            //  frUp frDown - Истина если появился НОВЫЙ фрактал Вверх/Вниз
            if (_frInd.TopSeries[Bars.Range.To - 5] > 0)
            {
                frUp = true;
            }
            else
            {
                frUp = false;
            }
            if (_frInd.BottomSeries[Bars.Range.To - 5] > 0)
            {
                frDown = true;
            }
            else
            {
                frDown = false;
            }

// ===  SET BARS FRACTAL ==============================================================================================================================
            // Срабатывает - когда появился новый фрактал - frUp frDown=true!
            // Запоминаем значения Свечи бара-фрактала(frUpH) и время (frUp_Time)
            if (frUp)                   // Print("Появился фрактал ВВЕРХ - {0}",Bars[Bars.Range.To-5].Time);
            {
                frUpH = Bars[Bars.Range.To - 5].High;
                frUpL = Bars[Bars.Range.To - 5].Low;
                if (frUpL > aGuba5 && frUpL > aChelust5 && frUpL > aZub5)                            //Низ Бар-Фрактала выше Alligator
                {
                    fr_all_Up = frUpH; fr_all_Up_L = true;
                    //======= UPDATE SELL  ====================================================================
                    if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Pending)
                    {
                        var result = Trade.UpdatePendingPosition(posGuidSell, 0.1, fr_all_Up + TP, fr_all_Up + SL, null);
                        if (result.IsSuccessful)
                        {
                            posGuidSell = result.Position.Id;
                            Print("00 UpdatePending - !появился новый фр-рабочий - переносим цену Open={0} SL={1} TP={2} - {3}", fr_all_Up, SL_Sell, fr_all_Down - TP, DTime);
                        }
                    }
                    Print("01 Фрактал ВВЕРХ и выше Алигатора! - {0} ", Bars[Bars.Range.To - 1].Time);
                }
                else
                {                                            // Если же фр-касается аллигатора - отменяем пред отложеній ордер
                    if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Pending)
                    {
                        var res = Trade.CancelPendingPosition(posGuidSell);
                        if (res.IsSuccessful)
                        {
                            posGuidSell = Guid.Empty;
                            Print("02 появился новый фр-рабочий ВВЕРХ и рабочий отменяем отложеный Sell CancelPending - {0}", DTime);
                        }
                    }
                }
                // Если есть активный ордер - установить новый стоп
                if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Active)
                {
                    var result2 = Trade.UpdateMarketPosition(posGuidSell, frUpH, null, null);
                    if (result2.IsSuccessful)
                    {
                        posGuidSell = result2.Position.Id;
                        Print("03 SELL - !UpdateActive StopLoss когда Появился новый фрактал Up - SL={0} - {1}", frUpH, DTime);
                    }
                }
            }

            if (frDown)                    // Print("Появился фрактал ВНИЗ - {0}",Bars[Bars.Range.To-5].Time);
            {
                frDownL = Bars[Bars.Range.To - 5].Low;
                frDownH = Bars[Bars.Range.To - 5].High;
                if (frDownH < aGuba5 && frDownH < aChelust5 && frDownH < aZub5)
                {
                    fr_all_Down = frDownL; fr_all_Down_L = true;
                    //======= UPDATE BUY  =========================================================================
                    if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Pending)
                    {
                        var result = Trade.UpdatePendingPosition(posGuidBuy, 0.1, fr_all_Down - TP, fr_all_Down - SL, null);
                        if (result.IsSuccessful)
                        {
                            posGuidBuy = result.Position.Id;
                            Print("04 Sell UpdatePending - появился новый фр-рабочий - переносим цену и StopLoss - Open={0} - SL={1} - TP={2} ", fr_all_Down, SL_Buy, fr_all_Up + TP, DTime);
                        }
                        else
                        {
                            Print("05 Sell UpdatePending - ERROR - - Open={0} - SL={1} - TP={2}  ", fr_all_Down, SL_Buy, fr_all_Up + TP, DTime);
                        }
                    }
                    Print("06 Фрактал НИЖЕ Алигатора! - {0}", Bars[Bars.Range.To - 1].Time);
                }
                else
                {                                                        // Если же фр-касается аллигатора - отменяем пред отложеній ордер
                    if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Pending)
                    {
                        var res = Trade.CancelPendingPosition(posGuidBuy);
                        if (res.IsSuccessful)
                        {
                            posGuidBuy = Guid.Empty;
                            Print("07 появился новый фр-рабочий ВНИЗ и касается АЛЛ отменяем отложеный Sell CancelPending - {0}", DTime);
                        }
                    }
                }
                // Появился новый фрактал ВНИЗ и ОТКРЫТА позиция ВВЕРХУ Buy - переносим стоп - Работает!
                if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Active)
                {
                    var result2 = Trade.UpdateMarketPosition(posGuidBuy, frDownL, null, null);
                    if (result2.IsSuccessful)
                    {
                        posGuidBuy = result2.Position.Id;
                        Print("08 BuyStop - !UpdatePending StopLoss когда Появился новый фрактал Down - SL={0} {1}", frDownL - TP, DTime);
                    }
                }
            }



            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State != PositionState.Closed)
            {
                frFirstU = true;
                vlR.Time = Bars[Bars.Range.To - 1000].Time;
            }
            if (frUp && frFirstU && posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Active)
            {
                frFirstU = false; vlR.Time = Bars[Bars.Range.To - 1].Time;
            }

            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State != PositionState.Closed)
            {
                frFirstD = true;
                vlB.Time = Bars[Bars.Range.To - 1000].Time;
            }
            if (frDown && frFirstD && posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Active)
            {
                frFirstD = false; vlB.Time = Bars[Bars.Range.To - 1].Time;
            }


//=== КОРЕКЦИЯ ===========================================================================================================

            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                var pos = Trade.GetPosition(posGuidBuy); if (pos.Profit < 0)
                {
                    TPB = false; TPS = true;
                }
                posGuidBuy = Guid.Empty; Print("09 - Buy - Закрыто по StopLoss (Корекция) - {0} ", DTime);
            }

            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                var pos = Trade.GetPosition(posGuidSell); if (pos.Profit < 0)
                {
                    TPB = true; TPS = false;
                }
                posGuidSell = Guid.Empty;  Print("10 -Sell - Закрыто по StopLoss (Корекция) - {0} ", DTime);
            }

//====  BUYLIMIT  START  =====================================================================================================
            if (frDown && fr_all_Down_L && posGuidBuy == Guid.Empty)
            {
                // Открываем BuyLimit
                var result = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.BuyLimit, 0.1, fr_all_Down - TP, 0, Stops.InPips(300, null), null, null, null);
                if (result.IsSuccessful)
                {
                    posGuidBuy = result.Position.Id;
                    Print("11 BuyLimit! OPEN - O={0} - SL={1} - TP={2} {3}", fr_all_Down, SL_Buy, fr_all_Up + TP, DTime);
                }
                else
                {
                    Print("12 BuyLimit! ERROR - O={0} - SL={1} - TP={2} {3}", fr_all_Down, SL_Buy, fr_all_Up + TP, DTime);
                }
            }
//==   SELLIMIT START   ================================================================================
            if (frUp && fr_all_Up_L && posGuidSell == Guid.Empty)
            {
                var result = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.SellLimit, 0.1, fr_all_Up + TP, 0, Stops.InPips(300, null), null, null, null);
                //Stops.InPips(200,null)
                if (result.IsSuccessful)
                {
                    posGuidSell = result.Position.Id;
                    Print("13 SellLimit OPEN - O={0} - SL={1} - TP={2} {3}", fr_all_Up, SL_Sell, fr_all_Down - TP, DTime);
                }
                else
                {
                    Print("14 SellLimit ERROR - O={0} - SL={1} - TP={2} {3}", fr_all_Up, SL_Sell, fr_all_Down - TP, DTime);
                }
            }
        }
Exemplo n.º 27
0
        protected override void NewBar()
        {
// =================================================================================================================================
            // Определение времени торговли ОТ и ДО
            if (Bars[Bars.Range.To - 1].Time.Hour == 5 && Bars[Bars.Range.To - 1].Time.Minute == 00)
            {
                Torg = true;
            }
            if (Bars[Bars.Range.To - 1].Time.Hour == 10 && Bars[Bars.Range.To - 1].Time.Minute == 00)
            {
                Torg = false;
            }

            // Значения текущего Бара
            BarH  = Bars[Bars.Range.To - 1].High;
            BarL  = Bars[Bars.Range.To - 1].Low;
            BarC  = Bars[Bars.Range.To - 1].Close;
            DTime = Bars[Bars.Range.To - 1].Time;

            //  frUp frDown - Истина если появился НОВЫЙ фрактал Вверх/Вниз
            frSU = _frInd.TopSeries[Bars.Range.To - 5];
            frSD = _frInd.BottomSeries[Bars.Range.To - 5];

            if (frSU > 0)
            {
                frUp = true;
            }
            else
            {
                frUp = false;
            }
            if (frSD > 0)
            {
                frDown = true;
            }
            else
            {
                frDown = false;
            }

            // Значения Alligator около фрактала
            aGuba5    = _allInd.LipsSeries[Bars.Range.To - 5];
            aZub5     = _allInd.TeethSeries[Bars.Range.To - 5];
            aChelust5 = _allInd.JawsSeries[Bars.Range.To - 5];

            // Значене Alligator у цены
            aGuba    = _allInd.LipsSeries[Bars.Range.To];   //З
            aZub     = _allInd.TeethSeries[Bars.Range.To];  //К
            aChelust = _allInd.JawsSeries[Bars.Range.To];   //С

            // Значене Alligator у цены
            aGuba1    = _allInd.LipsSeries[Bars.Range.To - 1];  //З
            aZub1     = _allInd.TeethSeries[Bars.Range.To - 1]; //К
            aChelust1 = _allInd.JawsSeries[Bars.Range.To - 1];  //С

            if (aGuba > aGuba1 && aZub > aZub1 && aChelust > aChelust1)
            {
                allEstU = true;
            }
            else
            {
                allEstU = false;
            }
            if (aGuba1 > aGuba && aZub1 > aZub && aChelust1 > aChelust)
            {
                allEstD = true;
            }
            else
            {
                allEstD = false;
            }
// =================================================================================================================================
            // Срабатывает - когда появился новый фрактал - frUp frDown=true!
            // Запоминаем значения Свечи бара-фрактала(frUpH) и время (frUp_Time)
            if (frSU > 0)
            {
                frUpH     = Bars[Bars.Range.To - 5].High;
                frUpL     = Bars[Bars.Range.To - 5].Low;
                frUpC     = Bars[Bars.Range.To - 5].Close;
                frUp_Time = Bars[Bars.Range.To - 5].Time;
            }
            if (frSD > 0)
            {
                frDownL     = Bars[Bars.Range.To - 5].Low;
                frDownH     = Bars[Bars.Range.To - 5].High;
                frDownC     = Bars[Bars.Range.To - 5].Close;
                frDown_Time = Bars[Bars.Range.To - 5].Time;
            }
//===============================================================================================================================
            // Появился новый фрактал и Свеча Fractalа ВЫШЕ Alligatora не касается Alligatorа
            // низ Бар-Фрактала выше Alligator  - Назначаем рабочим (fr_all_Up) для Buy
            // fr_all_Up_L - если появился рабочий фрактал Buy - true
            if (frSU > 0)
            {
                if (frUpL > aGuba5 && frUpL > aChelust5 && frUpL > aZub5)
                {
                    fr_all_Up = frUpH; fr_all_Up_Time = frUp_Time;  fr_all_Up_L = true;
                }
                else
                // Если появился новый фрактал  ВВЕРХ и касается Алигатора - отменяем "рабочий"
                {
                    fr_all_Up_L = false;   vlR.Time = Bars[Bars.Range.To - 1000].Time;
                    if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Pending)
                    {
                        var res = Trade.CancelPendingPosition(posGuidBuy);
                        if (res.IsSuccessful)
                        {
                            posGuidBuy = Guid.Empty;
                        }
                    }
                }
            }
//===============================================================================================================================
            // Появился новый фрактал ВНИЗ  и Свеча Fractalа НИЖЕ  Alligatora не касается Alligatorа
            // ВЕРХ Бар-Фрактала НИЖЕ Alligator  - Назначаем рабочим (fr_all_Down) для SellLimit

            if (frSD > 0)
            {
                if (frDownH < aGuba5 && frDownH < aChelust5 && frDownH < aZub5)
                {
                    fr_all_Down = frDownL;  fr_all_Down_Time = frDown_Time; fr_all_Down_L = true;
                }
                else
                // Если появился новый фрактал  ВНИЗ  и касается Алигатора - отменяем "рабочий"
                {
                    fr_all_Down_L = false;
                    if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Pending)
                    {
                        var res = Trade.CancelPendingPosition(posGuidSell);
                        if (res.IsSuccessful)
                        {
                            posGuidSell = Guid.Empty;
                        }
                    }
                }
            }
//===============================================================================================================================
            // Появился новый фрактал ВВЕРХ и назначен рабочим и есть отложеный ордер
            // меняем цену открытия и ставим TP - Низ/Верх противоположного фрактала
            if (frSU > 0 && fr_all_Up_L && posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Pending)
            {                 // Ордер SellLimit-ВНИЗ - Цена открытия=High раб фрактала
                var result = Trade.UpdatePendingPosition(posGuidBuy, 0.1, fr_all_Up + TP, frDownL - TP, null);
                if (result.IsSuccessful)
                {
                    posGuidBuy = result.Position.Id;
                }
            }
//===============================================================================================================================
            // Если появился новый фрактал и не подходит под рабочий и есть отрытый отложеный ордер - отменяем ордер Buy
            if (frSU > 0 && !fr_all_Up_L && posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Pending)
            {
                var res = Trade.CancelPendingPosition(posGuidBuy);  if (res.IsSuccessful)
                {
                    posGuidBuy = Guid.Empty;
                }
            }

//=============================----------------------------------------=====================================================
            if (Torg && frSU > 0 && fr_all_Up_L && posGuidBuy == Guid.Empty)
            {
                // $$== ТОРГУЕМ!!!! ==$$  открываем BuyStop - Цена=High раб. ВВЕРХ фрактала SL=200p TP=Низ против. фрактала
                var result = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.BuyStop, 0.1, fr_all_Up + TP, 0, Stops.InPips(200, null), null, null, null);
                if (result.IsSuccessful)
                {
                    posGuidBuy = result.Position.Id;
                }
            }

//===============================================================================================================================
            // Появился новый фрактал ВНИЗ  и назначен рабочим но есть отложеный ВНИЗУ ордер
            // переносим цену открытия и ставим Stop - Низ/Верх противоположного фрактала
            if (frSD > 0 && fr_all_Down_L && posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Pending)
            {
                var result = Trade.UpdatePendingPosition(posGuidSell, 0.1, fr_all_Down - TP, frUpH + TP, null);
                if (result.IsSuccessful)
                {
                    posGuidSell = result.Position.Id;
                }
            }
//===============================================================================================================================
            // Если появился новый фрактал ВНИЗ и не подходит под рабочий и есть отрытый отложеный ордер - отменяем ордер Sell
            if (frSD > 0 && !fr_all_Down_L && posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Pending)
            {
                var res = Trade.CancelPendingPosition(posGuidSell);
                if (res.IsSuccessful)
                {
                    posGuidSell = Guid.Empty;
                }
            }
//===============================================================================================================================
            // Алигатор переплетен и появился новый фрактал ВНИЗ и назначен рабочим и нет открытых Sell ордеров  (posGuidSell)-
            // ставим отложеный ордер SellStop и отменяем BuyStop - если открыт!
            if (Torg && frSD > 0 && fr_all_Down_L && posGuidSell == Guid.Empty)
            {               // Открываем SellStop
                var result = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.SellStop, 0.1, fr_all_Down - TP, 0, Stops.InPips(200, null), null, null, null);
                if (result.IsSuccessful)
                {
                    posGuidSell = result.Position.Id;
                }
            }
//===============================================================================================================================
            // Появился новый фрактал ВВЕРХ и открыта позиция Sell - переосим стоп
            if (frSU > 0 && posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Active)
            {
                var res3 = Trade.UpdateMarketPosition(posGuidSell, frUpH + TP, null, null);
                if (res3.IsSuccessful)
                {
                    posGuidSell = res3.Position.Id;
                }
            }

//===============================================================================================================================
            // Появился новый фрактал ВВЕРХ и открыта позиция Sell - переосим стоп
            if (frSD > 0 && posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Active)
            {
                var res4 = Trade.UpdateMarketPosition(posGuidBuy, frDownL - TP, null, null);
                if (res4.IsSuccessful)
                {
                    posGuidSell = res4.Position.Id;
                }
            }

// =================================================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty; Print("Buy - Закрыто по StopLoss (Корекция) - {0} ", DTime);
            }

            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty;  Print("Sell - Закрыто по StopLoss (Корекция) - {0} ", DTime);
            }
        }
Exemplo n.º 28
0
//===========================================================================
        protected override void NewBar()
        {
            DTime = Bars[ci].Time;
            ci    = Bars.Range.To - 1;

//=== КОРЕКЦИЯ =======================================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty;
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty;
            }
            if (posGuidBuy2 != Guid.Empty && Trade.GetPosition(posGuidBuy2).State == PositionState.Closed)
            {
                posGuidBuy2 = Guid.Empty;
            }
            if (posGuidSell2 != Guid.Empty && Trade.GetPosition(posGuidSell2).State == PositionState.Closed)
            {
                posGuidSell2 = Guid.Empty;
            }

//====  ИСТОРИЯ Fractal ==================================================================================
//Print("1");
            i = 0;
            while (!(frU1 > 0 && frU2 > 0 && frU3 > 0 && frD1 > 0 && frD2 > 0 && frD3 > 0) && i < 200)
            {
                ki++;
                if (_frInd.TopSeries[Bars.Range.To - frac - i] > 0)
                {
                    if (frU)
                    {
                        frU  = false;
                        frU1 = frU2; frU2 = frU3; frU3 = _frInd.TopSeries[Bars.Range.To - frac - i];
                        tmU1 = tmU2; tmU2 = tmU3; tmU3 = Bars[Bars.Range.To - frac - i].Time;
                    }
                    else
                    {
                        if (_frInd.TopSeries[Bars.Range.To - frac - i] > frU1)
                        {
                            frU3 = _frInd.TopSeries[Bars.Range.To - frac - i];
                            tmU3 = Bars[Bars.Range.To - frac - i].Time;
                        }
                    }
                    XXPrint("U ИТОРИЯ- {0} - frU1={1} frU2={2} frU3={3} frD1={4} frD2={5} frD3={6} i={7}", Bars[Bars.Range.To - frac - i].Time, frU1, frU2, frU3, frD1, frD2, frD3, i);
                }

                if (_frInd.BottomSeries[Bars.Range.To - frac - i] > 0)
                {
                    if (!frU)
                    {
                        frU  = true;
                        frD1 = frD2; frD2 = frD3; frD3 = _frInd.BottomSeries[Bars.Range.To - frac - i];
                        tmD1 = tmD2; tmD2 = tmD3; tmD3 = Bars[Bars.Range.To - frac - i].Time;
                    }
                    else
                    {
                        if (_frInd.BottomSeries[Bars.Range.To - frac - i] < frD1)
                        {
                            frD3 = _frInd.BottomSeries[Bars.Range.To - frac - i];
                            tmD3 = Bars[Bars.Range.To - frac - i].Time;
                        }
                    }
                    XXPrint("D ИСТОРИЯ - {0} - frU1={1} frU2={2} frU3={3} frD1={4} frD2={5} frD3={6} i={7}", Bars[Bars.Range.To - frac - i].Time, frU1, frU2, frU3, frD1, frD2, frD3, i);
                }
                i++;
            }

//====  Fractal вверх ====================================================================================
            if (_frInd.TopSeries[Bars.Range.To - frac] > 0)
            {
                if (frU)
                {
                    frU  = false;
                    frU3 = frU2; frU2 = frU1; frU1 = _frInd.TopSeries[Bars.Range.To - frac];
                    tmU3 = tmU2; tmU2 = tmU1; tmU1 = Bars[Bars.Range.To - frac].Time;
                }
                else
                {
                    if (_frInd.TopSeries[Bars.Range.To - frac] > frU1)
                    {
                        frU1 = _frInd.TopSeries[Bars.Range.To - frac];
                        tmU1 = Bars[Bars.Range.To - frac].Time;
                    }
                }
                if (tu)
                {
                    toolTriangle.Point1 = new ChartPoint(tmU1, frU1);
                    toolTriangle.Point2 = new ChartPoint(tmU2, frU2);
                    toolTriangle.Point3 = new ChartPoint(tmU3, frU3);
                }
                XXPrint("{0} - frU1={1} frU2={2} frU3={3} frD1={4} frD2={5} frD3={6} nkz4={7} nkz2={8} zmax={9} zmin={10}", Bars[Bars.Range.To - frac].Time, frU1, frU2, frU3, frD1, frD2, frD3, nkz4, nkz2, zmax, zmin);
            }
//====  Fractal вниз ====================================================================================
            if (_frInd.BottomSeries[Bars.Range.To - frac] > 0)
            {
                if (!frU)
                {
                    frU  = true;
                    frD3 = frD2; frD2 = frD1; frD1 = _frInd.BottomSeries[Bars.Range.To - frac];
                    tmD3 = tmD2; tmD2 = tmD1; tmD1 = Bars[Bars.Range.To - frac].Time;
                }
                else
                {
                    if (_frInd.BottomSeries[Bars.Range.To - frac] < frD1)
                    {
                        frD1 = _frInd.BottomSeries[Bars.Range.To - frac];
                        tmD1 = Bars[Bars.Range.To - frac].Time;
                    }
                }
                if (td)
                {
                    toolTriangle.Point1 = new ChartPoint(tmD1, frD1);
                    toolTriangle.Point2 = new ChartPoint(tmD2, frD2);
                    toolTriangle.Point3 = new ChartPoint(tmD3, frD3);
                }
                XXPrint("{0} - frU1={1} frU2={2} frU3={3} frD1={4} frD2={5} frD3={6} nkz4={7} nkz2={8} zmax={9} zmin={10}", Bars[Bars.Range.To - frac].Time, frU1, frU2, frU3, frD1, frD2, frD3, nkz4, zmax, zmin);
            }
// Первое начиртание зон	=====================================================================================
            if (k == 0)
            {
                nu = false; nd = false;

                if (tu)
                {
                    zmax  = dt1 * Instrument.Point;
                    nkz4  = zmax - ((NKZ - 5 - (NKZ * kf)) * Instrument.Point);
                    nkz2  = zmax - (((NKZ * 2) - (NKZ * 2 * kf)) * Instrument.Point);
                    nkz4v = zmax - ((NKZ - 5) * Instrument.Point);
                    nkz2v = zmax - ((NKZ * 2) * Instrument.Point);
                    nkz4  = Math.Round(nkz4, Instrument.PriceScale);
                    nkz2  = Math.Round(nkz2, Instrument.PriceScale);
                    nkz4v = Math.Round(nkz4v, Instrument.PriceScale);
                    nkz2v = Math.Round(nkz2v, Instrument.PriceScale);
                    XXPrint("Первая прорисовка ВВЕРХ - {0} - nkz2={1} nkz4={2} - zmax={3}", Bars[ci].Time, nkz2, nkz4, zmax);
                    NKZText.Point   = new ChartPoint(Bars[ci].Time, nkz4);
                    NKZText.Caption = string.Format("max={0} nkz4={1} nu={2}", zmax, nkz4, nu);
                }

                if (td)
                {
                    zmin  = dt1 * Instrument.Point;
                    nkz4  = zmin + ((NKZ - 5 - (NKZ * kf)) * Instrument.Point);
                    nkz2  = zmin + (((NKZ * 2) - (NKZ * 2 * kf)) * Instrument.Point);
                    nkz4v = zmin + ((NKZ - 5) * Instrument.Point);
                    nkz2v = zmin + (((NKZ * 2)) * Instrument.Point);
                    nkz4  = Math.Round(nkz4, Instrument.PriceScale);
                    nkz2  = Math.Round(nkz2, Instrument.PriceScale);
                    nkz4v = Math.Round(nkz4v, Instrument.PriceScale);
                    nkz2v = Math.Round(nkz2v, Instrument.PriceScale);
                    XXPrint("Первая прорисовка ВНИЗ - {0} - nkz2={1} nkz4={2} - zmin={3}", Bars[ci].Time, nkz2, nkz4, zmin);
                    NKZText.Point    = new ChartPoint(Bars[ci].Time, nkz4v);
                    NKZText.FontSize = 10;
                    NKZText.Caption  = string.Format("min={0} nkz4={1} nd={2}", zmin, nkz4, nd);
                }
                lnkz.Price           = nkz4;
                toolRectangle.Point1 = new ChartPoint(Bars[ci].Time, nkz4);
                toolRectangle.Point2 = new ChartPoint(Bars[ci].Time.AddHours(24), nkz4v);

                toolRectangle1.Point1 = new ChartPoint(Bars[ci].Time, nkz2);
                toolRectangle1.Point2 = new ChartPoint(Bars[ci].Time.AddHours(24), nkz2v);
            }
//====================================================================================================================
            if (k > 0)
            {
                if (tu && Bars[ci].High > zmax)
                {
                    zmax = Bars[ci].High;
                    toolArrowDown.Point = new ChartPoint(Bars[ci].Time, zmax + (50 * Instrument.Point));
                    toolArrowDown.Color = Color.Aqua;
                    nkz4  = zmax - ((NKZ - 5 - (NKZ * kf)) * Instrument.Point);
                    nkz2  = zmax - (((NKZ * 2) - (NKZ * 2 * kf)) * Instrument.Point);
                    nkz4v = zmax - ((NKZ - 5) * Instrument.Point);
                    nkz2v = zmax - ((NKZ * 2) * Instrument.Point);
                    nkz4  = Math.Round(nkz4, Instrument.PriceScale);
                    nkz2  = Math.Round(nkz2, Instrument.PriceScale);
                    nkz4v = Math.Round(nkz4v, Instrument.PriceScale);
                    nkz2v = Math.Round(nkz2v, Instrument.PriceScale);
                    XXPrint("ПЕРЕРИСОВКА ВВЕРХ - {0} - nkz2={1} nkz4={2}", Bars[ci].Time, nkz2, nkz4);
                    NKZText.Point        = new ChartPoint(Bars[ci].Time, nkz4);
                    NKZText.FontSize     = 10;
                    NKZText.Caption      = string.Format("max={0} nkz4={1} nkz2={2}", zmax, nkz4, nkz2);
                    lnkz.Price           = nkz4;
                    toolRectangle.Point1 = new ChartPoint(Bars[ci].Time, nkz4);
                    toolRectangle.Point2 = new ChartPoint(Bars[ci].Time.AddHours(48), nkz4v);
                    nd = false; nu = false;
                    toolRectangle1.Point1 = new ChartPoint(Bars[ci].Time, nkz2);
                    toolRectangle1.Point2 = new ChartPoint(Bars[ci].Time.AddHours(48), nkz2v);
                }

                if (td && Bars[ci].Low < zmin)
                {
                    zmin = Bars[ci].Low;
                    toolArrowUp.Point = new ChartPoint(Bars[ci].Time, zmin - (10 * Instrument.Point));
                    toolArrowUp.Color = Color.Aqua;
                    nkz4  = zmin + ((NKZ - 5 - (NKZ * kf)) * Instrument.Point);
                    nkz2  = zmin + (((NKZ * 2) - (NKZ * 2 * kf)) * Instrument.Point);
                    nkz4v = zmin + ((NKZ - 5) * Instrument.Point);
                    nkz2v = zmin + (((NKZ * 2)) * Instrument.Point);
                    nkz4  = Math.Round(nkz4, Instrument.PriceScale);
                    nkz2  = Math.Round(nkz2, Instrument.PriceScale);
                    nkz4v = Math.Round(nkz4v, Instrument.PriceScale);
                    nkz2v = Math.Round(nkz2v, Instrument.PriceScale);
                    XXPrint("ПЕРЕРИСОВКА ВНИЗ - {0} - nkz2={1} nkz4={2}", Bars[ci].Time, nkz2, nkz4);
                    NKZText.Point        = new ChartPoint(Bars[ci].Time, nkz4v);
                    NKZText.FontSize     = 10;
                    NKZText.Caption      = string.Format("min={0} nkz4={1} nkz2={2}", zmin, nkz4, nkz2);
                    lnkz.Price           = nkz4;
                    toolRectangle.Point1 = new ChartPoint(Bars[ci].Time, nkz4);
                    toolRectangle.Point2 = new ChartPoint(Bars[ci].Time.AddHours(48), nkz4v);
                    nd = false; nu = false;
                    toolRectangle1.Point1 = new ChartPoint(Bars[ci].Time, nkz2);
                    toolRectangle1.Point2 = new ChartPoint(Bars[ci].Time.AddHours(48), nkz2v);
                }
//== Касание зоны/1\2 ======================================================================================================
                if (tu && Bars[ci].Low < nkz4 && !nu)
                {
                    nu = true; XXPrint("ПЕРЕСЕЧЕНИЕ 1/4 ВНИЗ  - {0} - {1}", Bars[ci].Time, nkz4);
                }
                if (td && Bars[ci].High > nkz4 && !nd)
                {
                    nd = true; XXPrint("ПЕРЕСЕЧЕНИЕ 1/4 ВВЕРХ - {0} - {1}", Bars[ci].Time, nkz4);
                }
// Если пересечение зоны было (nu) и торгуем вверх (tu) ===========================================================
                if (nu && tu)
                {
// Определение патерна   ПИК ПОСЛЕДНЕГО ФРАКТАЛА ПАТЕРНА ВНИЗУ
                    if (_frInd.TopSeries[Bars.Range.To - frac] > 0)
                    {
                        if (frU3 > frU2 && frU1 > frU2)
                        {
                            var toolPolyLine = Tools.Create <PolyLine>(); toolPolyLine.Color = Color.Aqua; toolPolyLine.Width = 2;
                            toolPolyLine.AddPoint(new ChartPoint(tmU3, frU3));
                            toolPolyLine.AddPoint(new ChartPoint(tmU2, frU2));
                            toolPolyLine.AddPoint(new ChartPoint(tmU1, frU1));

                            TPU  = zmax;
                            SLU  = frD2 - Instrument.Spread - dlt; SLU = Math.Round(SLU, Instrument.PriceScale);
                            PRU  = Math.Round((TPU - Bars[ci].Close) * Math.Pow(10, Instrument.PriceScale), 0);
                            PRU2 = Math.Round((TP2 - Bars[ci].Close) * Math.Pow(10, Instrument.PriceScale), 0);
                            UBU  = Math.Round((Bars[ci].Close - SLU) * Math.Pow(10, Instrument.PriceScale), 0);
                            PCU  = Math.Round(PRU / UBU, 2);
                            PCU2 = Math.Round(PRU2 / UBU, 2);
                            Ot   = Math.Round((frU3 - frU2) / (frU1 - frU2), 2);
                            PRUi = (int)PRU;
                            UBUi = (int)UBU;
                            XXPrint("PATERN BUY -- {0} -- TP={1} SL={2} TP/SL={3} Fract 1-{4} 2-{5} 3-{6} Ot={7}", Bars[ci].Time, PRUi, UBUi, PCU, tmU1, tmU2, tmU3, Ot);

                            var toolText = Tools.Create <Text>();
                            toolText.Color    = Color.Blue;
                            toolText.FontSize = 10;
                            toolText.Point    = new ChartPoint(tmU1, frU1 + (50 * Instrument.Point));
                            toolText.Caption  = string.Format("TP={0} SL={1} C={2} C2={3}", PRU, UBU, PCU, PCU2);

                            if (UBU > 20 && UBU < SL1 && Bars[ci].Close > nkz2)
                            {
                                if (posGuidBuy != Guid.Empty && posGuidBuy2 == Guid.Empty && TP2 != 0 && PCU2 > 2)
                                {
                                    XXPrint("BUY1 -- {0} -- TP={1} SL={2} TP/SL={3} Fract 1-{4} 2-{5} 3-{6} Ot={7}", Bars[ci].Time, PRU, UBU, PCU, tmU1, tmU2, tmU3, Ot);
                                    var res4 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.01, Instrument.Bid, -1, Stops.InPips(UBUi, PRUi2), null, null);
                                    if (res4.IsSuccessful)
                                    {
                                        posGuidBuy2 = res4.Position.Id;
                                    }
                                }
                                if (posGuidBuy == Guid.Empty && PCU > 2)
                                {
                                    XXPrint("BUY2 -- {0} -- TP={1} SL={2} TP/SL={3} Fract 1-{4} 2-{5} 3-{6} Ot={7}", Bars[ci].Time, PRU, UBU, PCU, tmU1, tmU2, tmU3, Ot);
                                    var res1 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.01, Instrument.Bid, -1, Stops.InPips(UBUi, PRUi), null, null);
                                    if (res1.IsSuccessful)
                                    {
                                        posGuidBuy = res1.Position.Id;
                                    }
                                }
                                frD = false;
                            }
                        }
                    }
                }
// Если пересечение зоны было (nd) и торгуем вниз (td) ===========================================================
                if (nd && td)
                {
// Определение патерна   ПИК ПОСЛЕДНЕГО ФРАКТАЛА ПАТЕРНА ВВЕРХУ
                    if (_frInd.BottomSeries[Bars.Range.To - frac] > 0)
                    {
                        if (frD2 > frD3 && frD2 > frD1)
                        {
                            var toolPolyLine = Tools.Create <PolyLine>(); toolPolyLine.Color = Color.Aqua; toolPolyLine.Width = 2;
                            toolPolyLine.AddPoint(new ChartPoint(tmD3, frD3));
                            toolPolyLine.AddPoint(new ChartPoint(tmD2, frD2));
                            toolPolyLine.AddPoint(new ChartPoint(tmD1, frD1));

                            TPD  = zmin; TPD = Math.Round(TPD, Instrument.PriceScale);
                            SLD  = frU2 + Instrument.Spread + dlt; SLD = Math.Round(SLD, Instrument.PriceScale);
                            PRD  = Math.Round((Bars[ci].Close - TPD) * Math.Pow(10, Instrument.PriceScale), 0);
                            PRD2 = Math.Round((Bars[ci].Close - TP2) * Math.Pow(10, Instrument.PriceScale), 0);

                            Ot    = Math.Round((frD2 - frD3) / (frD2 - frD1), 2);
                            UBD   = Math.Round((SLD - Bars[ci].Close) * Math.Pow(10, Instrument.PriceScale), 0);
                            PRDi2 = (int)PRD2;
                            PRDi  = (int)PRD;
                            UBDi  = (int)UBD;
                            PCD   = Math.Round(PRD / UBD, 2);
                            PCD2  = Math.Round(PRD2 / UBD, 2);
                            XXPrint("PATERN SELL -- {0} -- TP(п)={1} SL(p)={2} TP/SL={3} Fract 1-{4} 2-{5} 3-{6}  Ot={7}", Bars[ci].Time, PRDi, UBDi, PCD, tmD1, tmD2, tmD3, Ot);
                            if (UBD > 20 && UBD < SL1 && PCD > 2 && Bars[ci].Close < nkz2)
                            {
                                var toolText1 = Tools.Create <Text>();
                                toolText1.Color    = Color.Blue;
                                toolText1.FontSize = 10;
                                toolText1.Point    = new ChartPoint(tmD1, frD1);
                                toolText1.Caption  = string.Format("TP={0} SL={1} C={2} C2={3}", PRD, UBD, PCD, PCD2);

                                if (UBD > 20 && UBD < SL1 && Bars[ci].Close > nkz2)
                                {
                                    if (posGuidSell != Guid.Empty && posGuidSell2 == Guid.Empty && TP2 != 0 && PCD2 > 2)
                                    {
                                        XXPrint("SELL1 -- {0} -- TP={1} SL={2} TP/SL={3} Fract 1-{4} 2-{5} 3-{6} Ot={7}", Bars[ci].Time, PRDi, UBDi, PCD, tmD1, tmD2, tmD3, Ot);
                                        var res3 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.01, Instrument.Ask, -1, Stops.InPips(UBDi, PRDi2), null, null);
                                        if (res3.IsSuccessful)
                                        {
                                            posGuidSell2 = res3.Position.Id;
                                        }
                                    }
                                    if (posGuidSell == Guid.Empty && PCD > 2)
                                    {
                                        XXPrint("SELL2 -- {0} -- TP={1} SL={2} TP/SL={3} Fract 1-{4} 2-{5} 3-{6} Ot={7}", Bars[ci].Time, PRDi, UBDi, PCD, tmD1, tmD2, tmD3, Ot);
                                        var res2 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.01, Instrument.Ask, -1, Stops.InPips(UBDi, PRDi), null, null);
                                        if (res2.IsSuccessful)
                                        {
                                            posGuidSell = res2.Position.Id;
                                        }
                                    }
                                    frU = false;
                                }
                            }
                        } // if( frD2>frD3 && frD2>frD1 )
                    }     // if(_frInd.BottomSeries[Bars.Range.To-frac]>0)
                }         //if(nd && td)
//===============================================================================================================================
            }             // k==0
            k++;
        }                 // NewBar
Exemplo n.º 29
0
//===============================================================================================================================
        protected override void NewBar()
        {
            DTime = Bars[Bars.Range.To - 1].Time;
            ci    = Bars.Range.To - 1;
//=========  Рисуем линию  начала торгов Европы =============================================================================
            if (DTime.Hour == 7 && DTime.Minute == 00)
            {
                var vl1 = Tools.Create <VerticalLine>(); vl1.Time = Bars[Bars.Range.To - 1].Time; vl1.Color = Color.Aqua; vl1.Width = 3;
            }
            if (DTime.Hour == 12 && DTime.Minute == 00)
            {
                var vl1 = Tools.Create <VerticalLine>(); vl1.Time = Bars[Bars.Range.To - 1].Time; vl1.Color = Color.DarkCyan; vl1.Width = 3;
            }
//=======  Определение разворота ====================================================================================================
            if (Fs1)
            {
                if (_ftoInd.FisherSeries[Bars.Range.To - 1] > _ftoInd.Ma1Series[Bars.Range.To - 1])
                {
                    FsU = true;
                }
                else
                {
                    FsU = false;
                }
                if (_ftoInd.FisherSeries[Bars.Range.To - 1] < _ftoInd.Ma1Series[Bars.Range.To - 1])
                {
                    FsD = true;
                }
                else
                {
                    FsD = false;
                }
            }
            if (Fs0)
            {
                if (_ftoInd.FisherSeries[Bars.Range.To - 2] < 0 && _ftoInd.FisherSeries[Bars.Range.To - 1] > 0)
                {
                    FsU = true;
                }
                else
                {
                    FsU = false;
                }
                if (_ftoInd.FisherSeries[Bars.Range.To - 2] > 0 && _ftoInd.FisherSeries[Bars.Range.To - 1] < 0)
                {
                    FsD = true;
                }
                else
                {
                    FsD = false;
                }
            }
// ======  Если не отмечен ни один способ входа - входим по рынку ===================================================================
            if (!Fs1 && !Fs0)
            {
                FsU = Buy7; FsD = Sell7;
            }
//====================================================================================================================================
            posActiveMineB = Trade.GetActivePositions(mgB, true);
            if (posActiveMineB != null && posActiveMineB.Length > 0 && posGuidBuy != posActiveMineB[0].Id)
            {
                posGuidBuy = posActiveMineB[0].Id;
            }
            posActiveMineS = Trade.GetActivePositions(mgS, true);
            if (posActiveMineS != null && posActiveMineS.Length > 0 && posGuidSell != posActiveMineS[0].Id)
            {
                posGuidSell = posActiveMineS[0].Id;
            }

//=== КОРЕКЦИЯ =======================================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty;
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty;
            }

//=== Трелинг  =======================================================================================================================
            TrailActiveOrders();
//====================================================================================================================================

            if (posGuidBuy == Guid.Empty && Buy7 && torg && FsU)
            {
                torg = false;
                var result107 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1,
                                                         Stops.InPips(SL1, null), null, mgB);
                if (result107.IsSuccessful)
                {
                    posGuidBuy = result107.Position.Id;
                }
                XXPrint("{0} BUY START posGuidBuy={1} ", Bars[ci].Time, posGuidBuy.ToString());
                var vl1 = Tools.Create <VerticalLine>(); vl1.Time = Bars[Bars.Range.To - 1].Time; vl1.Color = Color.Red;
            }

            if (posGuidSell == Guid.Empty && Sell7 && torg && FsD)
            {
                torg = false;
                var result207 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1,
                                                         Stops.InPips(SL1, null), null, mgS);
                if (result207.IsSuccessful)
                {
                    posGuidSell = result207.Position.Id;
                }
                XXPrint("{0} SELL SATRT posGuidSell={1}", Bars[ci].Time, posGuidSell.ToString());
                var vl1 = Tools.Create <VerticalLine>(); vl1.Time = Bars[Bars.Range.To - 1].Time; vl1.Color = Color.Blue;
            }
//------------------------------------------------------------------------------------------------------------------------------------

            if (posGuidBuy != Guid.Empty)
            {
                XXPrint("{0} ----- mgB={1} torg={2} Buy7={3},Sell7={4} Fs1={5} Fs0={6} FsU={7} FsD={8}  PS={9} {10}", Bars[ci].Time, mgB, torg, Buy7, Sell7, Fs1, Fs0, FsU, FsD, posActiveMineS.Length, posGuidBuy.ToString());
            }
            if (posGuidSell != Guid.Empty)
            {
                XXPrint("{0} ----- mgS={1} torg={2} Buy7={3},Sell7={4} Fs1={5} Fs0={6} FsU={7} FsD={8}  PS={9} {10}", Bars[ci].Time, mgS, torg, Buy7, Sell7, Fs1, Fs0, FsU, FsD, posActiveMineS.Length, posGuidSell.ToString());
            }
            if (posGuidSell == Guid.Empty && posGuidBuy == Guid.Empty)
            {
                XXPrint("{0} ----- torg={1} Buy7={2},Sell7={3} Fs1={4} Fs0={5} FsU={6} FsD={7} PS={8}", Bars[ci].Time, torg, Buy7, Sell7, Fs1, Fs0, FsU, FsD, posActiveMineS.Length);
            }
        }
Exemplo n.º 30
0
        protected override void NewBar()
        {
            DTime = Bars[Bars.Range.To - 1].Time;
            // Event occurs on every new bar
            _maInd1_1 = _ma1.SeriesMa[Bars.Range.To - 1];
            _maInd2_1 = _ma2.SeriesMa[Bars.Range.To - 1];
            _maInd3_1 = _ma3.SeriesMa[Bars.Range.To - 1];

            _maInd1_2 = _ma1.SeriesMa[Bars.Range.To - 2];
            _maInd2_2 = _ma2.SeriesMa[Bars.Range.To - 2];
            _maInd3_2 = _ma3.SeriesMa[Bars.Range.To - 2];

            _maInd1_3 = _ma1.SeriesMa[Bars.Range.To - 3];
            _maInd2_3 = _ma2.SeriesMa[Bars.Range.To - 3];
            _maInd3_3 = _ma3.SeriesMa[Bars.Range.To - 3];

            _maInd1_4 = _ma1.SeriesMa[Bars.Range.To - 4];
            _maInd2_4 = _ma2.SeriesMa[Bars.Range.To - 4];
            _maInd3_4 = _ma3.SeriesMa[Bars.Range.To - 4];
            //_maInd1,_maInd2,_maInd3,_maInd1_1,_maInd2_1,_maInd3_1,_maInd1_2,_maInd2_2,_maInd3_2,_maInd1_3,_maInd2_3,_maInd3_3,

            _macdInd1 = _macd.SeriesSignal[Bars.Range.To - 1];
            _macdInd2 = _macd.SeriesSignal[Bars.Range.To - 2];
            _macdInd3 = _macd.SeriesSignal[Bars.Range.To - 3];
            _macdInd4 = _macd.SeriesSignal[Bars.Range.To - 4];
            //_macdInd,_macdInd1,_macdInd2,_macdInd3

            sF = _ftoInd.FisherSeries[Bars.Range.To - 1];

            // Вверх 85<75
            //Speed = true;

/*			if (_ma3.SeriesMa[Bars.Range.To-1]<_ma3.SeriesMa[Bars.Range.To-2] &&  _ma3.SeriesMa[Bars.Range.To-2]<_ma3.SeriesMa[Bars.Range.To-3] && _ma3.SeriesMa[Bars.Range.To-3]<_ma3.SeriesMa[Bars.Range.To-4] && _ma3.SeriesMa[Bars.Range.To-4]<_ma3.SeriesMa[Bars.Range.To-5]) Speed=true;
 *              if (_ma3.SeriesMa[Bars.Range.To-1]>_ma3.SeriesMa[Bars.Range.To-2] &&  _ma3.SeriesMa[Bars.Range.To-2]>_ma3.SeriesMa[Bars.Range.To-3] && _ma3.SeriesMa[Bars.Range.To-3]>_ma3.SeriesMa[Bars.Range.To-4] && _ma3.SeriesMa[Bars.Range.To-4]>_ma3.SeriesMa[Bars.Range.To-5]) Speed=true;
 */

//=== КОРЕКЦИЯ ===========================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty;
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty;
            }



            if (_maInd3_4 < _maInd3_3 && _maInd3_3 < _maInd3_2 && _maInd3_2 < _maInd3_1)
            {
                verh = true;
            }
            else
            {
                verh = false;
            }
            if (_maInd3_4 > _maInd3_3 && _maInd3_3 > _maInd3_2 && _maInd3_2 > _maInd3_1)
            {
                niz = true;
            }
            else
            {
                niz = false;
            }

//  1
            if (verh && _maInd2_1 > _maInd1_1 && _maInd3_3 < _maInd1_3 && _maInd3_1 > _maInd2_1 && _macdInd1 > 0 && sF > 0)
            {
                var toolVerticalLine = Tools.Create <VerticalLine>();
                toolVerticalLine.Time  = Bars[Bars.Range.To - 1].Time;
                toolVerticalLine.Color = Color.Red;
                if (posGuidBuy == Guid.Empty)
                {
                    var result = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Ask, -1, Stops.InPips(SL1, TP1), null, null);
                    if (result.IsSuccessful)
                    {
                        posGuidBuy = result.Position.Id;
                    }
                }
            }

//  2
            if (niz && _maInd2_1 > _maInd1_1 && _maInd3_3 > _maInd2_3 && _maInd3_1 < _maInd1_1 && _macdInd1 < 0 && sF < 0)
            {
                var toolVerticalLine = Tools.Create <VerticalLine>();
                toolVerticalLine.Time  = Bars[Bars.Range.To - 1].Time;
                toolVerticalLine.Color = Color.Blue;
                if (posGuidSell == Guid.Empty)
                {
                    var result = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1, Stops.InPips(SL1, TP1), null, null);
                    if (result.IsSuccessful)
                    {
                        posGuidSell = result.Position.Id;
                    }
                }
            }

//  3
            if (verh && _maInd1_1 > _maInd2_1 && _maInd3_3 < _maInd2_3 && _maInd3_1 > _maInd1_1 && _macdInd1 > 0 && sF > 0)
            {
                var toolVerticalLine = Tools.Create <VerticalLine>();
                toolVerticalLine.Time  = Bars[Bars.Range.To - 1].Time;
                toolVerticalLine.Color = Color.Yellow;
                if (posGuidBuy == Guid.Empty)
                {
                    var result = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Ask, -1, Stops.InPips(SL1, TP1), null, null);
                    if (result.IsSuccessful)
                    {
                        posGuidBuy = result.Position.Id;
                    }
                }
            }


//  4
            if (niz && _maInd1_1 > _maInd2_1 && _maInd3_3 > _maInd1_3 && _maInd3_1 < _maInd2_1 && _macdInd1 < 0 && sF < 0)
            {
                var toolVerticalLine = Tools.Create <VerticalLine>();
                toolVerticalLine.Time  = Bars[Bars.Range.To - 1].Time;
                toolVerticalLine.Color = Color.White;
                if (posGuidSell == Guid.Empty)
                {
                    var result = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1, Stops.InPips(SL1, TP1), null, null);
                    if (result.IsSuccessful)
                    {
                        posGuidSell = result.Position.Id;
                    }
                }
            }

/*
 *                      //  1 ==================================================================================================================
 * if (Speed) {
 *                      if( _maInd1>_maInd2 && _maInd3_1<_maInd1_1 && _maInd3>_maInd1 &&  _macdInd>0)
 *                      {
 *                                      var toolVerticalLine=Tools.Create<VerticalLine>();
 *                              toolVerticalLine.Time = Bars[Bars.Range.To-1].Time;
 *                                  toolVerticalLine.Color=Color.Red;
 *                              Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Ask, -1, Stops.InPips(150,TP1), null, null);
 *                              //Print("MA1={0} MA2={1} MA3={2} MACD={3}  - {4} -- ---     BAY  1",_maInd1,_maInd2,_maInd3, _macdInd, Bars[Bars.Range.To-1].Time);
 *              //XXPrint("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}","BUY",Bars[Bars.Range.To-1].Time,Instrument.Name,Instrument.Ask,_maInd3_3,_maInd3_2,_maInd3_1,_maInd3,_maInd1,_maInd2);
 *                      }
 *
 * //  2 ==================================================================================================================
 *                      if( _maInd1>_maInd2 && _maInd3_1>_maInd2_1 && _maInd3<_maInd2  && _macdInd<0)
 *                      {
 *                                      var toolVerticalLine=Tools.Create<VerticalLine>();
 *                              toolVerticalLine.Time = Bars[Bars.Range.To-1].Time;
 *                                  toolVerticalLine.Color=Color.Blue;
 *                              Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1, Stops.InPips(150,TP1), null, null);
 *                              //Print("MA1={0} MA2={1} MA3={2} MACD={3}  - {4} -- ---     SELL  2",_maInd1,_maInd2,_maInd3, _macdInd, Bars[Bars.Range.To-1].Time);
 *                      //XXPrint("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}","SELL",Bars[Bars.Range.To-1].Time,Instrument.Name,Instrument.Ask,_maInd3_3,_maInd3_2,_maInd3_1,_maInd3,_maInd1,_maInd2);
 *                      }
 *
 *                      //  3 ==================================================================================================================
 *                      if( _maInd2>_maInd1 && _maInd3_1<_maInd2_1 && _maInd3>_maInd2 && _macdInd>0)
 *                      {
 *                                      var toolVerticalLine=Tools.Create<VerticalLine>();
 *                              toolVerticalLine.Time = Bars[Bars.Range.To-1].Time;
 *                                  toolVerticalLine.Color=Color.Yellow;
 *                              Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1, Stops.InPips(150,TP1), null, null);
 *                              //Print("MA1={0} MA2={1} MA3={2} MACD={3}  - {4}  ----  BUY  3",_maInd1,_maInd2,_maInd3, _macdInd, Bars[Bars.Range.To-1].Time);
 *                      //XXPrint("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}","BUY",Bars[Bars.Range.To-1].Time,Instrument.Name,Instrument.Ask,_maInd3_3,_maInd3_2,_maInd3_1,_maInd3,_maInd1,_maInd2);
 *                      }
 *
 *                      //  4 ==================================================================================================================
 *                      if( _maInd2>_maInd1 && _maInd3_1>_maInd1_1 && _maInd3<_maInd1  && _macdInd<0)
 *                      {
 *                                      var toolVerticalLine=Tools.Create<VerticalLine>();
 *                              toolVerticalLine.Time = Bars[Bars.Range.To-1].Time;
 *                                  toolVerticalLine.Color=Color.White;
 *                              Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1, Stops.InPips(150,TP1), null, null);
 *                      //	Print("MA1={0} MA2={1} MA3={2} MACD={3}  - {4} -- ---     SELL 4",Math.Round(_maInd1,5),Math.Round(_maInd2,5),Math.Round(_maInd3,5), Math.Round(_macdInd,5), Bars[Bars.Range.To-1].Time);
 *                      XXPrint("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}","SELL",Bars[Bars.Range.To-1].Time,Instrument.Name,Instrument.Ask,_maInd3_3,_maInd3_2,_maInd3_1,_maInd3,_maInd1,_maInd2);
 *                      }
 *
 *              }
 */

//			Print("MA1_1={0} MA2_1={1} MA3_1={2} -- MA1={3} MA2={4} MA3={5} MACD={6} - {7}",Math.Round(_maInd1_1,5),Math.Round(_maInd2_1,5),Math.Round(_maInd3_1,5),Math.Round(_maInd1,5),Math.Round(_maInd2,5),Math.Round(_maInd3,5), Math.Round(_macdInd,5), Bars[Bars.Range.To-1].Time);
        }