Пример #1
0
        public static bool SaveShortOrderParams(ShortOrderParam shortOrderParam)
        {
            Helper.Log("Saving short order parameters to config file...");
            Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            AddConf(conf, "TICKER", shortOrderParam.TickerChoice.Pair);
            AddConf(conf, "DISTRIBUTION_TYPE", Convert.ToString(shortOrderParam.DistributionType));
            AddConf(conf, "LOWER_PRICE", Convert.ToString(shortOrderParam.LowerPrice));
            AddConf(conf, "UPPER_PRICE", Convert.ToString(shortOrderParam.UpperPrice));
            AddConf(conf, "COEFFICIENT", Convert.ToString(shortOrderParam.Coefficient));
            AddConf(conf, "MIN_SIZE", Convert.ToString(shortOrderParam.MinSize));
            AddConf(conf, "AMOUNT", Convert.ToString(shortOrderParam.Amount));
            AddConf(conf, "ORDER_COUNT", Convert.ToString(shortOrderParam.OrderCount));
            conf.Save(ConfigurationSaveMode.Full);
            ConfigurationManager.RefreshSection("appSettings");
            Helper.Log("Saved key to config file...");
            return(true);
        }
Пример #2
0
        private void btnPreviewOrders_Click(object sender, EventArgs e)
        {
            var handler = UIEventListener;

            if (handler != null)
            {
                ShortOrderParam shortOrderParam = new ShortOrderParam
                {
                    TickerChoice = new Ticker()
                    {
                        Pair = Convert.ToString(comboTicker.SelectedItem)
                    },
                    DistributionType = (DISTRIBUTION_TYPE)comboDistribution.SelectedIndex,
                    LowerPrice       = (double)spinLowerPrice.Value,
                    UpperPrice       = (double)spinUpperPrice.Value,
                    Coefficient      = (double)spinCoefficient.Value,
                    MinSize          = (double)spinMinSize.Value,
                    Amount           = (double)spinAmount.Value,
                    OrderCount       = (int)spinOrderCount.Value
                };

                handler(UI_EVENT.PREVIEW_CLICK, shortOrderParam);
            }
        }
        public List <string> PreviewOrders(ShortOrderParam shortOrderParam)
        {
            Helper.Log(string.Format("Making orders preview with input {0}...", shortOrderParam.ToString()));
            var previews = new List <string>();
            var ticker   = shortOrderParam.TickerChoice;

            if (ticker == null)
            {
                return(previews);
            }

            Config.SaveShortOrderParams(shortOrderParam);
            var    market_price       = TickerAgent.TickerPrice(ticker.Pair);
            var    limited_order_size = TickerAgent.LimitedOrderSize(ticker.Pair);
            double amount             = shortOrderParam.Amount;
            double low        = shortOrderParam.LowerPrice;
            double high       = shortOrderParam.UpperPrice;
            int    count      = shortOrderParam.OrderCount;
            double pstep      = (high - low) / (count - 1);
            var    scale_type = shortOrderParam.DistributionType;
            double min_size   = Math.Max(limited_order_size, shortOrderParam.MinSize);
            double exp        = shortOrderParam.Coefficient;

            if (min_size * count > amount)
            {
                count = (int)Math.Floor(amount / min_size) - 1;
                Helper.Log(string.Format("min_size {0} amount {1} count was reset to {2}", min_size, amount, count));
                if (count < 0)
                {
                    return(previews);
                }
                previews.Add(count.ToString());
            }
            else
            {
                previews.Add("match");
            }
            if (count <= 0)
            {
                return(previews);
            }

            double size_max    = ((amount - min_size * count) * 2) / count + min_size;
            double sum         = 0;
            double first_value = 1;

            for (double i = 0; i < count; i++)
            {
                sum = sum + Math.Pow(exp, i);
            }
            first_value = (amount - min_size * count) / sum;
            if (first_value < 0)
            {
                first_value = 0;
            }

            double[] sizes = new double[count];
            for (int i = 0; i < count; i++)
            {
                switch (scale_type)
                {
                case DISTRIBUTION_TYPE.FLAT:
                    sizes[i] = amount / count;
                    break;

                case DISTRIBUTION_TYPE.LINEAR_UP:
                    sizes[i] = min_size + (size_max - min_size) / (count - 1) * i;
                    break;

                case DISTRIBUTION_TYPE.LINEAR_DN:
                    sizes[i] = min_size + (size_max - min_size) / (count - 1) * (count - i - 1);
                    break;

                case DISTRIBUTION_TYPE.EXPONENTIAL_UP:
                    sizes[i] = min_size + first_value * Math.Pow(exp, i);
                    break;

                case DISTRIBUTION_TYPE.EXPONENTIAL_DN:
                    sizes[i] = min_size + first_value * Math.Pow(exp, count - i - 1);
                    break;

                default:
                    sizes[i] = amount / count;
                    break;
                }
            }

            var previewContent = "";

            previewContent  = string.Format("Current price: {0}", market_price);
            previewContent += Environment.NewLine;

            lock (Global._previewOrders)
            {
                Global._previewOrders.Clear();
                for (int i = 0; i < count; i++)
                {
                    double p        = low + i * pstep;
                    string ex_price = p.ToString("F5");
                    var    txt      = "at " + ex_price;

                    var order = new TrailingOrder();
                    order.Symbol   = ticker.Pair;
                    order.Price    = Convert.ToDouble(ex_price);
                    order.Exchange = "bitfinex";

                    // buy order
                    order.BuyAmount = sizes[i];
                    var type = "TRAILING STOP";
                    order.BuyType = type;
                    txt          += string.Format(" buy {0} size: {1}", type, sizes[i].ToString());

                    // sell order
                    order.SellAmount = sizes[count - i - 1];
                    type             = p > market_price ? "LIMIT" : "TRAILING STOP";
                    order.SellType   = type;
                    txt += string.Format(" sell {0} size: {1}", type, sizes[count - i - 1].ToString());
                    txt += Environment.NewLine;

                    previewContent += txt;
                    Global._previewOrders.Add(order);
                }

                previews.Add(previewContent);
                Helper.Log(string.Format("order data was made {0} (preview {1})",
                                         string.Join <TrailingOrder>(" ", Global._previewOrders), previewContent));
            }

            return(previews);
        }