public LimitConfigViewModel(DataPoint start, DataPoint end, LineSeries line, string symbol, decimal minTradeSize)
        {
            OrdersPerHourOptions = new List <double>();

            for (double i = 1; i < 61; i++)
            {
                OrdersPerHourOptions.Add(i);
            }


            Start            = start;
            End              = end;
            OrderPlacedEvent = new ManualResetEvent(false);
            OrdersPerHour    = 60;
            LineOrder        = null;
            Symbol           = symbol;
            OrderType        = OrderTypeV3.Limit;
            TimeInForce      = TimeInForce.FillOrKill;
            QuantityPerOrder = minTradeSize;
            ErrorMsg         = "";
            limitLine        = line;
            CalculateSlope(start, end);
            CalculateYIntercept(start, end);


            StartString        = DateTimeAxis.ToDateTime(start.X).ToString();
            EndString          = DateTimeAxis.ToDateTime(end.X).ToString();
            StartStringPrice   = start.Y;
            EndStringPrice     = end.Y;
            PlaceOrderCommand  = new RelayCommand(PlaceOrder, CanPlaceOrder);
            CancelOrderCommand = new RelayCommand(CancelOrder, CanCancel);
        }
        private void PlaceOrder()
        {
            if (isSell)
            {
                OrderSide       = OrderSide.Sell;
                limitLine.Color = OxyColors.Green;
            }
            else if (isBuy)
            {
                OrderSide       = OrderSide.Buy;
                limitLine.Color = OxyColors.Blue;
            }
            else
            {
                ErrorMsg = "You must choose an Order Side";
                return;
            }

            if (FulfillmentThreshold == 0)
            {
                ErrorMsg = "You must choose a threshold for the line to be Fulfilled.  Use -1 if you want to run the line order to completion.";
                return;
            }
            else if (FulfillmentThreshold < 0)
            {
                if (FulfillmentThreshold != -1)
                {
                    ErrorMsg = "Invalid Fulfillment Threshold.  Use -1 if you want to run the line order to completion.";
                    return;
                }
            }

            string id;

            AddOrderPoints();



            LineOrder order = new LineOrder()
            {
                Symbol              = Symbol,
                TimeInForce         = TimeInForce,
                Start               = StartString,
                End                 = EndString,
                LimitLineSeries     = limitLine,
                OrderSide           = OrderSide,
                OrderType           = OrderType,
                OrderIDs            = new List <string>(),
                OrdersPerHour       = OrdersPerHour,
                QuantityPerOrder    = QuantityPerOrder,
                Slope               = Slope,
                YIntercept          = Yintercept,
                FulfilmentThreshold = FulfillmentThreshold
            };

            LineOrder = order;
            Screen.Close();
        }
Example #3
0
        public void MakeOrder(LineOrder order)
        {
            DateTime currenttime;
            decimal  currentlimitprice;
            WebCallResult <BittrexOrderV3> linePointOrder;


            currenttime = DateTime.Now;
            var limit = CalculateLimitAtSpecificTime(DateTimeAxis.ToDouble(currenttime), order);

            //cancel the previous order if needed
            if (order.TimeInForce == TimeInForce.GoodTillCancelled)
            {
                if (order.LastPlacedOrderId != null)//cancel previous open order
                {
                    var or = client.GetOrder(order.LastPlacedOrderId);
                    if (or.Error == null)
                    {
                        if (or.Data.Status == OrderStatus.Closed)
                        {
                            order.ActiveTotal += double.Parse(or.Data.Proceeds.ToString()); //increase the active total for this order based on the previous orders proceeds
                            if (order.ActiveTotal > order.FulfilmentThreshold)
                            {
                                FulfilledOrders.Add(order);
                                ActiveLineOrders.Remove(order);
                                ActionHistory += DateTime.Now.TimeOfDay.ToString() + " - " + (order.OrderSide == OrderSide.Buy ? "Buy Line" : "Sell Line") + order.Symbol + " Fulfilled" + "\n";
                            }
                            else
                            {
                                ActiveLineOrders.Add(order);
                            }
                        }
                    }

                    //cancel order
                    var res = client.CancelConditionalOrder(order.LastPlacedOrderId);
                    if (res.Error != null)
                    {
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            ActionHistory += DateTime.Now.TimeOfDay.ToString() + " - " + "Error: " + " Failed to Cancel Order" + res.Error.Message + "\n";
                        });
                    }
                    else
                    {
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            ActionHistory += DateTime.Now.TimeOfDay.ToString() + " - " + "Order Cancelled: ID = " + order.LastPlacedOrderId + "\n";
                        });
                    }
                }
            }

            if (limit != 0)
            {
                currentlimitprice = Convert.ToDecimal(limit);
                currentlimitprice = Convert.ToDecimal(currentlimitprice.ToString("F5"));
                //place order
                linePointOrder = client.PlaceOrder(CurrentLoadedSymbol, order.OrderSide, order.OrderType, order.TimeInForce, order.QuantityPerOrder, currentlimitprice);

                if (linePointOrder.Error != null)
                {//error occured, log it
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        ActionHistory += DateTime.Now.TimeOfDay.ToString() + " - " + "Error: " + linePointOrder.Error.Message + "\n";
                    });
                }
                else
                {
                    order.OrderIDs.Add(linePointOrder.Data.Id);
                    order.LastPlacedOrderId = linePointOrder.Data.Id;
                    if (linePointOrder.Data.Status == OrderStatus.Closed)
                    {
                        order.ActiveTotal += double.Parse(linePointOrder.Data.Proceeds.ToString()); //increase the active total for this order based on the previous orders proceeds
                    }

                    //log the order
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        ActionHistory += DateTime.Now.TimeOfDay.ToString() + " - " + (order.OrderSide == OrderSide.Buy ? "Buy" : "Sell") + order.Symbol + " at $" + currentlimitprice + "\n";
                        //new Popup("Order Placed", order.Symbol + " at $" + currentlimitprice).Show();
                        if (order.ActiveTotal > order.FulfilmentThreshold)
                        {
                            FulfilledOrders.Add(order);
                            ActiveLineOrders.Remove(order);
                            ActionHistory += DateTime.Now.TimeOfDay.ToString() + " - " + (order.OrderSide == OrderSide.Buy ? "Buy Line" : "Sell Line") + order.Symbol + " Fulfilled" + "\n";
                        }
                        else
                        {
                            ActiveLineOrders.Add(order);
                        }
                    });
                }
            }
            else
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    ActionHistory += DateTime.Now.TimeOfDay.ToString() + " - " + "Limit price could not be calculated" + "\n";
                });
                OnPropertyChanged("FulfilledOrders");

                Thread.Sleep(10000); //keep running every 10 seconds
            }
        }
Example #4
0
 public double CalculateTimeAtSpecificPrice(double price, LineOrder order)
 {//y = mx+b
     return((price - order.YIntercept) / order.Slope);
 }
Example #5
0
 public double CalculateLimitAtSpecificTime(double time, LineOrder order)
 { // y-b/ m = x
     return((order.Slope * time) + order.YIntercept);
 }