コード例 #1
0
ファイル: Mq4Parser.cs プロジェクト: matiasev/2calgo.Library
 private void HandleFunctions(string code, Algo algo, ParsingErrors parsingErrors)
 {
     var withoutProperties = code.RemoveMq4Properies();
     var mq4Functions = FunctionsParser.Parse(withoutProperties).ToArray();
     algo.Code.Functions = mq4Functions.ToList();
     HandleInit(algo, mq4Functions, parsingErrors);
 }
コード例 #2
0
ファイル: Mq4Parser.cs プロジェクト: matiasev/2calgo.Library
        private static void HandleFields(string code, Algo algo)
        {
            var onlyFields = code
                .RemoveMq4Properies()
                .RemoveParameters()
                .RemoveFunctions()
                .SplitDeclarations()
                .RemoveMq4Buffers(algo.Buffers)
                .RemoveReturnStatements()
                .ReplaceArraysToMq4Arrays();

            algo.Code.FieldsDeclarations = onlyFields;
        }
コード例 #3
0
ファイル: Mq4Parser.cs プロジェクト: matiasev/2calgo.Library
        public ParsingResult Parse(string code, AlgoType algotype, File[] includeFiles)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            var parsingErrors = new ParsingErrors();

            var algo = new Algo
                {
                    Mq4Code = code, 
                    AlgoType = algotype
                };
            string[] customIndicators;

            code = code
                .RemoveComments()
                .IncludeFiles(includeFiles)
                .HandleParsingErrors(parsingErrors);

            if (parsingErrors.Errors.Any(error => error.ErrorType >= ErrorType.NotSupportedCriticalError))
                return new ParsingResult(algo, parsingErrors.Errors);
            
            code = code
                .ReplaceDateTimeToInt()
                .ReplaceDateConstants()
                .ReplaceDefines()
                .ReplaceCSharpKeyWords()
                .RemoveDotsFromNames()
                .AddZeroToDecimalNumbers()
                .ReplaceUnknownSymbols()
                .ReplaceMq4RgbColorsToKnownColors()
                .ReplaceColorToInt()
                .ReplaceCAlgoKeyWords()
                .ReplacePrintToMq4Print()
                .RenameFunctions()
                .AddRefModifiers()
                .AddTypeParameterToICustom(out customIndicators);

            HandleProperties(code, algo);
            HandleParameters(code, algo);
            HandleFunctions(code, algo, parsingErrors);
            HandleFields(code, algo);

            algo.Code.ExtractStaticVariablesToFields();
            algo.Code.ReplaceSimpleTypesToMq4Types();
            algo.Code.RenameStandardFunctions();
            algo.Code.AddMq4InitFunctionIfDoesNotExist();
            algo.CustomIndicators = customIndicators;

            return new ParsingResult(algo, parsingErrors.Errors);
        }
コード例 #4
0
 public ParsingResult(Algo algo, IEnumerable<ParsingError> parsingErrors)
 {
     Algo = algo;
     ParsingErrors = parsingErrors;
 }
コード例 #5
0
ファイル: Mq4Parser.cs プロジェクト: matiasev/2calgo.Library
        private void HandleInit(Algo algo, IEnumerable<Function> mq4Functions, ParsingErrors parsingErrors)
        {
            var initFunction = mq4Functions.FirstOrDefault(function => function.Name == "init");

            if (initFunction == null)
                return;

            var methodCalls = MethodCallsParser.Parse(initFunction.Body).ToArray();

            HandleBufferIndexes(methodCalls, algo);
            HandleLevels(methodCalls, algo);
            HandleIndexStyles(methodCalls, algo, parsingErrors);
        }
コード例 #6
0
ファイル: Mq4Parser.cs プロジェクト: matiasev/2calgo.Library
        private static void HandleParameters(string code, Algo algo)
        {
            var parameters = ParametersParser.Parse(code).ToArray();

            algo.Parameters = parameters;
        }
コード例 #7
0
ファイル: Mq4Parser.cs プロジェクト: matiasev/2calgo.Library
        private static void HandleProperties(string code, Algo result)
        {
            var properties = PropertiesParser.Parse(code).ToArray();

            result.IsDrawingOnChartWindow = properties.Any(property => property.Name == "indicator_chart_window");

            var buffersProperty = properties.FirstOrDefault(property => property.Name == "indicator_buffers");
            if (buffersProperty != null)
                result.BuffersCount = int.Parse(buffersProperty.Value);

            var colorProperties = properties.Where(property => property.Name.StartsWith("indicator_color"));
            foreach (var property in colorProperties)
            {
                var index = int.Parse(property.Name["indicator_color".Length].ToString());
                result.Colors[index - 1] = GetKnownColor(property.Value);
            }

            var widthProperties = properties.Where(property => property.Name.StartsWith("indicator_width"));
            foreach (var property in widthProperties)
            {
                int index;
                int width;
                if (int.TryParse(property.Name["indicator_width".Length].ToString(), out index) 
                    && int.TryParse(property.Value, out  width))
                    result.Widths[index - 1] = width;
            }

            var levelProperties = properties.Where(property => property.Name.StartsWith("indicator_level"));
            foreach (var property in levelProperties)
            {
                double levelValue;
                if (double.TryParse(property.Value, out  levelValue))
                    result.Levels.Add(levelValue);
            }
        }
