示例#1
0
        protected override void NewBar()
        {
// =================================================================================================================================
            // Значения текущего Бара
            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 - Истина если появился НОВЫЙ фрактал Вверх/Вниз
            //Print("Fractal!! - Top={0} Bottom={1}",_frInd.TopSeries[Bars.Range.To-5],_frInd.BottomSeries[Bars.Range.To-5]);
            frSU = _frInd.TopSeries[Bars.Range.To - 5];
            frSD = _frInd.BottomSeries[Bars.Range.To - 5];
            //Print("Fractal2!! - Top={0} Bottom={1} -- {2}",frSU,frSD,Bars[Bars.Range.To-5].Time);
            //if (Trade.GetPosition(posGuidSell).State==PositionState.Active)
            //		Print("15 $$$$$$$$$$$$$$$$$$$$$$$$frSU>0={3} Top={4} frUpH={0} frUp_Time={1} -- {2}",frUpH,frUp_Time,Bars[Bars.Range.To-1],frSU,_frInd.TopSeries[Bars.Range.To-5]);



            if (frSU > 0)
            {
                frUp = true;
            }
            else
            {
                frUp = false;
            }
            if (frSD > 0)
            {
                frDown = true;
            }
            else
            {
                frDown = false;
            }
            //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;

            // Значения 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];   //С

            // Значения Gator Oscillator
            gNU = _goInd.NegativeSeriesUp[Bars.Range.To - 1];
            gND = _goInd.NegativeSeriesDown[Bars.Range.To - 1];
            gPD = _goInd.PositiveSeriesDown[Bars.Range.To - 1];
            gPU = _goInd.PositiveSeriesUp[Bars.Range.To - 1];

// =================================================================================================================================
// 1. Alligator СПИТ - Торгуем
            // Правильно ЗКС или СКЗ - если наоборот то  линии переплелись - Торг
            if (!((aGuba > aZub && aZub > aChelust) || (aChelust > aZub && aZub > aGuba)))
            {
                Torg = true;
                //Print("#01 Torg - Линии Алигатора переплелись! ",DTime);
            }

            //Print("gNU={0},gND={1},gPU={2},gPD={3} -- {4}",gNU,gND,gPU,gPD,Bars[Bars.Range.To-1].Time);
            //if (gPU>0 && gNU<0) allEst=true; else allEst=false;
            //Print("Алигатор ЕСТ - PU={0} NU={1} - {2} ",gPU,gNU,Bars[Bars.Range.To-1].Time);
            if ((gPD > 0 && gND < 0) || (gPD > 0 && gNU < 0) || (gPU > 0 && gND < 0))
            {
                allSpit = true;
                //Print("#01 Аллигатор СПИТ! -- {0} ",DTime);
            }
            else
            {
                allSpit = false;
            }


            //Print("Алигатор СПИТ - PD={0} ND={1} - {2} ",gPD,gND,Bars[Bars.Range.To-1].Time);
            //if ((gPD>0 && gNU<0) || (gPU>0 && gND<0))
            //Print("Алигатор ПРОСЫПАЕТСЯ - PD>0={0} NU<0={1} gPU>0={2} gND<0={3} - {4} ",gPD,gNU,gPU,gND,Bars[Bars.Range.To-1].Time);
// =================================================================================================================================
            // Срабатывает - когда появился новый фрактал - 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 && frUpL > aGuba5 && frUpL > aChelust5 && frUpL > aZub5)
            {
                fr_all_Up = frUpH; fr_all_Up_Time = frUp_Time;  fr_all_Up_L = true;
                Print("02 Фрактал ВВЕРХ и выше Алигатора - {0} ", Bars[Bars.Range.To - 1].Time);
            }
            // Если появился новый фрактал  ВВЕРХ и касается Алигатора - отменяем "рабочий"
            if (frSU > 0 && !(frUpL > aGuba5 && frUpL > aChelust5 && frUpL > aZub5))
            {
                fr_all_Up_L = false;
                Print("03 Фрактал ВВЕРХ но касается Алигатора - {0} }", Bars[Bars.Range.To - 1].Time);
            }
//===============================================================================================================================
            // Появился новый фрактал ВНИЗ  и Свеча Fractalа НИЖЕ  Alligatora не касается Alligatorа
            // ВЕРХ Бар-Фрактала НИЖЕ Alligator  - Назначаем рабочим (fr_all_Down) для SellLimit
            if (frSD > 0 && frDownH < aGuba5 && frDownH < aChelust5 && frDownH < aZub5)
            {
                fr_all_Down = frDownL;  fr_all_Down_Time = frDown_Time; fr_all_Down_L = true;
                Print("04 Фрактал НИЖЕ Алигатора - {0} ", Bars[Bars.Range.To - 1].Time);
            }
            // Если появился новый фрактал  ВНИЗ  и касается Алигатора - отменяем "рабочий"
            if (frSD > 0 && !(frDownH < aGuba5 && frDownH < aChelust5 && frDownH < aZub5))
            {
                fr_all_Down_L = false;
                Print("05 Фрактал ВНИЗ и касается Алигатора - {0} ", Bars[Bars.Range.To - 1].Time);
            }
//===============================================================================================================================
            // Появился новый фрактал ВВЕРХ и назначен рабочим и есть отложеный ордер
            // меняем цену открытия и ставим TP - Низ/Верх противоположного фрактала
            if (frSU > 0 && fr_all_Up_L && posGuidBuy != Guid.Empty)
            {
                if (Trade.GetPosition(posGuidBuy).State == PositionState.Pending)
                // Ордер SellLimit-ВНИЗ - Цена открытия=High раб фрактала, SL=200p TP=Low противопложного фрактала
                {
                    var result = Trade.UpdatePendingPosition(posGuidBuy, 0.1, fr_all_Up, frDownL, null);
                    if (result.IsSuccessful)
                    {
                        posGuidBuy = result.Position.Id;
                        Print("06 UpdatePending - !появился новый фр-рабочий - переносим цену и StopLoss TP -  {0} ", DTime);
                    }
                }
            }

            // Если появился новый фрактал и не подходит под рабочий и есть отрытый отложеный ордер - отменяем ордер Buy
            if (frSU > 0 && !fr_all_Up_L && posGuidBuy != Guid.Empty)
            {
                if (Trade.GetPosition(posGuidBuy).State == PositionState.Pending)
                {
                    var res = Trade.CancelPendingPosition(posGuidBuy);
                    if (res.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                    Print("07 Появился новый ВВЕРХ фрактал и не подходит под рабочий - отменяем пред. BuyStop - CancelPending - {0} ", DTime);
                }
            }

            // Алигатор СПИТ и появился новый фрактал и назначен рабочим и нет отложек- ставим отложеный ордер
            //  и при этом нет открытых ордеров  (posGuidBuy)
            if (allSpit && frSU > 0 && fr_all_Up_L && posGuidBuy == Guid.Empty)
            {
                // Закрываем SellStop
                if (posGuidSell != Guid.Empty)
                {
                    if (Trade.GetPosition(posGuidSell).State == PositionState.Pending)
                    {
                        var res = Trade.CancelPendingPosition(posGuidSell);
                        Print("12 Отменяем SellStop бо появился раб-фрактал  - {0} ", DTime);
                        if (res.IsSuccessful)
                        {
                            posGuidSell = Guid.Empty;
                        }
                    }
                    if (Trade.GetPosition(posGuidSell).State == PositionState.Active)
                    {
                        var res = Trade.CloseMarketPosition(posGuidSell);
                        if (res.IsSuccessful)
                        {
                            posGuidSell = Guid.Empty;
                        }
                    }
                }
                // открываем BuyStop - Цена=High раб. ВВЕРХ фрактала SL=200p TP=Низ против. фрактала
                var result = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.BuyStop, 0.1, fr_all_Up, 0, Stops.InPrice(frDownL - SL, null), null, null, null);
                if (result.IsSuccessful)
                {
                    posGuidBuy = result.Position.Id;
                    Print("09 BuyStop - {0}", DTime);
                }
                else
                {
                    //var result2=Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, fr_all_Up, -1, Stops.InPrice(frDownL,null), null, null);
                    var result2 = Trade.Buy(Instrument.Id, 0.1);
                    if (result2.IsSuccessful)
                    {
                        posGuidBuy = result2.Position.Id;
                        Print("09 Buy ОТКРЫТО!! - {0}", DTime);
                    }
                }
            }

//===============================================================================================================================
            // Появился новый фрактал ВНИЗ  и назначен рабочим но есть отложеный ВНИЗУ ордер
            // переносим цену открытия и ставим Stop - Низ/Верх противоположного фрактала
            if (allSpit && frSD > 0 && fr_all_Down_L && posGuidSell != Guid.Empty)
            {
                if (Trade.GetPosition(posGuidSell).State == PositionState.Pending)
                {
                    var result = Trade.UpdatePendingPosition(posGuidSell, 0.1, fr_all_Down, frUpH + SL, null);
                    if (result.IsSuccessful)
                    {
                        posGuidSell = result.Position.Id;
                        Print("10 Sell UpdatePending - !появился новый фр-рабочий - переносим цену и StopLoss - {0}  ", DTime);
                    }
                }
            }
            // Если появился новый фрактал ВНИЗ и не подходит под рабочий и есть отрытый отложеный ордер - отменяем ордер Sell
            if (frSD > 0 && !fr_all_Down_L && posGuidSell != Guid.Empty)
            {
                if (Trade.GetPosition(posGuidSell).State == PositionState.Pending)
                {
                    var res = Trade.CancelPendingPosition(posGuidSell);
                    if (res.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                    Print("11 Sell CancelPending - {0} ", DTime);
                }
            }

            // Алигатор переплетен и появился новый фрактал ВНИЗ и назначен рабочим и нет открытых Sell ордеров  (posGuidSell)-
            // ставим отложеный ордер SellStop и отменяем BuyStop - если открыт!
            if (allSpit && frSD > 0 && fr_all_Down_L && posGuidSell == Guid.Empty)
            {                         // Закрываем BuyStop
                if (posGuidBuy != Guid.Empty)
                {
                    if (Trade.GetPosition(posGuidBuy).State == PositionState.Pending)
                    {
                        var res = Trade.CancelPendingPosition(posGuidBuy);
                        Print("12 Отменяем BuyStop бо появился раб-фрактал  - {0} ", DTime);
                        if (res.IsSuccessful)
                        {
                            posGuidBuy = Guid.Empty;
                        }
                    }
                    if (Trade.GetPosition(posGuidBuy).State == PositionState.Active)
                    {
                        var res = Trade.CloseMarketPosition(posGuidBuy);
                        if (res.IsSuccessful)
                        {
                            posGuidBuy = Guid.Empty;
                        }
                    }
                }
                // Открываем SellStop
                var result = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.SellStop, 0.1, fr_all_Down, 0, Stops.InPrice(frUpH + SL, null), null, null, null);
                if (result.IsSuccessful)
                {
                    posGuidSell = result.Position.Id;
                    Print("13 SellStop - {0} ", DTime);
                }
                else
                {
                    //var result2=Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, fr_all_Up, -1, Stops.InPrice(frDownL,null), null, null);
                    var result2 = Trade.Sell(Instrument.Id, 0.1);
                    if (result2.IsSuccessful)
                    {
                        posGuidBuy = result2.Position.Id;
                        Print("09 Sell ОТКРЫТО!! - {0}", DTime);
                    }
                }
            }


//===============================================================================================================================
            // Отменяем торг если вошли в позицию - Следующий вход - когда снова переплетется Алигатор
            //if (Torg && posGuidBuy!=Guid.Empty && Trade.GetPosition(posGuidBuy).State ==PositionState.Active)  { Torg=false;  }
            //if (Torg && posGuidSell!=Guid.Empty && Trade.GetPosition(posGuidSell).State==PositionState.Active) { Torg=false; }

//===============================================================================================================================
            // Появился новый фрактал ВВЕРХ и открыта позиция Sell - переосим стоп
            if (frUpH != frUpH1)
            {
                frUpH1 = frUpH;

                if (Trade.GetPosition(posGuidSell).State == PositionState.Active)
                {
                    var res3 = Trade.UpdateMarketPosition(posGuidSell, frUpH1 + SL, null, null);
                    if (res3.IsSuccessful)
                    {
                        Print("15 SellStop - UpdatePending StopLoss когда Появился новый фрактал Up - {0} ", DTime);
                    }
                    else
                    {
                        var res2 = Trade.CloseMarketPosition(posGuidSell); if (res2.IsSuccessful)
                        {
                            posGuidSell = Guid.Empty;
                        }
                    }
                }
            }

            // Появился новый фрактал ВНИЗ и ОТКРЫТА позиция Buy - переносим стоп - Работает!
            if (frSD > 0 && Trade.GetPosition(posGuidBuy).State == PositionState.Active)
            {
                var result2 = Trade.UpdateMarketPosition(posGuidBuy, frDownL - SL, null, null);
                if (result2.IsSuccessful)
                {
                    Print("14 BuyStop - !UpdatePending StopLoss когда Появился новый фрактал Down - {0}", DTime);
                }
                else
                {
                    var res0 = Trade.CloseMarketPosition(posGuidBuy); if (res0.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
            }



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

            //if  (Trade.GetPosition(posGuidBuy).State.ToString()=="Active" && Trade.GetPosition(posGuidBuy).Pips>300) { Trade.CloseMarketPosition(posGuidBuy);  pBuy=false; Torg=false;}
            //if  (Trade.GetPosition(posGuidSell).State.ToString()=="Active" && Trade.GetPosition(posGuidSell).Pips>300) { Trade.CloseMarketPosition(posGuidSell);  pSell=false; Torg=false;}
            if (posGuidSell != Guid.Empty)
            {
                var posS   = Trade.GetPosition(posGuidSell);
                var stateS = posS.State;
                if (stateS.ToString() == "Active")
                {
                    var pos  = Trade.GetPosition(posGuidSell);
                    var pips = pos.Pips;
                    //Print("Pips={0} ",pips,Bars[Bars.Range.To-1].Time);
                }
            }


            if (posGuidBuy != Guid.Empty)
            {
                var posB   = Trade.GetPosition(posGuidBuy);
                var stateB = posB.State;
                if (stateB.ToString() == "Active")
                {
                    var pos  = Trade.GetPosition(posGuidBuy);
                    var pips = pos.Pips;
                    //Print("Pips={0} ",pips,Bars[Bars.Range.To-1].Time);
                }
            }
// =================================================================================================================================
            // Закрываем ордер на покупку когда свеча каснется Зубов Alligator
            // if(pBuy && Bars[Bars.Range.To-1].Close<aGuba && posGuidBuy!=Guid.Empty) { Trade.CloseMarketPosition(posGuidBuy);  pBuy=false; Torg=false;}
            //	if(Trade.GetPosition(posGuidBuy).Pips>100 && posGuidBuy!=Guid.Empty) {
            //		var res = Trade.CloseMarketPosition(posGuidBuy);  Print("Buy - Закрываем позицию - {0} ",DTime);
            //                if (res.IsSuccessful) posGuidBuy=Guid.Empty; Torg=false;}

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


            //if(pBuy && Bars[Bars.Range.To-1].Close<aChelust && _positionGuid!=Guid.Empty) { ClosePosition();  pBuy=false; Torg=false;}
            //			 if(pBuy && aoUp1>0 && aoDown>0 && _positionGuid!=Guid.Empty) { ClosePosition();  pBuy=false; Torg=false;}

            // Закрываем ордер на продажу когда свеча каснется Зубов
            // if(pSell && Bars[Bars.Range.To-1].Close>aGuba && posGuidSell!=Guid.Empty) { Trade.CloseMarketPosition(posGuidSell);  pSell=false; Torg=false;}
            //		if(Trade.GetPosition(posGuidSell).Pips>100 && posGuidSell!=Guid.Empty) {
            //			var res = Trade.CloseMarketPosition(posGuidSell);   Print("Sell - Закрываем позицию - {0} ",DTime);
            //                    if (res.IsSuccessful) posGuidSell=Guid.Empty;  Torg=false; }

            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty;  Print("Sell - Закрыто по StopLoss (Корекция) - {0} ", DTime);
            }
            //if(pSell && Bars[Bars.Range.To-1].Close>aChelust && _positionGuid!=Guid.Empty) { ClosePosition();  pSell=false; Torg=false;}
            //			if(pSell  && aoUp<0/ && aoDown1<0 && _positionGuid!=Guid.Empty) { ClosePosition();  pSell=false; Torg=false;}
        }
示例#2
0
//===============================================================================================================================
        protected override void NewBar()
        {
            if (first)
            {
                first = false;

                iFT = TimeToIndex(dt1, Timeframe);
                if (tu)
                {
                    nkz4  = Bars[iFT].High - ((NKZ - 5 - (NKZ * kf)) * Instrument.Point);
                    nkz2  = Bars[iFT].High - (((NKZ * 2) - (NKZ * 2 * kf)) * Instrument.Point);
                    nkz4v = Bars[iFT].High - ((NKZ - 5) * Instrument.Point);
                    nkz2v = Bars[iFT].High - ((NKZ * 2) * Instrument.Point);
                }
                if (td)
                {
                    nkz4  = Bars[iFT].Low + ((NKZ - 5 - (NKZ * kf)) * Instrument.Point);
                    nkz2  = Bars[iFT].Low + (((NKZ * 2) - (NKZ * 2 * kf)) * Instrument.Point);
                    nkz4v = Bars[iFT].Low + ((NKZ - 5) * Instrument.Point);
                    nkz2v = Bars[iFT].Low + (((NKZ * 2)) * Instrument.Point);
                }
                zmax = Bars[iFT].High;
                zmin = Bars[iFT].Low;
            }

            if (dt0 != dt1)
            {
                Tools.Remove(toolPolyLine);
                Tools.Remove(toolRectangle);
                Tools.Remove(toolRectangle1);

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

//=== КОРЕКЦИЯ ===========================================================================================================
                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 (posGuidBuy == Guid.Empty && tu)
                {
                    var result = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.BuyLimit, 0.1,
                                                           nkz4, 0,
                                                           Stops.InPrice(200, null), null, null, mgB);
                    if (result.IsSuccessful)
                    {
                        posGuidBuy = result.Position.Id;
                    }
                }

                if (posGuidSell == Guid.Empty && td)
                {
                    var result1 = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.SellLimit, 0.1,
                                                            nkz4, 0,
                                                            Stops.InPrice(200, null), null, null, mgS);
                    if (result1.IsSuccessful)
                    {
                        posGuidSell = result1.Position.Id;
                    }
                }


                toolPolyLine   = Tools.Create <PolyLine>(); toolPolyLine.Color = Color.Aqua;
                toolRectangle  = Tools.Create <Rectangle>(); toolRectangle.BorderColor = Color.Aqua; toolRectangle.Color = Color.DarkSeaGreen;
                toolRectangle1 = Tools.Create <Rectangle>(); toolRectangle1.BorderColor = Color.Aqua; toolRectangle1.Color = Color.DarkSeaGreen;

                toolPolyLine.AddPoint(new ChartPoint(Bars[iFT].Time, Bars[iFT].High));
                toolPolyLine.AddPoint(new ChartPoint(Bars[iFT].Time, nkz2v));

                toolRectangle.Point1 = new ChartPoint(Bars[iFT].Time, nkz4);
                toolRectangle.Point2 = new ChartPoint(Bars[Bars.Range.To - 1].Time.AddHours(24), nkz4v);

                toolRectangle1.Point1 = new ChartPoint(Bars[iFT].Time, nkz2);
                toolRectangle1.Point2 = new ChartPoint(Bars[Bars.Range.To - 1].Time.AddHours(24), nkz2v);

                dt0 = dt1;
            }
        }
