Пример #1
0
 public static void SafeStop(this PrimaryStrategy strategy)
 {
     if (strategy?.ProcessState == ProcessStates.Started)
     {
         strategy.PrimaryStopping();
     }
 }
        private void AssignCommonStrategyRules(string name, PrimaryStrategy strategy)
        {
            strategy.WhenStarted()
            .Do(() =>
            {
                _messageManager.ProceedAnswer("");
                _messageManager.ProceedAnswer($"{strategy} strategy started (key {name}).");
            })
            .Apply(strategy);

            strategy.WhenStopping()
            .Do(() =>
            {
                _messageManager.ProceedAnswer("");
                _messageManager.ProceedAnswer($"{name} strategy STOPPING, pos: {strategy.Position}");
            })
            .Apply(strategy);

            strategy.PrimaryStrategyStopped += () =>
            {
                {
                    _dataManager.MappedStrategies.Remove(name);
                    _messageManager.ProceedAnswer("");
                    _messageManager.ProceedAnswer($"{name} strategy STOPPED and removed from collection");
                }
            };
        }
 private void TryToStopStrategy(string name, PrimaryStrategy strategy)
 {
     if (strategy.ProcessState == ProcessStates.Started)
     {
         strategy.PrimaryStopping();
     }
     else
     {
         _messageManager.ProceedAnswer($"such a strategy cannot be stopped: {name} {strategy.ProcessState}", ConsoleColor.Red, false);
     }
 }
