Ejemplo n.º 1
0
 private void StrategyShortPut() => UseAccountManager(am => {
   var puts = OpenPuts().ToList();
   var distanceOk = (from curPut in CurrentPut
                     from openPut in puts.OrderBy(p => p.contract.Strike).Take(1).ToList()
                     let strikeAvg = curPut.strikeAvg
                     let openPutPrice = openPut.price.Abs()
                     let openPutStrike = openPut.contract.Strike
                     where curPut.option.LastTradeDateOrContractMonth == openPut.contract.LastTradeDateOrContractMonth
                     && strikeAvg + openPutPrice > openPutStrike
                     select true
                     ).IsEmpty();
   var hasOptions = puts.Count +
     am.UseOrderContracts(OrderContracts =>
     (from put in CurrentPut
      join oc in OrderContracts.Where(o => !o.isDone & o.order.Action == "SELL") on put.instrument equals oc.contract.Instrument
      select true
      )).Concat().Count();
   var hasSellOrdes = am.UseOrderContracts(OrderContracts =>
   (from oc in OrderContracts.Where(o => !o.isDone && o.contract.IsPut && !o.contract.IsCombo && o.order.Action == "SELL")
    select true
    )).Concat().Count();
   if(distanceOk && hasOptions < TradeCountMax) {
     TradeConditionsEval()
       .DistinctUntilChanged(td => td)
       .Where(td => td.HasUp())
       .Take(1)
       .ForEach(_ => {
         var pos = -puts.Select(p => p.position.Abs()).DefaultIfEmpty(TradingRatio.ToInt()).Max();
         CurrentPut?.ForEach(p => {
           Log = new Exception($"{nameof(TradeConditionsTrigger)}:{nameof(am.OpenTrade)}:{new { p.option, pos, Thread.CurrentThread.ManagedThreadId }}");
           am.OpenTrade(p.option, pos, p.ask, 0.2, true, ServerTime.AddMinutes(5));
         });
       });
   }
 });
Ejemplo n.º 2
0
 void ScanRatesLengthByM1Wave(Func <TradingMacro, WaveRange> wave)
 {
     if (BarPeriod != BarsPeriodType.t1)
     {
         throw new Exception("ScanRatesLengthByM1Wave is only supported for BarsPeriodType." + BarsPeriodType.t1);
     }
     TradingMacroM1(wave)
     .Select(wr => ServerTime.AddMinutes(-wr.TotalMinutes))
     .SelectMany(date => UseRatesInternal(rates => rates.SkipWhile(r => r.StartDate < date).Count()))
     .ForEach(count => BarsCountCalc = count.Max(BarsCount));
 }
Ejemplo n.º 3
0
 private void StrategyLong() => UseAccountManager(am => {
   var hasOrders = am.UseOrderContracts(ocs => ocs.Where(o => o.contract.Instrument == Pair && !o.isDone)).Concat().Any();
   if(Trades.IsEmpty() && !hasOrders) {
     TradeConditionsEval()
       .DistinctUntilChanged(td => td)
       .Where(td => td.HasUp())
       .Take(1)
       .ForEach(_ => {
         var pos = TradingRatio.ToInt();
         Log = new Exception($"{nameof(StrategyLong)}:{nameof(am.OpenTrade)}:{new { Pair }}");
         var p = CurrentPrice.Bid;
         am.OpenTrade(Pair, pos, p, CalculateTakeProfit(), true, ServerTime.AddMinutes(10));
       });
   }
 });
Ejemplo n.º 4
0
 private void StrategyShortStraddle() => UseAccountManager(am => {
   var straddles = OpenStraddles(am);
   var hasOrders = am.UseOrderContracts(ocs => ocs.Where(o => !o.isDone)).Concat().Any();
   if(straddles.IsEmpty() && !hasOrders) {
     TradeConditionsEval()
       .DistinctUntilChanged(td => td)
       .Where(td => td.HasUp())
       .Take(1)
       .ForEach(_ => {
         var pos = -TradingRatio.ToInt();
         CurrentStraddle?.ForEach(p => {
           Log = new Exception($"{nameof(StrategyShortStraddle)}:{nameof(am.OpenTrade)}:{new { p.combo.contract, pos, Thread.CurrentThread.ManagedThreadId }}");
           am.OpenTrade(p.combo.contract, pos, p.ask, 0.2, true, ServerTime.AddMinutes(10));
         });
       });
   }
 });
Ejemplo n.º 5
0
 void ScanRatesLengthByM1WaveAvg(bool doUseTotalMinutes, Func <TradingMacro, IEnumerable <WaveRange> > getWr)
 {
     if (BarPeriod != BarsPeriodType.t1)
     {
         throw new Exception("ScanRatesLengthByM1Wave is only supported for BarsPeriodType." + BarsPeriodType.t1);
     }
     try {
         (from tm in TradingMacroOther()
          let wr = getWr(tm)
                   where wr != null
                   let distMin = InPoints(wr.Where(w => w != null).Average(w => w.Distance))
                                 let dateMin = wr.Select(w => w.TotalMinutes).OrderByDescending(m => m).Take(1).Select(m => ServerTime.AddMinutes(-m))
                                               from dates in tm.UseRatesInternal(rates => rates.BackwardsIterator()
                                                                                 .Distances(_priceAvg).SkipWhile(t => t.Item2 < distMin)
                                                                                 .Select(t => t.Item1.StartDate)
                                                                                 .Take(1)
                                                                                 .Concat(dateMin.Where(_ => doUseTotalMinutes))
                                                                                 .Concat(tm.WaveRanges.Take(1).Select(wr => wr.StartDate))
                                                                                 .OrderBy(d => d)
                                                                                 .Take(1))
                                               from date in dates
                                               from counts in UseRatesInternal(rates => rates.FuzzyIndex(date, (d, r1, r2) => d.Between(r1.StartDate, r2.StartDate)))
                                               from count in counts
                                               select RatesInternal.Count - count
         ).ForEach(count => BarsCountCalc = count.Max(BarsCount));
     } catch (Exception exc) {
         Log = exc;
     }
 }