示例#3
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)
            {
                z50_3 = 0; z50_2 = 0; z50_1 = 0; torg = false;
                zzu5  = 0; zzu4 = 0; zzu3 = 0; zzu2 = 0; zzu1 = 0;
                zzd5  = 0; zzd4 = 0; zzd3 = 0; zzd2 = 0; zzd1 = 0;
                if (posGuidBuy != Guid.Empty)
                {
                    var res = Trade.CloseMarketPosition(posGuidBuy); if (res.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
                if (posGuidSell != Guid.Empty)
                {
                    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 == 01)
            {
                torg = true;
            }

//======================================================================================================================================
            if (_wprInd.MainIndicatorSeries[Bars.Range.To - 1] > 0)
            {
                if (zzd3 != 0 && zzd2 != 0 && zzd1 != 0)
                {
                    torg2 = true;
                }
                else
                {
                    torg2 = 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;
                    vy.Time = Bars[Bars.Range.To - 1].Time;
                    // ВВЕРХУ
                    Print("ВВЕРХУ {0} - {1}<{2}={3} [{4}]", Bars[Bars.Range.To - 1].Time, zzu1, zzu3, zzu3 < zzu1, torg2);
                    if (zzu3 < zzu1 && torg2)
                    {
                        var toolPolyLine = Tools.Create <PolyLine>();
                        toolPolyLine.Color = Color.SpringGreen;
                        toolPolyLine.Width = 4;
                        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 - 1].Time, Bars[Bars.Range.To - 1].Close));

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

                        if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Pending)
                        {
                            var result = Trade.UpdatePendingPosition(posGuidBuy, 0.1, Bars[zzd1].High + 0.000010, Bars[zzd1].High - 0.00005, Bars[zzd1].High + 0.00250);
                            if (result.IsSuccessful)
                            {
                                posGuidBuy = result.Position.Id;
                            }
                        }


                        if (posGuidBuy == Guid.Empty)
                        {
                            var result2 = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.BuyStop, 0.1, Bars[zzd1].High + Instrument.Spread + 0.00020, 0, Stops.InPrice(Bars[zzd1].High - Instrument.Spread - 0.00050, Bars[zzd1].High + Instrument.Spread + 0.00250), null, null, null);
                            if (result2.IsSuccessful)
                            {
                                posGuidBuy = result2.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;
                    vb.Time = Bars[Bars.Range.To - 1].Time;
                    Print("ВНИЗУ {0} - {1}<{2} = {3} [{4}]", Bars[Bars.Range.To - 1].Time, zzu1, zzu3, zzu3 > zzu1, torg2);
                    if (zzu3 > zzu1 && torg2)
                    {
                        var toolPolyLine = Tools.Create <PolyLine>();
                        toolPolyLine.Color = Color.Blue;
                        toolPolyLine.Width = 4;
                        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));
                        toolPolyLine.AddPoint(new ChartPoint(Bars[Bars.Range.To - 1].Time, Bars[Bars.Range.To - 1].Close));



                        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 result = Trade.UpdatePendingPosition(posGuidSell, 0.1, Bars[zzd1].Low - 0.0001, Bars[zzd1].Low + 0.0001, Bars[zzd1].Low - 0.00300);
                            if (result.IsSuccessful)
                            {
                                posGuidSell = result.Position.Id;
                            }
                        }

                        if (posGuidSell == Guid.Empty)
                        {
                            var result3 = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.SellStop, 0.1, Bars[zzd1].Low - Instrument.Spread - 0.00025, 0, Stops.InPrice(Bars[zzd1].Low + Instrument.Spread + 0.00050, Bars[zzd1].Low - Instrument.Spread - 0.00250), null, null, null);
                            if (result3.IsSuccessful)
                            {
                                posGuidSell = result3.Position.Id;
                            }
                        }
                    }
                }
            }
        }
