public void WhenNoPositionSetHistoricalBarsThrowsException()
        {
            var manager = new StopManager();
            var bars    = new BarCollection();

            Assert.Throws <InvalidOperationException>(() => manager.SetHistoricalBars(bars));
        }
        public void WhenNoPositionSetGetStopThrowsException()
        {
            var manager = new StopManager();
            var bars    = new BarCollection();

            Assert.Throws <InvalidOperationException>(() => manager.GetStop(DateTime.Today));
        }
        public void StopPriceSetCorrectlyForShort()
        {
            // Arrange
            const double StopPercentage = 10;
            const double Low            = 9.80;

            var manager = new StopManager();
            var bars    = new BarCollection
            {
                {
                    DateTime.Today,
                    new Bar(DateTime.Today, 10, 10.20, Low, 10)
                }
            };

            manager.Position = new Position
            {
                Direction    = Direction.Sell,
                EntryPrice   = 10,
                ExitStrategy = new AggressiveExitStrategy()
            };

            manager.SetHistoricalBars(bars);

            // Act
            var stop = manager.GetStop(DateTime.Today);

            // Assert
            Assert.IsType <TrailingStop>(stop);
            Assert.Equal(Low + Low * StopPercentage / 100, stop.Price);
        }
Esempio n. 4
0
    public void SerializeObject()
    {
        var barCollection = new BarCollection();
        var bar1          = new Bar()
        {
            Name = "bar1"
        };

        barCollection.Add(bar1);
        var bar2 = new Bar()
        {
            Name = "bar2"
        };

        barCollection.Add(bar2);
        var bar3 = new Bar()
        {
            Name = "bar3"
        };

        bar2.BarCollection.Add(bar3);
        var bar4 = new Bar()
        {
            Name = "bar4"
        };

        bar2.BarCollection.Add(bar4);
        var bar5 = new Bar()
        {
            Name = "bar 5"
        };

        barCollection.Add(bar5);
        var bar6 = new Bar()
        {
            Name = "bar 6"
        };

        barCollection.Add(bar6);
        var bar7 = new Bar()
        {
            Name = "bar 7"
        };

        bar6.BarCollection.Add(bar7);
        var bar8 = new Bar()
        {
            Name = "bar 8"
        };

        bar7.BarCollection.Add(bar8);
        var x = new XmlSerializer(typeof(BarCollection));

        x.Serialize(Console.Out, barCollection);
        Console.WriteLine("\n");
        WriteCollection(barCollection);
        Console.ReadLine();
    }
Esempio n. 5
0
        public void SetHistoricalData(string symbol, BarCollection bars)
        {
            if (_bars.ContainsKey(symbol))
            {
                _bars.Remove(symbol);
            }

            _bars.Add(symbol, bars);
        }
Esempio n. 6
0
 public void WriteCollection(BarCollection barCollection)
 {
     tierIndex++;
     foreach (Bar bar in barCollection)
     {
         Console.Write(new StringBuilder().Insert(0, "--", tierIndex) + "> ");
         Console.Write(bar.Name + "\n");
         WriteCollection(bar.BarCollection);
     }
     tierIndex--;
 }
        public async Task Get()
        {
            var db   = Context.GetDatabase();
            var bars = await db.GetEnumerable <Bar>(b => b.CommunityId == Community.Id);

            var collection = new BarCollection();

            foreach (var bar in bars)
            {
                collection.Bars.Add(new BarCollectionItem()
                {
                    Id       = bar.Id,
                    Name     = bar.Name,
                    IsShared = bar.IsShared
                });
            }
            await Respond(collection);
        }
