private static void AddParameters(IndicatorSource indicatorSource, FunctionCall createIndicatorCall)
        {
            foreach (var param in indicatorSource.Parameters)
            {
                switch (param)
                {
                case BoolParameter boolParam:
                    createIndicatorCall.AddParameter(boolParam.Value ? "true" : "false");
                    break;

                case IntParameter intParam:
                    createIndicatorCall.AddParameter(intParam.Value.ToString());
                    break;

                case StringParameter stringParam:
                    createIndicatorCall.AddParameter($"\"{stringParam.Value}\"");
                    break;

                case ExternalParameter externalParam:
                    createIndicatorCall.AddParameter($"instance.parameters.{externalParam.Value}");
                    break;

                case DoubleParameter doubleParam:
                    createIndicatorCall.AddParameter(doubleParam.Value.ToLuaDoubleString());
                    break;

                default:
                    throw new NotImplementedException();
                }
            }
        }
 private void AddIndicatorToList(List <Indicator> list, IndicatorSource source)
 {
     list.Add(new Indicator()
     {
         Name         = source.Name.ToUpper(),
         VariableName = source.Id ?? sourceIdGenerator.Generate(source)
     });
 }
        private IndicatorValueViewModel BuildIndicatorTypeValue(Guid leadId, string serializedValue, IndicatorType type,
                                                                IndicatorSource dataSource)
        {
            var value = _serializer.Deserialize(type.ValueType, serializedValue);

            return(new IndicatorValueViewModel {
                LeadId = leadId, Value = value, Type = type, Source = dataSource
            });
        }
        public IMetaSource Create(Source source)
        {
            if (source == null)
            {
                return(new MainInstrumentSource());
            }

            switch (source.SourceType)
            {
            case Source.INDICATOR:
            {
                if (source.IndicatorSource == null)
                {
                    var sourceBuilder = new SourceParser(model);
                    if (source.IndicatorSourceId != null)
                    {
                        source.IndicatorSource = sourceBuilder.Parse(source.IndicatorSourceId);
                    }
                    else
                    {
                        source.IndicatorSource = new FormulaItem()
                        {
                            ValueType  = FormulaItem.STREAM,
                            StreamType = FormulaItem.INSTRUMENT
                        };
                    }
                }
                var newSource = new IndicatorSource()
                {
                    Name   = source.Name,
                    Id     = source.Id,
                    Source = Create(source.IndicatorSource)
                };
                if (source.Parameters != null)
                {
                    foreach (var param in source.Parameters)
                    {
                        newSource.Parameters.Add(MetaParameterFactory.Create(param));
                    }
                }
                return(newSource);
            }

            case Source.INSTRUMENT:
            {
                return(new InstrumentSource()
                    {
                        Id = source.Id,
                        PriceType = ParsePriceType(source.PriceType),
                        Timeframe = source.Timeframe
                    });
            }

            default:
                throw new NotImplementedException();
            }
        }
        public override void Assign(IndicatorBase ind)
        {
            base.Assign(ind);
            SingleValueIndicator svi = ind as SingleValueIndicator;

            if (svi != null)
            {
                Source = svi.Source;
            }
        }
 private IndicatorSource[] GetAllDataSources()
 {
     return(IndicatorSource.GetAll().OrderBy(x => x.DisplayName).ToArray());
 }
