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

boolean array logic

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

    boolean array logic

    If I have this boolean array below which is for 4 different instruments, does the line under Initialize() belong in the Initialize() section or should I have it under OBU()?

    Initialize()

    bool[ ] entrySubmit = {false, false, false, false};

    OnBarUpdate()

    for (int Index = 0, Index < 4, Index++)
    for(int i = 0; i < 4; i++)
    if(BarsInProgress = Index)
    if(entrySubmit[i] == false)//other logic here
    {
    EnterLong();
    entrySubmit[i] = true;
    }

    #2
    Hi zachj,

    You will need to add the array variable declaration outside of Initialize() and OnBarUpdate(). I recommend that you add this to the variables region.

    If added to Initialize() the variable will not be accessible with OnBarUpdate. If added to OnBarUpdate this would be overwritten on every bar close and you would not be able to use it to store values.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Oh ok gotcha. So under variables...

      private bool[ ] entrySubmit = {false, false, false, false};

      the first false in the array will be i = 0 and will correspond to the first instrument if BIP = 0, the second false will be i = 1 and will correspond to the second instrument(or first added instrument) if BIP = 1, and so on correct?

      Comment


        #4
        Hello zachj,

        You are using a nested for loop and comparing BarsInProgress to Index and then using i.

        That means on the first loop, BarsInProgress will equal 0 which will equal Index. Then there will be 4 loops through i. So entrySubmit[0], entrySubmit[1], entrySubmit[2], entrySubmit[3] will all be set to false.

        Then Index will be 1. And on BarsInProgress 1 entrySubmit[0], entrySubmit[1], entrySubmit[2], entrySubmit[3] will all be set to false.

        You are going to be overwriting every entrySubmit element on each loop through Index.

        Instead I think you do not want a nested loop.

        Code:
        for (int i = 0; i < 4; i++)
        if(BarsInProgress = i && entrySubmit[i] == false)
        {
        EnterLong(); 
        entrySubmit[i] = true;
        }
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Oh right! Very good, and much simpler. One other related question.. if I have a IOrder array of

          private IOrder[ ] entryOrder = {null, null, null, null};

          entryOrder[i] = EnterLong()

          after it enters the trade does it automatically switch to entryOrder != null or do I have to manually switch it like I did with the bool?

          Comment


            #6
            Hi zachj,

            You will need to manually set the IOrder handle to null. After a position closes you can still use the IOrder handle to get the profit loss etc.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I have the entrySubmit has to equal false to enter a trade, it gets set to true after entering, then when it's flat I have it reset to false. I don't have any condition for IOrder entryOrder has to equal null to enter a trade. It doesn't switch back to null on the next day on it's own or any other way?

              I was using the IOrder array entryOrder for a seperate action for shutting down the strategy after PnL goes too low ..

              if (entryOrder[i] != null)
              {
              CancelOrder (entryOrder);
              }

              Comment


                #8
                Hello zachj,

                After entering (and exiting) an order using the IOrder handle, the handle will not be null until the strategy is restarted or the handle is manually set to null (or to a new order).

                This means that you can use if (entryOrder[i] != null) to check to see if the position exists as long as you are manually setting the order to null after the order is exited.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  I'm not even sure if it's really cancelling any orders anyway and having any effect. What does != null really mean. If it's null it's flat, if it's not null the position is already long so what order is there to really cancel anyway? Does this maybe apply to partial fills or something. I'm not using anything fancy, just a straight order of 200 shares.

                  Comment


                    #10
                    Hello zachj,

                    As I mentioned the order can be flat but will not be set to null unless you manually set the IOrder handle to null.

                    If you have submitted a limit order using that handle that has not filled and call CancelOrder() on the handle, this will exit as intended. If you try and cancel and order that has already filled, such as with a market order, you will get an overfill error.

                    You will need to set your order to null after it is filled. Otherwise, I recommend that you check the state of the order to see if it is filled or not.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Ok so prior to EnterLongLimit() I have..
                      if(entryOrder[i]==null)

                      then right after EnterLongLimit() I have
                      entryOrder[i]=null; to reset it to null after it is filled as you say.

                      This should do it right?

                      When I use this the Pnl didn't change at all over 100 trades on a backtest run but either way I will keep it in there. I guess it turns out I didn't even need the if(entrySubmit == false) and entrySubmit = true and then a reset to false. entryOrder = null takes care of it all. Thanks

                      Comment


                        #12
                        Hi zachj,

                        Are you using Position.GetProfitLoss() to get the PnL of the current position and Performance.AllTrades.TradesPerformance.Currency.C umProfit to get the PnL of all closed trades?

                        Are these not showing the correct profit and loss?

                        Position.GetProfitLoss - http://www.ninjatrader.com/support/h...profitloss.htm
                        TradesPerformance.Currency - http://www.ninjatrader.com/support/h...7/currency.htm
                        Last edited by NinjaTrader_ChelseaB; 11-18-2013, 10:18 AM.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Yes I am using the Position.GetProfitLoss() to get the PnL of the current position. I didn't mean that they were off, just was saying nothing changed when I added in the null script. I was using if(entrySubmit == false) and resetting that which was taking care of trade activity. I removed that and just used null to control it instead. I think I am all set though.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by cmtjoancolmenero, Yesterday, 03:58 PM
                          8 responses
                          31 views
                          0 likes
                          Last Post NinjaTrader_ChelseaB  
                          Started by helpwanted, Today, 03:06 AM
                          2 responses
                          21 views
                          0 likes
                          Last Post NinjaTrader_LuisH  
                          Started by DayTradingDEMON, Today, 09:28 AM
                          0 responses
                          8 views
                          0 likes
                          Last Post DayTradingDEMON  
                          Started by navyguy06, Today, 09:28 AM
                          0 responses
                          2 views
                          0 likes
                          Last Post navyguy06  
                          Started by rjbtrade1, 11-30-2023, 04:38 PM
                          2 responses
                          77 views
                          0 likes
                          Last Post DavidHP
                          by DavidHP
                           
                          Working...
                          X