Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multi-Instrument strategy

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

    Multi-Instrument strategy

    Hello,

    This is my first time working with multi-instrument strategies. I already have an algorithm that is working good on single instrument. However, I want to trade two instruments with the same strategy on the same algorithm. It is not working good so far; but after reviewing the documentation, i haven't been able to identify the mistake (https://ninjatrader.com/support/help...ryhandling.htm)

    Would you please guide me?

    else if (State == State.Configure)
    {
    AddDataSeries("PYPL", Data.BarsPeriodType.Day, 1, Data.MarketDataType.Last);
    }
    else if (State == State.DataLoaded)
    {
    SetProfitTarget("", CalculationMode.Percent, Limite);
    SetStopLoss("", CalculationMode.Percent, Stop, false);

    ///First instrument
    ADX1 = ADX(Close, Convert.ToInt32(ADXperiod));
    trendADX1 = XXX(ADX1, Convert.ToInt32(YYYYY));
    ZonasdeOperacionCompra2 = ZonasdeOperacionCompra1(Close, Convert.ToInt32(ZZZZZZ));

    ///second instrument
    ADX2 = ADX(Closes[1], Convert.ToInt32(ADXperiod));
    trendADX2 = XXX(ADX2, Convert.ToInt32(YYYYY));
    ZonasdeOperacionCompra3 = ZonasdeOperacionCompra1(Closes[1], Convert.ToInt32(ZZZZZZ));

    }

    protected override void OnBarUpdate()
    {
    if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < BarsRequiredToTrade)
    {
    return;
    }


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

    if (barNumberOfLongEntries != 0)
    if ( CurrentBar > barNumberOfLongEntries + OrderActiveBars )
    CancelOpenOrders();

    if (barNumberOfLongEntries1 != 0)
    if ( CurrentBar > barNumberOfLongEntries1 + OrderActiveBars )
    CancelOpenOrders1();

    /// Entry Instrument 1
    if (BarsInProgress == 0)
    {
    if (trendADX1[0] < ADXinput)
    {
    barNumberOfLongEntries = CurrentBar;
    EnterLongLimit(Convert.ToInt32(DefaultQuantity), ZonasdeOperacionCompra2[0], @"Compra");

    }



    if (Position.MarketPosition != MarketPosition.Flat)
    {
    // Exit signal 1 = if position is losing
    if (Position.GetUnrealizedProfitLoss(PerformanceUnit. Currency, Close[0]) < 0 && Time[0] == myDate.AddDays(364))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), @"Exitloss", @"Compra"); Print(string.Format("{0} | Exit Losing Date: {1}", Time[0], myDate.AddDays(364)));
    }

    // Exit signal 1 = if position is losing
    if (Position.GetUnrealizedProfitLoss(PerformanceUnit. Currency, Close[0]) > 0 && Time[0] == myDate.AddDays(366))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), @"ExitWin", @"Compra"); Print(string.Format("{0} | Exit Winning date: {1}", Time[0], myDate.AddDays(364)));
    }
    }
    }

    /// Entry Instrument 2
    if (BarsInProgress == 1)
    {
    if (trendADX2[0] < ADXinput)
    {
    barNumberOfLongEntries1 = CurrentBar;
    EnterLongLimit(Convert.ToInt32(DefaultQuantity), ZonasdeOperacionCompra3[0], @"Compra1");

    }



    if (Position.MarketPosition != MarketPosition.Flat)
    {
    // Exit signal 1 = if position is losing
    if (Position.GetUnrealizedProfitLoss(PerformanceUnit. Currency, Close[0]) < 0 && Time[0] == myDate1.AddDays(364))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), @"Exitloss2", @"Compra1"); Print(string.Format("{0} | 2 Exit Losing Date: {1}", Time[0], myDate.AddDays(364)));
    }

    // Exit signal 1 = if position is losing
    if (Position.GetUnrealizedProfitLoss(PerformanceUnit. Currency, Close[0]) > 0 && Time[0] == myDate1.AddDays(366))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), @"ExitWin2", @"Compra1"); Print(string.Format("{0} | 2 Exit Winning date: {1}", Time[0], myDate.AddDays(364)));
    }
    }
    }
    Last edited by Jorge.andres.o; 09-22-2021, 08:44 AM.

    #2
    Hello Jorge.andres.o,

    Thank you for your reply.

    Could you elaborate on what you mean by "It is not working good so far"?

    I'm unable to compile the code as given as I don't have one of the indicators in use and it appears to be calling code that isn't included - could you export the script from Tools > Export > NinjaScript Add-on so I may test on my end to see the current behavior? Please do not check the box to export as a compiled assembly or I will not be able to review the logic. Please attach the resulting .Zip file to your reply.

    Thanks in advance; I look forward to assisting you further.

    Comment


      #3
      Hello Kate,

      i think I found the problem. Thank you

      The strategy was opening positions only in one instrument.

      Comment


        #4
        Kate,

        i wonder what settings I need to put in order to make the multi-instrument strategy enter in the instruments at the same time without letting the strategy open two positions in the same instrument?

        Comment


          #5
          Hello Jorge,

          Thank you for your reply.

          I would give the entries intended for different instruments different signal names, and ensure you're only submitting orders with that name when the correct BarsInProgress is being evaluated. You can then set the Entry Handling to Unique Entries and the Entries Per Direction to 1 and it will submit up to 1 of each uniquely named entry. This would allow you to have simultaneous positions in both instruments, without allowing multiple entries on each instrument before returning to a flat position.

          Please let us know if we may be of further assistance to you.

          Comment


            #6
            Kate,

            thanks for your answer. Just to make sure I understand, giving a signal’s name is the same as giving the name to the order. Isn’t it?
            IE: the name of the signal on the following code is “compra1”. Isn’t it?

            /// Entry Instrument 2
            if (BarsInProgress == 1)
            {
            if (trendADX2[0] < ADXinput)
            {
            barNumberOfLongEntries1 = CurrentBar;
            EnterLongLimit(Convert.ToInt32(DefaultQuantity), ZonasdeOperacionCompra3[0], @"Compra1");

            }

            Comment


              #7
              Hello Jorge.andres.o,

              Thank you for your reply.

              Yes, that's correct. Give each entry a unique name like you've added there, set Entries Per Direction to 1 and Entry Handling to unique entries and you'll be in good shape.

              Please let us know if we may be of further assistance to you.

              Comment


                #8
                thank you Kate for your help

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Yesterday, 05:17 AM
                0 responses
                65 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                139 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                75 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