Esempio n. 8
0
    public static int Main()
    {
        var collection = new BarCollection()
        {
            new Bar {
                Name = "a"
            },
            new Bar {
                Name = "b"
            },
            new Bar {
                Name = "c"
            },
        };

        foreach (var bar in collection)
        {
            Console.WriteLine(bar.Name);
        }

        return(0);
    }
        public void StandardDeviationCalculatedCorrectly()
        {
            var service = new OrderCalculationService();

            var barCollection = new BarCollection();

            var bars = new List <Bar>
            {
                new Bar {
                    Close = 9
                },
                new Bar {
                    Close = 2
                },
                new Bar {
                    Close = 5
                },
                new Bar {
                    Close = 4
                },
                new Bar {
                    Close = 12
                },
                new Bar {
                    Close = 7
                },
                new Bar {
                    Close = 8
                },
                new Bar {
                    Close = 11
                },
                new Bar {
                    Close = 9
                },
                new Bar {
                    Close = 3
                },
                new Bar {
                    Close = 7
                },
                new Bar {
                    Close = 4
                },
                new Bar {
                    Close = 12
                },
                new Bar {
                    Close = 5
                },
                new Bar {
                    Close = 4
                },
                new Bar {
                    Close = 10
                },
                new Bar {
                    Close = 9
                },
                new Bar {
                    Close = 6
                },
                new Bar {
                    Close = 9
                },
                new Bar {
                    Close = 4
                },
            };

            var r = new Random();

            foreach (var item in bars)
            {
                barCollection.Add(DateTime.Now.AddMilliseconds(r.NextDouble() * 10000000), item);
            }

            service.SetHistoricalData("MSFT", barCollection);
            var sd = Math.Round(service.CalculateStandardDeviation("MSFT"), 3);

            Assert.Equal(2.983, sd);
        }
