Example #1
0
 public static DualPosition GetPosition(this InstrumentStrategy strategy)
 {
     if (strategy.OrderManager.Server is DatabaseOrderServer server)
     {
         return(server.PositionStore.GetPosition(strategy.Id, strategy.Instrument.Id));
     }
     return(null);
 }
Example #2
0
 public static void SetTradingRange(this InstrumentStrategy strategy)
 {
     if (strategy.IsInstance)
     {
         var range = TradingCalendar.Instance.GetTimeRange(strategy.Instrument, strategy.Clock.DateTime);
         strategy.Instrument.SetTimeRange(range);
         SetRangeUpdateReminder(range, strategy);
     }
     else
     {
         SetTradingRange((Strategy)strategy);
     }
 }
Example #3
0
        public void RemoveStrategies(string instrumentId)
        {
            InstrumentStrategy instrumentStrategy = GetInstrumentStrategy(instrumentId);

            if (instrumentStrategy == null)
            {
                return;
            }

            instrumentStrategies.Remove(instrumentStrategy);
            dictStrategies.Remove(instrumentId);
            strategyRepo.Delete(instrumentStrategy);
        }
Example #4
0
        public void InitStrategies(Instrument instrument)
        {
            var instrumentStrategy = strategyRepo.FirstOrDefault(strat => strat.InstrumentID == instrument.InstrumentID);

            if (instrumentStrategy == null)
            {
                instrumentStrategy = new InstrumentStrategy();

                instrumentStrategy.InstrumentID     = instrument.InstrumentID;
                instrumentStrategy.InstrumentName   = instrument.InstrumentName;
                instrumentStrategy.VolumeMultiple   = instrument.VolumeMultiple;
                instrumentStrategy.PriceTick        = instrument.PriceTick;
                instrumentStrategy.LongMarginRatio  = instrument.LongMarginRatio;
                instrumentStrategy.ShortMarginRatio = instrument.ShortMarginRatio;

                //instrumentStrategy.Strategies.Add(Container.Resolve<BollStrategy>());

                //instrumentStrategy.Strategies.Add(Container.Resolve<RoundMAStrategy>());

//                instrumentStrategy.StopLosses.Add(Container.Resolve<PriceStopLoss>());
//
//                instrumentStrategy.StopProfits.Add(Container.Resolve<PriceStopProfit>());

                //Strategies.Add(new AboveMAStrategy(indicatorManager, 20));
                //Strategies.Add(new BelowMAStrategy(indicatorManager, 20));

                strategyRepo.Add(instrumentStrategy);
            }

            instrumentStrategy.Configure(Container);
            instrumentStrategy.BindEvent();


            instrumentStrategies.Add(instrumentStrategy);

            dictStrategies.Add(instrument.InstrumentID, instrumentStrategy);
        }
Example #5
0
 public static OrderList CloseShort(this InstrumentStrategy strategy, double qty, double price)
 {
     return(CloseShort(strategy, strategy.Instrument, qty, price));
 }
Example #6
0
 public static OrderList CloseShort(this InstrumentStrategy strategy, double qty,
                                    OrderPriceAdjustMethod method = OrderPriceAdjustMethod.MatchPrice)
 {
     return(CloseShort(strategy, strategy.Instrument, qty, method));
 }
Example #7
0
 public static Order OpenShort(this InstrumentStrategy strategy, double qty, double price)
 {
     return(OpenShort(strategy, strategy.Instrument, qty, price));
 }
Example #8
0
 public static Order OpenLong(this InstrumentStrategy strategy, double qty, OrderPriceAdjustMethod method = OrderPriceAdjustMethod.MatchPrice)
 {
     return(OpenLong(strategy, strategy.Instrument, qty, method));
 }
Example #9
0
 public static DateTime GetNextMarketCloseTime(this InstrumentStrategy strategy)
 {
     return(GetNextMarketCloseTime(strategy, strategy.Instrument));
 }
Example #10
0
        public void PrcessData(MarketData marketData)
        {
            if (!isStart)
            {
                return;
            }

            InstrumentStrategy instrumentStrategy = dictStrategies[marketData.InstrumentId];

            if (!instrumentStrategy.StartTrade)
            {
                return;
            }

            foreach (UserStrategy strategy in instrumentStrategy.Strategies)
            {
                if (!strategy.AutoTrade)
                {
                    continue;
                }

                if (needWait)
                {
                    tick++;
                    if (tick >= 240)
                    {
                        needWait = false;
                        tick     = 0;
                    }
                    return;
                }

                var sc = (StringCollection)InstrumentType.Default[marketData.Code];

                var today = DateTime.Today;

                if (sc != null)
                {
                    foreach (var time in sc)
                    {
                        int hour   = Convert.ToInt32(time.Split(':')[0]);
                        int minute = Convert.ToInt32(time.Split(':')[1]);



                        var endTime = new DateTime(today.Year, today.Month, today.Day, hour, minute, 0);

                        if (endTime > DateTime.Now && DateTime.Now > endTime.AddMinutes(-1))
                        {
                            OrderManager.CancelAllOrder();
                            needWait = true;
                            log.Info("canceling order");
                            return;
                        }
                    }
                }


                List <Order> orders = strategy.Match(marketData);

                foreach (Order order in orders)
                {
                    order.Unit = instrumentStrategy.VolumeMultiple;

                    if (order.Direction == TThostFtdcDirectionType.Buy)
                    {
                        order.UseMargin = order.Price * order.Unit * order.Volume * instrumentStrategy.LongMarginRatio;
                    }
                    else
                    {
                        order.UseMargin = order.Price * order.Unit * order.Volume * instrumentStrategy.ShortMarginRatio;
                    }

                    log.Info(order);
                    OrderManager.OrderInsert(order);
                }
            }
        }