Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Getting a Bug on One Line Only: Any Suggestions to Fix?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Getting a Bug on One Line Only: Any Suggestions to Fix?

    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Identify divergence of selected indicator with Price
        /// </summary>
        [Description("Identify divergence of selected indicator with Price. I use CCI, MACD & Ergodic indicator, two indicators forming divergence provides confirmation")]
        public class DivergencePatternTrailingStops : Strategy
        {
            #region Variables
            // Wizard generated variables
            private string positiveD = @"YES"; // Default setting for PositiveD
            //private string indicatorType = @"MACD"; // Default setting for IndicatorType
            private int macdfastPeriod = 12; // Default setting for FastPeriod
            private int macdslowPeriod = 26; // Default setting for SlowPeriod
            private int macdsignalPeriod = 9; // Default setting for SignalPeriod
            private int ergodicfastPeriod = 20; // Default setting for Ergodic FastPeriod
            private int ergodicslowPeriod = 12; // Default setting for Ergodic SlowPeriod	
            private int cciPeriod = 40; // Default setting for CCI Period		
            private double stopLossPct = 0.020; // Default setting for StopLossPct
            private double trailingStopPct = 0.020; // Default setting for TrailingStopPct		
            // User defined variables (add any user defined variables below)
    		private double trailStop = 0; // Default setting for TrailStop
    		private double StopLoss = 0;
            private double prevValue = 0; // Default setting for PrevValue
            private bool justEntered = false; // Default setting for JustEntered		
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
    
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Condition set 1 - Positive Divergence
                if (((Divergence(ergodicfastPeriod, "ergodic", positiveD, 1,ergodicslowPeriod).Plot0[0] == 2 && Divergence(cciPeriod, "cci", positiveD, 9,26).Plot0[0] == 2) ||
    				(Divergence(ergodicfastPeriod, "ergodic", positiveD, 1,ergodicslowPeriod).Plot0[0] == 2 && Divergence(macdfastPeriod, "macd", positiveD, macdsignalPeriod,macdslowPeriod).Plot0[0] == 2) ||
    				(Divergence(cciPeriod, "cci", positiveD, 9,26).Plot0[0] == 2 && Divergence(macdfastPeriod, "macd", positiveD, macdsignalPeriod,macdslowPeriod).Plot0[0] == 2)) &&
    				Position.MarketPosition == MarketPosition.Flat)
                {
                    EnterLong(1, "Long");
    				justEntered = true;
                }
    			
    		}	
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
    
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate(
    		
    			
    		[COLOR="Red"]{[/COLOR]
                // Condition set 1 - Positive Divergence
                if (((Divergence(ergodicfastPeriod, "ergodic", positiveD, 1,ergodicslowPeriod).Plot0[0] == 2 && Divergence(cciPeriod, "cci", positiveD, 9,26).Plot0[0] == 2) ||
    				(Divergence(ergodicfastPeriod, "ergodic", positiveD, 1,ergodicslowPeriod).Plot0[0] == 2 && Divergence(macdfastPeriod, "macd", positiveD, macdsignalPeriod,macdslowPeriod).Plot0[0] == 2) ||
    				(Divergence(cciPeriod, "cci", positiveD, 9,26).Plot0[0] == 2 && Divergence(macdfastPeriod, "macd", positiveD, macdsignalPeriod,macdslowPeriod).Plot0[0] == 2)) &&
    				Position.MarketPosition == MarketPosition.Flat)
                {
                    EnterLong(1, "Long");
    				justEntered = true;
                }
    			
    			if (justEntered = true)
    			{
                SetTrailStop("", CalculationMode.Ticks, 8, false);
    
                CalculateOnBarClose = true;
    			}
    		)	
    				
    
    			
    			
    			
               
    			// Condition set 2 - Negative divergence (DOESNT ACTUALLY TRADE ANYTHING THOUGH)
                if (((Divergence(ergodicfastPeriod, "ergodic", positiveD, 1,ergodicslowPeriod).Plot0[0] == -2 && Divergence(cciPeriod, "cci", positiveD, 9,26).Plot0[0] == -2) ||
    				(Divergence(ergodicfastPeriod, "ergodic", positiveD, 1,ergodicslowPeriod).Plot0[0] == -2 && Divergence(macdfastPeriod, "macd", positiveD, macdsignalPeriod,macdslowPeriod).Plot0[0] == -2) ||
    				(Divergence(cciPeriod, "cci", positiveD, 9,26).Plot0[0] == -2 && Divergence(macdfastPeriod, "macd", positiveD, macdsignalPeriod,macdslowPeriod).Plot0[0] == -2)) &&
    				Position.MarketPosition == MarketPosition.Flat)
                {
                    EnterShort(0, "Short");
    				justEntered = true;
                }
    			if (Position.MarketPosition == MarketPosition.Short && justEntered == true)
    			{
    				trailStop = Position.AvgPrice + (trailingStopPct * Position.AvgPrice);
    				StopLoss = Position.AvgPrice + (stopLossPct * Position.AvgPrice);
        			prevValue = Position.AvgPrice;
        			justEntered = false;
        			Print(" TRAIL STOP TRACKING: " + trailStop + " symbol " + Instrument.FullName);
    			}
    			if (Position.MarketPosition == MarketPosition.Short)
    			{
    				if (Low[0] < Position.AvgPrice && Low[0] < prevValue)
    				{
    					trailStop = trailStop - (prevValue - Low[0]);
           				prevValue = Low[0];
           				Print(" TRAIL STOP Lowered: " + trailStop + " PrevValue " + prevValue + " symbol " + Instrument.FullName);
    				}
    				Print(Time[0] + " Low " + Low[0] + " PrevValue " + prevValue + " symbol " + Instrument.FullName);
    			}			
    			if (High[0] >= trailStop && Position.MarketPosition == MarketPosition.Short && Position.AvgPrice > trailStop)
    			{
    				Print(" TRAIL STOP HIT: " + trailStop + " " + Close[0] + " symbol " + Instrument.FullName);
        			// Trailing stop has been hit; do whatever you want here
    				ExitShort(0,"Trailing Stop" ,"Short");
       				trailStop = 0;
    				StopLoss = 0;
        			prevValue = 0;
    			}
    			else if (High[0] >= StopLoss && Position.MarketPosition == MarketPosition.Short && Position.AvgPrice < StopLoss)
    			{
    				Print(" STOP LOSS HIT: " + StopLoss + " " + Close[0] + " symbol " + Instrument.FullName);
    				// Trailing stop has been hit; do whatever you want here
    				ExitShort(0,"Stop Loss" ,"Short");
    				trailStop = 0;
    				StopLoss = 0;
    				prevValue = 0;					
    			}			
            }
    
            #region Properties
            [Description("Enter 'YES' for positive divergence and 'NO' for negative divergence")]
            [Category("Parameters")]
            public string PositiveD
            {
                get { return positiveD; }
                set { positiveD = value; }
            }
    
    //        [Description("MACD,Ergodic,FStoch,StochRSI,CCI,BOP,CMO,ChaikinMoneyFlow,Momentum,MFI,RSI,ROC")]
    //        [Category("Parameters")]
    //        public string IndicatorType
    //        {
    //            get { return indicatorType; }
    //            set { indicatorType = value; }
    //        }
    
            [Description("Used only on the MACD indicator")]
            [Category("Parameters")]
            public int MACDFastPeriod
            {
                get { return macdfastPeriod; }
                set { macdfastPeriod = Math.Max(1, value); }
            }
    
            [Description("Used only on the MACD indicator")]
            [Category("Parameters")]
            public int MACDSlowPeriod
            {
                get { return macdslowPeriod; }
                set { macdslowPeriod = Math.Max(1, value); }
            }
    
            [Description("Used only on the MACD indicator")]
            [Category("Parameters")]
            public int MACDSignalPeriod
            {
                get { return macdsignalPeriod; }
                set { macdsignalPeriod = Math.Max(0, value); }
            }		
    		
            [Description("Used only on the Ergodic indicator")]
            [Category("Parameters")]
            public int ErgodicFastPeriod
            {
                get { return ergodicfastPeriod; }
                set { ergodicfastPeriod = Math.Max(1, value); }
            }
    
            [Description("Used only on the Ergodic indicator")]
            [Category("Parameters")]
            public int ErgodicSlowPeriod
            {
                get { return ergodicslowPeriod; }
                set { ergodicslowPeriod = Math.Max(1, value); }
            }		
    
    		[Description("Used only on the CCI indicator")]
            [Category("Parameters")]
            public int CCIPeriod
            {
                get { return cciPeriod; }
                set { cciPeriod = Math.Max(1, value); }
            }
    		
            [Description("")]
            [Category("Parameters")]
            public double StopLossPct
            {
                get { return stopLossPct; }
                set { stopLossPct = Math.Max(0.010, value); }
            }
    
            [Description("")]
            [Category("Parameters")]
            public double TrailingStopPct
            {
                get { return trailingStopPct; }
                set { trailingStopPct = Math.Max(0.010, value); }
            }		
            #endregion
        }
    }
    all seems well, except for line 83. It gives me a "Type Expected" error. All there is on that line is a close curly bracket. Any advice?

    #2
    Hello outstretchedarm,

    You have multiple OnBarUpdate() and Initialize() handlers in your script. Can only have/need 1.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      thanks a bunch I'll give that a try

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      627 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      359 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      105 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      562 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      567 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X