Example #1
0
        void Usrednenie() // усреднение позиций при снижении рынка
        {
            List <Position> positions = _tab.PositionsOpenAll;
            decimal         per       = Percent_birgi();

            Price_kon_trade();
            Lot(min_sum.ValueDecimal);
            decimal z = _tab.PositionsLast.EntryPrice;

            if (z > market_price + _deltaUsredn + per && bol_Down > market_price) // если цена ниже последнего трейда и выше машки
            //if (z > market_price + _deltaUsredn + per && volum_ma_short < market_price) // если цена ниже последнего трейда и выше машки
            {
                min_lot = Lot(min_sum.ValueDecimal);
                Balans_kvot(kvot_name);
                Balans_tovara(tovar_name);
                decimal v = VolumForUsred();
                if (v > min_lot)
                {
                    if (_tab.PositionsLast.MyTrades.Count != 0)
                    {
                        Price_kon_trade();
                        if (positions[0].State != PositionStateType.Opening)
                        {
                            _tab.BuyAtMarketToPosition(positions[0], MyBlanks.Okruglenie(v, 6));
                        }
                    }
                    Price_kon_trade(); // перепроверяем цену последних сделок
                    Thread.Sleep(1500);
                    Percent_tovara();  // смотрим процент купленного товара
                    Console.WriteLine("Усреднились НА - " + v * _tab.PriceBestAsk + " $");
                }
            }
        }
Example #2
0
        void Usrednenie() // усреднение позиций при снижении рынка // добавь! в релиз + _kom
        {
            List <Position> positions = _vkl.PositionsOpenAll;

            Percent_birgi();                                                                                               // расчет величины в пунктах  процента биржи
            Kol_Trad();
            if (_vkl.PositionsLast.EntryPrice > _vkl.MarketDepth.Asks[0].Price + Step(DeltaUsredn) * _mnog + _kom * _mnog) // условия усреднения _kom +
            {
                if (Price_can_trade() > _vkl.MarketDepth.Asks[0].Price + Step(DeltaUsredn) * _mnog + _kom)
                {
                    if (volum_ma < _vkl.MarketDepth.Asks[0].Price)
                    {
                        ZaprosBalahca();
                        Rac4et_baz_bal();
                        if (tek_bal_potfela / dola_depa * veli4_usrednen > 10.1m)
                        {
                            _vkl.BuyAtMarketToPosition(positions[0], Okreglenie(Depo / dola_depa * veli4_usrednen));
                            Kol_Trad();
                            Mnog();
                            Percent_birgi();
                            _vkl_PositionNetVolumeChangeEvent(positions[0]);
                        }
                        Dola_depa();
                    }
                }
            }
        }
