Exemplo n.º 1
0
        public void TestGetInstruments()
        {
            _repository.AddInstrument(new Instrument("Instrument1", InstrumentType.Bond));
            _repository.AddInstrument(new Instrument("Instrument2", InstrumentType.Forex));

            Assert.AreEqual(2, _repository.GetInstruments().Count());
        }
Exemplo n.º 2
0
        public async Task <IEnumerable <InsideBarView> > GetInsideBars()
        {
            var instrumentsAsync = await _instrumentRepository.GetInstruments();

            var instruments = instrumentsAsync.Where(x => (x.GroupName == "Major" || x.GroupName == "Minor") ||
                                                     x.Symbol == "DE30" || x.Symbol == "US500" || x.Symbol == "US100" || x.Symbol == "GOLD" || x.Symbol == "W20").ToList();
            var chartsAsync = instruments.Select(async x => await _chartRepository.GetChartAsync(x.Symbol, "D1", 10, x.Precision)).ToList();
            var charts      = await Task.WhenAll(chartsAsync);

            var list = new List <InsideBarView>();

            foreach (var chart in charts)
            {
                chart.SetInsideBar(DateTime.Now.AddDays(-1));
                if (chart.InsideBar != null)
                {
                    list.Add(new InsideBarView
                    {
                        HighInsideBar       = chart.InsideBar.HighInsideBar,
                        LowInsideBar        = chart.InsideBar.LowInsideBar,
                        HighMotherInsideBar = chart.InsideBar.HighMotherInsideBar,
                        LowMotherInsideBar  = chart.InsideBar.LowMotherInsideBar,
                        Symbol = chart.Symbol,
                        Side   = chart.InsideBar.Side
                    });
                }
            }
            return(list);
        }
Exemplo n.º 3
0
 public Pricer(IInstrumentRepository instrumentRepository, double interval)
 {
     _timer = new Timer(interval);
     _instrumentRepository = instrumentRepository;
     _instruments          = _instrumentRepository.GetInstruments().Select(instrument => instrument.Name).ToArray();
     _rand = new Random((int)DateTime.Now.Ticks);
 }
Exemplo n.º 4
0
        protected async override void Handle(PlayInsideBarCommand request)
        {
            if (DateTime.Now.Second >= 58)
            {
                try
                {
                    if (request.InsideBars.Count > 0)
                    {
                        var account = await _accountRepository.GetAccountAsync("11181613");

                        var instruments = await _instrumentRepository.GetInstruments();

                        foreach (var insideBars in request.InsideBars)
                        {
                            var instrument = instruments.FirstOrDefault(x => x.Symbol == insideBars.Symbol);
                            var insideBar  = new InsideBarStrategy(instrument, 1000, account);
                            var chart      = await _chartRepository.GetChartAsync(insideBars.Symbol, "M1", 4000, instrument.Precision);

                            insideBar.Play(chart);
                            await _mediator.DispatchDomainEventsAsync(account);

                            _logger.LogInformation($"Play inside bar {insideBar.Instrument.Symbol}");
                        }
                    }
                    else
                    {
                        _logger.LogInformation("No inside bars");
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "Error Inside bar");
                }
            }
        }
Exemplo n.º 5
0
        public async Task <IActionResult> GetInstrument()
        {
            var items = await _repository.GetInstruments();

            var itemsDto = _mapper.Map <List <DtoOutputInstrumentForList> >(items);

            return(Ok(itemsDto));
        }
Exemplo n.º 6
0
 public IEnumerable <Instrument> GetInstruments()
 {
     if (!CurrentUser.HasRole(UserRole.Member))
     {
         throw new DomainValidationException(Messages.InsufficientSecurityClearance);
     }
     return(_instrumentRepository.GetInstruments());
 }
Exemplo n.º 7
0
        public TrainingForm()
        {
            InitializeComponent();
            _instrumentRepository = new MyInstrumentRepository();
            _instrumentRepository.Init(20);

            _pricer = new Pricer(_instrumentRepository, 1);

            _instruments =
                new ObservableCollection <InstrumentViewModel>(
                    _instrumentRepository.GetInstruments().Select(instrument => new InstrumentViewModel(instrument)));
        }
Exemplo n.º 8
0
        public ViewModel()
        {
            _instrumentRepository = new MyInstrumentRepository();
            _instrumentRepository.Init(50);
            Instruments =
                new ObservableCollection <InstrumentViewModel>(
                    _instrumentRepository.GetInstruments().Select(instrument => new InstrumentViewModel(instrument)));
            _pricer = new Pricer(_instrumentRepository, 1);

            StartCommand   = new RelayCommand(o => { _pricer.Price(); }, o => true);
            StopCommand    = new RelayCommand(o => { _pricer.StopPrice(); }, o => true);
            RestartCommand = new RelayCommand(o => { _pricer.Restart(); }, o => true);
        }
Exemplo n.º 9
0
        public void TestPrice()
        {
            _repository = Substitute.For <IInstrumentRepository>();
            _repository.GetInstruments()
            .Returns(new List <Instrument>
            {
                new Instrument("Toto", InstrumentType.Bond)
            });
            _pricer = new Pricer(_repository, 500);
            _pricer.Price();

            Thread.Sleep(800);

            _repository.Received(1).PriceUpdate("Toto", Arg.Any <double>());
        }
Exemplo n.º 10
0
        public async Task <List <Instrument> > Handle(GetInstrumentsToPlayRequest request, CancellationToken cancellationToken)
        {
            try
            {
                var instruments = await _instrumentRepository.GetInstruments();

                return(instruments.Where(
                           x => (x.GroupName == "Major" || x.GroupName == "Minor") || x.Symbol == "DE30" || x.Symbol == "US500" || x.Symbol == "US100" || x.Symbol == "GOLD" ||
                           x.Symbol == "W20")
                       .ToList());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "GetInstrumentsToPlayRequestHandler");
                throw ex;
            }
        }
