Exemple #1
0
        protected virtual IndicatorContext CreateIndicatorContext(DateTime dateUnderAnalysis)
        {
            var context = new IndicatorContext();

            context.DateUnderAnalysis = dateUnderAnalysis;

            return(context);
        }
        public bool IsSatifiedBy(Stock stock)
        {
            if (Expression == null)
            {
                return false;
            }

            var indicators = Expression.Indicators == null ? new List<IIndicator>() : Expression.Indicators.ToList();
            if (indicators.Count == 0)
            {
                return Expression.Evaluator.Evaluate().Equals(1);
            }

            var indicatorcontext = new IndicatorContext(stock.Id, CutoffTime);
            indicators.ForEach(i => i.Context = indicatorcontext);
            var param = indicators.Select(i => new ExpressionParam(i.Name, i.GetValue())).ToArray();
            return Expression.Evaluator.Evaluate(param).Equals(1);
        }
Exemple #3
0
    public IndicatorResult GetResult(IndicatorContext context)
    {
        try {
            IPriceInstrument instrument = context.PriceInstrument; // Its pre-created for speed based on the "TicksBack" property field
            int      position = instrument.Close.Count() - (1 + context.Offset);
            int      outBegIdx, outNBElement;
            double[] outMAMA = new double[position];
            double[] outFAMA = new double[position];

            double[] o           = new double[position];
            double[] c           = new double[position];
            double[] h           = new double[position];
            double[] l           = new double[position];
            double[] hl2         = new double[position];
            double[] hlc3        = new double[position];
            double[] ohlc4       = new double[position];
            double[] priceSource = new double[position];

            for (int i = 0; i < position; i++)
            {
                o[i]     = instrument.Open[i];
                c[i]     = instrument.Close[i];
                h[i]     = instrument.High[i];
                l[i]     = instrument.Low[i];
                hl2[i]   = (instrument.High[i] + instrument.Low[i]) / 2;
                hlc3[i]  = (instrument.High[i] + instrument.Low[i] + instrument.Close[i]) / 3;
                ohlc4[i] = (instrument.Open[i] + instrument.High[i] + instrument.Low[i] + instrument.Close[i]) / 4;
            }

            // priceSource = (double[])this.GetType().GetField(sourceConfig).GetValue(this);
            // is not working. take this instead until i found a solution:

            if (sourceConfig == "o")
            {
                priceSource = o;
            }
            else
            if (sourceConfig == "c")
            {
                priceSource = c;
            }
            else
            if (sourceConfig == "h")
            {
                priceSource = h;
            }
            else
            if (sourceConfig == "l")
            {
                priceSource = l;
            }
            else
            if (sourceConfig == "hl2")
            {
                priceSource = hl2;
            }
            else
            if (sourceConfig == "hlc3")
            {
                priceSource = hlc3;
            }
            else
            if (sourceConfig == "ohlc4")
            {
                priceSource = ohlc4;
            }

            var mamaReturnCode = Core.Mama(0, position - 1, priceSource, fastLimit, slowLimit, out outBegIdx, out outNBElement, outMAMA, outFAMA);
            //context.Logger.LogToFile("outNBElement: " + outNBElement); //Log to file

            if (mamaReturnCode == Core.RetCode.Success)
            {
                if (outNBElement == 0)
                {
                    // Something went wrong
                    mama = instrument.Close[position - 1];
                    fama = instrument.Close[position - 1];
                    return(IndicatorResult.StayNoPosition);
                }
                else
                {
                    mama = outMAMA[outNBElement - 1]; //Take current MAMA (last one of the valid values)
                    fama = outFAMA[outNBElement - 1]; //Take current FAMA (last one of the valid values)

                    //context.Logger.LogToFile("MAMA: " + mama + "\tFAMA:" + fama); //Log to file


                    //Determine indicator signal
                    if (mama > fama)
                    {
                        //context.Logger.LogToFile("Buying at " + instrument.Close + "\tMAMA: " + mama + "\tFAMA:" + fama); //Log to file
                        return(IndicatorResult.BuyLong);
                    }
                    else if (fama > mama)
                    {
                        //context.Logger.LogToFile("Selling at " + instrument.Close + "\tMAMA: " + mama + "\tFAMA:" + fama); //Log to file
                        return(IndicatorResult.SellShort);
                    }
                    else
                    {
                        return(IndicatorResult.StayNoPosition);
                    }
                }
            }
        }
        catch (Exception ex) // This try catch block is an highly recommend practice
        {
            context.Logger.LogToFile(string.Format("Exception detected: {0}", ex.Message));
        }

        return(IndicatorResult.StayNoPosition); // Default output
    }
Exemple #4
0
 public IndicatorsController(IndicatorContext context)
 {
     _context = context;
 }