public void SendOrderToMarketGenetic(Trade trade)
        {
            if (trade.BuyorSell != Trade.BuySell.None)
            {
                double price = WebSettings.TradeApproach.AdjustPriceToStrategy(trade, HiSat.LivePrice.Bid, HiSat.LivePrice.Offer);
                trade.TradedPrice =  price;
                ExcelLink.xlTradeOrder o = new xlTradeOrder()
                {
                    BS = trade.BuyorSell,
                    Price = price,
                    Volume = trade.TradeVolume,
                    Contract = trade.InstrumentName,

                };

               
                e.Connect();
                e.WriteOrder(o);
                e.Disconnect();
                OrderSendEvent oe = new OrderSendEvent();
                oe.Success = true;
                oe.Trade = trade;
                onOrderSend(this, oe);
                e.StartCheckWhenOrderCompletedTimer(10000);
            }
        }
        private xlTradeOrder ReadLastOrder()
        {
            xlBook = xlApp.ActiveWorkbook;
            xlSheet = xlBook.Worksheets.get_Item(2);
            Excel.Range last = xlSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
            Excel.Range range = xlSheet.get_Range("A1", last);
            int lastUsedRow = last.Row;
            int lastUsedColumn = last.Column + 1;
            Debug.WriteLine("Rows used " + lastUsedRow);
            var Order = new xlTradeOrder();

            Order.TimeStamp = DateTime.UtcNow.AddHours(2);
            Order.Contract = xlSheet.Cells[lastUsedRow, 1].Value;
            Order.BS = (xlSheet.Cells[lastUsedRow, 2].Value == "B") ? Trade.BuySell.Buy : Trade.BuySell.Sell;
            Order.Volume = (int)xlSheet.Cells[lastUsedRow, 3].Value;
            Order.Price = (long)xlSheet.Cells[lastUsedRow, 4].Value;
            Order.Status = StringToOrder(xlSheet.Cells[lastUsedRow, 12].Value);
            Order.GetReference();
            return Order;
        }
        public void SendOrderToMarket(Trade trade)
        {
            if (trade.BuyorSell != Trade.BuySell.None)
            {
                double price = WebSettings.TradeApproach.AdjustPriceToStrategy(trade, HiSat.LivePrice.Bid, HiSat.LivePrice.Offer);

                ExcelLink.xlTradeOrder o = new xlTradeOrder()
                {
                    BS = trade.BuyorSell,
                    Price = price,
                    Volume = trade.TradeVolume,
                    Contract = trade.InstrumentName,

                };

                try
                {

                    if (ManualTrade.CanCloseTrade(trade))
                    {
                        WebUpdate.SetManualTradeTrigger(false);
                        e.Connect();
                        e.WriteOrder(o);
                        e.Disconnect();
                        OrderSendEvent oe = new OrderSendEvent();
                        oe.Success = true;
                        oe.Trade = trade;
                        onOrderSend(this, oe);
                        e.StartCheckWhenOrderCompletedTimer(10000);
                    }

                }
                catch (Exception ex)
                {
                    OrderSendEvent oe = new OrderSendEvent();
                    oe.Success = false;
                    oe.Trade = trade;
                    onOrderSend(this, oe);
                }
            }
        }
        public void WriteOrder(xlTradeOrder Order)
        {
            xlBook = xlApp.ActiveWorkbook;
            xlSheet = xlBook.Worksheets.get_Item(2);
            Excel.Range last = xlSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
            Excel.Range range = xlSheet.get_Range("A1", last);
            int lastUsedRow = last.Row + 1;
            int lastUsedColumn = last.Column + 1;
            //Debug.WriteLine("Rows used " + lastUsedRow);

            xlSheet.Cells[lastUsedRow, 1] = Order.Contract;
            xlSheet.Cells[lastUsedRow, 2] = (Order.BS == Trade.BuySell.Buy) ? "B" : "S";
            xlSheet.Cells[lastUsedRow, 3] = Order.Volume;
            xlSheet.Cells[lastUsedRow, 4] = Order.Price;
            xlSheet.Cells[lastUsedRow, 5] = Order.Principle;
            xlSheet.Cells[lastUsedRow, 6] = Order.Dealer;
            xlSheet.Cells[lastUsedRow, 9] = Order.Member;
            xlSheet.Cells[lastUsedRow, 10] = Order.Type;
            xlSheet.Cells[lastUsedRow, 11] = Order.Exchange;
            xlSheet.Cells[lastUsedRow, 12] = OrderToString(xlTradeOrder.orderStatus.Ready);

            _lastOrderMatched = false;
        }
 public void InsertNewOrder(xlTradeOrder Order)
 {
     TradeUpdate.Orders.Add(Order);
 }
        private List<xlTradeOrder> ReadAllInputOrders()
        {
            var Orders = new List<xlTradeOrder>();
            xlBook = xlApp.ActiveWorkbook;
            xlSheet = xlBook.Worksheets.get_Item(2);
            Excel.Range last = xlSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
            Excel.Range range = xlSheet.get_Range("A1", last);
            int lastUsedRow = last.Row;
            int lastUsedColumn = last.Column + 1;
            Debug.WriteLine("Rows used " + lastUsedRow);

            for (int x = 2; x <= lastUsedRow; x++)
            {
                var order = new xlTradeOrder();
                order.Contract = xlSheet.Cells[x, 1].Value;
                order.BS = (xlSheet.Cells[x, 2].Value == "B") ? Trade.BuySell.Buy : Trade.BuySell.Sell;
                order.Volume = (int)xlSheet.Cells[x, 3].Value;
                order.Price = (long)xlSheet.Cells[x, 4].Value;
                order.Status = StringToOrder(xlSheet.Cells[x, 12].Value);
                order.Reference = xlSheet.Cells[x, 8].Value;
                Orders.Add(order);

            }

            return Orders;
        }
 private string OrderToString(xlTradeOrder.orderStatus status)
 {
     if (status == xlTradeOrder.orderStatus.Ready) return "Ready";
     if (status == xlTradeOrder.orderStatus.Completed) return "Completed";
     if (status == xlTradeOrder.orderStatus.Cancelled) return "Cancelled";
     return "";
 }
 public bool isLastOrderComplete()
 {
     Connect();
     var Order = ReadLastOrder();
     _lastOrder = Order;
     if (Order.Status == xlTradeOrder.orderStatus.Completed) return true;
     return false;
 }
        private List<xlTradeOrder> GetMatchedOrders(out xlTradeOrder LastMatched)
        {
            Connect();
            List<xlTradeOrder> insert = ReadAllInputOrders();
            List<xlTradeOrder> matched = ReadAllMatchedOrders();
            Disconnect();

            var merged =  from i in insert
                         join m in matched
                         on i.Reference equals m.Reference
                         select m;
                       
            LastMatched = merged.OrderByDescending(z => z.TimeStamp).First();
            return merged.ToList();
        }
        public void SendOrderToMarketMANUALCLOSE(Trade trade)
        {
            double price = trade.BuyorSell == Trade.BuySell.Buy ? HiSat.LivePrice.Offer : HiSat.LivePrice.Bid;

            ExcelLink.xlTradeOrder o = new xlTradeOrder()
            {
                BS = trade.BuyorSell,
                Price = price,
                Volume = trade.TradeVolume,
                Contract = WebSettings.General.OTS_INST,

            };

            try
            {
                e.Connect();
                e.WriteOrder(o);
                e.Disconnect();
                OrderSendEvent oe = new OrderSendEvent();
                oe.Success = true;
                oe.Trade = trade;
                onOrderSend(this, oe);
                e.StartCheckWhenOrderCompletedTimer(10000);
            }
            catch (Exception ex)
            {
                OrderSendEvent oe = new OrderSendEvent();
                oe.Success = false;
                oe.Trade = trade;
                onOrderSend(this, oe);
            }
        }