Example #1
0
        /// <summary>
        /// Clones the Instrument_Properties.
        /// </summary>
        public InstrumentProperties Clone()
        {
            var copy = new InstrumentProperties(Symbol, InstrType)
            {
                Symbol          = Symbol,
                InstrType       = InstrType,
                Comment         = Comment,
                Digits          = Digits,
                LotSize         = LotSize,
                Spread          = Spread,
                SwapType        = SwapType,
                SwapLong        = SwapLong,
                SwapShort       = SwapShort,
                CommissionType  = CommissionType,
                CommissionScope = CommissionScope,
                CommissionTime  = CommissionTime,
                Commission      = Commission,
                PriceIn         = PriceIn,
                Slippage        = Slippage,
                RateToEUR       = RateToEUR,
                RateToUSD       = RateToUSD,
                BaseFileName    = BaseFileName
            };

            return(copy);
        }
        private Bar[] _aBar; // An array containing the data

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Constructor
        /// </summary>
        public Instrument(InstrumentProperties instrProperties, int period)
        {
            DataDir = "." + Path.DirectorySeparatorChar + "Data" + Path.DirectorySeparatorChar;
            EndTime = new DateTime(2020, 1, 1, 0, 0, 0);
            StartTime = new DateTime(1990, 1, 1, 0, 0, 0);
            MaxBars = 20000;
            _instrProperties = instrProperties;
            Period = period;
        }
