Beispiel #1
0
        private void AddTakeProfitExecute(object param)
        {
            int ind = TakeProfit.Count + 1;
            var tpQuantityPercent = TakeProfit.Sum(x => x.QuantityPercent);
            var tp = new TakeProfitViewModel(this, 1.00 - tpQuantityPercent, ind > 1 ? TakeProfit[ind - 2] : null)
            {
                Price = Math.Round(Buy.Price * (1.0m + 0.05m * ind), SymbolInformation.PriceDecimals), Caption = $"Тейк {ind}"
            };

            TakeProfit.Add(tp);
        }
Beispiel #2
0
        private async Task <bool> SubmitImpl(string param)
        {
            Buy.Model.Symbol = SymbolInformation.Symbol;
            Buy.Model.Side   = TradeSide.Buy;
            Buy.Model.Kind   = OrderKind.Buy;
            Buy.Model.Type   = IsMarketBuy ? OrderType.MARKET : OrderType.LIMIT;

            StopLoss.Model.Symbol = SymbolInformation.Symbol;
            StopLoss.Model.Side   = TradeSide.Sell;
            StopLoss.Model.Kind   = OrderKind.StopLoss;
            StopLoss.Model.Type   = IsLimitStop ? OrderType.STOP_LIMIT : OrderType.MARKET;
            StopLoss.Quantity     = Buy.Quantity;

            Debug.Assert(Buy.Model.Quantity >= SymbolInformation.MinQuantity);
            Debug.Assert((Buy.Model.Price * Buy.Model.Quantity) >= SymbolInformation.MinNotional);
            Debug.Assert((StopLoss.Model.Price * StopLoss.Model.Quantity) >= SymbolInformation.MinNotional);

            foreach (var tp in TakeProfit)
            {
                tp.Model.Symbol = SymbolInformation.Symbol;
                tp.Model.Side   = TradeSide.Sell;
                tp.Model.Kind   = OrderKind.TakeProfit;
                tp.Model.Type   = OrderType.MARKET;
                if (tp.QuantityPercent > 0.01)
                {
                    tp.Quantity = SymbolInformation.ClampQuantity(Buy.Quantity * (decimal)tp.QuantityPercent);
                    Debug.Assert(tp.Quantity >= SymbolInformation.MinQuantity);
                    Debug.Assert(tp.Total >= SymbolInformation.MinNotional);
                }
                else
                {
                    tp.Quantity = 0m;
                }
            }

            var tpQuantityTotal   = TakeProfit.Sum(tp => tp.Quantity);
            var tpQuantityPercent = TakeProfit.Sum(tp => tp.QuantityPercent);

            if (tpQuantityPercent > 0.98)
            {
                tpQuantityPercent = 1.00;
            }
            var tpValidQuantity = SymbolInformation.ClampQuantity(Buy.Quantity * (decimal)tpQuantityPercent);

            var tpQuantityLeft = tpValidQuantity - tpQuantityTotal;

            while (tpQuantityLeft >= SymbolInformation.StepSize)
            {
                foreach (var tp in TakeProfit.Where(x => x.QuantityPercent >= 0.01))
                {
                    if (tpQuantityLeft >= SymbolInformation.StepSize)
                    {
                        tp.Quantity    += SymbolInformation.StepSize;
                        tpQuantityLeft -= SymbolInformation.StepSize;
                    }
                }
                tpQuantityLeft = Buy.Quantity - TakeProfit.Sum(tp => tp.Quantity);
            }

            Debug.Assert(Buy.Quantity >= TakeProfit.Sum(tp => tp.Quantity));

            if (tpQuantityLeft > 0m)
            {
                //System.Windows.MessageBox.Show($"Остаток: {tpQuantityLeft} {SymbolInformation.BaseAsset}");
                var ok = await Confirm.Handle($"Остаток: {tpQuantityLeft} {SymbolInformation.BaseAsset}");

                if (!ok)
                {
                    return(false);
                }
            }

            Model.Jobs.Add(Buy.Model);
            foreach (var tp in TakeProfitCollection)
            {
                // NOTE: if SL is TRAILING by STEPS,
                // then create new object with corresponding price
                // for every TP.
                Model.Jobs.Add(StopLoss.Model);
                Model.Jobs.Add(tp.Model);
            }

            Model.Created = DateTime.Now;
            Model.Updated = DateTime.Now;
            Model.Events.Add(Model.Created, "Task created.");

            SerializeModel(Model);

            return(true);
        }