示例#4
0
        protected override void NewBar()
        {
            // Event occurs on every new bar
            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];

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

            //	sF  = _ftoInd.FisherSeries[Bars.Range.To-1];  // Фішеер - Плюс-зверху Мінус - знизу
            //	sF1 = _ftoInd.FisherSeries[Bars.Range.To-2];

            //  Пересечение Фишер
            //  if (sF1<0 && sF>0)    vlB.Time=Bars[Bars.Range.To-1].Time; // ВВЕРХ
            //	if (sF1>0 && sF<0)    vlR.Time=Bars[Bars.Range.To-1].Time; // ВНИЗ

            // AO - пересечение 0

            if (aoUp2 <= 0 && aoUp1 >= 0)
            {
                DV = 0; UV = 0; DN = 0; UN = 0; U0 = true; D0 = false;
            }
            //Print("aoUp2({0})<=0 && aoUp1({1})>=0  {2} - {3}",aoUp2,aoUp1,U0,Bars[Bars.Range.To-1].Time);

            if (aoDown2 >= 0 && aoDown1 <= 0)
            {
                DV = 0; UV = 0; DN = 0; UN = 0; U0 = false; D0 = true;
            }
            //Print("aoDown2({0})<=0 && aoDown1({1})>=0 {2} - {3}",aoDown2,aoDown1,D0,Bars[Bars.Range.To-1].Time);
            // 1  UV
            if (aoUp4 > 0 && aoUp3 > 0 && aoDown2 > 0 && aoDown1 > 0)
            {
                UV++; if (UV == 1)
                {
                    MaxU = aoUp3;
                }
            }
            // 2  DV
            if (aoDown4 < 0 && aoDown3 < 0 && aoUp2 < 0 && aoUp1 < 0)
            {
                DV++; if (DV == 1)
                {
                    MaxD = aoDown3;
                }
            }
            // 3 UN
            if (aoDown4 > 0 && aoDown3 > 0 && aoUp2 > 0 && aoUp1 > 0)
            {
                UN++;
            }
            // 4 DN
            if (aoUp4 < 0 && aoUp3 < 0 && aoDown2 < 0 && aoDown1 < 0)
            {
                DN++;
            }

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

            if (_positionGuidB != Guid.Empty && Trade.GetPosition(_positionGuidB).State == PositionState.Closed)
            {
                _positionGuidB = Guid.Empty;
            }
            if (_positionGuidS != Guid.Empty && Trade.GetPosition(_positionGuidS).State == PositionState.Closed)
            {
                _positionGuidS = Guid.Empty;
            }
            // =======================================================================================================================
            //if (U0) Print("U0={0} UV={1} UN={2} MaxU={3} -- {4}",U0,UV,UN,MaxU,Bars[Bars.Range.To-1].Time);
            if (U0 && UV == 1 && UN == 1 && MaxU > 0.00005)
            {
                Print("U0={0} UV={1} UN={2} MaxU={3} -- {4}", U0, UV, UN, MaxU, Bars[Bars.Range.To - 1].Time);

                if (_positionGuidB == Guid.Empty)
                {
                    var vlR1 = Tools.Create <VerticalLine>();
                    vlR1.Color = Color.Red;
                    vlR1.Time  = Bars[Bars.Range.To - 1].Time;
                    var result2 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1, Stops.InPrice(frDownL - TP, null), null, null);
                    if (result2.IsSuccessful)
                    {
                        _positionGuidB = result2.Position.Id;
                    }
                }
            }
            //if (D0)  Print("D0={0} DV={1} DN={2} MaxD={3} -- {4}",D0,DV,DN,MaxD,Bars[Bars.Range.To-1].Time);
            if (D0 && DV == 1 && DN == 1 && MaxD < -0.00005)
            {
                Print("D0={0} DV={1} DN={2} MaxD={3} -- {4}", D0, DV, DN, MaxD, Bars[Bars.Range.To - 1].Time);

                if (_positionGuidS == Guid.Empty)
                {
                    var vlR2 = Tools.Create <VerticalLine>();
                    vlR2.Color = Color.Blue;
                    vlR2.Time  = Bars[Bars.Range.To - 1].Time;

                    var result2 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1, Stops.InPrice(frUpH + TP, null), null, null);
                    if (result2.IsSuccessful)
                    {
                        _positionGuidS = result2.Position.Id;
                    }
                }
            }

            if (_positionGuidB != Guid.Empty && frDown && frD != frDownL)
            {
                frD = frDownL;
                var result2 = Trade.UpdateMarketPosition(_positionGuidB, frDownL, null, null);
                if (result2.IsSuccessful)
                {
                    _positionGuidB = result2.Position.Id;
                }
            }

            if (_positionGuidS != Guid.Empty && frUp)
            {
                frU = frUpH;
                var result2 = Trade.UpdateMarketPosition(_positionGuidS, frUpH, null, null);
                if (result2.IsSuccessful)
                {
                    _positionGuidS = result2.Position.Id;
                }
            }

            // Закриття --------------------------------------------------

            /*if ( U0 && UV==2 && UN==1)  {
             *      if (_positionGuidB!=Guid.Empty)
             *         {  var result =Trade.CloseMarketPosition(_positionGuidB);
             *            if (result.IsSuccessful) _positionGuidB = Guid.Empty;}
             * }*/

            if (D0 || (aoDown4 > 0 && aoDown1 < 0) || (aoDown3 > 0 && aoDown1 < 0))
            {
                if (_positionGuidB != Guid.Empty)
                {
                    var result = Trade.CloseMarketPosition(_positionGuidB);
                    if (result.IsSuccessful)
                    {
                        _positionGuidB = Guid.Empty;
                    }
                }
            }

            /*
             *
             *    if ( D0 &&  DV==2 && DN==1)  {
             *            if (_positionGuidS!=Guid.Empty)
             *               {  var result =Trade.CloseMarketPosition(_positionGuidS);
             *                  if (result.IsSuccessful) _positionGuidS = Guid.Empty;}
             *
             * } */
            if (U0 || (aoUp4 < 0 && aoUp1 > 0) || (aoUp4 < 0 && aoUp1 > 0))
            {
                if (_positionGuidS != Guid.Empty)
                {
                    var result = Trade.CloseMarketPosition(_positionGuidS);
                    if (result.IsSuccessful)
                    {
                        _positionGuidS = Guid.Empty;
                    }
                }
            }
        }
