Пример #1
0
        void OnReceiveQuotes(object sender, EventHandler.BackTesting.Quotes e)
        {
            if (int.TryParse(e.Time, out int time) && (time > 153459 && time < 180000) == false && string.IsNullOrEmpty(bt.Classification) == false)
            {
                string   classification = bt.Classification;
                var      check = classification.Equals(buy);
                var      max = Max(specify.Assets / ((check ? e.BuyPrice : e.SellPrice) * Const.TransactionMultiplier * Const.MarginRate200402), classification);
                int      i, being = (int)max;
                double[] sp = new double[5], bp = new double[5];
                bt.MaxAmount = max * (classification.Equals(buy) ? 1 : -1);

                for (i = 4; i > -1; i--)
                {
                    sp[i] = e.SellPrice + Const.ErrorRate * (4 - i);
                    bp[i] = e.BuyPrice - Const.ErrorRate * (4 - i);
                }
                switch (classification)
                {
                case sell:
                    if (bt.Quantity < 0 && string.IsNullOrEmpty(bt.AvgPurchase) == false)
                    {
                        var price = GetExactPrice(bt.AvgPurchase);

                        switch (bt.BuyOrder.Count)
                        {
                        case 0:
                            if (price.Equals(Price) == false)
                            {
                                bt.SendNewOrder(price, buy);
                                Price = price;

                                return;
                            }
                            else if (max < Math.Abs(API.Quantity))
                            {
                                bt.SendNewOrder(e.BuyPrice, max, buy);

                                return;
                            }
                            break;

                        case 1:
                            var number = bt.BuyOrder.First().Key;

                            if (bt.BuyOrder.TryGetValue(number, out double cbp))
                            {
                                if (cbp.ToString("F2").Equals(price) == false)
                                {
                                    bt.SendCorrectionOrder(price, number);

                                    return;
                                }
                                else if (Array.Exists(bp, o => o == cbp) == false)
                                {
                                    bt.SendClearingOrder(number);

                                    return;
                                }
                            }
                            break;

                        default:
                            var order = bt.BuyOrder.First(f => f.Value == bt.BuyOrder.Max(o => o.Value)).Key;

                            if (bt.BuyOrder.ContainsKey(order))
                            {
                                bt.SendClearingOrder(order);

                                return;
                            }
                            break;
                        }
                    }
                    for (i = 4; i > -1; i--)
                    {
                        if (being + bt.Quantity <= i && bt.SellOrder.ContainsValue(sp[i]))
                        {
                            var number = bt.SellOrder.First(o => o.Value == bt.SellOrder.Min(m => m.Value)).Key;
                            var price  = bt.SellOrder.Max(o => o.Value) + Const.ErrorRate;

                            if (bt.SellOrder.ContainsKey(number))
                            {
                                bt.SendCorrectionOrder(price.ToString("F2"), number);

                                return;
                            }
                        }
                    }
                    break;

                case buy:
                    if (bt.Quantity > 0 && string.IsNullOrEmpty(bt.AvgPurchase) == false)
                    {
                        var price = GetExactPrice(bt.AvgPurchase);

                        switch (bt.SellOrder.Count)
                        {
                        case 0:
                            if (price.Equals(Price) == false)
                            {
                                bt.SendNewOrder(price, sell);
                                Price = price;

                                return;
                            }
                            else if (max < API.Quantity)
                            {
                                bt.SendNewOrder(e.SellPrice, max, sell);

                                return;
                            }
                            break;

                        case 1:
                            var number = bt.SellOrder.First().Key;

                            if (bt.SellOrder.TryGetValue(number, out double csp))
                            {
                                if (csp.ToString("F2").Equals(price) == false)
                                {
                                    bt.SendCorrectionOrder(price, number);

                                    return;
                                }
                                else if (Array.Exists(sp, o => o == csp) == false)
                                {
                                    bt.SendClearingOrder(number);

                                    return;
                                }
                            }
                            break;

                        default:
                            var order = bt.SellOrder.First(f => f.Value == bt.SellOrder.Min(o => o.Value)).Key;

                            if (bt.SellOrder.ContainsKey(order))
                            {
                                bt.SendClearingOrder(order);

                                return;
                            }
                            break;
                        }
                    }
                    for (i = 4; i > -1; i--)
                    {
                        if (being - bt.Quantity <= i && bt.BuyOrder.ContainsValue(bp[i]))
                        {
                            var number = bt.BuyOrder.First(o => o.Value == bt.BuyOrder.Max(m => m.Value)).Key;
                            var price  = bt.BuyOrder.Min(o => o.Value) - Const.ErrorRate;

                            if (bt.BuyOrder.ContainsKey(number))
                            {
                                bt.SendCorrectionOrder(price.ToString("F2"), number);

                                return;
                            }
                        }
                    }
                    break;
                }
                foreach (var kv in check ? bt.BuyOrder : bt.SellOrder)
                {
                    if (Array.Exists(check ? bp : sp, o => o == kv.Value) == false && (check ? bt.BuyOrder.ContainsKey(kv.Key) : bt.SellOrder.ContainsKey(kv.Key)))
                    {
                        bt.SendClearingOrder(kv.Key);

                        return;
                    }
                }
                bt.SendNewOrder(check ? e.BuyPrice : e.SellPrice, max, classification);
            }
        }