Exemplo n.º 7
0
        /// <summary>
        /// Generate code for indicator source.
        /// </summary>
        /// <param name="source">Source</param>
        /// <exception cref="NotSupportedSourceException">When indicator is not supported.</exception>
        /// <returns>Generated code</returns>
        public static string Generate(IndicatorSource source)
        {
            switch (source.Name.ToUpper())
            {
            case "MVA":
            {
                var period = GetIntParamValue(source.Parameters, "period", 7, 0);
                return($"{source.Id} = sma({FormatSource(source.Source, "close")}, {period})");
            }

            case "RSI":
            {
                var period = GetIntParamValue(source.Parameters, "period", 7, 0);
                return($"{source.Id} = rsi({FormatSource(source.Source, "close")}, {period})");
            }

            case "MACD":
            {
                var fastlen = GetIntParamValue(source.Parameters, "fastlen", 12, 0);
                var slowlen = GetIntParamValue(source.Parameters, "slowlen", 26, 1);
                var siglen  = GetIntParamValue(source.Parameters, "siglen", 9, 2);
                return($"[{source.Id}_macd, {source.Id}_signal, {source.Id}_hist] = macd({FormatSource(source.Source, "close")}, {fastlen}, {slowlen}, {siglen})");
            }

            case "STOCHASTIC":
            {
                var close   = FormatSource(source.Source, "close");
                var high    = FormatSource(source.Source, "high");
                var low     = FormatSource(source.Source, "low");
                var length  = GetIntParamValue(source.Parameters, "length", 3, 0);
                var periodK = GetIntParamValue(source.Parameters, "periodK", 5, 1);
                var periodD = GetIntParamValue(source.Parameters, "periodD", 3, 2);
                return(new StringBuilder()
                       .AppendLine($"{source.Id}_k = sma(stoch({close}, {high}, {low}, {length}), {periodK})")
                       .Append($"{source.Id}_d = sma({source.Id}_k, {periodD})")
                       .ToString());
            }

            case "BB":
            {
                var period = GetIntParamValue(source.Parameters, "period", 20, 0);
                var dev    = GetDoubleParamValue(source.Parameters, "dev", 2.0, 1);
                return(new StringBuilder()
                       .AppendLine($"{source.Id}_middle_band = sma(close, {period})")
                       .AppendLine($"{source.Id}_sma_deviation = {dev} * stdev(close, {period})")
                       .AppendLine($"{source.Id}_TL = {source.Id}_middle_band + {source.Id}_sma_deviation")
                       .Append($"{source.Id}_BL = {source.Id}_middle_band - {source.Id}_sma_deviation")
                       .ToString());
            }

            case "SAR":
            {
                var step = GetDoubleParamValue(source.Parameters, "step", 0.02, 0);
                var max  = GetDoubleParamValue(source.Parameters, "max", 0.2, 1);
                return($"{source.Id} = sar({step}, {step}, {max})");
            }

            case "ATR":
            {
                var period = GetIntParamValue(source.Parameters, "period", 14, 0);
                return($"{source.Id} = atr({period})");
            }

            case "CCI":
            {
                var period = GetIntParamValue(source.Parameters, "period", 14, 0);
                return($"{source.Id} = cci({FormatSource(source.Source, "close")}, {period})");
            }

            case "EMA":
            {
                var period = GetIntParamValue(source.Parameters, "period", 10, 0);
                return($"{source.Id} = ema({FormatSource(source.Source, "close")}, {period})");
            }

            case "SWMA":
            {
                return($"{source.Id} = swma({FormatSource(source.Source, "close")})");
            }

            case "TSI":
            {
                var shortPeriod = GetIntParamValue(source.Parameters, "shortPeriod", 7, 0);
                var longPeriod  = GetIntParamValue(source.Parameters, "longPeriod", 14, 0);
                return($"{source.Id} = tsi({FormatSource(source.Source, "close")}, {shortPeriod}, {longPeriod})");
            }

            case "VWAP":
            {
                return($"{source.Id} = vwap({FormatSource(source.Source, "close")})");
            }

            case "VWMA":
            {
                var period = GetIntParamValue(source.Parameters, "period", 15, 0);
                return($"{source.Id} = vwma({FormatSource(source.Source, "close")}, {period})");
            }

            case "WMA":
            {
                var period = GetIntParamValue(source.Parameters, "period", 14, 0);
                return($"{source.Id} = wma({FormatSource(source.Source, "close")}, {period})");
            }
            }
            throw new NotSupportedSourceException($"Indicator {source.Name} is not supporeted in Pine Script yet");
        }