示例#5
0
        protected override void NewBar()
        {
            O1    = Bars[Bars.Range.To - 1].Open;
            C1    = Bars[Bars.Range.To - 1].Close;
            H1    = Bars[Bars.Range.To - 1].High;
            L1    = Bars[Bars.Range.To - 1].Low;
            DTime = Bars[Bars.Range.To - 1].Time;

//=== КОРЕКЦИЯ ===========================================================================================================
            if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Closed)
            {
                posGuidBuy = Guid.Empty; LogU = false;
            }
            if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Closed)
            {
                posGuidSell = Guid.Empty; LogD = 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;
                    }
                }
            }
//======================================================================================================================================
            //Print("{0} - torg={1} LogU={2} LogD={3} Hour={4} Min={5}",DTime,torg,LogU,LogD,DTime.Hour,DTime.Minute);
            // Максимальне та мінімальне знячення за день - на H1 - періоді..
            if (DTime.Hour == 23 && DTime.Minute == 00)
            {
                LogU = true; LogD = true; torg = false;
                var highestIndex = Series.Highest(Bars.Range.To, 24, PriceMode.High);
                var highestPrice = Bars[highestIndex].High;
                U1 = highestPrice;
                var lowestIndex = Series.Lowest(Bars.Range.To, 24, PriceMode.Low);
                var lowestPrice = Bars[lowestIndex].Low;
                D1 = lowestPrice;
                // Різниця в пунктах між MAX-MIN
                Dt = (int)Math.Round((U1 - D1) * 100000, 0);
                // Малюємо горизонтальну лінію на рівнях
                Line1.Price = U1;
                Line1.Text  = string.Format("{0}", Dt);
                Line2.Price = D1;

                TPU = Math.Round(U1 + (U1 - D1) * 0.2353, 5);
                TPD = Math.Round(D1 - (U1 - D1) * 0.2353, 5);
            }

            if (DTime.Hour == 04 && DTime.Minute == 00 && LogU && LogD)
            {
                torg = true;
            }
            if (DTime.Hour == 20 && DTime.Minute == 00)
            {
                torg = false;
            }


