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 Entry Order (Pair Trading)

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

    Multi Instrument Entry Order (Pair Trading)

    Hello,

    I have a strategy that seeks to trade the spread of NQ futures vs ES futures. My conditions are very simple right now, for testing purposes. Specifically, I am recording the most recent daily close of each market, and then comparing that price to the current price called on every tick. If the NQ declines more than 0.40% from yesterdays close, and the ES is still trading above its previous days close, then I want to buy the NQ and sell the ES.

    To be clear, my primary data series or BarsArray[0] is NQ daily bars.
    Secondary data series BarsArray[1] is ES daily bars.
    Third series BarsArray[2] is NQ 1 tick data.
    Lastly BarsArray[3] is ES 1 tick data.

    The first 2 data series are strictly used to record the previous days close for each respective market.

    For the tick data, I want to enter as soon as my condition takes place. In the script, you will see my entry orders are placed under BarsInProgress == 2.

    The script is buildable and showing no syntax errors. However it is producing no trades.

    I am having trouble finding where my logic needs correcting.

    I have created 2 private integers YCQ and YCE. (Yesterdays close NQ and Yesterdays close ES.

    I have not included exit orders as I set ExitOnSessionClose equal to true.

    else if (State == State.Configure)
    {
    AddDataSeries("ES 12-18", Data.BarsPeriodType.Day, 1, Data.MarketDataType.Last);
    AddDataSeries("NQ 12-18", Data.BarsPeriodType.Tick, 1, Data.MarketDataType.Last);
    AddDataSeries("ES 12-18", Data.BarsPeriodType.Tick, 1, Data.MarketDataType.Last);
    }
    }

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

    if (CurrentBars[0] < 0
    || CurrentBars[1] < 0
    || CurrentBars[2] < 0
    || CurrentBars[3] < 0)
    return;

    if (Open[0] > 1)
    {
    YCQ = Convert.ToInt32(Close[1]);
    YCE = Convert.ToInt32(Closes[1][1]);
    }


    if (BarsInProgress == 2)

    if (((Closes[2][0]) < (YCQ - (YCQ * .004)))
    && ((Closes[3][0]) > YCE))
    {
    EnterLong(2,1, "MyLong");
    EnterShort(3,1, "MyShort");

    }
    Last edited by murraym324; 12-23-2018, 07:16 PM.

    #2
    Hello murraym324,

    To understand why the script is behaving as it is, either placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints that print the values of every variable used in every condition that places an order along with the time of that bar.

    This will print to the output window. Backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output from both computers to your reply.

    Below I am providing a link to videos that demonstrate adding prints to a script to get further information about the behavior of the script.
    NT8 — https://www.youtube.com/watch?v=BA0W...tu.be&t=51m51s
    NT7 — https://www.youtube.com/watch?v=K8v_...tu.be&t=48m35s

    If you are using the Strategy Builder in NinjaTrader 8, you can also build prints in the Actions window under Misc -> Print.
    NT8 Strategy Builder — https://drive.google.com/open?id=1CC...D4JjJgEIwJKv6L

    To copy a script and modify the copy (allowing the original to remain in the Strategy Wizard):
    NT7 — https://www.youtube.com/watch?v=K8v_...utu.be&t=5m33s
    NT8 — https://www.youtube.com/watch?v=BA0W...utu.be&t=8m15s

    The prints should include the time of the bar and should print all values from all variables in all conditions that must evaluate as true for this action to be triggered. It is also helpful to enable TraceOrders and print the order object in OnOrderUpdate.
    TraceOrders - https://ninjatrader.com/support/help...raceorders.htm
    OnOrderUpdate() - https://ninjatrader.com/support/help...rderupdate.htm

    Please add prints to your script and send the output from the script as well as the code used to produce the print and I will assist in analyzing this output to understand the behavior.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you for your reply. After adding the print commands, I was able to detect the problem. My private integers YCQ and YCE are printing as expected, the problem is with the line

      Print(Closes[2][0]);

      If my second data series or BarsArray[2] is NQ 1 tick data, then shouldnt this print the close of each tick? I have made sure to place this logic under if (BarsInProgress == 2)

      In the output window, I am getting the following error message.

      Error on calling 'OnBarUpdate' method on bar -1: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
      Attached Files

      Comment


        #4
        Hello murraym324,

        The error is saying an invalid index is used somewhere.
        Hello ninjatrader team I have looked at all the documentation published about it and I can not find a solution. My intention is to extract a series of data


        With the script you have provided the print on line 102 'Print(Closes[3][0].ToString()); is not within the action block of any branching command (if statement) and will always print.

        The line above it on line 101 is a one line action block that does not use brackets.

        if (BarsInProgress == 2 || BarsInProgress == 3)
        Print(Closes[2][0].ToString());

        The line below it is not included with the condition set's action block. This means the print of Closes[3][0] will always occur when all BarsInProgress are being processed in OnBarUpdate.

        Also, I highly recommend adding labels to your prints so you know what value is being printed.


        That said, are you backtesting this or running this with real-time data?
        (I notice you are using 12-18 expiry contracts which are all out of date.)

        If you are backtesting this, what is the from and to date range you are using and also who are you connected to for data?


        I gave the provided script a backtest while connected to a cqg demo from Nov 1st to Dec 12th when these expiry contract months where still trading and the printed on my end without error.

        Below is a link to a video of the test.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by alifarahani, Today, 09:40 AM
        3 responses
        15 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by RookieTrader, Today, 09:37 AM
        4 responses
        18 views
        0 likes
        Last Post RookieTrader  
        Started by PaulMohn, Today, 12:36 PM
        0 responses
        5 views
        0 likes
        Last Post PaulMohn  
        Started by love2code2trade, 04-17-2024, 01:45 PM
        4 responses
        40 views
        0 likes
        Last Post love2code2trade  
        Started by junkone, Today, 11:37 AM
        3 responses
        25 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X