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 strategy question

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

    #31
    Hello,

    Can you provide a paste of just the loops you are using without the extra logic? If you are running a loop, the next loop would need to wait until that is done to continue. The script is read top to bottom, so if the loops are one after another it would be evaluated one after the other.

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

    Comment


      #32
      // firecounter is the variable created in loop 1 - problem is that I get positions created that //dont relate to that number being complete - especially with the limit entry orders





      for (i = 0; i < 68; i++)
      {
      if (!Historical && fire[i] == 0)
      {
      fire[i] = 1;
      }




      if (fire[i] == 1
      && Positions[i].MarketPosition == MarketPosition.Flat
      && (BarsInProgress == i & !Historical)
      && Bars.TickCount < 50)

      {
      firecounter++;
      }
      // end loop 1

      // begin loop 2
      for (i = 0; i < 68; i++)
      {
      if (!Historical && fire[i] == 0)
      {
      fire[i] = 1;
      }

      adjust1 = (size * (dailyrisk*0.01))/firecounter;
      voladjust1 = (int)(adjust1 / voldivide0);

      voldivide0 = (DailyATR(Closes[i], 5)[0] * 0.3);
      string tag = "";



      if (fire[i] == 1
      && Positions[i].MarketPosition == MarketPosition.Flat
      && (BarsInProgress == i & !Historical)
      && Bars.TickCount < 200)


      {

      if (BarsInProgress == i & (GetCurrentAsk()- GetCurrentBid()<=0.04);

      {
      tag = "SP STOCK" + i + "U-H Short";
      EnterShort(i, (int)(adjust1 / voldivide0), tag);
      SetStopLoss(tag, CalculationMode.Price, ((DailyATR(Closes[i], 5)[0] * 0.3) + CurrentDayOHL(Opens[i]).CurrentOpen[0]), false);
      SetProfitTarget(tag, CalculationMode.Price, PriorDayOHLC(Closes[i]).PriorClose[0]+0.01);
      fire[i] = 2;
      }

      if (BarsInProgress == i & (GetCurrentAsk()- GetCurrentBid())>0.04);

      {
      tag = "SP STOCK" + i + "U-H NoSpread Short";
      EnterShortLimit(i,liveUntilCancelled, (int)(adjust1 / voldivide0),GetCurrentAsk()+0.00, tag);
      SetStopLoss(tag, CalculationMode.Price, ((DailyATR(Closes[i], 5)[0] * 0.3) + CurrentDayOHL(Opens[i]).CurrentOpen[0]), false);
      SetProfitTarget(tag, CalculationMode.Price, PriorDayOHLC(Closes[i]).PriorClose[0]+0.01);
      fire[i] = 2;
      //orderList[BarsInProgress].Add(EnterShort("SP STOCK" + i + "U-H NoSpread Short")); // Unfilled limit order cancellation code
      }

      }

      Comment


        #33
        Hello,

        Based on this, I would not expect the loops to happen one after the other.

        You currently have a loop inside of a loop that are using the same increment variable.

        Lets look at the basic for loop you have here:

        Code:
        int i;
        for(i = 0; i < 68; i++)
        {
        	Print("loop1 " + i);
        	for(i = 0; i < 68; i++)
        	{
        		Print("loop2 " + i);
        		// i = 0 - 68
        		// runs 68 times before the first loop moves on again . 
        		// because i is not a local variable, the first loop would not be run again because i is now == 68
        	}
        }
        so for iteration the first loop is called, the second loop is then called but because you this inside the first loop, the index of i would be reset to 0 on each time this is called.

        You can try the paste above to see the Print of what your loop is actually doing.

        For these to happen separate of each other, they would need to be defined outside of each others scope and not use the same increment.

        for(int i = 0; i < 68; i++)
        {
        // i is 1 - 68
        }
        for(int i = 0; i < 68; i++)
        {
        // i is 1 - 68
        }

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

        Comment


          #34
          I have just checked that I have two for i statements -

          so

          for (i = 0; i < 68; i++)
          loop 1

          for (i = 0; i < 68; i++)
          loop 2


          check this out: the output window shows the problem:

          0Firecounter V6
          1Firecounter V6
          16.07.2015 12:00:00 Entered internal PlaceOrder() method at 16.07.2015 12:00:00: BarsInProgress=19 Action=SellShort OrderType=Market Quantity=4'340 LimitPrice=0 StopPrice=0 SignalName='SP STOCK V619D-OC Short' FromEntrySignal=''
          16.07.2015 12:00:00 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='SP STOCK V619D-OC Short' Mode=Price Value=49.8304 Currency=0 Simulated=False
          16.07.2015 12:00:00 Entered internal SetStopTarget() method: Type=Target FromEntrySignal='SP STOCK V619D-OC Short' Mode=Price Value=49.34 Currency=0 Simulated=False
          1Firecounter V6
          1Firecounter V6
          1Firecounter V6
          1Firecounter V6
          1Firecounter V6
          1Firecounter V6
          1Firecounter V6
          1Firecounter V6
          1Firecounter V6
          1Firecounter V6
          1Firecounter V6
          2Firecounter V6
          16.07.2015 12:00:01 Entered internal PlaceOrder() method at 16.07.2015 12:00:01: BarsInProgress=56 Action=SellShort OrderType=Market Quantity=1'515 LimitPrice=0 StopPrice=0 SignalName='SP STOCK V656U-H Short' FromEntrySignal=''
          16.07.2015 12:00:01 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='SP STOCK V656U-H Short' Mode=Price Value=91.21 Currency=0 Simulated=False
          16.07.2015 12:00:01 Entered internal SetStopTarget() method: Type=Target FromEntrySignal='SP STOCK V656U-H Short' Mode=Price Value=90.5 Currency=0 Simulated=False
          2Firecounter V6
          2Firecounter V6
          2Firecounter V6
          2Firecounter V6



          you can see the orders going in before the firecounter loop has completed......should be a firecounter value of 9 in this case before one order gets fired....

          it takes 8 seconds for firecounter to = 9 on the output window... and yet the orders kicked off at 1,2 ,5,6 seconds....
          Last edited by elliot5; 07-16-2015, 10:19 AM.

          Comment


            #35
            Hello,

            Again this is a counting error with your for statements.

            I will provide a example below that you can test on your end to see this visually. this will show the counting error by printing the values. This is exactly your code from the prior post without the order logic, just the two for loops and added print statements to show how the nesting is resetting your count.

            Code:
            int i = 0;
            for(i = 0; i < 68; i++)
            {
            	Print("Inside loop 1: " + i);
            	for(i = 0; i < 68; i++)
            	{
            		Print("Inside loop 2: " + i);
            	}
            }
            A for statement increments the variable you supply it by the amount you tell it to for every iteration. Lets step through the count on this:

            you have defined i outside of both for loops meaning that both loops will increment the same variable, but also the for loop initially sets a starting point for its increment variable.



            Code:
            int i = 0;
            i starts at 0

            Code:
            for(i = 0; i < 68; i++) 
            {
            i is again set to 0 when this first gets called and for its first iteration i is equal to 1 at this point

            Code:
            for(i = 0; i < 68; i++) 
            {
            	Print("Inside loop 2: " + i);
            }
            now i is now reset to 0 again because this for loop is nested in the first for loop and is also using the same variable i as its increment

            in order for two for statements to be independent of each other and happen one after another, you need to use two different variables and do not nest the for statements. please note the different variables being used instead of i and also how they are defined.

            Code:
            for(int a = 0; a < 68; a++) //starts
            {
            // runs 1 - 68
            } //finishes
            
            
            for(int b = 0; b < 68; b++) //starts loop 2
            {
            // runs 1 - 68
            } //finishes
            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #36
              Thx Jesse I will try that. One question.... today when I right click on a chart and try to select strategies tab NT freezes and has to be shut down.. any idea why that might be? Never had that before. Tried restart which didnt help either. I have 1000.26 version of NT

              Regards and thx

              Comment


                #37
                Hello everington_f,

                Thanks for your post.

                In order to diagnose the issue please send your log and trace files. This can be done through your platform via Control Center-> Help-> Mail to Platform Support. Please make sure your email address is added (if not already there) and in the subject line place: Atten:Paul and a link to this thread.
                Paul H.NinjaTrader Customer Service

                Comment


                  #38
                  Loop problems

                  I ran these two separate loops and despite the non nesting and different variables they both fire at the same time onbarupdate.... how can I ensure that loop 1 fires and completes before Loop 2 commences? Regards


                  onbarupdate()
                  {

                  for(int a = 0; a < 68; a++) //starts
                  {
                  // runs 1 - 68
                  } //finishes


                  for(int b = 0; b < 68; b++) //starts loop 2
                  {
                  // runs 1 - 68
                  } //finishes


                  }

                  Comment


                    #39
                    Hello everington_f,

                    Thanks for your post.

                    Your code, as posted, would run A before B. If you are not seeing this then you might want to test using print statements to show that the code in your strategy is or is not executing as expected.
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #40
                      Hello,

                      I am unsure on your results as I have tested this and the outcome was what I had expected.

                      Using the following:

                      Code:
                      for(int a = 0; a < 68; a++) //starts
                      {
                      	Print("loop1 " + a);
                      } 
                      
                      
                      for(int b = 0; b < 68; b++) //starts loop 2
                      {
                      	Print("loop2 " + b);
                      }
                      produces the following output:

                      Code:
                      loop1 0
                      loop1 1
                      loop1 2
                      loop1 3
                      loop1 4
                      loop1 5
                      loop1 6
                      loop1 7
                      loop1 8
                      loop1 9
                      loop1 10
                      loop1 11
                      loop1 12
                      loop1 13
                      loop1 14
                      loop1 15
                      loop1 16
                      loop1 17
                      loop1 18
                      loop1 19
                      loop1 20
                      loop1 21
                      loop1 22
                      loop1 23
                      loop1 24
                      loop1 25
                      loop1 26
                      loop1 27
                      loop1 28
                      loop1 29
                      loop1 30
                      loop1 31
                      loop1 32
                      loop1 33
                      loop1 34
                      loop1 35
                      loop1 36
                      loop1 37
                      loop1 38
                      loop1 39
                      loop1 40
                      loop1 41
                      loop1 42
                      loop1 43
                      loop1 44
                      loop1 45
                      loop1 46
                      loop1 47
                      loop1 48
                      loop1 49
                      loop1 50
                      loop1 51
                      loop1 52
                      loop1 53
                      loop1 54
                      loop1 55
                      loop1 56
                      loop1 57
                      loop1 58
                      loop1 59
                      loop1 60
                      loop1 61
                      loop1 62
                      loop1 63
                      loop1 64
                      loop1 65
                      loop1 66
                      loop1 67
                      loop2 0
                      loop2 1
                      loop2 2
                      loop2 3
                      loop2 4
                      loop2 5
                      loop2 6
                      loop2 7
                      loop2 8
                      loop2 9
                      loop2 10
                      loop2 11
                      loop2 12
                      loop2 13
                      loop2 14
                      loop2 15
                      loop2 16
                      loop2 17
                      loop2 18
                      loop2 19
                      loop2 20
                      loop2 21
                      loop2 22
                      loop2 23
                      loop2 24
                      loop2 25
                      loop2 26
                      loop2 27
                      loop2 28
                      loop2 29
                      loop2 30
                      loop2 31
                      loop2 32
                      loop2 33
                      loop2 34
                      loop2 35
                      loop2 36
                      loop2 37
                      loop2 38
                      loop2 39
                      loop2 40
                      loop2 41
                      loop2 42
                      loop2 43
                      loop2 44
                      loop2 45
                      loop2 46
                      loop2 47
                      loop2 48
                      loop2 49
                      loop2 50
                      loop2 51
                      loop2 52
                      loop2 53
                      loop2 54
                      loop2 55
                      loop2 56
                      loop2 57
                      loop2 58
                      loop2 59
                      loop2 60
                      loop2 61
                      loop2 62
                      loop2 63
                      loop2 64
                      loop2 65
                      loop2 66
                      loop2 67
                      If you are using a multi timeframe script this could be the cause otherwise I am unsure because this is standard C# syntax and will perform exactly as it is told to.

                      Both loops should be called in one OnBarUpdate call, there is nothing saying to not run loop 2 until another call to OnBarUpdate happens. In the current state, it should be exactly as the output I have posted is and just run through all of the first loop and then proceed into the second loop.

                      Please let me know if I may be of additional assistance.
                      JesseNinjaTrader Customer Service

                      Comment


                        #41
                        {
                        firecounter++;
                        fire[i] = 2;
                        Print("loop1 " + i); // added
                        }

                        when I add the print loop 1 to the code the firecounter doesnt add up at all.... and doesnt even show up on the output



                        0Firecounter V6
                        0Firecounter V6
                        0Firecounter V6
                        0Firecounter V6
                        0Firecounter V6
                        0Firecounter V6
                        0Firecounter V6
                        loop2 0
                        loop2 1
                        loop2 2
                        loop2 3
                        loop2 4
                        loop2 5
                        loop2 6
                        loop2 7
                        loop2 8
                        loop2 9
                        loop2 10
                        loop2 11
                        loop2 12
                        loop2 13
                        loop2 14
                        loop2 15
                        loop2 16
                        loop2 17
                        loop2 18
                        loop2 19
                        loop2 20
                        loop2 21
                        loop2 22
                        loop2 23
                        loop2 24
                        0Firecounter V6
                        0Firecounter V6
                        0Firecounter V6
                        0Firecounter V6
                        0Firecounter V6
                        0Firecounter V6
                        0Firecounter V6
                        Last edited by elliot5; 07-20-2015, 10:58 AM.

                        Comment


                          #42
                          Hello,

                          I am unsure based on your output, this would be related to the other logic in the script that you are using which is changing the outcome of the loops.

                          I would recommend placing the example I had provided in a separate script and run that and that alone to see the correct output. The two loops one after another would print exactly as I had pasted it would return exactly the prints that are shown in the prior post. Anything other than this would be logic related somewhere else in the script but related to how you are using the for loops.

                          If you can not locate the error, please remove any unnecessary code from the script and please post the script in its entirety so I can see the logic you are using.

                          Please let me know if I may be of additional assistance.
                          JesseNinjaTrader Customer Service

                          Comment


                            #43
                            Multi Instrument Strategy

                            I use two strategies - the first trades IBIS german stocks with a 25 stock array

                            the second trades LSE UK stocks with a 25 stock array.

                            I have historical data for all the stocks concerned, but when I initiate the UK stocks strategy the array collects data back for up to 90 days from each stock in the array from my IB connection even though the historical minute data is already available. This is not the case with the IBIS strategy that quickly gets the array loaded without trying to download data prior to today from IB servers.

                            Any idea why this might be and more importantly a way to fix this?

                            Regards
                            Last edited by elliot5; 02-23-2016, 02:42 AM.

                            Comment


                              #44
                              Hello everington_f,

                              Thanks for your post.

                              Can you confirm that both strategies are identical (other than the instruments) and they are set to load the same number of days of data?

                              Does the behavior change if you load one strategy before the other?

                              Are both strategies loaded from the strategies tab or from a chart?
                              Paul H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by inanazsocial, Today, 01:15 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post inanazsocial  
                              Started by trilliantrader, 04-18-2024, 08:16 AM
                              5 responses
                              22 views
                              0 likes
                              Last Post trilliantrader  
                              Started by Davidtowleii, Today, 12:15 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Davidtowleii  
                              Started by guillembm, Yesterday, 11:25 AM
                              2 responses
                              10 views
                              0 likes
                              Last Post guillembm  
                              Started by junkone, 04-21-2024, 07:17 AM
                              9 responses
                              71 views
                              0 likes
                              Last Post jeronymite  
                              Working...
                              X