//======================================================================================================================================
            if (C1 > U1 && O1 < U1)
            {
                zkU = Math.Abs((C1 - U1) * 100 / (C1 - O1));
            }
            if (O1 > D1 && C1 < D1)
            {
                zkD = Math.Abs((D1 - C1) * 100 / (C1 - O1));
            }
            if (C1 < U1)
            {
                zkU = 0;
            }
            if (O1 > U1)
            {
                zkU = 100;
            }
            if (C1 > D1)
            {
                zkD = 0;
            }
            if (O1 < D1)
            {
                zkD = 100;
            }
            //Print("zkU={0} zkD={1} {2} {3}",zkU,zkD,TPU,TPD);
//======================================================================================================================================
            if (LogU && C1 > U1)
            {
                LogU = false; Print("1 - {0}", _stoIndH1.SignalLine[Bars.Range.To - 1] > 80.0);
                if (_stoIndH1.SignalLine[Bars.Range.To - 1] > 80.0)
                {
                    Print("2");
                    zkU = Math.Abs((C1 - U1) * 100 / (C1 - O1));
                    Print("{0} zkU={1}", DTime, zkU);

                    if (torg && posGuidBuy == Guid.Empty && zkU > 20)
                    {
                        SLD = Math.Round(Bars[Bars.Range.To - 1].Close - 0.002, 5);
                        //var result=Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1,Instrument.Ask,-1,Stops.InPips(200,TPU),null);
                        var result = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Ask, -1, Stops.InPrice(SLU, TPU), null);
                        if (result.IsSuccessful)
                        {
                            posGuidBuy = result.Position.Id;
                        }
                    }

                    var vline = Tools.Create <VerticalLine>();
                    vline.Color = Color.Red; vline.Width = 4;
                    vline.Time  = Bars[Bars.Range.To - 2].Time;
                }
            }
