Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Correlation question

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

    Correlation question

    I have a question that seems complicated, but maybe not??? Using the BarsArray feature, how can I program it so that it checks to see if both currencies are flat, then enters a position for both of them. With the example below, it only enters the the AUD/USD position because once it fills that, the BarsInProgress checks and sees that they are not both flat and will not enter the NZD/USD because the AUD/USD has an open position.

    Assuming this has already been established.......

    Add("$AUDUSD", PeriodType.Minute, 1);
    Add("$NZDUSD", PeriodType.Minute, 1);

    How can I change this below to solve the problem?

    if ((BarsInProgress == 1)
    && Positions[1].MarketPosition == MarketPosition.Flat
    && Positions[2].MarketPosition == MarketPosition.Flat)
    {
    if (Close[0] < EMA(300)[0])
    EnterLong(DefaultQuantity, "AUD/USD Long");
    }


    if ((BarsInProgress == 2)
    && Positions[1].MarketPosition == MarketPosition.Flat
    && Positions[2].MarketPosition == MarketPosition.Flat)
    {
    if (Close[0] < EMA(300)[0])
    EnterShort(DefaultQuantity, "NZD/USD Short");
    }

    #2
    edgeliner, you are entering long with AUD/USD, and then your second condition set checks to make sure AUD/USD is flat before entering long for NZD/USD. It appears as if your conditions will never allow simultaneous positions to occur.

    Maybe you could create some bool flag at the beginning of each OnBarUpdate() and check if both positions are flat, and then if the conditions are right, enter your positions:
    Code:
    private bool bothFlat = false;
    OnBarUpdate()
    {
      if (Positions[1].MarketPosition == Flat && Positions[2] == Flat)
        bothFlat = true;
    
      if (EntryCondtionsForInst1 == true && bothFlat)
         EnterLong(AUD/USD);
      if (EnteryConditionsForInst2 == true && bothFlat)
         EnterLong(NZD/USD);
    AustinNinjaTrader Customer Service

    Comment


      #3
      Austin........

      Do you have an idea how I can do that?

      Comment


        #4
        edgeliner, I outlined how you could do that with some code in post #2. If you have a specific question, please let me know.
        AustinNinjaTrader Customer Service

        Comment


          #5
          Sorry Austin......Looked at it too quickly before posting response........ I do see it now......thanks for your reply......will try that.......

          Edgeliner

          Comment


            #6
            Austin......

            The code that you suggested works great! Here is all it took.....

            After adding the private bool, I added........

            if ( Positions[1].MarketPosition == MarketPosition.Flat
            && Positions[2].MarketPosition == MarketPosition.Flat)

            bothFlat = true;

            {
            if ((BarsInProgress == 1)
            && bothFlat == true)
            {
            EnterLong(DefaultQuantity, "AUD/USD Long");
            }

            if ((BarsInProgress == 2)
            && bothFlat == true)
            {
            EnterLong(DefaultQuantity, "NZD/USD Long");
            }

            Now, here is the problem......

            When I run the system on the AUD/USD or the NZD/USD, it gives me the 2 positions in the Control Center, but under Strategies in the Control Center, it only shows me having one position in whichever chart I am running it on. In other words, if I run the strategy no the AUD/USD chart, it shows me with 2 positions under the Positions section of the Control Center, but under Strategies, it only shows me having a position in the AUD/USD and not the NZD/USD?????

            Any ideas???? Thanks!

            Comment


              #7
              edgeliner, there should be a little + button next to the strategy that will open up all the strategy information. In the attached screenshot, it is a "-" sign because I've opened it up to demonstrate.
              Attached Files
              AustinNinjaTrader Customer Service

              Comment


                #8
                Thanks Austin....... but I don't have that + sign??? I am still using 6.5 as I just haven't taken the time to switch over and V6.5 has been doing all that I needed. Do I need to get 7.0 for this to be there?

                Also, I have 1 more question. You have been wonderful answering all of them so far, so I know you will know the answer to this.......

                If I have both positions open and want to exit both of them once I get a certain total profit of say 5 ticks, why doesn't the following exit them for me???

                if (Positions[1].MarketPosition - Positions[2].MarketPosition >=5 )
                {
                ExitLong();
                }
                else if (Positions[2].MarketPosition - Positions[1].MarketPosition >=5 )
                {
                ExitLong();
                }

                Comment


                  #9
                  edgeliner, yes that is a NT7 specific feature.

                  As for why your script isn't causing an exit, just subtracting a market position from another market position would give you a meaningless result. I'm actually a bit surprised the script compiled for you.

                  Instead, you can work with Position.AvgPrice, and compare the current close to get the net profit/loss:
                  Code:
                  double primaryPnL, secondaryPnL = 0;
                  if (Positions[1].MarketPosition == MarketPosition.Long)
                     primaryPnL = (Closes[1][0] - Positions[1].AvgPrice) * Positions[1].Quantity;
                  else if (Positions[1].MarketPosition == MarketPosition.Short)
                     primaryPnL = (Positions[1].AvgPrice - Closes[1][0]) * Positions[1].Quantity;
                  
                  if (Positions[2].MarketPosition == MarketPosition.Long)
                     secondaryPnL = (Closes[2][0] - Positions[2].AvgPrice) * Positions[2].Quantity;
                  else if (Positions[2].MarketPosition == MarketPosition.Short)
                     secondaryPnL = (Positions[2].AvgPrice - Closes[2][0]) * Positions[2].Quantity;
                  
                  private double profitTarget = 100; // here, you can set the profit target in $
                  if ((primaryPnL + secondaryPnL) > profitTarget)
                     // run exit logic
                  If you want to use ticks instead of actual currency P/L, you can just remove the Position.Quantity multiplier.

                  Please let me know if you have any other questions.
                  Last edited by NinjaTrader_Austin; 08-09-2010, 09:15 AM.
                  AustinNinjaTrader Customer Service

                  Comment


                    #10
                    WOW....you are great Austin.....I'll give it a try and let you know..........

                    Edgeliner

                    Comment


                      #11
                      The system is working great except for one thing.

                      Assuming that......

                      Add("$AUDUSD", PeriodType.Minute, 1);
                      Add("$NZDUSD", PeriodType.Minute, 1);

                      has been established..........

                      and, given the fact that my positions are filled.....

                      {
                      EnterLong(DefaultQuantity, "AUD/USD Long");
                      }

                      {
                      EnterShort(DefaultQuantity, "NZD/USD Short");
                      }

                      When the profit conditions are met, my code reads......

                      if (Positions[1].MarketPosition == MarketPosition.Long)
                      ExitLong();
                      if (Positions[2].MarketPosition == MarketPosition.Short)
                      ExitShort();

                      The problem is that only the positive position closes, and not both positions????

                      Originally, I simply tried ExitLong() and ExitShort(), but that didn't work.

                      Is there a code that I can put in that simply does ExitAllPositions somehow???

                      Comment


                        #12
                        Have you tried pointing to the direct BIP in your ExitLong / ExitShort calls?

                        The following method variation is for experienced programmers who fully understand Advanced Order Handling concepts.

                        ExitShort(int barsInProgressIndex, int quantity, string signalName, string fromEntrySignal)

                        Comment


                          #13
                          Thanks Bertrand....I'll give it a try..........

                          Comment


                            #14
                            Bertrand......

                            If I have established BarsInProgress == 1 to be AUD/USD and I EnterLong(DefaultQuantity, "AUD/USD Long"); , how would the exit look with regard to exiting the position with

                            ExitLong(int barsInProgressIndex, int quantity, string signalName, string fromEntrySignal)

                            I have tried it, but it won't compile properly???

                            Thanks

                            Comment


                              #15
                              edgeliner, you can't just copy this in, this was just an example, you would need to fit it to your situation -

                              if (Positions[1].MarketPosition == MarketPosition.Long)
                              ExitLong(1, DefaultQuantity, "", "AUD/USD Long");
                              if (Positions[2].MarketPosition == MarketPosition.Short)
                              ExitShort(2, DefaultQuantity, "", "NZD/USD Long");

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by sjsj2732, Yesterday, 04:31 AM
                              0 responses
                              36 views
                              0 likes
                              Last Post sjsj2732  
                              Started by NullPointStrategies, 03-13-2026, 05:17 AM
                              0 responses
                              287 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              287 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              133 views
                              1 like
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              95 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Working...
                              X