コード例 #8
0
ファイル: Mq4Parser.cs プロジェクト: matiasev/2calgo.Library
        private void HandleLevels(IEnumerable<MethodCall> methodCalls, Algo algo)
        {
            var setLevelCalls = methodCalls.Where(call => call.MethodName == "SetLevelValue");

            var levels = new Dictionary<int, double>();
            foreach (var methodCall in setLevelCalls)
            {
                var levelIndex = int.Parse(methodCall.Parameters[0]);
                double value;
                if (double.TryParse(methodCall.Parameters[1], out value))
                    levels[levelIndex] = value;
            }

            var levelValues = levels.Select(pair => pair.Value);
            algo.Levels.AddRange(levelValues);
        }
コード例 #9
0
ファイル: Mq4Parser.cs プロジェクト: matiasev/2calgo.Library
        private void HandleBufferIndexes(IEnumerable<MethodCall> methodCalls, Algo algo)
        {
            var setIndexBufferCalls = methodCalls.Where(call => call.MethodName == "SetIndexBuffer");

            var indexesBuffers = new Dictionary<int, string>();
            foreach (var methodCall in setIndexBufferCalls)
            {
                var index = int.Parse(methodCall.Parameters[0]);
                var value = methodCall.Parameters[1];

                indexesBuffers[index] = value;
            }

            algo.Buffers = indexesBuffers.OrderBy(pair => pair.Key).Select(pair => pair.Value).ToArray();
        }
コード例 #10
0
ファイル: Mq4Parser.cs プロジェクト: matiasev/2calgo.Library
        private static void HandleIndexStyles(MethodCall[] methodCalls, Algo algo, ParsingErrors parsingErrors)
        {
            var setIndexStyleCalls = methodCalls.Where(call => call.MethodName == "SetIndexStyle");
            var indexesStyles = new Dictionary<int, DrawingShapeStyle>();
            foreach (var methodCall in setIndexStyleCalls)
            {
                int index;
                if (!int.TryParse(methodCall.Parameters[0], out index))
                {
                    parsingErrors.Add(ErrorType.NotSupportedWarning, "SetIndexStyle", "Can't cast to int: " + methodCall.Parameters[0]);
                    continue;
                }
                var drawingShapeStyle = methodCall.Parameters[1].ToDrawingShapeStyle();
                indexesStyles[index] = drawingShapeStyle;

                if (methodCall.Parameters.Length >= 4)
                {
                    int width;
                    if (int.TryParse(methodCall.Parameters[3], out width))
                        algo.Widths[index] = width;
                }
            }
            foreach (var keyValuePair in indexesStyles)
            {
                algo.Styles[keyValuePair.Key] = keyValuePair.Value;
            }
        }