//======================================================================================================================================
            if (LogD && C1 < D1)
            {
                LogD = false; Print("3 - {0}", _stoIndH1.SignalLine[Bars.Range.To - 1] < 20.0);
                if (_stoIndH1.SignalLine[Bars.Range.To - 1] < 20.0)
                {
                    Print("2");
                    zkD = Math.Abs((D1 - C1) * 100 / (C1 - O1));
                    Print("{0} zkD={1}", DTime, zkD);


                    if (torg && posGuidSell == Guid.Empty && zkD > 20)
                    {
                        SLD = Math.Round(Bars[Bars.Range.To - 1].Close + 0.002, 5);
                        //var result=Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1,Instrument.Bid,-1,Stops.InPips(200,TPU),null);
                        var result = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Bid, -1, Stops.InPrice(SLD, TPD), null);
                        if (result.IsSuccessful)
                        {
                            posGuidSell = result.Position.Id;
                        }
                    }

                    var vline = Tools.Create <VerticalLine>();
                    vline.Color = Color.Blue; vline.Width = 4;
                    vline.Time  = Bars[Bars.Range.To - 2].Time;
                }
            }


            Print("{0} - torg={1} LogU={2} LogD={3} Hour={4} Min={5} TPU={6} TPD={7} Stoh={8}", DTime, torg, LogU, LogD, DTime.Hour, DTime.Minute, TPU, TPD, _stoIndH1.SignalLine[Bars.Range.To - 1]);


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

            /*	if (_ftoInd.FisherSeries[Bars.Range.To-1]<_ftoInd.Ma1Series[Bars.Range.To-1] &&
             *                                 _ftoInd.FisherSeries[Bars.Range.To]>_ftoInd.Ma1Series[Bars.Range.To]) isFMU=true; else isFMU=false;
             *      if (_ftoInd.FisherSeries[Bars.Range.To-1]>_ftoInd.Ma1Series[Bars.Range.To-1] &&
             *                                 _ftoInd.FisherSeries[Bars.Range.To]<_ftoInd.Ma1Series[Bars.Range.To]) isFMD=true; else isFMD=false;
             *
             *
             * if(isFMU)
             *      {   if (posGuidBuy!=Guid.Empty && Trade.GetPosition(posGuidBuy).State==PositionState.Active)
             *                      {var res1 = Trade.CloseMarketPosition(posGuidBuy); if (res1.IsSuccessful) posGuidBuy = Guid.Empty;}  }
             *
             *      if(isFMD)
             *  {  if (posGuidSell!=Guid.Empty && Trade.GetPosition(posGuidSell).State==PositionState.Active)
             *             {var res3 = Trade.CloseMarketPosition(posGuidSell); if (res3.IsSuccessful) posGuidSell = Guid.Empty;}  }
             */
        }
示例#6
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];

            _maInd200   = _ma200.SeriesMa[Bars.Range.To - 1];
            _maInd200_4 = _ma200.SeriesMa[Bars.Range.To - 4];

            if (_maInd200_4 > _maInd1_4 && _maInd200 > _maInd1_1)
            {
                rl = ((_maInd200_4 - _maInd1_4) < (_maInd200 - _maInd1_1));
            }
            else
            {
                rl = false;
            }
            if (_maInd200_4 < _maInd1_4 && _maInd200 < _maInd1_1)
            {
                rh = ((_maInd1_4 - _maInd200_4) < (_maInd1_1 - _maInd200));
            }
            else
            {
                rh = false;
            }
            //_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];

//=== Определение уровня STOP ===========================================================================================================
            if (_frInd.TopSeries[Bars.Range.To - 5] > 0)
            {
                frUp = true; frUpH = Bars[Bars.Range.To - 5].High;
            }
            else
            {
                frUp = false;
            }
            if (_frInd.BottomSeries[Bars.Range.To - 5] > 0)
            {
                frDown = true; frDownL = Bars[Bars.Range.To - 5].Low;
            }
            else
            {
                frDown = false;
            }

//=== КОРЕКЦИЯ ===========================================================================================================
            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;
            }

//=== КОРЕКЦИЯ STOP ===========================================================================================================
            var posBuy  = Trade.GetPosition(posGuidBuy);
            var posSell = Trade.GetPosition(posGuidSell);

            if (frUp && posGuidSell != Guid.Empty && frUpH < (posSell.OpenPrice + 0.00200))
            {
                Trade.UpdateMarketPosition(posGuidSell, frUpH + Instrument.Spread, null, null);
            }
            if (frDown && posGuidBuy != Guid.Empty && frDownL > (posBuy.OpenPrice - 0.00200))
            {
                Trade.UpdateMarketPosition(posGuidBuy, frDownL - Instrument.Spread, null, null);
            }
//=== БЕЗУБЫТОК STOP ===========================================================================================================
//		      if (posGuidBuy!=Guid.Empty)  { var posBuy = Trade.GetPosition(posGuidBuy);
//				             Print("Buy Price={0} Pips{1}", posBuy.OpenPrice,posBuy.Pips);
//				             if(posBuy.Pips>100 && posBuy.StopLoss<posBuy.OpenPrice)
//								       Trade.UpdateMarketPosition(posGuidBuy, posBuy.OpenPrice, null, null); }
//		      if (posGuidSell!=Guid.Empty) { var posSell = Trade.GetPosition(posGuidSell);
//				             Print("Sell Price={0} Pips{1}", posSell.OpenPrice,posSell.Pips);
//				             if(posSell.Pips>100  && posSell.StopLoss>posSell.OpenPrice)
//								       Trade.UpdateMarketPosition(posGuidSell, posSell.OpenPrice, null, null); }

//  1
            if (_maInd3_1 > _maInd2_1 && _maInd2_1 > _maInd1_1)
            {
                L1 = true;
            }
            else
            {
                L1 = false;
            }
            if (_maInd3_4 > _maInd2_4 && _maInd2_4 > _maInd1_4)
            {
                L2 = false;
            }
            else
            {
                L2 = true;
            }

            if (_maInd1_1 > _maInd2_1 && _maInd2_1 > _maInd3_1)
            {
                H1 = true;
            }
            else
            {
                H1 = false;
            }
            if (_maInd1_4 > _maInd2_4 && _maInd2_4 > _maInd3_4)
            {
                H2 = false;
            }
            else
            {
                H2 = true;
            }

            if (posGuidBuy != Guid.Empty)
            {
                var pos       = Trade.GetPosition(posGuidBuy);
                var openTime  = pos.OpenTime;
                var openPrice = pos.OpenPrice;
                //			  Print("Buy Time : {0} - {1} = {2}",Bars[Bars.Range.To-1].Time.Hour,openTime.Hour,(Bars[Bars.Range.To-1].Time.Hour-openTime.Hour) );
                if (Bars[Bars.Range.To - 1].Time.Hour - openTime.Hour > 2 && openPrice > Bars[Bars.Range.To - 1].Close)
                {
                    var res = Trade.CloseMarketPosition(posGuidBuy);
                    if (res.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
            }

            if (posGuidSell != Guid.Empty)
            {
                var pos       = Trade.GetPosition(posGuidSell);
                var openTime  = pos.OpenTime;
                var openPrice = pos.OpenPrice;
                //			  Print("Buy Time : {0} - {1} = {2}",Bars[Bars.Range.To-1].Time.Hour,openTime.Hour,(Bars[Bars.Range.To-1].Time.Hour-openTime.Hour) );
                if (Bars[Bars.Range.To - 1].Time.Hour - openTime.Hour > 2 && Bars[Bars.Range.To - 1].Close > openPrice)
                {
                    var res = Trade.CloseMarketPosition(posGuidSell);
                    if (res.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
            }


            if (!H1 && posGuidBuy != Guid.Empty)
            {
                var res = Trade.CloseMarketPosition(posGuidBuy);
                if (res.IsSuccessful)
                {
                    posGuidBuy = Guid.Empty;
                }
            }

            if (!L1 && posGuidSell != Guid.Empty)
            {
                var res = Trade.CloseMarketPosition(posGuidSell);
                if (res.IsSuccessful)
                {
                    posGuidSell = Guid.Empty;
                }
            }


            if (L1 && L2 && sF < 0 && _maInd200 > _maInd3_1)
            {
                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.InPrice(frUpH, null), null, null);
                    //	var result = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1, Instrument.Ask, -1, Stops.InPips(200,null), null, null);
                    if (result.IsSuccessful)
                    {
                        posGuidSell = result.Position.Id;
                    }
                }
            }
//  2
            if (H1 && H2 && sF > 0 && _maInd200 < _maInd3_1)
            {
                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.Bid, -1, Stops.InPrice(frDownL, null), null, null);
                    //var result = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1, Instrument.Bid, -1, Stops.InPips(200,null), null, null);
                    if (result.IsSuccessful)
                    {
                        posGuidBuy = result.Position.Id;
                    }
                }
            }

