예제 #1
0
파일: Sig5.cs 프로젝트: sprgn/LeanITrend
        public OrderSignal CheckSignal(TradeBars data, IndicatorDataPoint trendCurrent, out string comment)
        {
            OrderSignal retval = OrderSignal.doNothing;

            comment = string.Empty;
            decimal price = (data[symbol].Close + data[symbol].Open) / 2;

            trend.Update(idp(data[symbol].EndTime, price));
            if (trendCurrent.Value != trend.Current.Value)
            {
                comment = "not equal";
            }
            UpdateTrendArray(trend.Current);
            bReverseTrade  = false;
            v.ReverseTrade = false;

            v.NTrigGTEP   = false;
            v.NTrigLTEP   = false;
            v.NTrigGTTA0  = false;
            v.NTrigLTTA0  = false;
            v.OrderFilled = true;
            v.IsLong      = IsLong;
            v.IsShort     = IsShort;
            v.BarcountLT4 = false;

            if (Barcount < 4)
            {
                v.BarcountLT4 = true;
                comment       = "Barcount < 4";
                retval        = OrderSignal.doNothing;
            }
            else
            {
                nTrig = 2 * trendArray[0] - trendArray[2];


                #region "Selection Logic Reversals"

                try
                {
                    if (nTrig < (Math.Abs(nEntryPrice) / RevPct))
                    {
                        v.NTrigLTEP = true;
                        if (IsLong)
                        {
                            retval         = OrderSignal.revertToShort;
                            bReverseTrade  = true;
                            v.ReverseTrade = true;
                            comment        =
                                string.Format("nTrig {0} < (nEntryPrice {1} * RevPct {2}) {3} IsLong {4} )",
                                              Math.Round(nTrig, 4),
                                              nEntryPrice,
                                              RevPct,
                                              v.NTrigLTEP,
                                              IsLong);
                        }
                        else
                        {
                            v.NTrigLTEP = false;
                        }
                    }
                    else
                    {
                        if (nTrig > (Math.Abs(nEntryPrice) * RevPct))
                        {
                            v.NTrigGTEP = true;
                            if (IsShort)
                            {
                                retval         = OrderSignal.revertToLong;
                                bReverseTrade  = true;
                                v.ReverseTrade = true;
                                comment        =
                                    string.Format("nTrig {0} > (nEntryPrice {1} * RevPct {2}) {3} IsLong {4} )",
                                                  Math.Round(nTrig, 4),
                                                  nEntryPrice,
                                                  RevPct,
                                                  v.NTrigLTEP,
                                                  IsLong);
                            }
                            else
                            {
                                v.NTrigGTEP = false;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.StackTrace);
                }

                #endregion
                #region "selection logic buy/sell"

                try
                {
                    if (!bReverseTrade)
                    {
                        if (nTrig > trendArray[0])
                        {
                            v.NTrigGTTA0 = true;
                            if (xOver == -1)
                            {
                                if (!IsLong)
                                {
                                    if (!orderFilled)
                                    {
                                        v.OrderFilled = false;
                                        retval        = OrderSignal.goLong;
                                        comment       =
                                            string.Format(
                                                "nTrig {0} > trend {1} xOver {2} !IsLong {3} !orderFilled {4}",
                                                Math.Round(nTrig, 4),
                                                Math.Round(trendArray[0], 4),
                                                xOver,
                                                !IsLong,
                                                !orderFilled);
                                    }
                                    else
                                    {
                                        retval  = OrderSignal.goLongLimit;
                                        comment =
                                            string.Format("nTrig {0} > trend {1} xOver {2} !IsLong {3} !orderFilled {4}",
                                                          Math.Round(nTrig, 4),
                                                          Math.Round(trendArray[0], 4),
                                                          xOver,
                                                          !IsLong,
                                                          !orderFilled);
                                    }
                                }
                            }

                            if (comment.Length == 0)
                            {
                                comment = "Trigger over trend - setting xOver to 1";
                            }
                            xOver             = 1;
                            v.xOverisNegative = xOver < 0;
                            v.xOverIsPositive = xOver > 0;
                        }
                        else
                        {
                            if (nTrig < trendArray[0])
                            {
                                v.NTrigLTTA0 = true;
                                if (xOver == 1)
                                {
                                    if (!IsShort)
                                    {
                                        if (!orderFilled)
                                        {
                                            v.OrderFilled = false;
                                            retval        = OrderSignal.goShort;
                                            comment       =
                                                string.Format(
                                                    "nTrig {0} < trend {1} xOver {2} !isShort {3} orderFilled {4}",
                                                    Math.Round(nTrig, 4),
                                                    Math.Round(trendArray[0], 4),
                                                    xOver,
                                                    !IsShort,
                                                    !orderFilled);
                                        }
                                        else
                                        {
                                            retval  = OrderSignal.goShortLimit;
                                            comment =
                                                string.Format(
                                                    "nTrig {0} < trend {1} xOver {2} !isShort {3} orderFilled {4}",
                                                    Math.Round(nTrig, 4),
                                                    Math.Round(trendArray[0], 4),
                                                    xOver,
                                                    !IsShort,
                                                    !orderFilled);
                                        }
                                    }
                                }
                                if (comment.Length == 0)
                                {
                                    comment = "Trigger under trend - setting xOver to -1";
                                }
                                xOver             = -1;
                                v.xOverisNegative = xOver < 0;
                                v.xOverIsPositive = xOver > 0;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.StackTrace);
                }

                #endregion
            }
            StringBuilder sb = new StringBuilder();
            sb.Append(comment);
            sb.Append(",");
            sb.Append(retval.ToString());
            sb.Append(",");
            sb.Append(v.ToInt32());
            sb.Append(",");
            sb.Append(v.ToIntCsv());
            comment = sb.ToString();
            return(retval);
        }
예제 #2
0
        public OrderSignal CheckSignal(TradeBars data, IndicatorDataPoint trendCurrent, out string comment)
        {
            OrderSignal retval = OrderSignal.doNothing;

            comment = string.Empty;
            //decimal price = (data[symbol].Close + data[symbol].Open) / 2;

            // Here is an attempt to smooth the price
            //ema.Update(idp(data[symbol].EndTime, price));
            //if (ema.IsReady)
            //    UpdatePriceArray(ema.Current);
            //else
            //    UpdatePriceArray(idp(data[symbol].EndTime, price));
            //if (Barcount == 17)
            //    comment = "";
            //// Update the trend with the smoothed price
            //trend.Update(ema.Current);
            //trend.Current.Symbol = data[symbol].Symbol; // make sure the symbol is correct

            //if (trend.Current.Value != trendCurrent.Value)
            //    comment = "trends not equal";
            UpdateTrendArray(trendCurrent.Value);

            bReverseTrade = false;
            ReverseTrade  = false;

            NTrigGTEP   = false;
            NTrigLTEP   = false;
            NTrigGTTA0  = false;
            NTrigLTTA0  = false;
            BarcountLT4 = false;

            if (Barcount < 4)
            {
                BarcountLT4 = true;
                comment     = "Barcount < 4";
                retval      = OrderSignal.doNothing;
            }
            else
            {
                nTrig = 2m * trendArray[0] - trendArray[2];


                #region "Selection Logic Reversals"

                try
                {
                    if (nTrig < (Math.Abs(nEntryPrice) / RevPct))
                    {
                        NTrigLTEP = true;
                        if (IsLong)
                        {
                            retval        = OrderSignal.revertToShort;
                            bReverseTrade = true;
                            ReverseTrade  = true;
                            comment       =
                                string.Format("nTrig {0} < (nEntryPrice {1} * RevPct {2}) {3} IsLong {4} )",
                                              Math.Round(nTrig, 4),
                                              nEntryPrice,
                                              RevPct,
                                              NTrigLTEP,
                                              IsLong);
                        }
                        else
                        {
                            NTrigLTEP = false;
                        }
                    }
                    else
                    {
                        if (nTrig > (Math.Abs(nEntryPrice) * RevPct))
                        {
                            NTrigGTEP = true;
                            if (IsShort)
                            {
                                retval        = OrderSignal.revertToLong;
                                bReverseTrade = true;
                                ReverseTrade  = true;
                                comment       =
                                    string.Format("nTrig {0} > (nEntryPrice {1} * RevPct {2}) {3} IsLong {4} )",
                                                  Math.Round(nTrig, 4),
                                                  nEntryPrice,
                                                  RevPct,
                                                  NTrigLTEP,
                                                  IsLong);
                            }
                            else
                            {
                                NTrigGTEP = false;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.StackTrace);
                }

                #endregion
                #region "selection logic buy/sell"

                try
                {
                    if (!bReverseTrade)
                    {
                        if (nTrig > trendArray[0])
                        {
                            NTrigGTTA0 = true;
                            if (xOver == -1)
                            {
                                #region "If Not Long"
                                if (!IsLong)
                                {
                                    if (!orderFilled)
                                    {
                                        OrderFilled = false;
                                        retval      = OrderSignal.goLong;
                                        comment     =
                                            string.Format(
                                                "nTrig {0} > trend {1} xOver {2} !IsLong {3} !orderFilled {4}",
                                                Math.Round(nTrig, 4),
                                                Math.Round(trendArray[0], 4),
                                                xOver,
                                                !IsLong,
                                                !orderFilled);
                                    }
                                    else
                                    {
                                        retval  = OrderSignal.goLongLimit;
                                        comment =
                                            string.Format(
                                                "nTrig {0} > trend {1} xOver {2} !IsLong {3} !orderFilled {4}",
                                                Math.Round(nTrig, 4),
                                                Math.Round(trendArray[0], 4),
                                                xOver,
                                                !IsLong,
                                                !orderFilled);
                                    }
                                }
                                #endregion
                            }

                            if (comment.Length == 0)
                            {
                                comment = "Trigger over trend - setting xOver to 1";
                            }
                            xOver           = 1;
                            xOverisNegative = xOver < 0;
                            xOverIsPositive = xOver > 0;
                        }
                        else
                        {
                            if (nTrig < trendArray[0])
                            {
                                NTrigLTTA0 = true;
                                if (xOver == 1)
                                {
                                    #region "If Not Short"
                                    if (!IsShort)
                                    {
                                        if (!orderFilled)
                                        {
                                            OrderFilled = false;
                                            retval      = OrderSignal.goShort;
                                            comment     =
                                                string.Format(
                                                    "nTrig {0} < trend {1} xOver {2} !isShort {3} orderFilled {4}",
                                                    Math.Round(nTrig, 4),
                                                    Math.Round(trendArray[0], 4),
                                                    xOver,
                                                    !IsShort,
                                                    !orderFilled);
                                        }
                                        else
                                        {
                                            retval  = OrderSignal.goShortLimit;
                                            comment =
                                                string.Format(
                                                    "nTrig {0} < trend {1} xOver {2} !isShort {3} orderFilled {4}",
                                                    Math.Round(nTrig, 4),
                                                    Math.Round(trendArray[0], 4),
                                                    xOver,
                                                    !IsShort,
                                                    !orderFilled);
                                        }
                                    }
                                    #endregion
                                }
                                if (comment.Length == 0)
                                {
                                    comment = "Trigger under trend - setting xOver to -1";
                                }
                                xOver           = -1;
                                xOverisNegative = xOver < 0;
                                xOverIsPositive = xOver > 0;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.StackTrace);
                }

                #endregion
            }
            StringBuilder sb = new StringBuilder();
            sb.Append(comment);
            sb.Append(",");
            sb.Append(retval.ToString());
            sb.Append(",");
            //sb.Append(ToInt32());
            sb.Append(",");
            //sb.Append(ToIntCsv());
            comment = sb.ToString();
            return(retval);
        }