Пример #1
0
        /// <summary>Handle the hello message</summary>
        private void HandleMsg(OutMsg.RequestInstrumentStop msg)
        {
            // Remove the transmitter for the requested instrument
            Transmitter trans;

            if (!Transmitters.TryGetValue(msg.SymbolCode, out trans))
            {
                return;
            }

            // Remove the unwanted time frame
            if (msg.TimeFrame != ETimeFrame.None)
            {
                trans.TimeFrames = trans.TimeFrames.Except(msg.TimeFrame).ToArray();
            }
            else
            {
                trans.TimeFrames = new ETimeFrame[0];
            }

            // Remove transmitters containing no time frames
            if (trans.TimeFrames.Length == 0)
            {
                Transmitters.Remove(msg.SymbolCode);
                trans.Dispose();
            }
        }
Пример #2
0
        /// <summary>Step the simulation by one 'StepSize'</summary>
        public void Step()
        {
            var time0 = UtcNow;
            var time1 = UtcNow + StepSize;

            // Collect the candle data to send from each transmitter over the step period
            var data = Transmitters.SelectMany(x => x.Value.EnumCandleData(time0, time1)).OrderBy(x => x.Candle.Timestamp);

            // "Post" the candle data to tradee
            foreach (var d in data)
            {
                var candle = d.Candle as Candle;

                // Fake the price data using the candle data
                var trans = Transmitters[d.SymbolCode];
                foreach (var price in new[] { candle.Open, candle.High, candle.Low, candle.Median, candle.Close })
                {
                    var pd = new PriceData(trans.PriceData)
                    {
                        AskPrice = price,
                        BidPrice = price + trans.PriceData.AvrSpread,
                    };
                    Model.DispatchMsg(new InMsg.SymbolData(trans.SymbolCode, pd));
                }

                // Send the candle
                Model.DispatchMsg(d);
            }

            UtcNow = time1;
        }
Пример #3
0
        /// <summary>Handle the hello message</summary>
        private void HandleMsg(OutMsg.RequestInstrument msg)
        {
            // Get or create a transmitter for the requested instrument
            var trans = Transmitters.GetOrAdd(msg.SymbolCode, k => new Transmitter(msg.SymbolCode, Instrument.CacheDBFilePath(Settings, msg.SymbolCode)));

            // Add the requested time frame
            trans.TimeFrames = trans.TimeFrames.Concat(msg.TimeFrame).Distinct().ToArray();
        }
Пример #4
0
        private void OnAddTransmitter()
        {
            var bleTrans  = new BleTransmitter();
            var viewModel = new TransmitterEntryViewModel(bleTrans);

            viewModel.OnRemove += vm => Transmitters.Remove(vm);
            Transmitters.Add(viewModel);
        }
Пример #5
0
        private async Task DeleteItemExecute(object arg)
        {
            if (arg is TransmitterViewModel itemViewModel)
            {
                await _transmitterProvider.DeleteAsync(itemViewModel.DataItem);

                Transmitters.Remove(itemViewModel);
            }
        }
Пример #6
0
        private async Task CopyItemExecute(object arg)
        {
            if (arg is TransmitterViewModel viewModel)
            {
                var item = await _transmitterProvider.CopyAsync(viewModel.DataItem);

                Transmitters.Add(new TransmitterViewModel(item, _dialogService, _mainView));
            }
        }
Пример #7
0
        private Task NewTransmitterExecute(object arg)
        {
            var transmitter = new Transmitter();

            transmitter.Id          = Guid.NewGuid();
            transmitter.DisplayName = "New transmitter";
            transmitter.Encoding    = Encoding.UTF8;
            _transmitterProvider.SaveAsync(transmitter);

            var viewModel = new TransmitterViewModel(transmitter, _dialogService, _mainView);

            Transmitters.Add(viewModel);

            if (_applicationSettings.FocusTabOnCreate)
            {
                OpenItemExecute(viewModel);
            }

            return(Task.CompletedTask);
        }
