Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multi Instrument Long and Short Strategy

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

    Multi Instrument Long and Short Strategy

    I am trying to code a simple strategy where MES is the primary instrument and MNQ is the secondary instrument. As such, the secondary instrument will always take the opposite direction of the main instrument. I have started coding it. Basically, it (the code) opens a position on every bar...nothing much. However, after compiling it, it doesn't seem to start. Here is the code. Can someone help!

    region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class MultiInstrumentTest : Strategy
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "MultiInstrumentTest";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;

    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    }
    else if (State == State.Configure)
    {
    AddDataSeries("MNQ DEC23", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
    }
    }

    protected override void OnBarUpdate()
    {
    if(Position.MarketPosition == MarketPosition.Flat)
    {
    EnterLong(0, 1, "MESENTRY");
    EnterShort(1, 1, "MNQENTRY");​

    #2
    Hello cranky,

    Are there errors appearing on the Log tab of the Control Center when the strategy is enabled?

    Enable TraceOrders in State.Configure, open the the NinjaScript Output window, and re-run the script.

    Save the output to a text file and include this with your next post so we may assist with analyzing the behavior.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you for your support.

      Yes, there are errors on the Log tab when the strategy is enabled.

      Here are the screenshots:

      1. Screenshot of the log errors
      2. My Screen (Basically, this is the window that I'm using for my strategy; MES is the primary instrument, and MNQ is the instrument 1.) I wanted the system to open a trade on every bar. It means taking a long on MES, and at the same time short MNQ​

      3/4. Images of Complete Error Messages
      Attached Files
      Last edited by cranky; 09-28-2023, 04:25 AM.

      Comment


        #4
        Hello cranky,

        With the ignored message, this would be due to the order being submitted before CurrentBar is equal to or greater than BarsRequiredToTrade.

        To alleviate the message, wait until CurrentBar is equal to BarsRequiredToTrade.

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

        If you would like to submit multiple entries in a direction, set EntriesPerDirection to the number of total orders you would like submitted.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thank You for your support. I have included the code. Now, there is no error in the log. The code I'm referring to is:


          if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < BarsRequiredToTrade)
          return;​

          However, the strategy doesn't execute as it should. When I revert to historical performance, it longs MES and short MNQ at the start of the day and closes the trade on the session end. It happens every day. It means it is at least working as I want it to.

          Still, I want it to open a trade when I start the strategy, not at the start of the day.

          Why would it do it. Why it doesn't open a trade when I try to initiate the strategy. At the time, there is no open trade. Then, why it doesn't allow me to start trading.

          Comment


            #6
            Hello cranky,

            If you only want the strategy to run in real-time, return in historical.

            if (State == State.Historical)
            return;

            Below is a link to a forum post that discusses.



            If you have an order that is not being submitted on the bar you expect, enable TraceOrders and add prints to understand why.


            For the condition that submits the order, print the time of the bar and all values in the condition with labels for each value and comparison operator.
            Save the output to a text file. State the bar date and time where you are expecting the order.
            Include this with your next post and we would be happy to assist with analyzing the output.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Yesterday, 05:17 AM
            0 responses
            58 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            133 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            73 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            45 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            50 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X