Example #3
0
        /// <summary>
        /// логика торговли
        /// </summary>
        /// <param name="candles"></param>
        private void LogicOpenPosition(List <Candle> candles)
        {
            if (_lines == null ||
                _lines.Count == 0)
            {
                return;
            }
            // 1 выясняем каким объёмом и в какую сторону нам надо заходить
            decimal totalDeal = 0;

            decimal lastPrice = candles[candles.Count - 2].Close;
            decimal nowPrice  = candles[candles.Count - 1].Close;

            for (int i = 0; i < _lines.Count; i++)
            {
                if (lastPrice < _lines[i] &&
                    nowPrice > _lines[i])
                { // пробой снизу вверх
                    totalDeal--;
                }

                if (lastPrice > _lines[i] &&
                    nowPrice < _lines[i])
                { // пробой сверху вниз
                    totalDeal++;
                }
            }

            if (totalDeal == 0)
            {
                return;
            }

            // 2 заходим в нужную сторону

            if (totalDeal > 0)
            { // нужно лонговать
                List <Position> positionsShort = _tab.PositionOpenShort;

                if (positionsShort != null && positionsShort.Count != 0)
                {
                    if (positionsShort[0].OpenVolume <= totalDeal)
                    {
                        _tab.CloseAtMarket(positionsShort[0], positionsShort[0].OpenVolume);
                        totalDeal -= positionsShort[0].OpenVolume;
                    }
                    else
                    {
                        _tab.CloseAtMarket(positionsShort[0], totalDeal);
                        totalDeal = 0;
                    }
                }

                if (totalDeal > 0 && totalDeal != 0)
                {
                    List <Position> positionsLong = _tab.PositionOpenLong;

                    if (positionsLong != null && positionsLong.Count != 0)
                    {
                        _tab.BuyAtMarketToPosition(positionsLong[0], totalDeal);
                    }
                    else
                    {
                        _tab.BuyAtMarket(totalDeal);
                    }
                }
            }

            if (totalDeal < 0)
            {
                // нужно шортить
                totalDeal = Math.Abs(totalDeal);

                List <Position> positionsLong = _tab.PositionOpenLong;

                if (positionsLong != null && positionsLong.Count != 0)
                {
                    if (positionsLong[0].OpenVolume <= totalDeal)
                    {
                        _tab.CloseAtMarket(positionsLong[0], positionsLong[0].OpenVolume);
                        totalDeal -= positionsLong[0].OpenVolume;
                    }
                    else
                    {
                        _tab.CloseAtMarket(positionsLong[0], totalDeal);
                        totalDeal = 0;
                    }
                }

                if (totalDeal > 0)
                {
                    List <Position> positionsShort = _tab.PositionOpenShort;

                    if (positionsShort != null && positionsShort.Count != 0)
                    {
                        _tab.SellAtMarketToPosition(positionsShort[0], totalDeal);
                    }
                    else
                    {
                        _tab.SellAtMarket(totalDeal);
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        /// trade logic /
        /// логика торговли
        /// </summary>
        /// <param name="candles"></param>
        private void LogicOpenPosition(List <Candle> candles)
        {
            if (_lines == null ||
                _lines.Count == 0)
            {
                return;
            }
            // 1 find out how much and in what direction we need to go
            // 1 выясняем каким объёмом и в какую сторону нам надо заходить
            decimal totalDeal = 0;

            decimal lastPrice = candles[candles.Count - 2].Close;
            decimal nowPrice  = candles[candles.Count - 1].Close;

            for (int i = 0; i < _lines.Count; i++)
            {
                if (lastPrice < _lines[i] &&
                    nowPrice > _lines[i])
                {
                    totalDeal--;
                }

                if (lastPrice > _lines[i] &&
                    nowPrice < _lines[i])
                {
                    totalDeal++;
                }
            }

            if (totalDeal == 0)
            {
                return;
            }

            // 2 go in the right direction
            // 2 заходим в нужную сторону

            if (totalDeal > 0)
            {
                List <Position> positionsShort = _tab.PositionOpenShort;

                if (positionsShort != null && positionsShort.Count != 0)
                {
                    if (positionsShort[0].OpenVolume <= totalDeal)
                    {
                        _tab.CloseAtMarket(positionsShort[0], positionsShort[0].OpenVolume);
                        totalDeal -= positionsShort[0].OpenVolume;
                    }
                    else
                    {
                        _tab.CloseAtMarket(positionsShort[0], totalDeal);
                        totalDeal = 0;
                    }
                }

                if (totalDeal > 0 && totalDeal != 0)
                {
                    List <Position> positionsLong = _tab.PositionOpenLong;

                    if (positionsLong != null && positionsLong.Count != 0)
                    {
                        _tab.BuyAtMarketToPosition(positionsLong[0], totalDeal);
                    }
                    else
                    {
                        _tab.BuyAtMarket(totalDeal);
                    }
                }
            }

            if (totalDeal < 0)
            {
                totalDeal = Math.Abs(totalDeal);

                List <Position> positionsLong = _tab.PositionOpenLong;

                if (positionsLong != null && positionsLong.Count != 0)
                {
                    if (positionsLong[0].OpenVolume <= totalDeal)
                    {
                        _tab.CloseAtMarket(positionsLong[0], positionsLong[0].OpenVolume);
                        totalDeal -= positionsLong[0].OpenVolume;
                    }
                    else
                    {
                        _tab.CloseAtMarket(positionsLong[0], totalDeal);
                        totalDeal = 0;
                    }
                }

                if (totalDeal > 0)
                {
                    List <Position> positionsShort = _tab.PositionOpenShort;

                    if (positionsShort != null && positionsShort.Count != 0)
                    {
                        _tab.SellAtMarketToPosition(positionsShort[0], totalDeal);
                    }
                    else
                    {
                        _tab.SellAtMarket(totalDeal);
                    }
                }
            }
        }
Example #5
0
        decimal all_volum_trade_min;                  //все объемы за N минуту

        private void _vklad_NewTickEvent(Trade trade) // событие новых тиков для счета объема торгов
        {
            price = _vklad.PriceCenterMarketDepth;    // записываем текущую цену рынка
            if (vkl_vol_trade.ValueBool == false)
            {
                return;
            }
            List <Position> positions = _vklad.PositionsOpenAll;
            DateTime        time_add_n_min;

            time_add_n_min = dateTrade.AddMinutes(n_min.ValueInt);
            if (trade.Time < time_add_n_min)
            {
                if (trade.Side == Side.Buy)
                {
                    decimal b = trade.Volume;
                    bid_vol_tr = bid_vol_tr + b;
                }
                if (trade.Side == Side.Sell)
                {
                    decimal a = trade.Volume;
                    ask_vol_tr = ask_vol_tr + a;
                }
                all_volum_trade_min = bid_vol_tr + ask_vol_tr;
            }
            else
            {
                dateTrade           = trade.Time;
                all_volum_trade_min = 0;
                bid_vol_tr          = 0;
                ask_vol_tr          = 0;
            }
            if (bid_vol_tr > volum_piramid.ValueInt)
            {
                Price_kon_trade();
                if (volum_ma > Price_kon_trade() && positions.Count > 0)
                {
                    if (piramid_stop == false)
                    {
                        return;
                    }
                    _vklad.BuyAtMarketToPosition(positions[0], Okreglenie(VolumForUsred()));
                    Kol_Trad();
                    Mnog();
                    Console.WriteLine("Докупились при объеме покупок больше " + volum_piramid.ValueInt + " по- " + Price_kon_trade() + "НА " + VolumForUsred() * _vklad.PriceBestAsk);
                    piramid_stop = false;
                    bid_vol_tr   = 0;
                }
            }

            if (ask_vol_tr > volum_alarm.ValueInt && positions.Count > 0) // условие для аварийного выключения
            {
                if (alarm == false)
                {
                    return;
                }
                slippage.ValueDecimal = slippage.ValueDecimal + 1m;
                _vklad.CloseAtStop(positions[0], _vklad.MarketDepth.Asks[0].Price, _vklad.MarketDepth.Asks[0].Price - slippage.ValueDecimal);
                vkl_Robota.ValueBool = false; // после выставления стопа выключаем робот
                Console.WriteLine(" Аварийное выключение!!! ОБЪЕМЫ продаж больше " + volum_alarm.ValueInt + " РОБОТ ВЫКЛЮЧЕН по цене - " + Price_kon_trade());
                slippage.ValueDecimal = slippage.ValueDecimal - 1m;
                Console.WriteLine("Вернули проскальзыванию начальное значение - " + slippage.ValueDecimal);
                if (positions.Count == 0)
                {
                    alarm      = false;
                    bid_vol_tr = 0;
                }
            }
        }