//Print("{0} {1} {2} {3} {4} {5} {6} {7}",Bars[Bars.Range.To-1].Time,_maInd3_3,_maInd3_2,_maInd3_1,_maInd1_1,_maInd2_1,_macdInd1,sF);
//Print("{0} - {1} {2} {3} {4}",Bars[Bars.Range.To-1].Time, (_maInd1_1>_maInd2_1), (_maInd3_3>_maInd1_3), (_maInd3_1>_maInd2_1), sF );
            //Print("{0}",Bars[Bars.Range.To-1].Time);
            //Print("OK!");
        }
示例#7
0
        protected override void NewBar()
        {
            i++;
            // Event occurs on every new bar
            //    if (firstBar)
            //{
            //    var result = Trade.BuyStop(Instrument.Id, 0.1, Bars[Bars.Range.To-1].High+0.002);
            //  if (result.IsSuccessful) posGuid = result.Position.Id;
            // firstBar = false;
            //}

            //Print("{0} - Ask={1} Bid={2} - {3}",Bars[Bars.Range.To-1],Instrument.Ask,Instrument.Bid,Bars[Bars.Range.To-1].Time);

            if (firstBar)
            {
                firstBar = false;
                Print("Ask={0}  Point={1} PriceScale={2}", Instrument.Ask, Instrument.Point, Instrument.PriceScale);

                var result = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.SellStop, 0.1, Instrument.Ask - 0.001, 0, Stops.InPrice(Instrument.Bid + 0.002), null, null, null);
                if (result.IsSuccessful)
                {
                    posGuid = result.Position.Id;
                }
            }
            if (Trade.GetPosition(posGuid).State == PositionState.Closed)
            {
                var result = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.SellStop, 0.1, Instrument.Ask - 0.001, 0, Stops.InPrice(Instrument.Bid + 0.002), null, null, null);
                if (result.IsSuccessful)
                {
                    posGuid = result.Position.Id;
                }
            }

            Print("State={0} -- {1}", Trade.GetPosition(posGuid).State, Bars[Bars.Range.To - 1].Time);
            //if (i==20)
            //{
            //var res = Trade.UpdatePendingPosition(posGuid, 0.1, Bars[Bars.Range.To-1].High+0.002, null, null);
            //var res = Trade.UpdateMarketPosition(posGuid, Bars[Bars.Range.To-1].Low-0.003, null, "Added stoploss and takeprofit");
            //}
            //if (i>30)
            //{
            //  if (posGuid==Guid.Empty) return;
            //  var res = Trade.CancelPendingPosition(posGuid);
            //   if (res.IsSuccessful) posGuid = Guid.Empty;
            //}

            //if (i>20) {
            // if (posGuid!=Guid.Empty) {
            //var res = Trade.CloseMarketPosition(posGuid);
            //if (res.IsSuccessful) posGuid = Guid.Empty;
            //} }

// var pos = Trade.GetPosition(posGuid);
//        var state = pos.State;
            //Print("State={0}",state);
        }
