Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with error

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

    Help with error

    Hello,

    Can anyone help me solve these two errors? I can't seem to correct them.

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < BarsRequiredToTrade)
    return;

    // Set 1
    if (((CrossAbove(HMA1, EMA1, 1))
    && (CrossAbove(TSSuperTrend1.UpTrend, TSSuperTrend1.DownTrend, 1))
    && (Position.MarketPosition == MarketPosition.Flat)))
    Draw.ArrowUp(this, @"Buy"+CurrentBar, false, 0, (Low[0] + (-10 * TickSize)), Brushes.Lime);
    EnterLong();

    else if (((CrossBelow(HMA2, EMA2, 1))
    && (CrossBelow(TSSuperTrend1.UpTrend, TSSuperTrend1.DownTrend, 1))
    && (Position.MarketPosition == MarketPosition.Flat)))
    Draw.ArrowDown(this, @"Sell"+CurrentBar, false, 0, (High[0] + (10 * TickSize)), Brushes.Red);
    EnterShort();
    }

    #region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="TSPeriod", Order=1, GroupName="Parameters")]
    public int TSPeriod
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, double.MaxValue)]
    [Display(Name="Multiplier", Order=2, GroupName="Parameters")]
    public double Multiplier
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="Smooth", Order=3, GroupName="Parameters")]
    public int Smooth
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="EMAPeriod", Order=4, GroupName="Parameters")]
    public int EMAPeriod
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="HMAPeriod", Order=5, GroupName="Parameters")]
    public int HMAPeriod
    { get; set; }
    #endregion

    }
    }
    Errors are:
    1) Invalid expression term 'else' CS1525 Line 112 Column 4
    2) ; expected CS1002 Line 112 Column 9

    I'm getting a red marker after EnterLong();

    Any help will be great,
    Thanks,
    Chris

    #2
    Hello chrisca,
    Thanks for your post.

    In the code you provided it appears that you are missing your curly braces after your "if" and "else if" statements. When creating a condition with more than one action you must use curly braces.

    I have modified the sample you provided and have marked the curly braces in bold that you will need to add to your code.

    Code:
    			// Set 1
    			if (((CrossAbove(HMA1, EMA1, 1))
    			&& (CrossAbove(TSSuperTrend1.UpTrend, TSSuperTrend1.DownTrend, 1))
    			&& (Position.MarketPosition == MarketPosition.Flat)))
    			[B][SIZE="5"]{[/SIZE][/B]
    				Draw.ArrowUp(this, @"Buy"+CurrentBar, false, 0, (Low[0] + (-10 * TickSize)), Brushes.Lime);
    				EnterLong();
    			[B][SIZE="5"]{[/SIZE][/B]
    			else if (((CrossBelow(HMA2, EMA2, 1))
    			&& (CrossBelow(TSSuperTrend1.UpTrend, TSSuperTrend1.DownTrend, 1))
    			&& (Position.MarketPosition == MarketPosition.Flat)))
    			[B][SIZE="5"]{[/SIZE][/B]
    				Draw.ArrowDown(this, @"Sell"+CurrentBar, false, 0, (High[0] + (10 * TickSize)), Brushes.Red);
    				EnterShort();
    			[B][SIZE="5"]{[/SIZE][/B]
    Please let me know if you have any further questions.
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Thanks Josh,

      I actually took those out as I was trying to 'copy' the SampleMACrossOver strategy, which is done without the curly braces. By adding these, will this change the order management behavior? I am wanting the behavior to be the same as the SampleMA strategy.

      Thanks,
      Chris

      Comment


        #4
        Hello Chris,
        Thanks for writing back.

        On the contrary, your script will not work unless you add those braces.
        In the SampleMACrossOver strategy there is only one action for those conditions, so no braces are required. Your "if" and "else if" statements have two actions so curly braces are required. Anything that happens inside of an "if" statement would count as an action.

        I am including the publicly available link to the msdn page for ""if-else" statements. There you will see several examples that demonstrate how to use them.
        https://docs.microsoft.com/en-us/dot...ywords/if-else

        Please let me know if you have any further questions.
        Josh G.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        52 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        130 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        70 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        44 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        49 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X