Example #3
0
        private Bar[] _aBar;                                    // An array containing the data

        /// <summary>
        /// Constructor
        /// </summary>
        public Instrument(InstrumentProperties instrProperties, int period)
        {
            DataDir          = "." + Path.DirectorySeparatorChar + "Data" + Path.DirectorySeparatorChar;
            EndTime          = new DateTime(2020, 1, 1, 0, 0, 0);
            StartTime        = new DateTime(1990, 1, 1, 0, 0, 0);
            MaxBars          = 20000;
            _instrProperties = instrProperties;
            Period           = period;
        }
        /// <summary>
        /// Loads, filters and saves all data files.
        /// </summary>
        private void LoadSaveData()
        {
            var files = Directory.GetFiles(Data.OfflineDataDir, "*.csv");

            foreach (var file in files)
            {
                var symbol = GetSymbolFromFileName(file);
                var period = GetPeriodFromFileName(file);
                if (string.IsNullOrEmpty(symbol) || period == 0)
                {
                    continue;
                }

                InstrumentProperties instrProperties = Instruments.InstrumentList[symbol].Clone();
                var instrument = new Instrument(instrProperties, period)
                {
                    DataDir      = Data.OfflineDataDir,
                    MaxBars      = Configs.MaxBars,
                    StartTime    = Configs.DataStartTime,
                    EndTime      = Configs.DataEndTime,
                    UseStartTime = Configs.UseStartTime,
                    UseEndTime   = Configs.UseEndTime
                };

                int loadDataResult = instrument.LoadData();

                if (instrument.Bars > 0 && loadDataResult == 0)
                {
                    var stringBuilder = new StringBuilder(instrument.Bars);
                    for (int bar = 0; bar < instrument.Bars; bar++)
                    {
                        stringBuilder.AppendLine(
                            instrument.Time(bar).ToString("yyyy-MM-dd") + "\t" +
                            instrument.Time(bar).ToString("HH:mm") + "\t" +
                            instrument.Open(bar).ToString(CultureInfo.InvariantCulture) + "\t" +
                            instrument.High(bar).ToString(CultureInfo.InvariantCulture) + "\t" +
                            instrument.Low(bar).ToString(CultureInfo.InvariantCulture) + "\t" +
                            instrument.Close(bar).ToString(CultureInfo.InvariantCulture) + "\t" +
                            instrument.Volume(bar).ToString(CultureInfo.InvariantCulture)
                            );
                    }
                    try
                    {
                        var sw = new StreamWriter(file);
                        sw.Write(stringBuilder.ToString());
                        sw.Close();

                        TbxOutput.Text += symbol + period + " bars: " + instrument.Bars + Environment.NewLine;
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(exc.Message);
                    }
                }
            }
        }
        /// <summary>
        /// Parses the instruments file.
        /// </summary>
        private static void ParseInstruments()
        {
            int instrumentsCount = XMLInstruments.GetElementsByTagName("instrument").Count;

            _dictInstrument = new Dictionary <string, InstrumentProperties>(instrumentsCount);

            try
            {
                foreach (XmlNode nodeInstr in XMLInstruments.GetElementsByTagName("instrument"))
                {
                    string symbol    = nodeInstr.SelectSingleNode("symbol").InnerText;
                    var    instrType = (InstrumetType)Enum.Parse(typeof(InstrumetType), nodeInstr.SelectSingleNode("instrumentType").InnerText);
                    var    instrProp = new InstrumentProperties(symbol, instrType)
                    {
                        Comment         = nodeInstr.SelectSingleNode("comment").InnerText,
                        Digits          = int.Parse(nodeInstr.SelectSingleNode("digits").InnerText),
                        LotSize         = int.Parse(nodeInstr.SelectSingleNode("contractSize").InnerText),
                        Spread          = StringToFloat(nodeInstr.SelectSingleNode("spread").InnerText),
                        SwapType        = (CommissionType)Enum.Parse(typeof(CommissionType), nodeInstr.SelectSingleNode("swapType").InnerText),
                        SwapLong        = StringToFloat(nodeInstr.SelectSingleNode("swapLong").InnerText),
                        SwapShort       = StringToFloat(nodeInstr.SelectSingleNode("swapShort").InnerText),
                        CommissionType  = (CommissionType)Enum.Parse(typeof(CommissionType), nodeInstr.SelectSingleNode("commissionType").InnerText),
                        CommissionScope = (CommissionScope)Enum.Parse(typeof(CommissionScope), nodeInstr.SelectSingleNode("commissionScope").InnerText),
                        CommissionTime  = (CommissionTime)Enum.Parse(typeof(CommissionTime), nodeInstr.SelectSingleNode("commissionTime").InnerText),
                        Commission      = StringToFloat(nodeInstr.SelectSingleNode("commission").InnerText),
                        Slippage        = int.Parse(nodeInstr.SelectSingleNode("slippage").InnerText),
                        PriceIn         = nodeInstr.SelectSingleNode("priceIn").InnerText,
                        RateToUSD       = StringToFloat(nodeInstr.SelectSingleNode("rateToUSD").InnerText),
                        RateToEUR       = StringToFloat(nodeInstr.SelectSingleNode("rateToEUR").InnerText),
                        BaseFileName    = nodeInstr.SelectSingleNode("baseFileName").InnerText
                    };
                    _dictInstrument.Add(symbol, instrProp);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Parsing Instruments");
            }
        }
        /// <summary>
        /// Generates instrument.xml file.
        /// </summary>
        private static XmlDocument GenerateXMLFile()
        {
            // Create the XmlDocument.
            var xmlDoc = new XmlDocument();

            xmlDoc.LoadXml("<fsb></fsb>");

            // Create the XML declaration.
            XmlDeclaration xmldecl = xmlDoc.CreateXmlDeclaration("1.0", null, null);

            // Add new node to the document.
            XmlElement root = xmlDoc.DocumentElement;

            xmlDoc.InsertBefore(xmldecl, root);

            foreach (var kvp in _dictInstrument)
            {
                InstrumentProperties instrProp = kvp.Value;

                // Creates an instrument element.
                XmlElement instrument = xmlDoc.CreateElement("instrument");

                XmlElement element = xmlDoc.CreateElement("symbol");
                element.InnerText = instrProp.Symbol;
                instrument.AppendChild(element);

                element           = xmlDoc.CreateElement("instrumentType");
                element.InnerText = instrProp.InstrType.ToString();
                instrument.AppendChild(element);

                element           = xmlDoc.CreateElement("comment");
                element.InnerText = instrProp.Comment;
                instrument.AppendChild(element);

                element           = xmlDoc.CreateElement("digits");
                element.InnerText = instrProp.Digits.ToString(CultureInfo.InvariantCulture);
                instrument.AppendChild(element);

                element           = xmlDoc.CreateElement("contractSize");
                element.InnerText = instrProp.LotSize.ToString(CultureInfo.InvariantCulture);
                instrument.AppendChild(element);

                element           = xmlDoc.CreateElement("spread");
                element.InnerText = instrProp.Spread.ToString(CultureInfo.InvariantCulture);
                instrument.AppendChild(element);

                element           = xmlDoc.CreateElement("swapType");
                element.InnerText = instrProp.SwapType.ToString();
                instrument.AppendChild(element);

                element           = xmlDoc.CreateElement("swapLong");
                element.InnerText = instrProp.SwapLong.ToString(CultureInfo.InvariantCulture);
                instrument.AppendChild(element);

                element           = xmlDoc.CreateElement("swapShort");
                element.InnerText = instrProp.SwapShort.ToString(CultureInfo.InvariantCulture);
                instrument.AppendChild(element);

                element           = xmlDoc.CreateElement("commissionType");
                element.InnerText = instrProp.CommissionType.ToString();
                instrument.AppendChild(element);

                element           = xmlDoc.CreateElement("commissionScope");
                element.InnerText = instrProp.CommissionScope.ToString();
                instrument.AppendChild(element);

                element           = xmlDoc.CreateElement("commissionTime");
                element.InnerText = instrProp.CommissionTime.ToString();
                instrument.AppendChild(element);

                element           = xmlDoc.CreateElement("commission");
                element.InnerText = instrProp.Commission.ToString(CultureInfo.InvariantCulture);
                instrument.AppendChild(element);

                element           = xmlDoc.CreateElement("slippage");
                element.InnerText = instrProp.Slippage.ToString(CultureInfo.InvariantCulture);
                instrument.AppendChild(element);

                element           = xmlDoc.CreateElement("priceIn");
                element.InnerText = instrProp.PriceIn;
                instrument.AppendChild(element);

                element           = xmlDoc.CreateElement("rateToUSD");
                element.InnerText = instrProp.RateToUSD.ToString("F4");
                instrument.AppendChild(element);

                element           = xmlDoc.CreateElement("rateToEUR");
                element.InnerText = instrProp.RateToEUR.ToString("F4");
                instrument.AppendChild(element);

                element           = xmlDoc.CreateElement("baseFileName");
                element.InnerText = instrProp.BaseFileName;
                instrument.AppendChild(element);

                if (xmlDoc.DocumentElement != null)
                {
                    xmlDoc.DocumentElement.AppendChild(instrument);
                }
            }

            return(xmlDoc);
        }
        /// <summary>
        /// Parses the instruments file.
        /// </summary>
        private static void ParseInstruments()
        {
            int instrumentsCount = XMLInstruments.GetElementsByTagName("instrument").Count;
            _dictInstrument = new Dictionary<string, InstrumentProperties>(instrumentsCount);

            try
            {
                foreach (XmlNode nodeInstr in XMLInstruments.GetElementsByTagName("instrument"))
                {
                    string symbol = nodeInstr.SelectSingleNode("symbol").InnerText;
                    var instrType = (InstrumetType) Enum.Parse(typeof (InstrumetType), nodeInstr.SelectSingleNode("instrumentType").InnerText);
                    var instrProp = new InstrumentProperties(symbol, instrType)
                    {
                        Comment = nodeInstr.SelectSingleNode("comment").InnerText,
                        Digits = int.Parse(nodeInstr.SelectSingleNode("digits").InnerText),
                        LotSize = int.Parse(nodeInstr.SelectSingleNode("contractSize").InnerText),
                        Spread = StringToFloat(nodeInstr.SelectSingleNode("spread").InnerText),
                        SwapType = (CommissionType) Enum.Parse(typeof (CommissionType), nodeInstr.SelectSingleNode("swapType").InnerText),
                        SwapLong = StringToFloat(nodeInstr.SelectSingleNode("swapLong").InnerText),
                        SwapShort = StringToFloat(nodeInstr.SelectSingleNode("swapShort").InnerText),
                        CommissionType = (CommissionType) Enum.Parse(typeof (CommissionType), nodeInstr.SelectSingleNode("commissionType").InnerText),
                        CommissionScope = (CommissionScope) Enum.Parse(typeof (CommissionScope), nodeInstr.SelectSingleNode("commissionScope").InnerText),
                        CommissionTime = (CommissionTime) Enum.Parse(typeof (CommissionTime), nodeInstr.SelectSingleNode("commissionTime").InnerText),
                        Commission = StringToFloat(nodeInstr.SelectSingleNode("commission").InnerText),
                        Slippage = int.Parse(nodeInstr.SelectSingleNode("slippage").InnerText),
                        PriceIn = nodeInstr.SelectSingleNode("priceIn").InnerText,
                        RateToUSD = StringToFloat(nodeInstr.SelectSingleNode("rateToUSD").InnerText),
                        RateToEUR = StringToFloat(nodeInstr.SelectSingleNode("rateToEUR").InnerText),
                        BaseFileName = nodeInstr.SelectSingleNode("baseFileName").InnerText
                    };
                    _dictInstrument.Add(symbol, instrProp);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Parsing Instruments");
            }
        }
 /// <summary>
 /// BtnAdd Clicked.
 /// </summary>
 private void BtnAddInstrAddClick(object sender, EventArgs e)
 {
     if (
         ValidateSymbol(TbxAddInstrSymbol.Text,
                        (InstrumetType) Enum.Parse(typeof (InstrumetType), CbxAddInstrType.Text)) &&
         !LbxInstruments.Items.Contains(TbxAddInstrSymbol.Text))
     {
         _instrPropSelectedInstrument = new InstrumentProperties(TbxAddInstrSymbol.Text,
                                                                 (InstrumetType)
                                                                 Enum.Parse(typeof (InstrumetType),
                                                                            CbxAddInstrType.Text));
         SetPropertiesForm();
         SetSelectedInstrument();
     }
     else
     {
         MessageBox.Show(Language.T("Wrong Symbol!"), Language.T("Instrument Properties"), MessageBoxButtons.OK,
                         MessageBoxIcon.Exclamation);
     }
 }
        /// <summary>
        /// The lbxInstruments selected index changed
        /// </summary>
        private void LbxInstrumentsSelectedValueChanged(object sender, EventArgs e)
        {
            if (LbxInstruments.SelectedItem == null) return;

            _instrPropSelectedInstrument = Instruments.InstrumentList[LbxInstruments.SelectedItem.ToString()].Clone();
            SetPropertiesForm();
        }
 /// <summary>
 /// Clones the Instrument_Properties.
 /// </summary>
 public InstrumentProperties Clone()
 {
     var copy = new InstrumentProperties(Symbol, InstrType)
                    {
                        Symbol = Symbol,
                        InstrType = InstrType,
                        Comment = Comment,
                        Digits = Digits,
                        LotSize = LotSize,
                        Spread = Spread,
                        SwapType = SwapType,
                        SwapLong = SwapLong,
                        SwapShort = SwapShort,
                        CommissionType = CommissionType,
                        CommissionScope = CommissionScope,
                        CommissionTime = CommissionTime,
                        Commission = Commission,
                        PriceIn = PriceIn,
                        Slippage = Slippage,
                        RateToEUR = RateToEUR,
                        RateToUSD = RateToUSD,
                        BaseFileName = BaseFileName
                    };
     return copy;
 }