Exemple #1
0
        public override bool HandleMessage(IMessage message)
        {
            //ForTest:if(UnlTradingData.Margin > 0){ }

            if (base.HandleMessage(message))
            {
                return(true);
            }

            switch (message.APIDataType)
            {
            case EapiDataTypes.AccountSummaryData:
                AccountSummaryData = message as AccountSummaryData;
                return(true);

            case EapiDataTypes.TradingTimeEvent:
                var tradingTimeEvent = (TradingTimeEvent)message;
                try
                {
                    var eventType = tradingTimeEvent.TradingTimeEventType;
                    switch (eventType)
                    {
                    case ETradingTimeEventType.StartTrading:
                        _taskId = UNLManager.AddScheduledTaskOnUnl(TimeSpan.FromSeconds(TRADING_CYCLE_TYME), DoTradingWork, true);
                        break;

                    case ETradingTimeEventType.EndTradingIn30Seconds:
                        break;

                    case ETradingTimeEventType.EndTradingIn60Seconds:
                        break;

                    case ETradingTimeEventType.EndTrading:
                        UNLManager.RemoveScheduledTaskOnUnl(_taskId);
                        break;
                    }
                }
                finally
                {
                    //Propagate by event:
                    OnSendTradingTimeEvent(tradingTimeEvent);
                }

                return(true);

            case EapiDataTypes.BrokerConnectionStatus:
                var connectionStatusMessage = (BrokerConnectionStatusMessage)message;
                if (connectionStatusMessage.AfterConnectionToApiWrapper)
                {
                    DoWorkAfterConnection();
                }
                return(true);

            case EapiDataTypes.TransactionData:
                //Create the adequate UnlOptins object and save it in db:
                CreateOrUpdateUnlOption(message as TransactionData);
                break;
            }
            return(false);
        }
        /// <summary>
        /// Waiting for notifications from the OptionNegotiator and sell ATM option.
        /// </summary>
        /// <param name="orderStatus"></param>
        /// <param name="orderId"></param>
        private void OrdersManager_OrderTradingNegotioationWasTerminated(OrderStatus orderStatus, string orderId)
        {
            if (_shutDownOptimizerReceived)
            {
                _pendingSellDic.Clear();
                _pendingCloseDic.Clear();
            }
            if (_pendingSellDic.ContainsKey(orderId))
            {
                HandleSellOrders(orderStatus, orderId);
                return;
            }

            if (!_pendingCloseDic.ContainsKey(orderId))
            {
                return;
            }

            var orderData = _pendingCloseDic[orderId];

            _pendingCloseDic.Remove(orderId);
            if (orderStatus == OrderStatus.Filled)
            {
                //Sell ATM Position:
                Logger.Info($"Position Closed successfully: {orderData}");
                SellATMPosition(orderData);
            }
            else if (orderStatus == OrderStatus.NegotiationFailed)
            {
                Logger.Info($"Negotiation Failed: {orderData}");
                _notAllPositionClosed = true;
            }
            else
            {
                Logger.Info($"Close position Failed ({orderStatus}): {orderData}");
                _notAllPositionClosed = true;
            }
            //Keep waiting for finish all pending close positions:
            if (_pendingCloseDic.Count != 0)
            {
                return;
            }

            //Unregister to the event:
            UNLManager.OrdersManager.OrderTradingNegotioationWasTerminated -=
                OrdersManager_OrderTradingNegotioationWasTerminated;

            //Resend for buy the positions that still not closed
            if (_notAllPositionClosed)
            {
                UNLManager.AddScheduledTaskOnUnl(TimeSpan.FromMilliseconds(100), () => { OptimizePositions(); });
            }
            else if (_pendingSellDic.Count == 0)
            {
                //Send notification that mission is accomplished:
                OnMissionAccomplished();
            }
        }
 private void AddTaskForUpdateOptionDataOnPosition()
 {
     //Add the repeated task to start in 10 sec, to let the options manager to load them.
     UNLManager.AddScheduledTaskOnUnl(TimeSpan.FromSeconds(10), () =>
     {
         UNLManager.AddScheduledTaskOnUnl(TimeSpan.FromSeconds(1), AddOrUpdateDataToPosition, true);
         Logger.InfoFormat("{0} PDB start repeated task for add or update options to position objects.", Symbol);
     });
 }
        private (List <OptionsPositionData>, bool, bool) PrepareForOptimizationNew()
        {
            //var atmOption = UNLManager.OptionsManager.GetATMOptionData(ExpiryDate, EOptionType.Call);
            //Check for trading time, Don't act if now isn't working time.
            if ((UNLManager.IsNowWorkingTime == false) && (UNLManager.IsSimulatorAccount == false))
            {
                throw new Exception("The operation can't be done now! it can be done only within working time.");
            }

            _notAllPositionClosed = false;

            if (_pendingCloseDic == null)
            {
                _pendingCloseDic = new Dictionary <string, OrderData>();
            }
            else
            {
                _pendingCloseDic.Clear();
            }

            if (_pendingSellDic == null)
            {
                _pendingSellDic = new Dictionary <string, OrderData>();
            }
            else
            {
                _pendingSellDic.Clear();
            }
            List <OptionsPositionData> outLimitList;

            if (!GetOutOfLimitPositions(out outLimitList))
            {
                throw new Exception($"{Symbol}: There is no out of limit Positions!");
            }

            bool doForPutPositions  = UNLManager.OptionsManager.CheckForATMOption(EOptionType.Put, ExpiryDate);
            bool doForCallPositions = UNLManager.OptionsManager.CheckForATMOption(EOptionType.Call, ExpiryDate);

            if (!doForCallPositions && !doForPutPositions)
            {
                throw new Exception("There are no ATM Positions!!");
            }
            //return false;

            //Initialize watchDog: Finish all works immediatly:
            _shutDownTaskId = UNLManager.AddScheduledTaskOnUnl(TimeSpan.FromMinutes(1), ShutDown);

            //Register for done notifications.
            UNLManager.OrdersManager.OrderTradingNegotioationWasTerminated +=
                OrdersManager_OrderTradingNegotioationWasTerminated;
            //var outLimit = (outLimitList: List<OptionsPositionData>, doForPutPositions: bool, doForCallPositions: bool);
            return(outLimitList, doForPutPositions, doForCallPositions);
        }