// CandleBuilders call this function when registered with them
        // and a new candle is built
        // one distinguished calling CandleBuilder by handle in candle and period
        public override void ReceiveCandle(sCandle pCandle, int pPeriod, string pCBTitle)
        {
            if ((pPeriod == Minutes) && (pCBTitle == "cbx"))
            {
                if (cbx.candlecount > PeriodsLong + PeriodsShort)
                {
                    MaxAbsDeriv = -1.0E-25;

                    double  Sum;
                    sCandle Candle;

                    Sum = 0;
                    for (int i = 1; i < PeriodsLong + 1; i++)
                    {
                        Candle = cbx.GetCandle(i);
                        double a = (Candle.H + Candle.L) / 2;
                        Sum += a;

                        Candle = cbx.GetCandle(i + 1);
                        double b = (Candle.H + Candle.L) / 2;

                        Candle = cbx.GetCandle(i + 2);
                        double c = (Candle.H + Candle.L) / 2;

                        double deriv2 = Math.Abs(a - 2 * b + c);
                        if (deriv2 > MaxAbsDeriv)
                        {
                            MaxAbsDeriv = deriv2;
                        }

                        if (i == 1)
                        {
                            Deriv2 = deriv2;
                        }
                    }

                    AvgLong = Sum / PeriodsLong;

                    Sum = 0;
                    for (int i = PeriodsLong + 1; i < PeriodsLong + PeriodsShort + 1; i++)
                    {
                        Candle = cbx.GetCandle(i);
                        Sum   += (Candle.H + Candle.L) / 2;
                    }

                    AvgShort = Sum / PeriodsShort;

                    // after that, call decision function
                    DecisionFunction();
                }
            }
        }
        // decide if we are entering or exiting a long or short trade
        // different criteria can be used for each condition
        private void DecisionFunction()
        {
            switch (Framework.Account.InTrade())
            {
            // if we are not in a trade, see to enter long or short
            case 0:
            {
                // seems like a repetitive check
                if (Framework.Account.InTrade() == 0)
                {
                    if (EntryCriteriaLong())
                    {
                        Framework.Account.EnterTrade(1);
                        Framework.Account.SetTrailingStop(0.0018);
                    }
                }

                // now is not a repetitive check since
                // EntryCriteriaLong might have entered a trade
                // so check again
                if (Framework.Account.InTrade() == 0)
                {
                    if (EntryCriteriaShort())
                    {
                        Framework.Account.EnterTrade(1);
                        Framework.Account.SetTrailingStop(0.0018);
                    }
                }

                break;
            }

            // if we are currently long, look for a long exit strategy
            case 1:
            {
                if (EntryCriteriaShort())
                {
                    Framework.Account.ExitTrade();
                    //Framework.Account.EnterTrade(-1);
                }
                break;
            }

            // if we are currently short, look for a short exit strategy
            case -1:
            {
                if (EntryCriteriaLong())
                {
                    Framework.Account.ExitTrade();
                    //Framework.Account.EnterTrade(1);
                }
                break;
            }
            }

            // extract values from indicators and account
            // and log them to a file (used to create a graph with a separate app)
            double logInTrade = Framework.Account.InTrade();
            double logMargin  = Framework.Account.GetAccount().C;
            double C          = cbx.GetCandle(0).C;
            double TP         = (cbx.GetCandle(0).C + cbx.GetCandle(0).H + cbx.GetCandle(0).L) / 3;
            double hma        = HMA.Value();
            double fma        = FMA.Value();
            double deriv1     = ((double)Deriv1.GetItem(0));
            double deriv2     = ((double)Deriv2.GetItem(0));
            double sma;
            double bband1;
            double bband2;
            double pb;
            double bw;
            double srsi = StochRSI.Value();
            double cci  = CCI.Value();

            BBands.Value(out bband1, out sma, out bband2, out pb, out bw);

            // write monitored valued to the numerical output file
            // Framework.WriteGraphLine("InTrade,Margin,C,TP,FMA,HMA,Deriv1,Deriv2,SMA,BBand1,BBand2,%b,Bandwidth,StochRSI,CCI");
            Framework.WriteGraphLine(logInTrade + "," + logMargin + "," + C + "," + TP + "," + fma + "," + hma + "," + deriv1 + "," + deriv2 + "," + sma + "," + bband1 + "," + bband2 + "," + pb + "," + bw + "," + srsi + "," + cci);
        }