Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Is there a way to assign pre-existing position to new IOrder object

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

    Is there a way to assign pre-existing position to new IOrder object

    Hi,

    I'll start by saying I know (or at least think) this isn't supported, so I don't know if this will get an answer.

    I have been able to get the strategy to check OnStartUp for any open positions in the strategy's account, and identify if any of them are the same instrument the strategy is trading. What I want to do next is assign the Position.Quantity to an IOrder object I use called EntryOrder. The reason for this is because my strategy uses this way:

    if(EntryOrder != null && EntryOrder.OrderState == OrderState.Filled)
    {
    if exit conditions exist
    ExitOrder = SubmitOrder(0, OrderAction.Sell, OrderType.Market, EntryOrder.Quantity, 0, 0, "", "Exit Trigger");
    }

    So I need some way to bridge the gap between the strategy knowing that there's an open position and being able to fill my EntryOrder object with the .Quantity of the pre-existing position so the strategy can take over management of it.

    I'm assuming full risk of what this strategy does, and I want to assert again that this type of coding can be dangerous if not done extremely carefully.

    I have this much code so far:

    Code:
    //Check for pre-existing positions on this symbol that this strategy should take over
                if(Account.Positions != null)
                   {
                    PositionCollection positions = Account.Positions;
                       foreach (Position pos in positions)
                       {
                        if(pos.Instrument.MasterInstrument.Name == Instrument.MasterInstrument.Name && pos.MarketPosition == MarketPosition.Long)
                        {
                               [B]//In here I want to assign Position.Quantity to EntryOrder.Quantity [/B]somehow
                               PositionLabel.Text = Position.MarketPosition.ToString();
                        }
                    }
                   }

    #2
    After thinking about it a bit I am wondering if I shouldn't just make my strategy work this way instead

    If(Position.MarketPosition == MarketPosition.Long)
    {
    if exit conditions exist
    ExitOrder = SubmitOrder(0, OrderAction.Sell, OrderType.Market, Position.Quantity, 0, 0, "", "Exit Trigger")
    }

    I'll spend some time messing around seeing if that works until I see if anyone adds any insight - thanks everyone!

    ** Edit: It seems to run just fine this way. Now I am under the impression that my strategy can assume any prior open long positions on the instrument being traded by using the code shown above. I hope that's right, I haven't gotten a chance to test it yet - but I've been noticing that with my broker's data connection I have to restart things a lot and I don't want my positions running unmanaged.
    Last edited by RunnrX; 04-06-2013, 12:27 AM. Reason: updating with extra info

    Comment


      #3
      Hello RunnrX,

      This would be something that we could not support as an Automated Strategy is not intended to be able to manage any orders that was not submitted from the Strategy itself.

      I will leave this thread open if any members want to share their experiences with this.
      JCNinjaTrader Customer Service

      Comment


        #4
        That's fair NinjaTrader_JC, and I thought it would be the case. I feel like I'm close to getting my solution working anyway, but if any users want to share I'll be happy to talk about it. Once I have my outcome I'll post it here also for reference.

        ** Edit: My aim for this solution would be for the strategy to be able to recover instances of orders created by itself in a past instance before perhaps a loss of connection or power failure causing the computer to shut off. Hopefully this ends up working.

        Comment


          #5
          Solution found, works in live sim testing

          I've found out what my solution will be, and it's not to assign market position to an IOrder object, as it seems you can't say IOrder EntryOrder = Position, etc.. so instead I've found a way to just check for true market position, even those that pre-exist the startup of the strategy. This is just a copy and paste of a post I put on someone else's thread talking about the same kind of thing:


          I'm about to post the solution I just came up with a few minutes ago, but I have to say first that this kind of coding isn't supported by the NT moderators and should be used at own risk. Also, I can't verify 100% that the things I'm saying are true but I have been testing them for the past 3 hours and they seem to add up.

          So I have found (with the help of someone else) that you can in fact find out the true account position and I'll include the code below. But first, to make a distinction: The true account position that pre-existed before the strategy was run can be checked and put to use by the strategy, but if you then make a closing trade based on that account position, the STRATEGY position, Position.MarketPosition will now consider itself to be MarketPosition.Short even though the account is truly flat, because it will consider having done a sell order from flat to short.

          For that reason, I have made all my code and orders to evaluate market position based on true market position, and not strategy market position. This is further complicated by having historical bars, so I have also set my strategy to not process OnBarUpdate() if bars are historical.

          So without further ado, here are three methods I made up that you can call to find out true account info, even of positions that exist before the strategy was started:

          Code:
                  private int GetAccountPositionQuantity()
                  {
                      if(Account.Positions != null)
                         {
                          PositionCollection positions = Account.Positions;
                             foreach (Position pos in positions)
                             {
                              if(pos.Instrument.MasterInstrument.Name == Instrument.MasterInstrument.Name)
                              {
                                  return (pos.Quantity);
                              }
                              
                          }
                         }
                      
                      return (0);  //if it doesn't find that there's a position in the account matching this symbol, it returns 0 as the current position quantity
                  }
          
                  
                  private double GetAccountPositionAvgPrice()
                  {
                      if(Account.Positions != null)
                         {
                          PositionCollection positions = Account.Positions;
                             foreach (Position pos in positions)
                             {
                              if(pos.Instrument.MasterInstrument.Name == Instrument.MasterInstrument.Name)
                              {
                                  return (pos.AvgPrice);
                              }
                              
                          }
                         }
                      
                      return (0);  //if it doesn't find that there's a position in the account matching this symbol, it returns 0 as the current position Average Fill Price
                  }
                  
                  private [B]MarketPosition[/B] GetAccountPositionDirection()
                  {
                      if(Account.Positions != null)
                         {
                          PositionCollection positions = Account.Positions;
                             foreach (Position pos in positions)
                             {
                              if(pos.Instrument.MasterInstrument.Name == Instrument.MasterInstrument.Name)
                              {
                                  return (pos.MarketPosition);
                              }
                              
                          }
                         }
                      
                      return (MarketPosition.Flat);  //if it doesn't find that there's a position in the account matching this symbol, it returns 0 as the current position direction
                  }
          You would put these methods in your code, and then they can be called in this way:

          For example, in OnStartUp(), you want to first of all check if there were any open positions prior to starting the strategy. You could do this:

          if(GetAccountPositionDirection() != MarketPosition.Flat)
          {
          Print("THERE IS A PRE-EXISTING POSITION ON THIS INSTRUMENT OF " + GetAccountPositionQuantity() + " SHARES");

          //Set a live emergency stop at entry price - 5% to cover this position based on whatever its entry price was
          SubmitOrder(0, OrderAction.Sell, OrderType.Stop, GetAccountPositionQuantity(), 0, (GetAccountPositionAvgPrice() * 0.95), "", "Pre-existing position's emergency stop");

          }

          My code may have errors, but you can see that the Account.Positions collection code shown above will draw from the true account details rather than the strategy's Position.MarketPosition details. I hope this helps.

          And I hope this isn't against the forum rules to post this kind of stuff. If it is, may the moderators please remove it.

          Anyway, for me it has been a great breakthrough as now I truly have a strategy that I can run 24 hours a day, five days a week in automatic trading on forex, and if I get a disconnetion, I don't have to worry anymore about the strategy being thrown off. I can just reconnect, reload the strategy, and it will pick up where it left off which changes everything for me.


          On top of this, not only does the strategy take over pre-existing positions and manage their exit conditions, with the help of other users on this forum I was able to add a Windows Form control panel that lets this strategy pause and resume without having to reload it, and a flatten all button, and a cancel pending orders button, meaning I can now just run it from Sunday 5pm to Friday 5pm and not worry about resetting the strategy and losing all my orders and history

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by sjsj2732, Yesterday, 04:31 AM
          0 responses
          32 views
          0 likes
          Last Post sjsj2732  
          Started by NullPointStrategies, 03-13-2026, 05:17 AM
          0 responses
          286 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          283 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
          91 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Working...
          X