Пример #2
0
        void OnReceiveQuotes(object sender, EventHandler.BackTesting.Quotes e)
        {
            if (int.TryParse(e.Time.Substring(6, 6), out int time) && (time < 090007 && time > 045959) == false && (time > 153459 && time < 180000) == false && string.IsNullOrEmpty(bt.Classification) == false)
            {
                string   classification = bt.Classification, price;
                var      check = classification.Equals(buy);
                var      max   = Max(specify.Assets / ((check ? e.BuyPrice : e.SellPrice) * Const.TransactionMultiplier * specify.MarginRate), classification);
                double[] sp    = new double[10], bp = new double[10];
                bt.MaxAmount = max * (classification.Equals(buy) ? 1 : -1);

                for (int i = 0; i < 10; i++)
                {
                    if (double.TryParse((e.BuyPrice - Const.ErrorRate * (9 - i)).ToString("F2"), out double bt) && double.TryParse((e.SellPrice + Const.ErrorRate * (9 - i)).ToString("F2"), out double st))
                    {
                        sp[i] = st;
                        bp[i] = bt;
                    }
                }
                if (bt.PurchasePrice > 0)
                {
                    price = GetExactPrice(bt.PurchasePrice.ToString());

                    switch (classification)
                    {
                    case sell:
                        if (bt.Quantity < 0)
                        {
                            if (bt.BuyOrder.Count == 0 && max < 1 - bt.Quantity && ForTheLiquidationOfSellOrder(e.Time, price, bp, e.BuyQuantity))
                            {
                                return;
                            }

                            if (bt.BuyOrder.Count > 0 && ForTheLiquidationOfSellOrder(e.Time, bp))
                            {
                                return;
                            }

                            if (bt.SellOrder.Count > 0 && SetCorrectionSellOrder(e.Time, price, sp[sp.Length - 1], e.SellQuantity))
                            {
                                return;
                            }
                        }
                        else if (bt.BuyOrder.Count > 1 && SetBuyDecentralize(e.Time, bp[bp.Length - 1], e.BuyQuantity))
                        {
                            return;
                        }

                        break;

                    case buy:
                        if (bt.Quantity > 0)
                        {
                            if (bt.SellOrder.Count == 0 && max < bt.Quantity - 1 && ForTheLiquidationOfBuyOrder(e.Time, price, sp, e.SellQuantity))
                            {
                                return;
                            }

                            if (bt.SellOrder.Count > 0 && ForTheLiquidationOfBuyOrder(e.Time, sp))
                            {
                                return;
                            }

                            if (bt.BuyOrder.Count > 0 && SetCorrectionBuyOrder(e.Time, price, bp[bp.Length - 1], e.BuyQuantity))
                            {
                                return;
                            }
                        }
                        else if (bt.SellOrder.Count > 1 && SetSellDecentralize(e.Time, sp[sp.Length - 1], e.SellQuantity))
                        {
                            return;
                        }

                        break;
                    }
                }
                foreach (var kv in check ? bt.BuyOrder : bt.SellOrder)
                {
                    if (double.TryParse(kv.Key, out double key) && Array.Exists(check ? bp : sp, o => o == key) == false && (check ? bt.BuyOrder.ContainsKey(kv.Key) : bt.SellOrder.ContainsKey(kv.Key)))
                    {
                        bt.SendClearingOrder(e.Time, kv.Value);

                        return;
                    }
                }
                SendNewOrder(e.Time, check ? bp : sp, classification, check ? e.BuyQuantity : e.SellQuantity);
            }
        }