示例#8
0
        protected override void NewBar()
        {
            _wprInd.ReInit();
            // ЗАКРИТТЯ ПОЗИЦІЙ !!!
            // ЗНИЗУ ВВЕРХ
            if (_awoInd.SeriesUp[Bars.Range.To - 3] < 0 && _awoInd.SeriesUp[Bars.Range.To - 2] > 0)
            {
                iU1 = Bars.Range.To; iU3 = Bars.Range.To - 3;
                var vr = Tools.Create <VerticalLine>(); vr.Color = Color.BlueViolet; vr.Time = Bars[iU1].Time;
                if (Trade.GetPosition(posGuidSell).State == PositionState.Active)
                {
                    var res = Trade.CloseMarketPosition(posGuidSell);
                    if (res.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
                if (Trade.GetPosition(posGuidSell).State == PositionState.Pending)
                {
                    var res1 = Trade.CancelPendingPosition(posGuidSell);
                    if (res1.IsSuccessful)
                    {
                        posGuidSell = Guid.Empty;
                    }
                }
            }
            // ЗВЕРХУ ВНИЗ
            if (_awoInd.SeriesDown[Bars.Range.To - 3] > 0 && _awoInd.SeriesDown[Bars.Range.To - 2] < 0)
            {
                iD1 = Bars.Range.To; iD3 = Bars.Range.To - 3;
                var vr = Tools.Create <VerticalLine>(); vr.Color = Color.Black; vr.Time = Bars[iD1].Time;
                if (Trade.GetPosition(posGuidBuy).State == PositionState.Active)
                {
                    var res2 = Trade.CloseMarketPosition(posGuidBuy);
                    if (res2.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
                if (Trade.GetPosition(posGuidBuy).State == PositionState.Pending)
                {
                    var res3 = Trade.CancelPendingPosition(posGuidBuy);
                    if (res3.IsSuccessful)
                    {
                        posGuidBuy = Guid.Empty;
                    }
                }
            }

            //=== КОРЕКЦИЯ ===========================================================================================================
            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 (_wprInd.MainIndicatorSeries[Bars.Range.To - 1] > 0)
            {             // Всі точки  зиззага
                vy.Time = Bars[Bars.Range.To - 1].Time;

                zz3  = zz2;       zz2 = zz1;   zz1 = _wprInd.MainIndicatorSeries[Bars.Range.To - 1];            // Значення ZigZag
                zzi3 = zzi2;      zzi2 = zzi1;  zzi1 = Bars.Range.To - 1;                                       // Індекс свічок
                //  Індекси свічок в Куті перегибу

                // ПИК ВВЕРХУ - закрыть покупку
                if (zz3 < zz2 && zz2 > zz1)
                {
                    zzd3 = zzd2; zzd2 = zzd1; zzd1 = zzi2;
                    if (posGuidBuy != Guid.Empty && Trade.GetPosition(posGuidBuy).State == PositionState.Active)
                    {
                        var res = Trade.CloseMarketPosition(posGuidBuy); if (res.IsSuccessful)
                        {
                            posGuidBuy = Guid.Empty;
                        }
                    }
                }
                // ПИК ВНИЗУ - закрыть продажа
                if (zz3 > zz2 && zz2 < zz1)
                {
                    zzd3 = zzd2; zzd2 = zzd1; zzd1 = zzi2;
                    if (posGuidSell != Guid.Empty && Trade.GetPosition(posGuidSell).State == PositionState.Active)
                    {
                        var res = Trade.CloseMarketPosition(posGuidSell); if (res.IsSuccessful)
                        {
                            posGuidSell = Guid.Empty;
                        }
                    }
                }

//============== Тренд ВВЕРХ - індекси свічок zzd1-3  ====================================================
                if (Bars[zzd2].High > Bars[zzd1].High &&
                    Bars[zzd2].High > Bars[zzd3].High &&
                    Bars[zzd1].High > Bars[zzd3].High &&
                    (Bars[zzd2].High - Bars[zzd1].Low) > (Bars[zzd2].High - Bars[zzd3].Low) * 0.23 &&
                    (Bars[zzd2].High - Bars[zzd1].Low) < (Bars[zzd2].High - Bars[zzd3].Low) * 0.8
                    )
                {                       //vr.Time=Bars[zzd1].Time; vb.Time=Bars[zzd2].Time; vg.Time=Bars[zzd3].Time;
                    Print("{0}  Buy={1} iU={2} zzd3={3} zzd2={4}", Bars[Bars.Range.To - 1].Time, posGuidBuy, iU1, zzd3, zzd2);
                    //var vr=Tools.Create<VerticalLine>(); vr.Color=Color.Red; vr.Time=Bars[zzd1].Time;
                    if (posGuidBuy == Guid.Empty && zzd3 < iU1 && zzd2 > iU3)
                    {
                        var result = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.BuyStop, 0.1, Bars[zzd2].High, 0, Stops.InPrice(Bars[zzd3].Low, null), null, null, null);
                        //var result = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Buy, 0.1,Instrument.Bid, -1, Stops.InPrice(Bars[zzd3].Low,null), null, null);
                        if (result.IsSuccessful)
                        {
                            posGuidBuy = result.Position.Id;
                        }
                    }
                }

//============= Тренд ВНИЗ - індекси свічок zzd1  ==========================================================
                if (Bars[zzd3].Low > Bars[zzd2].Low &&
                    Bars[zzd1].Low > Bars[zzd2].Low &&
                    Bars[zzd3].Low > Bars[zzd1].Low &&
                    (Bars[zzd1].High - Bars[zzd2].Low) > (Bars[zzd3].High - Bars[zzd2].Low) * 0.23 &&
                    (Bars[zzd1].High - Bars[zzd2].Low) < (Bars[zzd3].High - Bars[zzd2].Low) * 0.8
                    )
                {                   // vr.Time=Bars[zzd1].Time; vb.Time=Bars[zzd2].Time; vg.Time=Bars[zzd3].Time;
                    // var vr=Tools.Create<VerticalLine>(); vr.Color=Color.Blue; vr.Time=Bars[zzd1].Time;
                    Print("{0}  Sell={1} iD={2} zzd3={3} zzd2={4} ", Bars[Bars.Range.To - 1].Time, posGuidSell, iD1, zzd3, zzd2);
                    if (posGuidSell == Guid.Empty && zzd3 < iD1 && zzd2 > iD3)
                    {
                        var result1 = Trade.OpenPendingPosition(Instrument.Id, ExecutionRule.SellStop, 0.1, Bars[zzd2].Low, 0, Stops.InPrice(Bars[zzd3].High, null), null, null, null);
                        //var result1 = Trade.OpenMarketPosition(Instrument.Id, ExecutionRule.Sell, 0.1,Instrument.Ask, -1, Stops.InPrice(Bars[zzd3].High,null), null, null);
                        if (result1.IsSuccessful)
                        {
                            posGuidSell = result1.Position.Id;
                            //XXPrint("{0} ",Bars[Bars.Range.To-1].Time,);
                        }
                    }
                }
            }
        }
示例#9
0
        protected void setPendingBuyOrder()
        {
            if (listOfBuyStop.Count >= MaxQuantityOfBuyOrders)
            {
                return;
            }

            double OP         = CalculateOpenPrice(ExecutionRule.BuyStop);
            double CurrentAsk = Instrument.Ask;
            double CurrentBid = Instrument.Bid;

            if (OP <= (CurrentAsk + 2 * Instrument.Spread))
            {
                OP = Math.Round(CurrentAsk + 3.14 * Instrument.Spread, Instrument.PriceScale);
            }

            double SL = CalculateSL(ExecutionRule.BuyStop, OP);

            if (OP <= SL)
            {
                SL = (double)Math.Round(OP - staticSL * Instrument.Point, Instrument.PriceScale);
            }
            double TP = CalculateTP(ExecutionRule.BuyStop, OP);

            if (CurrentAsk <= SL)
            {
                SL = (double)Math.Round(CurrentAsk - (CurrentAsk - SL) - 3.14 * Instrument.Spread, Instrument.PriceScale);
            }

            if (TP <= OP)
            {
                TP = (double)Math.Round(OP + (OP - TP) + 3.14 * Instrument.Spread, Instrument.PriceScale);
            }


            Stops SLTP = Stops.InPrice(
                SL,
                TP
                );


            int         attempt = 0;
            TradeResult tR      = null;

            do
            {
                attempt++;
                tR = Trade.OpenPendingPosition(Instrument.Id,
                                               ExecutionRule.BuyStop,
                                               vol,
                                               OP,
                                               -1,
                                               SLTP,
                                               Bars[_CIndex].Time.AddDays(90.0),
                                               "buyStop on bar signal",
                                               magicNumber);
            }while ((tR == null? true: !tR.IsSuccessful) && attempt < 10);

            if ((tR == null? true: !tR.IsSuccessful))
            {
                XXPrint("[{2}>>]{1} Cann't Send  BuyStop order on Price {3} at Bid: {0} ",
                        Instrument.Bid, DateTime.Now, Instrument.Name, OP);
            }
            else
            {
                TradeResult ntR = null;
                do
                {
                    attempt++;
                    ntR = Trade.UpdatePendingPosition(tR.Position.Id,
                                                      vol,
                                                      OP,
                                                      -1,
                                                      Bars[_CIndex].Time.AddDays(90.0),
                                                      SL,
                                                      TP,
                                                      "buyStop set SL,TP");
                }while ((ntR == null? true: !ntR.IsSuccessful) && attempt < 10);

                if (ntR == null? true: !ntR.IsSuccessful)
                {
                    XXPrint("[{2}>>]{1} Cann't modify  BuyStop order on Price {3} at Bid: {0} ",
                            Instrument.Bid, DateTime.Now, Instrument.Name, OP);

                    attempt = 0;
                    do
                    {
                        attempt++;
                        ntR = Trade.CancelPendingPosition(tR.Position.Id);
                    }while((ntR == null? true: !ntR.IsSuccessful) && attempt < 10);

                    if (ntR == null? true: !ntR.IsSuccessful)
                    {
                        XXPrint("[*EC*{2}>>]{1} Cann't cancel strange BuyStop order on Price {3} at Bid: {0} ",
                                Instrument.Bid, DateTime.Now, Instrument.Name, OP);
                    }
                }
                else
                {
                    listOfBuyStop.Add(ntR.Position.Id);
                    XXPrint("[{3}>>]{2} Sended successfully BuyStop order for position {0} at Price: {1} ",
                            tR.Position.Id, tR.Position.OpenPrice, Bars[_CIndex].Time, Instrument.Name);
                }
            }
        }