Esempio n. 10
0
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(
            IProjectRepository projectRepository,
            IProjectTrussRepository projectTrussRepository,
            ITrussRepository trussRepository,
            ISectionRepository sectionRepository,
            ISteelRepository steelRepository
            )
        {
            this._projectRepository      = projectRepository;
            this._projectTrussRepository = projectTrussRepository;
            this._trussRepository        = trussRepository;
            this._sectionRepository      = sectionRepository;
            this._steelRepository        = steelRepository;

            ProjectsCollection = new ObservableCollection <Project>(this._projectRepository.GetAll());

            #region openProject

            Action openProjectAction = () =>
            {
                ActiveUserControl = new SelectProjectControl();
            };
            this._actions.Add(openProjectAction);
            OpenProjectMenuCommand = new RelayCommand(openProjectAction);

            #endregion

            #region addProject

            this.AddProjectCommand = new RelayCommand(() =>
            {
                AddProjectDialog addNewProj = new AddProjectDialog();
                if (addNewProj.ShowDialog() == true)
                {
                    Project tempProj = addNewProj.NewProject;
                    try
                    {
                        this._projectRepository.Add(tempProj);
                        ProjectsCollection.Add(tempProj);
                    }
                    catch (DbUpdateException e)
                    {
                        MessageBox.Show("Проект с данным кодом уже существует!");
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message, "Ошибка при добавлении проекта");
                    }
                }
            });

            #endregion

            #region UpdateProject

            UpdateProjectCommand = new RelayCommand(
                () =>
            {
                var updateProjectDialog = new UpdateProjectDialog(SelectedProject);
                if (updateProjectDialog.ShowDialog() == true)
                {
                    try
                    {
                        this._projectRepository.Update(SelectedProject);
                    }
                    catch (DbUpdateException e)
                    {
                        MessageBox.Show(e.Message);
                    }
                }
            },
                () => SelectedProject != null);

            #endregion

            #region RemoveProject

            this.RemoveProjectCommand = new RelayCommand(() =>
            {
                if (MessageBox.Show("Вы уверены, что хотите удалить проект?", "Удаление проекта", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    try
                    {
                        this._projectRepository.Remove(SelectedProject);
                        ProjectsCollection.Remove(SelectedProject);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message, "Ошибка при удалении проекта");
                    }
                }
            }, () => SelectedProject != null);
            #endregion

            #region openTruss

            Action openTrussMenuAction = () =>
            {
                var x = this._projectRepository.GetProjectTruss(SelectedProject);
                //Получить список ферм!
                ProjectsTrussesCollection = new ObservableCollection <ProjectTruss>(x);

                ActiveUserControl = new SelectProjectTrussControl();

                //MessageBox.Show(SelectedProject.Code);
            };

            this._actions.Add(openTrussMenuAction);


            OpenTrussMenuCommand = new RelayCommand(openTrussMenuAction, () => SelectedProject != null);

            #endregion

            #region addTruss

            AddTrussCommand = new RelayCommand(() =>
            {
                var newTrussDialog = new AddTrussDialog(this._trussRepository.GetTypeOfLoads());
                if (newTrussDialog.ShowDialog() == true)
                {
                    var newProjectTruss = newTrussDialog.NewProjectTruss;
                    var newTruss        = newProjectTruss.Truss;
                    this._trussRepository.Add(newTruss);
                    newProjectTruss.Truss     = null;
                    newProjectTruss.TrussId   = newTruss.TrussId;
                    newProjectTruss.ProjectId = SelectedProject.ProjectId;
                    this._projectTrussRepository.Add(newProjectTruss);
                    ProjectsTrussesCollection.Add(newProjectTruss);
                }
            }, () => SelectedProject != null);

            #endregion

            #region EditTruss

            EditTrussCommand = new RelayCommand(() =>
            {
                var editTussDialog = new AddTrussDialog(SelectedProjectTruss, this._trussRepository.GetTypeOfLoads());
                if (editTussDialog.ShowDialog() == true)
                {
                    this._projectTrussRepository.Update(editTussDialog.NewProjectTruss);
                    editTussDialog.NewProjectTruss.Truss.TrussId = editTussDialog.NewProjectTruss.TrussId;
                    this._trussRepository.Update(editTussDialog.NewProjectTruss.Truss);
                    //SelectedTruss = editTussDialog.NewProjectTruss.Truss;
                }
            }, () => SelectedProjectTruss != null);

            #endregion

            #region RemoveTruss

            RemoveTrussCommand = new RelayCommand(() =>
            {
                if (MessageBox.Show("Вы уверены, что хотите удалить ферму?", "Удаление фермы",
                                    MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    try
                    {
                        this._projectTrussRepository.Remove(SelectedProjectTruss);
                        ProjectsTrussesCollection.Remove(SelectedProjectTruss);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message, "Ошибка при удалении фермы");
                    }
                }
            }, () => SelectedProjectTruss != null
                                                  );

            #endregion

            #region DetailTruss

            DetailTrussMenuCommand = new RelayCommand(() =>
            {
                SelectedTruss   = SelectedProjectTruss.Truss;
                BarTemplateList = new List <BarTemplate>(this._trussRepository.GetBarTypes());

                SectionCollection = new ObservableCollection <Section>(this._sectionRepository.GetAll().Where(sec => ShortListOfSections? sec.ShortList == ShortListOfSections:true));
                SteelCollection   = new ObservableCollection <Steel>(this._steelRepository.GetAll());
                BarCollection     = new ObservableCollection <Bar>(SelectedTruss.Bar.OrderBy(bar => bar.BarNumber));

                if (SelectedTruss.UnitForce)
                {
                    BarCollection.AsParallel().ForAll(bar =>
                    {
                        if (bar.SteelId != null)
                        {
                            bar.SteelId = bar.SteelId;
                            bar.Steel   = SteelCollection.First(steel => steel.SteelId == bar.SteelId);
                        }
                        if (bar.SectionId != null)
                        {
                            bar.SectionId = bar.SectionId;
                            bar.Section   = SectionCollection.First(sec => sec.SectionId == bar.SectionId);
                        }
                    });
                }
                else
                {
                    BarCollection.AsParallel().ForAll(bar =>
                    {
                        if (bar.SteelId != null)
                        {
                            bar.SteelId = bar.SteelId;
                            bar.Steel   = SteelCollection.First(steel => steel.SteelId == bar.SteelId);
                        }
                        if (bar.SectionId != null)
                        {
                            bar.SectionId = bar.SectionId;
                            bar.Section   = SectionCollection.First(sec => sec.SectionId == bar.SectionId);
                        }
                        bar.ActualForce  = bar.Force;
                        bar.ActualMoment = bar.Moment;
                    });
                }


                LoadChangedCommand.Execute(null);

                ActiveUserControl = new TrussDetailControl(SelectedTruss);
            }, () => SelectedProjectTruss != null);

            #endregion

            #region SaveProjectChangesCmd

            SaveChangesCommand = new RelayCommand(() =>
            {
                this._projectTrussRepository.SaveChanges(SelectedProjectTruss);
            }, () => SelectedProjectTruss != null);

            #endregion

            #region LoadChangedCmd

            LoadChangedCommand = new RelayCommand(() =>
            {
                var coeff = SelectedTruss.UnitForce
                    ? SelectedProjectTruss.Load * SelectedProjectTruss.TrussSpacing / 100000
                    : 1;
                foreach (var item in BarCollection)
                {
                    item.ActualForce  = item.Force * coeff;
                    item.ActualMoment = item.Moment * coeff;
                }
            }, () => /*ActiveUserControl is TrussDetailControl && */ SelectedTruss.UnitForce && SelectedProjectTruss != null);
            #endregion

            #region BarTemplateSelectionChangedCmd

            BarTemplateSelectionChangedCommand = new RelayCommand(() =>
            {
                BarCollection
                .Where(bar => bar.ElementType == SelectedBarTemplate.ShortName)
                .AsParallel()
                .ForAll(bar =>
                {
                    if (SelectedBarTemplate.Steel != null)
                    {
                        bar.Steel   = SelectedBarTemplate.Steel;
                        bar.SteelId = SelectedBarTemplate.Steel.SteelId;
                    }
                    if (SelectedBarTemplate.Section != null)
                    {
                        bar.Section   = SelectedBarTemplate.Section;
                        bar.SectionId = SelectedBarTemplate.Section.SectionId;
                    }
                });
            }, () => SelectedBarTemplate != null);
            #endregion

            #region CalculateTruss

            CalculateTrussCommand = new RelayCommand(() =>
            {
                TrussWeight = 0;
                var f       = true;
                foreach (var bar in BarCollection)
                {
                    TrussWeight += bar.Length / 1000F * bar.Section.Mass / 1000F;
                    if (bar.Length == 0 || bar.ActualForce == 0 || bar.Section == null || bar.Steel == null)
                    {
                        f = false;
                        //break;
                    }
                }
                if (f)
                {
                    try
                    {
                        this._report     = TrussCalculate.Calculate(SelectedProjectTruss, this._steelRepository);
                        Calculated       = true;
                        ResultVisibility = Visibility.Visible;
                    }
                    catch (ArgumentException e)
                    {
                        MessageBox.Show(e.Message);
                    }
                }
                else
                {
                    TrussWeight = 0;
                    MessageBox.Show("Заполните данные о сечениях фермы");
                }
            }, () => SelectedProjectTruss != null);

            #endregion

            #region CreateReport

            CreateReportCommand = new RelayCommand(() =>
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog
                {
                    Filter = "Word File (*.docx)|*.docx"
                };
                if (saveFileDialog.ShowDialog() == true)
                {
                    var filename = saveFileDialog.FileName.Replace(".docx", "") + ".png";
                    if (ActiveUserControl is TrussDetailControl tdc)
                    {
                        tdc.GetTrussPicture(filename);

                        using (WordprocessingDocument wordprocessingDocument =
                                   WordprocessingDocument.Create(saveFileDialog.FileName, WordprocessingDocumentType.Document))
                        {
                            var mainPart      = wordprocessingDocument.AddMainDocumentPart();
                            var doc           = new Document();
                            mainPart.Document = doc;

                            var body    = doc.AppendChild(new Body());
                            ImagePart x = wordprocessingDocument.MainDocumentPart.AddImagePart(ImagePartType.Jpeg);


                            using (FileStream stream = new FileStream(filename, FileMode.Open))
                            {
                                x.FeedData(stream);
                            }

                            AddImageToBody(doc, mainPart.GetIdOfPart(x), tdc.TrussWidthToHeight);
                            CalculateTrussCommand.Execute(null);
                            wordprocessingDocument.MainDocumentPart.Document.Body.Append(this._report);
                            wordprocessingDocument.MainDocumentPart.Document.Save();
                        }
                        System.Diagnostics.Process.Start(saveFileDialog.FileName);
                    }
                }
            }, () => this._report != null);

            #endregion

            #region Settings

            SettingsMenuCommand = new RelayCommand(() =>
            {
                this.ActiveUserControl = new SettingsControl();
            });

            #endregion
        }
Esempio n. 11
0
 public HistoricalDataCompletedMessage(string symbol, BarCollection bars)
     : base(symbol)
 {
     Bars = bars;
 }