Exemplo n.º 11
0
        public async Task <ChartView> SimulateView(string symbol, string interval)
        {
            var listModel = new List <ChartWithEma>();

            var symbols = await _instrumentRepository.GetInstruments();

            var instrument = symbols.FirstOrDefault(x => x.Symbol == symbol);

            var chartTakeProfit = await _chartRepository.GetChartAsync(instrument.Symbol, interval, 150, instrument.Precision);

            var takeProfit = (decimal)Math.Round((chartTakeProfit.Quotations
                                                  .Sum(x =>
            {
                var low = x.Low * Math.Pow(10, instrument.Precision);
                var high = x.High * Math.Pow(10, instrument.Precision);
                var pipsDiff = Math.Round(Math.Abs(low - high), 1);
                return(pipsDiff / 10);
            }) / chartTakeProfit.Quotations.Count()), 2);
            var chart = await _chartRepository.GetChartAsync(instrument.Symbol, interval, 10000, instrument.Precision);

            var emaValue = 50;
            var emasH    = Indicators.EMA(chart.Quotations.Select(x => x.Close).ToArray(), emaValue);

            for (int i = 0; i < emasH.Count(); i++)
            {
                chart.Quotations[i].AddEma(emasH[i]);
            }

            var quotations = chart.Quotations.ToArray();

            var timeArray  = quotations.Select(x => x.Time.ConvertDateTimeToTicks()).ToArray();
            var openArray  = quotations.Select(x => x.Open).ToArray();
            var highArray  = quotations.Select(x => x.High).ToArray();
            var lowArray   = quotations.Select(x => x.Low).ToArray();
            var closeArray = quotations.Select(x => x.Close).ToArray();

            listModel.AddRange(quotations.Select(
                                   x => new ChartWithEma
            {
                Time  = x.Time.ConvertDateTimeToTicks(),
                Open  = x.Open,
                High  = x.High,
                Low   = x.Low,
                Close = x.Close,
            }));

            var buyTrades  = listModel.Where(x => x.TypeAction == "B").ToList();
            var sellTrades = listModel.Where(x => x.TypeAction == "S").ToList();
            var c          = CombineArrays(timeArray, openArray, highArray, lowArray, closeArray);

            var series = new List <dynamic>()
            {
                CombineArrays(timeArray, openArray, highArray, lowArray, closeArray),
                CombineArrays(timeArray, emasH),
                GetTradeSignals(buyTrades.Select(x => x.Time).ToArray(), "B"),
                GetTradeSignals(sellTrades.Select(x => x.Time).ToArray(), "S")
            };
            var chartView = new ChartView {
                Series = series
            };

            return(chartView);
        }
Exemplo n.º 12
0
        protected async override void Handle(PlayGapCommand request)
        {
            if ((DateTime.Now.Hour == 8 && DateTime.Now.Minute > 56) && DateTime.Now.Hour < 9) //EU
            {
                try
                {
                    var account = await _accountRepository.GetAccountAsync("11181613");

                    var instruments = await _instrumentRepository.GetInstruments();

                    var symbolsEu     = request.SymbolsToPlay.BottomGapSymbolEu.Union(request.SymbolsToPlay.TopGapSymbolEu);
                    var instrumentsEu = instruments.Where(x => symbolsEu.Contains(x.Symbol)).ToList();
                    if (instrumentsEu.Any())
                    {
                        var chartsAsync = instrumentsEu.Select(async x => await _chartRepository.GetChartAsync(x.Symbol, "M1", 2250 * 5, x.Precision));
                        var charts      = await Task.WhenAll(chartsAsync);

                        foreach (var chart in charts)
                        {
                            var instrument  = instruments.FirstOrDefault(x => x.Symbol == chart.Symbol);
                            var gapStrategy = new GapStrategy(instrument, 1000, account);
                            gapStrategy.Play(chart);
                        }
                        _logger.LogInformation($"Play eu gap - {instrumentsEu.Aggregate("", (current, next) => current + ", " + next.Symbol)}");
                    }
                    await _mediator.DispatchDomainEventsAsync(account);

                    _logger.LogInformation($"No symbols eu to play gap");
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "Error gap");
                }
            }
            if ((DateTime.Now.Hour == 15 && DateTime.Now.Minute > 26) && (DateTime.Now.Hour >= 15 && DateTime.Now.Minute > 30)) // US
            {
                try
                {
                    var account = await _accountRepository.GetAccountAsync("11181613");

                    var instruments = await _instrumentRepository.GetInstruments();

                    var symbolsEu     = request.SymbolsToPlay.BottomGapSymbolUs.Union(request.SymbolsToPlay.TopGapSymbolUs);
                    var instrumentsUs = instruments.Where(x => symbolsEu.Contains(x.Symbol)).ToList();
                    if (instrumentsUs.Any())
                    {
                        var chartsAsync = instrumentsUs.Select(async x => await _chartRepository.GetChartAsync(x.Symbol, "M1", 2250 * 5, x.Precision));
                        var charts      = await Task.WhenAll(chartsAsync);

                        foreach (var chart in charts)
                        {
                            var instrument  = instruments.FirstOrDefault(x => x.Symbol == chart.Symbol);
                            var gapStrategy = new GapStrategy(instrument, 1000, account);
                            gapStrategy.Play(chart);
                        }
                        _logger.LogInformation($"Play us gap - {instrumentsUs.Aggregate("", (current, next) => current + ", " + next.Symbol)}");
                    }
                    await _mediator.DispatchDomainEventsAsync(account);

                    _logger.LogInformation($"No symbols us to play gap");
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "Error gap");
                }
            }
        }