Beispiel #1
0
        public IEnumerable <(Contract contract, int position, double open, double openPrice, double takeProfit, int orderId)> ComboTradesImpl()
        {
            var positions = Positions.Where(p => p.position != 0).ToArray();
            var orders    = OrderContractsInternal.Where(oc => !oc.isDone).ToArray();
            var combos    = (
                from c in positions /*.ParseCombos(orders)*/.Do(c => IbClient.SetContractSubscription(c.contract))
                let order = orders.Where(oc => oc.isSubmitted && oc.contract.Key == c.contract.Key).Select(oc => (oc.order.OrderId, oc.order.LmtPrice)).FirstOrDefault()
                            select(c.contract, c.position, c.open, c.open / c.position.Abs() / c.contract.ComboMultiplier, order.LmtPrice, order.OrderId)
                );
            var comboAll = ComboTradesAllImpl().ToArray();

            return(combos.Concat(comboAll).Distinct(c => c.contract.Instrument));
        }
 public PendingOrder OpenTrade(Contract contract, string type, int quantity, double price, double profit, bool useTakeProfit, DateTime goodTillDate, int minTickMultiplier = 1, [CallerMemberName] string Caller = "") {
   var timeoutInMilliseconds = 5000;
   if(!Monitor.TryEnter(_OpenTradeSync, timeoutInMilliseconds)) {
     var message = new { contract, quantity, Method = nameof(OpenTrade), Caller, timeoutInMilliseconds } + "";
     Trace(new TimeoutException(message));
     return null;
   }
   var aos = OrderContractsInternal
     .Where(oc => !oc.isDone && oc.contract.Key == contract.Key && oc.order.TotalPosition().Sign() == quantity.Sign())
     .ToArray();
   if(aos.Any()) {
     aos.ForEach(ao => {
       Trace($"OpenTrade: {contract} already has active order {ao.order.OrderId} with status: {ao.status}.\nUpdating {new { price }}");
       UpdateOrder(ao.order.OrderId, OrderPrice(price, contract, minTickMultiplier));
     });
     ExitMomitor();
     return null;
   }
   var orderType = price == 0 ? "MKT" : type.IfEmpty("LMT");
   bool isPreRTH = orderType == "LMT";
   var order = OrderFactory(contract, quantity, price, goodTillDate, minTickMultiplier, orderType, isPreRTH);
   //if(!contract.IsCombo && !contract.IsFutureOption)
   //  FillAdaptiveParams(order, "Normal");
   var tpOrder = (useTakeProfit ? MakeTakeProfitOrder(order, contract, profit, minTickMultiplier) : new(IBApi.Order order, double price)[0].ToObservable()).Select(x => new { x.order, x.price, useTakeProfit = false });