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

Can a Strategy close another account's position?

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

    Can a Strategy close another account's position?

    Can a Strategy close another account's position?

    i.e.
    I have
    Strategy A trading on Sim101
    Strategy B trading on Sim102

    When the conditions are met,
    Can Strategy A be programmed to close positions on Sim102?

    #2
    Hello johnnybegoode,

    Through the strategy class provisions no, a strategy is only able to trade the account selected for the strategy. But using the addon approach you can send orders to any account in any script including a strategy.

    Below are public links to the help guide on the addon approach of sending an order.

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      This is how I close all the positions on "Strategy B" which is trading on "Sim102"
      Can this line be replicated using the addon approach:
      Code:
      ExitShort(0,[B]Position.Quantity[/B],"EXIT SHORT EURUSD (ProfitTarget)","");
      How could I replicate the above onto "Strategy A" which is attached to account "Sim101" to exits all positions of "Sim102"?
      Code:
      Account b = Account.All.First(t => t.Name == "Sim102");
      
      Order closeAll_102 = null;
      closeAll_102 = b.CreateOrder("EURUSD", OrderAction.BuyToCover, OrderType.Market, OrderEntry.Automated, TimeInForce.Gtc, [B]Position.Quantity[/B], 0, 0, "myOCO", "ProfitTarget", Core.Globals.MaxDate, null,);
      
      b.Submit(new[] { closeAll_102 });
      Does this look right?

      Comment


        #4
        Hello johnnybegoode,

        This appears to be correct for sending an order to the Sim102 account for the quantity of the strategies position.

        Is this sending an order when you are testing it?

        Are you receiving errors?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I got errors (attached screenshot)

          Code:
          closeAll_102 = b.CreateOrder("EURUSD", OrderAction.BuyToCover, OrderType.Market, OrderEntry.Automated, TimeInForce.Gtc, Position.Quantity, 0, 0, "myOCO", "ProfitTarget", Core.Globals.MaxDate, null,);
          Attached Files

          Comment


            #6
            Hello johnnybegoode,

            Ah the Instrument needs to be an instrument object not a string.

            You can use Instrument.GetInstrument() to get an instrument by a string name.
            https://ninjatrader.com/support/help...instrument.htm
            https://ninjatrader.com/support/helpGuides/nt8/en-us/createorder.htm
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Code:
              Order closeAll_102 = null;
              Instrument myInstrument = Instrument.GetInstrument("EURUSD");
              closeAll_102 = b.CreateOrder(myInstrument, OrderAction.BuyToCover, OrderType.Market, OrderEntry.Automated, TimeInForce.Gtc, Position.Quantity, 0, 0, "myOCO", "ProfitTarget", Core.Globals.MaxDate, null);
              b.Submit(new[] { closeAll_102 });
              It works only for one time.
              Then I get an error - See Attachment
              Attached Files

              Comment


                #8
                Hello johnnybegoode,

                The message is correct. OCO IDs cannot be re-used once they have been used for an order that has been filled, cancelled, or rejected.

                Reusing an OCO ID from a closed order is an attempt to pair a new order with an already closed order using OCO which is not possible.

                You will need to use unique OCO strings.

                Below is a link to an example.


                And another thread where this is discussed.


                You can write your own logic to make OCO strings unique.
                GetAtmStrategyUniqueId() and System.Guid.NewGuid() can also be used to generate unique strings.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  I added
                  Code:
                  string myOCO = GetAtmStrategyUniqueId();
                  Still getting the OCO resue message followed by the quantity error message under log.
                  It works for one time but then the strategy is disabled by the quantity error.

                  Error: See attachment


                  Code:
                  // ********* CLOSING ***********
                  
                              if     (
                                  Profit>= ProfitTarget
                                  )                            
                              {                        
                                  ExitLong(0,Position.Quantity,"EXIT LONG EURUSD (ProfitTarget)","");        
                                  Order closeAll_102 = null;
                                  Instrument myInstrument = Instrument.GetInstrument("EURUSD");
                                  string myOCO = GetAtmStrategyUniqueId();
                                  closeAll_102 = b.CreateOrder(myInstrument, OrderAction.BuyToCover, OrderType.Market, OrderEntry.Automated, TimeInForce.Gtc, Position.Quantity, 0, 0, "myOCO", "ProfitTarget", Core.Globals.MaxDate, null);
                                  b.Submit(new[] { closeAll_102 });
                              }
                          }
                  Attached Files
                  Last edited by johnnybegoode; 05-13-2019, 12:20 AM.

                  Comment


                    #10
                    Hello johnnybegoode,

                    The error message is saying a quantity of 0 was used.

                    What line is causing the error?

                    What was the quantity supplied?
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      I think that was caused by the OCO reuse problem.
                      I'm still getting the problem message.
                      Last edited by johnnybegoode; 05-12-2019, 04:29 PM.

                      Comment


                        #12
                        Hello johnnybegoode,

                        If an order is rejected due to re-using OCO you will get a message that says:
                        "I am getting the error "Order XXXXXX can't be submitted: The OCO ID "XXXXXX" cannot be reused. Please use a new OCO ID. affected Order: BuyToCover XXXX Market"

                        May I confirm this is the error you are getting?

                        Do you want to address the other error you are getting about the order quantity?
                        Chelsea B.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by sidlercom80, 10-28-2023, 08:49 AM
                        172 responses
                        2,277 views
                        0 likes
                        Last Post sidlercom80  
                        Started by Irukandji, Yesterday, 02:53 AM
                        2 responses
                        17 views
                        0 likes
                        Last Post Irukandji  
                        Started by adeelshahzad, Today, 03:54 AM
                        0 responses
                        3 views
                        0 likes
                        Last Post adeelshahzad  
                        Started by CortexZenUSA, Today, 12:53 AM
                        0 responses
                        3 views
                        0 likes
                        Last Post CortexZenUSA  
                        Started by CortexZenUSA, Today, 12:46 AM
                        0 responses
                        1 view
                        0 likes
                        Last Post CortexZenUSA  
                        Working...
                        X