Пример #8
0
        private void LoadExistingConfiguration()
        {
            var config = BleLocatorModel.Instance.BleSystemConfiguration;

            ReceiverPaths     = config.ReceiverPaths.HasSomething()? new List <ReceiverPath>(config.ReceiverPaths):new List <ReceiverPath>();
            UseWeightedPoints = config.UseWeightedPaths;
            if (config.BleReceivers.HasSomething())
            {
                foreach (var bleReceiver in config.BleReceivers)
                {
                    var viewModel = new ReceiverEntryViewModel(bleReceiver.Value);
                    viewModel.OnRemove += vm => Receivers.Remove(vm);
                    Receivers.Add(viewModel);
                }
            }
            else
            {
                var viewModel = new ReceiverEntryViewModel(new BleReceiver());
                viewModel.OnRemove += vm => Receivers.Remove(vm);
                Receivers.Add(viewModel);
            }
            if (config.BleTransmitters.HasSomething())
            {
                foreach (var transmitter in config.BleTransmitters)
                {
                    var viewModel = new TransmitterEntryViewModel(transmitter.Value);
                    viewModel.OnRemove += vm => Transmitters.Remove(vm);
                    Transmitters.Add(viewModel);
                }
            }
            else
            {
                var viewModel = new TransmitterEntryViewModel(new BleTransmitter());
                viewModel.OnRemove += vm => Transmitters.Remove(vm);
                Transmitters.Add(viewModel);
            }
        }
Пример #9
0
        /// <summary>Handle a request to place a market order</summary>
        private void HandleMsg(OutMsg.PlaceMarketOrder msg)
        {
            var order = msg.Order;

            // Find the transmitter of the instrument that the order is for
            Transmitter trans;

            if (!Transmitters.TryGetValue(order.SymbolCode, out trans))
            {
                Model.DispatchMsg(new InMsg.MarketOrderChangeResult(false, EErrorCode.Failed));
                return;
            }

            // Check the volume is valid
            if (order.Volume < trans.PriceData.VolumeMin ||
                order.Volume > trans.PriceData.VolumeMax ||
                order.Volume % trans.PriceData.VolumeStep != 0)
            {
                Model.DispatchMsg(new InMsg.MarketOrderChangeResult(false, EErrorCode.InvalidVolume));
                return;
            }

            // Place an immediate order
            if (msg.Position != null)
            {
                var res = ActionTrade(msg.Position);
                if (res != EErrorCode.NoError)
                {
                    Model.DispatchMsg(new InMsg.MarketOrderChangeResult(false, res));
                    return;
                }
            }

            // Place a limit order
            else if (msg.PendingLimit != null)
            {
                // Limit orders are rejected if the price is not on the profit side of the order
                if (msg.PendingLimit.TradeType == ETradeType.Long && trans.PriceData.AskPrice < msg.PendingLimit.EntryPrice)
                {
                    Model.DispatchMsg(new InMsg.MarketOrderChangeResult(false, EErrorCode.Failed));
                    return;
                }
                if (msg.PendingLimit.TradeType == ETradeType.Short && trans.PriceData.BidPrice > msg.PendingLimit.EntryPrice)
                {
                    Model.DispatchMsg(new InMsg.MarketOrderChangeResult(false, EErrorCode.Failed));
                    return;
                }

                // Add the pending order
                Pending.Add(msg.PendingLimit);
            }

            // Place a stop order
            else if (msg.PendingStop != null)
            {
                // Stop orders are rejected if the price is not on the loss side of the order
                if (msg.PendingStop.TradeType == ETradeType.Long && trans.PriceData.AskPrice > msg.PendingStop.EntryPrice)
                {
                    Model.DispatchMsg(new InMsg.MarketOrderChangeResult(false, EErrorCode.Failed));
                    return;
                }
                if (msg.PendingStop.TradeType == ETradeType.Short && trans.PriceData.BidPrice < msg.PendingStop.EntryPrice)
                {
                    Model.DispatchMsg(new InMsg.MarketOrderChangeResult(false, EErrorCode.Failed));
                    return;
                }

                // Add the pending order
                Pending.Add(msg.PendingStop);
            }

            // Send an update of the current/pending positions
            SendCurrentPositions();
            SendPendingPositions();
        }