CurrentBullPuts(string symbol, double strikeLevel, int expirationDaysSkip, int count, int gap) { return ( from cd in IbClient.ReqContractDetailsCached(symbol) from price in IbClient.ReqPriceSafe(cd.Summary, 5, false).Select(p => p.ask.Avg(p.bid)) from combo in MakeBullPuts(symbol, strikeLevel.IfNaN(price), expirationDaysSkip, 1, count, gap) from p in IbClient.ReqPriceSafe(combo.contract, 2, true).DefaultIfEmpty() let strikeAvg = combo.options.Average(o => o.Strike) select ( instrument: combo.contract.Instrument, p.bid, p.ask, p.time,//.ToString("HH:mm:ss"), delta: p.ask.Avg(p.bid) - combo.options.Sum(o => o.IntrinsicValue(price)), strikeAvg, price, breakEven: (up: strikeAvg + price, dn: strikeAvg - price), combo )).ToArray() .Select(b => b .OrderByDescending(t => t.delta) //.Select((t, i) => (t, i)) //.OrderBy(t => t.i > 1) //.ThenBy(t => t.t.ask.Avg(t.t.bid) / t.t.delta) //.ThenByDescending(t => t.t.delta) //.Select(t => t.t) .ToArray() ); }
public COMBO_TRADES ComboTrades(double priceTimeoutInSeconds) { var combos = ( from c in ComboTradesImpl().ToObservable() from underPrice in UnderPrice(c.contract).DefaultIfEmpty() from price in IbClient.ReqPriceSafe(c.contract, priceTimeoutInSeconds, true).DefaultIfEmpty().Take(1) let multiplier = c.contract.ComboMultiplier let closePrice = (c.position > 0 ? price.bid : price.ask) let close = (closePrice * c.position * multiplier).Round(4) let openPrice = c.open / c.position.Abs() / multiplier let isOk = openPrice == c.openPrice ? true : throw new Exception(new { calc = new { openPrice }, c.openPrice } +"") let pmc = Account.ExcessLiquidity / (multiplier * c.position.Abs()) select( c: IbClient.SetContractSubscription(c.contract) , c.position , c.open , close , pl: close - c.open , underPrice , strikeAvg: c.contract.ComboStrike() , openPrice , closePrice , price: (price.bid, price.ask) , c.takeProfit , profit: (c.takeProfit * c.position * multiplier - c.open).Round(2) , pmc , c.orderId ) ); return(combos .ToArray() .SelectMany(cmbs => cmbs .OrderBy(c => c.c.Legs().Count()) .ThenBy(c => c.c.IsOption) .ThenByDescending(c => c.strikeAvg - c.underPrice) .ThenByDescending(c => c.c.Instrument) )); IObservable <double> UnderPrice(Contract contract) { if (!contract.IsOption && !contract.IsCombo) { return(Observable.Return(0.0)); } var underSymbol = contract.Symbol + (contract.HasFutureOption ? "U8" : ""); return( from symbol in IbClient.ReqContractDetailsCached(underSymbol) from underPrice in IbClient.ReqPriceSafe(symbol.Summary, priceTimeoutInSeconds, false) select underPrice.ask.Avg(underPrice.bid)); } }
CurrentStraddles(string symbol, double strikeLevel, int expirationDaysSkip, int count, int gap) { return(( from cd in IbClient.ReqContractDetailsCached(symbol) from price in IbClient.ReqPriceSafe(cd.Summary, 5, false).Select(p => p.ask.Avg(p.bid)) from combo in MakeStraddles(symbol, strikeLevel.IfNaN(price), expirationDaysSkip, 1, count, gap) from p in IbClient.ReqPriceSafe(combo.contract, 2, true).DefaultIfEmpty() select CurrentComboInfo(price, combo, p)).ToArray() .Select(b => b .OrderBy(t => t.ask.Avg(t.bid)) .Select((t, i) => ((t, i))) .OrderBy(t => t.i > 1) .ThenBy(t => t.t.ask.Avg(t.t.bid) / t.t.delta) .ThenByDescending(t => t.t.delta) .Select(t => t.t) .ToArray() )); }
public void OpenLimitOrder(Contract contract, int quantity, double profit, bool useMarketPrice, bool useTakeProfit, int minTickMultiplier = 1, [CallerMemberName] string Caller = "") { double ask((double ask, double bid, DateTime time) p) => useMarketPrice ? p.ask : p.bid; double bid(double a, double b) => useMarketPrice ? b : a; IbClient.ReqPriceSafe(contract, 1, true).Select(p => quantity > 0 ? ask(p) : bid(p.ask, p.bid)) .Subscribe(price => OpenTrade(contract, "", quantity, price, profit, useTakeProfit, DateTime.MaxValue, minTickMultiplier, Caller)); }