コード例 #11
0
        public string GenerateCodeFrom(Algo algo)
        {
            _algoBuilder.Mq4Code = algo.Mq4Code;
            _algoBuilder.AlgoName = GetMq4Name(algo.Mq4Code, algo.AlgoType);
            _algoBuilder.Fields = algo.Code.FieldsDeclarations;

            foreach (var parameter in algo.Parameters)
            {
                if (parameter.Type != "color")
                {
                    if (parameter.DefaultValue != null)
                        _algoBuilder.Parameters.AppendLine(string.Format("[Parameter(\"{0}\", DefaultValue = {1})]", parameter.Name, parameter.DefaultValue));
                    else
                        _algoBuilder.Parameters.AppendLine(string.Format("[Parameter(\"{0}\")]", parameter.Name));
                }
                _algoBuilder.Parameters.AppendLine(string.Format("public {0} {1} {2}", parameter.Type, parameter.Name + "_parameter", "{ get; set; }"));
                _algoBuilder.Parameters.AppendLine(string.Format("bool _{0}Got;", parameter.Name));
                _algoBuilder.Parameters.AppendLine(string.Format("{0} {1}_backfield;", parameter.BackfieldType, parameter.Name));
                _algoBuilder.Parameters.AppendLine(parameter.BackfieldType + " " + parameter.Name + " { get { if (!_" + parameter.Name + "Got) " + parameter.Name
                                              + "_backfield = " + parameter.Name + "_parameter; return " + parameter.Name + "_backfield;	} set { " + parameter.Name + "_backfield = value; } }");
                _algoBuilder.Parameters.AppendLine();
            }

            _algoBuilder.Mq4Functions = GetFunctions(algo.Code.Functions);
            foreach (var customIndicator in algo.CustomIndicators)
            {
                _algoBuilder.References.AppendLine(string.Format(@"//#reference: ..\Indicators\{0}.algo", customIndicator));
            }
#if DEBUG
            _algoBuilder.DebugActions.AppendLine("Debug.Activate();");
            _algoBuilder.DebugActions.AppendLine("Debug.Initialize(m => Print(m));");
            _algoBuilder.HandleException.AppendLine(@"

			var exceptionReportDir = @""D:\2calgo"";
			Directory.CreateDirectory(exceptionReportDir);
			var fileName = Path.Combine(exceptionReportDir, ""last exception.txt"");
			File.WriteAllText(fileName, e.ToString());
            ");
#endif

            _algoBuilder.ColorParameters.AppendFormat("int indicator_buffers = {0};\n", algo.BuffersCount);

            for (var index = 0; index < algo.Colors.Length; index++)
            {
                if (algo.Colors[index] != null)
                    _algoBuilder.ColorParameters.AppendFormat("int indicator_color{0} = {1};\n", index + 1, algo.Colors[index]);
            }
            for (var index = 0; index < algo.Buffers.Length; index++)
            {
                var buffer = algo.Buffers[index];

                AddLineDeclaration(algo, _algoBuilder, index, buffer);
                
                _algoBuilder.InvertedBuffersDeclarations.AppendFormat("private Mq4OutputDataSeries {0};\n", buffer);
                _algoBuilder.BuffersSetCurrentIndex.AppendFormat("{0}.SetCurrentIndex(index);\n", buffer);
                _algoBuilder.InitialzeBuffers.AppendFormat("if ({0}_AlgoOutputDataSeries == null) {0}_AlgoOutputDataSeries = CreateDataSeries();\n", buffer);

                var style = algo.Styles[index];
                if (style != DrawingShapeStyle.Arrow && style != DrawingShapeStyle.Histogram && !IsLineVisible(algo, index))
                    style =  DrawingShapeStyle.None;
                
                var color = algo.Colors[index] != null ? "Colors." + algo.Colors[index] : "null";
                var lineWidth = algo.Widths[index];

                _algoBuilder.InitialzeBuffers.AppendFormat("{0} = new Mq4OutputDataSeries(this, {0}_AlgoOutputDataSeries, ChartObjects, {1}, {2}, () => CreateDataSeries(), {3}, {4});\n", buffer, (int)style, index, lineWidth, color);
                _algoBuilder.InitialzeBuffers.AppendFormat("AllBuffers.Add({0});\n", buffer);
            }

            _algoBuilder.Levels = string.Join(", ", algo.Levels);
            for (var i = 0; i < algo.Levels.Count; i++)
            {
                _algoBuilder.LevelParameters.AppendLine(string.Format("Mq4Double indicator_level{0} = {1};", i + 1,
                                                                 algo.Levels[i]));
            }
            for (var i = 0; i < algo.Widths.Length; i++)
            {
                _algoBuilder.WidthParameters.AppendLine(string.Format("Mq4Double indicator_width{0} = {1};", i + 1,
                                                                 algo.Widths[i]));
            }
            _algoBuilder.IsDrawingOnChartWindow = algo.IsDrawingOnChartWindow ? "true" : "false";
           
            return _algoBuilder.Build();
        }
コード例 #12
0
 private static bool IsLineVisible(Algo algo, int bufferIndex)
 {
     return algo.Colors[bufferIndex] != null 
         && algo.Styles[bufferIndex] != DrawingShapeStyle.Arrow 
         && !(algo.IsDrawingOnChartWindow && algo.Styles[bufferIndex] == DrawingShapeStyle.Histogram);
 }
コード例 #13
0
        private static void AddLineDeclaration(Algo algo, AlgoBuilder template, int bufferIndex, string bufferName)
        {
            if (algo.Styles[bufferIndex] != DrawingShapeStyle.None && IsLineVisible(algo, bufferIndex))
            {
                var colorPart = string.Empty;
                if (algo.Colors[bufferIndex] != null)
                    colorPart = ", Color = Colors." + algo.Colors[bufferIndex];

                var plotTypePart = string.Empty;
                if (algo.Styles[bufferIndex] != DrawingShapeStyle.None)
                    plotTypePart = ", PlotType = PlotType." + algo.Styles[bufferIndex].ToPlotTypeString();

                template.LinesDeclarations.AppendFormat("[Output(\"{0}\"{1}{2})]\n", bufferName, colorPart, plotTypePart);
            }
            template.LinesDeclarations.AppendFormat("public IndicatorDataSeries {0}_AlgoOutputDataSeries {1}\n", bufferName,
                                                    "{ get; set; }");

            template.InitialzeAllOutputDataSeries.AppendLine(string.Format("AllOutputDataSeries.Add({0}_AlgoOutputDataSeries);", bufferName));
        }