Exemplo n.º 1
0
        /// <summary>
        /// Initializes necessary fields and parameters
        /// </summary>
        /// <param name="strategyType">User Strategy Type</param>
        /// <param name="ctorArguments">Constructor arguments to initialize strategy</param>
        private void Initialize(Type strategyType, object[] ctorArguments)
        {
            _manualReset = new ManualResetEvent(false);
            _logger      = new AsyncClassLogger("StrategyExecutorGeneticAlgo");
            _logger.SetLoggingLevel();

            // Save Strategy Type
            _strategyType = strategyType;

            //Save Arguments
            _ctorArguments = ctorArguments;

            // Set Logging levels
            _logger.SetLoggingLevel();

            // Get new strategy instance
            var strategyInstance = StrategyHelper.CreateStrategyInstance(_strategyType, _ctorArguments);

            if (strategyInstance != null)
            {
                // Cast to TradeHubStrategy Instance
                _tradeHubStrategy = strategyInstance as TradeHubStrategy;

                InitializeStrategyListeners();
                OverrideMarketRequestCalls();
                OverrideOrderRequestCalls();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Starts custom strategy execution
        /// </summary>
        public void ExecuteStrategy()
        {
            try
            {
                // Verify Strategy Instance
                if (_tradeHubStrategy == null)
                {
                    //create DB strategy
                    Strategy strategy = new Strategy();
                    strategy.Name          = _strategyType.Name;
                    strategy.StartDateTime = DateTime.Now;

                    // Get new strategy instance
                    var strategyInstance = LoadCustomStrategy.CreateStrategyInstance(_strategyType, CtorArguments);

                    if (strategyInstance != null)
                    {
                        // Cast to TradeHubStrategy Instance
                        _tradeHubStrategy = strategyInstance as TradeHubStrategy;
                    }

                    if (_tradeHubStrategy == null)
                    {
                        if (_asyncClassLogger.IsInfoEnabled)
                        {
                            _asyncClassLogger.Info("Unable to initialize Custom Strategy: " + _strategyType.FullName, _type.FullName, "ExecuteStrategy");
                        }

                        // Skip execution of further actions
                        return;
                    }

                    // Set Strategy Name
                    _tradeHubStrategy.StrategyName = LoadCustomStrategy.GetCustomClassSummary(_strategyType);

                    // Register Events
                    _tradeHubStrategy.OnStrategyStatusChanged += OnStrategyStatusChanged;
                    _tradeHubStrategy.OnNewExecutionReceived  += OnNewExecutionReceived;
                }

                if (_asyncClassLogger.IsInfoEnabled)
                {
                    _asyncClassLogger.Info("Executing user strategy: " + _strategyType.FullName, _type.FullName, "ExecuteStrategy");
                }

                //Overriding if running on simulated exchange
                ManageBackTestingStrategy();

                // Start Executing the strategy
                _tradeHubStrategy.Run();
            }
            catch (Exception exception)
            {
                _asyncClassLogger.Error(exception, _type.FullName, "ExecuteStrategy");
            }
        }
        public void StartUp()
        {
            //_applicationController = ContextRegistry.GetContext()["ApplicationController"] as ApplicationController;
            //if (_applicationController != null) _applicationController.StartServer();

            _tradeHubStrategy = new EmaStrategy(1, 2, "LAST", "ERX", 2, "TIME", "LAST",
                                                Common.Core.Constants.MarketDataProvider.Simulated,
                                                Common.Core.Constants.OrderExecutionProvider.Simulated);
            _stopwatch = new Stopwatch();
        }
Exemplo n.º 4
0
 /// <summary>
 /// Disposes strategy objects
 /// </summary>
 public void Close()
 {
     try
     {
         if (_tradeHubStrategy != null)
         {
             _dataHandler.Shutdown();
             _tradeHubStrategy.Dispose();
             _tradeHubStrategy = null;
         }
     }
     catch (Exception exception)
     {
         _asyncClassLogger.Error(exception, _type.FullName, "Close");
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Argument Constructor
        /// </summary>
        /// <param name="strategyKey">Unique Key to Identify the Strategy Instance</param>
        /// <param name="strategyType">C# Class Type which implements TradeHubStrategy.cs</param>
        /// <param name="ctorArguments">Holds selected ctor arguments to execute strategy</param>
        public StrategyExecutor(string strategyKey, Type strategyType, object[] ctorArguments)
        {
            //_asyncClassLogger = ContextRegistry.GetContext()["StrategyRunnerLogger"] as AsyncClassLogger;
            _asyncClassLogger = new AsyncClassLogger("StrategyExecutor");
            {
                _asyncClassLogger.SetLoggingLevel();
                //set logging path
                string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) +
                              "\\TradeHub Logs\\Client";
                _asyncClassLogger.LogDirectory(path);
            }

            _tradeHubStrategy = null;
            _strategyKey      = strategyKey;
            _strategyType     = strategyType;
            _ctorArguments    = ctorArguments;
            _strategyStatus   = Infrastructure.Constants.StrategyStatus.None;

            // Initialize statistics
            _statistics = new Statistics(_strategyKey);

            // Initialze Utility Classes
            //_orderExecutor = new OrderExecutor(_asyncClassLogger);
            _orderExecutor        = new OrderExecutorGeneric();
            _marketDataListener   = new MarketDataListener(_asyncClassLogger);
            _orderRequestListener = new OrderRequestListener(_orderExecutor, _asyncClassLogger);

            // Use MarketDataListener.cs as Event Handler to get data from DataHandler.cs
            _dataHandler =
                new DataHandler(
                    new IEventHandler <TradeHub.Common.HistoricalDataProvider.ValueObjects.MarketDataObject>[]
                    { _marketDataListener });

            _marketDataListener.BarSubscriptionList  = _dataHandler.BarSubscriptionList;
            _marketDataListener.TickSubscriptionList = _dataHandler.TickSubscriptionList;

            // Initialize MarketRequestListener.cs to manage incoming market data requests from strategy
            _marketRequestListener = new MarketRequestListener(_dataHandler, _asyncClassLogger);

            //Register OrderExecutor Events
            RegisterOrderExecutorEvents();

            //Register Market Data Listener Events
            RegisterMarketDataListenerEvents();
        }
Exemplo n.º 6
0
        /// <summary>
        /// Argument Constructor
        /// </summary>
        /// <param name="strategyInstance">Holds necessary information for Instance Execution and UI-Update</param>
        /// <param name="currentDispatcher"></param>
        public StrategyExecutor(StrategyInstance strategyInstance, Dispatcher currentDispatcher)
        {
            this._currentDispatcher = currentDispatcher;

            _asyncClassLogger = new AsyncClassLogger("StrategyExecutor");

            //set logging path
            string path = DirectoryStructure.CLIENT_LOGS_LOCATION;

            _asyncClassLogger.SetLoggingLevel();
            _asyncClassLogger.LogDirectory(path);

            _tradeHubStrategy = null;
            _strategyInstance = strategyInstance;
            _strategyKey      = _strategyInstance.InstanceKey;
            _strategyType     = _strategyInstance.StrategyType;
            _ctorArguments    = _strategyInstance.GetParameterValues();
        }
Exemplo n.º 7
0
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    Close();
                }

                // Release unmanaged resources.
                _asyncClassLogger      = null;
                _marketDataListener    = null;
                _marketRequestListener = null;
                _dataHandler           = null;
                _tradeHubStrategy      = null;
                _currentDispatcher     = null;
                _disposed = true;
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Starts custom strategy execution
        /// </summary>
        public void ExecuteStrategy()
        {
            try
            {
                bool parameterChanged = true;

                // Check if any strategy parameter was changed
                if (_ctorArguments != null)
                {
                    parameterChanged = _strategyInstance.ParametersChanged(_ctorArguments);

                    // Get updated values to be used
                    if (parameterChanged)
                    {
                        // Get parameter values to be used
                        _ctorArguments = _strategyInstance.GetParameterValues();
                    }
                }

                // Verify Strategy Instance
                if (_tradeHubStrategy == null || parameterChanged)
                {
                    //create DB strategy
                    Strategy strategy = new Strategy();
                    strategy.Name          = _strategyType.Name;
                    strategy.StartDateTime = DateTime.Now;

                    // Get parameter values to be used
                    _ctorArguments = _strategyInstance.GetParameterValues();

                    // Get new strategy instance
                    var strategyInstance = StrategyHelper.CreateStrategyInstance(_strategyType, _ctorArguments);

                    if (strategyInstance != null)
                    {
                        // Cast to TradeHubStrategy Instance
                        _tradeHubStrategy = strategyInstance as TradeHubStrategy;
                    }

                    if (_tradeHubStrategy == null)
                    {
                        if (_asyncClassLogger.IsInfoEnabled)
                        {
                            _asyncClassLogger.Info("Unable to initialize Custom Strategy: " + _strategyType.FullName, _type.FullName, "ExecuteStrategy");
                        }

                        // Skip execution of further actions
                        return;
                    }

                    // Set Strategy Name
                    _tradeHubStrategy.StrategyName = StrategyHelper.GetCustomClassSummary(_strategyType);

                    // Set notificaitons preference
                    _tradeHubStrategy.SetNewOrderNotification(_newOrderNotification);
                    _tradeHubStrategy.SetAcceptedOrderNotification(_acceptedOrderNotification);
                    _tradeHubStrategy.SetExecutionNotification(_executionNotification);
                    _tradeHubStrategy.SetRejectionNotification(_rejectionNotification);

                    // Register Events
                    RegisterTradeHubStrategyEvent();
                }

                if (_asyncClassLogger.IsInfoEnabled)
                {
                    _asyncClassLogger.Info("Executing user strategy: " + _strategyType.FullName, _type.FullName, "ExecuteStrategy");
                }

                //Overriding if running on simulated exchange
                ManageBackTestingStrategy();

                _stopInstanceRequested = false;

                // Start Executing the strategy
                _tradeHubStrategy.Run();
            }
            catch (Exception exception)
            {
                _asyncClassLogger.Error(exception, _type.FullName, "ExecuteStrategy");
            }
        }