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

Multi-Instrument

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

    Multi-Instrument

    Hi,
    I am trying to write a strategy to take reading from an index, "^comp" to give me a value in Variable2, and a future what will store a value in Variable3. And then on the primary instruments, base on the reading and value in Variable0 and evaluate values in Variable2 and variable3, I will take either long or short.

    Although, I am having trouble to have the script to run. Please help.

    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// Enter the description of your strategy here
    /// </summary>
    [Description("Enter the description of your strategy here")]
    public class MyCustomStrategy1 : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int longValue = 1; // Default setting for MyInput0
    private int shortValue = -1; // Default setting for ShortValue
    private int stopValue = 0; // Default setting for StopValue
    private double stoploss = 0.100; // Default setting for Stoploss
    private int quit = 0; // Default setting for Quit
    // User defined variables (add any user defined variables below)
    #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()
    {
    Add(DEMA(2));
    Add(TEMA(1));
    Add(DEMA(2));
    Add(TEMA(1));
    Add(SMA(0));
    Add(SMA(2));
    Add(DEMA(2));
    Add(DEMA(2));
    Add(TEMA(1));
    Add(DEMA(2));
    Add(TEMA(1));
    SetStopLoss("", CalculationMode.Percent, Stoploss, false);
    Add("^comp", PeriodType.Minute, 5);
    Add("CL 07-17", PeriodType.Minute, 5);
    Add(SMA(0));
    Add(SMA(2));
    Add(SMA(0));
    Add(SMA(2));

    CalculateOnBarClose = false;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {


    // Condition set 1
    if (CrossAbove(SMA(BarsArray[1],0), SMA(BarsArray[1],2), 1))
    {
    Variable2 = LongValue;
    }

    // Condition set 2
    if (CrossBelow(SMA(BarsArray[1],0), SMA(BarsArray[1],2), 1))
    {
    Variable2 = ShortValue;
    }



    // Condition set 1
    if (CrossAbove(SMA(BarsArray[2],0), SMA(BarsArray[2],2), 1))
    {
    Variable3 = LongValue;
    }

    // Condition set 2
    if (CrossBelow(SMA(BarsArray[2],0), SMA(BarsArray[2],2), 1))
    {
    Variable3 = ShortValue;
    }


    if (CrossBelow(SMA(BarsArray[0],0), SMA(BarsArray[0],2), 1))
    {
    Variable0 = ShortValue;
    }
    if (CrossAbove(SMA(BarsArray[0],0), SMA(BarsArray[0],2), 1))
    {
    Variable0 = LongValue;
    }
    // Condition set 1
    if (Variable2 == LongValue
    && Variable0 == LongValue)
    {
    EnterLongLimit(2000, Median[0], "");
    }

    // Condition set 2
    if (Variable2 == ShortValue
    && Variable0 == ShortValue)
    {
    EnterShortLimit(2000, Median[0], "");
    }
    // Condition set 1
    if (Variable3 == LongValue
    && Variable0 == LongValue)
    {
    EnterLongLimit(2000, Median[0], "");
    }

    // Condition set 2
    if (Variable3 == ShortValue
    && Variable0 == ShortValue)
    {
    EnterShortLimit(2000, Median[0], "");
    }



    }


    #region Properties
    [Description("")]
    [GridCategory("Parameters")]
    public int LongValue
    {
    get { return longValue; }
    set { longValue = Math.Max(1, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public int ShortValue
    {
    get { return shortValue; }
    set { shortValue = Math.Max(-1, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public int StopValue
    {
    get { return stopValue; }
    set { stopValue = Math.Max(0, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public double Stoploss
    {
    get { return stoploss; }
    set { stoploss = Math.Max(0.100, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public int Quit
    {
    get { return quit; }
    set { quit = Math.Max(0, value); }
    }
    #endregion
    }
    }
    Attached Files

    #2
    Hello,

    Thank you for the post.

    I noted that you are using lowercase for the index instrument name, lowercase would not be used for instruments.I changed this and can see that the script is able to be enabled. Could you try doing this?

    Code:
    Add("^COMP", PeriodType.Minute, 5);

    In this case, if you keep the output window open while developing or the control center log tab, this script shows the following error when you try to apply it:

    The strategy 'MyCustomStrategy1/' has called the Add() method with an invalid instrument. Either '^comp' does not exist in the Instrument Manager or the specified exchange has not been configured.
    After making the change, if you are unable to see it placing trades I would suggest using Print statements to check if the conditions are becoming true or the values being used:


    Also, TraceOrders is helpful: http://ninjatrader.com/support/forum...63&postcount=1

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Error on calling &quot;OnBarUpdate&quot;

      Hi Jesse,

      Thanks for point out the index that need to be capitalized, and I fixed that.

      However, the Output screen now showed a message: **NT** Error on calling 'OnBarUpdate' method for strategy 'MyCustomStrategy1/a0bc74042e77416989a10d8c7cc7afc1': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

      I don't have any script to in my strategy that trying to access 5 bar ago. Can you help me further? Thanks!

      Note: same script as before, but revised the index to be all capitalized.

      Comment


        #4
        Hello,

        In this case, you would need to add a check to ensure enough bars are available. It looks like in my test I had enough bars so I had not got the error whereas you have different settings used so you see the error.

        To resolve this, you would need to add a check for the number of BarsAgo you use.

        It looks like you are using 1 BarsAgo per series, so you would need the following:

        Code:
        protected override void OnBarUpdate()
        {
            if(CurrentBars[0] < 1 || CurrentBars[1] < 1 || CurrentBars[2] < 1) return;
        Could you try adding this and check if this resolves the error?

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by thanajo, 05-04-2021, 02:11 AM
        3 responses
        468 views
        0 likes
        Last Post tradingnasdaqprueba  
        Started by Christopher_R, Today, 12:29 AM
        0 responses
        10 views
        0 likes
        Last Post Christopher_R  
        Started by sidlercom80, 10-28-2023, 08:49 AM
        166 responses
        2,237 views
        0 likes
        Last Post sidlercom80  
        Started by thread, Yesterday, 11:58 PM
        0 responses
        4 views
        0 likes
        Last Post thread
        by thread
         
        Started by jclose, Yesterday, 09:37 PM
        0 responses
        9 views
        0 likes
        Last Post jclose
        by jclose
         
        Working...
        X