Пример #4
0
        public OrderSynchronizer(PrimaryStrategy strategy)
        {
            Timeout = 1000;

            _currentOrder       = null;
            IsAnyOrdersInWork   = false;
            _isOrderRegistering = false;
            _isOrderCanceling   = false;
            _strategy           = strategy;

            _strategy.WhenStopping()
            .Or(_strategy.WhenStopped())
            .Do(s =>
            {
                _strategy.AddWarningLog($"trying to stop {_strategy.Name}: {_strategy.ProcessState}");
                _eventWaiter.Set();
            })
            .Apply(_strategy);
        }
        public PrimaryStrategy CreateStrategyFromString(StrategyTypes strategyType, string inputParams)
        {
            PrimaryStrategy resultStrategy         = null;
            Security        resultStrategySecurity = null;
            var             strategyParams         = inputParams.Split(SeparatorAfterVal.ToCharArray(), StringSplitOptions.None);

            if (_connector == null)
            {
                throw new NullReferenceException("_connector");
            }

            switch (strategyType)
            {
            case StrategyTypes.Dhs:
                var dhs1FutCode     = strategyParams[0];
                var dhs2OptCode     = strategyParams[1];
                var dhs3DeltaStep   = strategyParams[2];
                var dhs4DeltaBuffer = strategyParams[3];
                var dhs5HedgeLevels = strategyParams[4];
                var dhs6MinFPos     = strategyParams[5];
                var dhs7MaxFPos     = strategyParams[6];

                var tempOptList = ParseToStringArr(dhs2OptCode)
                                  .Select(s => _dataManager.LookupThroughExistingSecurities(s));

                decimal futPosition = _connector.GetSecurityPosition(_defaultPortfolio,
                                                                     _dataManager.LookupThroughExistingSecurities(dhs1FutCode));
                SynchronizedDictionary <Security, decimal> optionsPositions =
                    _connector.GetSecuritiesPositions(_defaultPortfolio, tempOptList.ToList());

                var dhsStrategy = new DeltaHedgerStrategy(futPosition, optionsPositions);

                if (!CheckIfDefault(dhs3DeltaStep))
                {
                    dhsStrategy.DeltaStep = ParseDecimalValue(dhs3DeltaStep);
                }
                if (!CheckIfDefault(dhs4DeltaBuffer))
                {
                    dhsStrategy.DeltaBuffer = ParseDecimalValue(dhs4DeltaBuffer);
                }
                if (!CheckIfDefault(dhs5HedgeLevels))
                {
                    ParseToStringArr(dhs5HedgeLevels).ForEach(s =>
                    {
                        if (s.ToCharArray()[0] == 'u')
                        {
                            dhsStrategy.AddHedgeLevel(PriceDirection.Up, ParseDecimalValue(s.Substring(1)));
                        }
                        else if (s.ToCharArray()[0] == 'd')
                        {
                            dhsStrategy.AddHedgeLevel(PriceDirection.Down, ParseDecimalValue(s.Substring(1)));
                        }
                        else
                        {
                            throw new ArgumentException("cannot parse such a value into a price level, possible directions are 'u' or 'd'.");
                        }
                    });
                }
                if (!CheckIfDefault(dhs6MinFPos))
                {
                    dhsStrategy.MinFuturesPositionVal = ParseDecimalValue(dhs6MinFPos);
                }
                if (!CheckIfDefault(dhs7MaxFPos))
                {
                    dhsStrategy.MaxFuturesPositionVal = ParseDecimalValue(dhs7MaxFPos);
                }

                resultStrategySecurity = _dataManager.LookupThroughExistingSecurities(dhs1FutCode);
                resultStrategy         = dhsStrategy;
                break;

            case StrategyTypes.Lqs:
                var lqs1SecCode      = strategyParams[0];
                var lqs2Side         = strategyParams[1];
                var lqs3Volume       = strategyParams[2];
                var lqs4PriceShift   = strategyParams[3];
                var lqs5WorstPrice   = strategyParams[4];
                var lqs6AlwaysPlaces = strategyParams[5];

                Sides lSide;
                var   worstPrice = 0M;

                if (!Enum.TryParse(lqs2Side, true, out lSide))
                {
                    throw new ArgumentException("cannot parse side value (enum exception)");
                }

                if (!CheckIfDefault(lqs5WorstPrice))
                {
                    worstPrice = ParseDecimalValue(lqs5WorstPrice);
                }

                var lqsStrategy = new LimitQuoterStrategy(
                    lSide,
                    ParseDecimalValue(lqs3Volume),
                    ParseDecimalValue(lqs4PriceShift),
                    worstPrice);

                if (!CheckIfDefault(lqs6AlwaysPlaces))
                {
                    lqsStrategy.IsLimitOrdersAlwaysRepresent = ParseBoolValue(lqs6AlwaysPlaces);
                }

                resultStrategySecurity = _dataManager.LookupThroughExistingSecurities(lqs1SecCode);
                resultStrategy         = lqsStrategy;

                break;

            case StrategyTypes.Mqs:
                var mqs1SecCode     = strategyParams[0];
                var mqs2Side        = strategyParams[1];
                var mqs3Volume      = strategyParams[2];
                var mqs4TargetPrice = strategyParams[3];

                Sides mSide;
                if (!Enum.TryParse(mqs2Side, true, out mSide))
                {
                    throw new ArgumentException("cannot parse side value (enum exception)");
                }

                var mqsStrategy = new MarketQuoterStrategy(
                    mSide,
                    ParseDecimalValue(mqs3Volume),
                    ParseDecimalValue(mqs4TargetPrice));

                resultStrategySecurity = _dataManager.LookupThroughExistingSecurities(mqs1SecCode);
                resultStrategy         = mqsStrategy;

                break;

            case StrategyTypes.Pcs:
                var pcs1SecCode       = strategyParams[0];
                var pcs2ClosePrice    = strategyParams[1];
                var pcs3SignalSecCode = strategyParams[2];
                var pcs4SecDirection  = strategyParams[3];
                var pcs5PosToClose    = strategyParams[4];

                PriceDirection pDirection;
                if (!Enum.TryParse(pcs4SecDirection, true, out pDirection))
                {
                    throw new ArgumentException("cannot parse price direction value (enum exception)");
                }

                var pcsStrategy = new PositionCloserStrategy(
                    ParseDecimalValue(pcs2ClosePrice),
                    pDirection,
                    ParseDecimalValue(pcs5PosToClose));

                if (!CheckIfDefault(pcs3SignalSecCode))
                {
                    pcsStrategy.SecurityWithSignalToClose =
                        _dataManager.LookupThroughExistingSecurities(pcs3SignalSecCode);
                }

                resultStrategySecurity = _dataManager.LookupThroughExistingSecurities(pcs1SecCode);
                resultStrategy         = pcsStrategy;

                break;

            case StrategyTypes.Sss:
                var sss1SecCode          = strategyParams[0];
                var sss2CurPosition      = strategyParams[1];
                var sss3CurPositionPrice = strategyParams[2];
                var sss4Spread           = strategyParams[3];
                var sss5Lot                  = strategyParams[4];
                var sss6EnterSide            = strategyParams[5];
                var sss7LimitedFuturesNumber = strategyParams[6];

                Sides sSide;
                if (!Enum.TryParse(sss6EnterSide, true, out sSide))
                {
                    throw new ArgumentException("cannot parse side value (enum exception)");
                }

                var sssStrategy = new SpreaderStrategy(
                    ParseDecimalValue(sss2CurPosition),
                    ParseDecimalValue(sss3CurPositionPrice),
                    ParseDecimalValue(sss4Spread),
                    ParseDecimalValue(sss5Lot),
                    sSide);

                if (!CheckIfDefault(sss7LimitedFuturesNumber))
                {
                    sssStrategy.LimitedFuturesValueAbs = ParseDecimalValue(sss7LimitedFuturesNumber);
                }

                resultStrategySecurity = _dataManager.LookupThroughExistingSecurities(sss1SecCode);
                resultStrategy         = sssStrategy;

                break;
            }

            resultStrategy?.SetStrategyEntitiesForWork(_connector, resultStrategySecurity, _defaultPortfolio);

            return(resultStrategy);
        }