Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 Aviram Y, Today, 05:29 AM
        0 responses
        1 view
        0 likes
        Last Post Aviram Y  
        Started by quantismo, 04-17-2024, 05:13 PM
        3 responses
        25 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by ScottWalsh, 04-16-2024, 04:29 PM
        7 responses
        34 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by cls71, Today, 04:45 AM
        0 responses
        6 views
        0 likes
        Last Post cls71
        by cls71
         
        Started by mjairg, 07-20-2023, 11:57 PM
        3 responses
        216 views
        1 like
        Last Post PaulMohn  
        Working...
        X