Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Set strategy position without submitting order

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

    Set strategy position without submitting order

    Is there a way to set a strategy position without using entry/exit signals? Here's some pseudo code that I've been trying within my OnBarUpdate() method:

    Code:
    //Set strategy position
    Position.MarketPosition = MarketPosition.Long;
    
    //Flatten strategy position
    if(Condition)
    {
         ExitLong("", "");
         Print("Market Position "+Position.MarketPosition);
    }
    When I backtest this it says "Market Position Long," which means the strategy position WAS set to long, but for some reason ExitLong didn't flatten the position. Why is this?

    #2
    Hello sjwelch,

    Thank you for your post and welcome to the NinjaTrader Support Forum!

    Position.MarketPosition is not intended for this purpose and thus undesired results are expected.

    In addition, ExitLong() only exits a position that truly exists.

    There would be no method to generate a fake position. Please let me know if I may be of further assistance.

    Comment


      #3
      Patrick,

      Thanks for your reply. As a follow-up question, is there any way to submit an entry signal to the strategy only without submitting it to the account? Like forcing a virtual entry signal maybe?

      Here's why I'm asking: I don't like how NT "syncs" the account to the strategy, so I'm trying to do it vice versa. I have code that will tell me the account position. Now, I just need to sync the strategy position to the account position. I know this is not exactly supported, but if anybody has any hints I would be very grateful!

      Comment


        #4
        Hello,

        Yes if you leave the strategy property Sync = false

        and check mark immediately submit live working orders (go to tools -> options -> Strategies tab -> NinjaScript tab)

        I strongly suggest reading through these options here: http://www.ninjatrader.com/support/h..._positions.htm

        Let me know if this is not what you were looking for.
        LanceNinjaTrader Customer Service

        Comment


          #5
          Lance,

          Thanks for the feedback, but that's not exactly what I'm looking for. The problem with that implementation is when I enable a strategy when the strategy is long but my account is flat. When the strategy exits long it will actually create a short position in my account. I can prevent the strategy from exiting long when my account is flat, but then the strategy will forever be long so that doesn't work.

          If there was a way to send the exit long signal to the strategy ONLY without sending it to my account, that would solve the issue. I tried sending EnterLong/ExitLong with quantity 0 but that defaults to quantity 1.

          Another thing I'm thinking about trying - if I send ExitLong within the OnBarUpdate() method, but then cancel it within the OnOrderUpdate() method, will my strategy still go to a flat position or will it realize that the order was cancelled and thus stay long?

          Comment


            #6
            Hello sjwelch,

            Thank you for your response.

            There may be a viable solution for what you are looking for. However, I would like to take this back a bit to understand this item better.

            What is NinjaTrader doing to sync the account and strategy that is not desired on your end?

            Why ExitLong() when there is no position? What are we trying to "simulate" here?

            I look forward to your response.

            Comment


              #7
              Patrick,

              This is the behavior I want: if I enable a strategy and the strategy position is in sync with the account position, the strategy should immediately start executing live orders. However, if I enable a strategy position and they are NOT in sync, I want the strategy to WAIT until the strategy matches my account position, THEN start normal operation.

              Between the 4 possibilities of "Wait until flat" or "Immediately submit live..." and sync account = true/false, none of these iterations describes the behavior I want.

              In other words, I want NT to sync the strategy to the account, not the account to the strategy. That's why I'm trying to see if there are ways to have the strategy send an order only to itself, without altering my account position.

              Thanks again for your feedback.

              Comment


                #8
                Originally posted by sjwelch View Post
                Lance,

                Thanks for the feedback, but that's not exactly what I'm looking for. The problem with that implementation is when I enable a strategy when the strategy is long but my account is flat. When the strategy exits long it will actually create a short position in my account. I can prevent the strategy from exiting long when my account is flat, but then the strategy will forever be long so that doesn't work.

                If there was a way to send the exit long signal to the strategy ONLY without sending it to my account, that would solve the issue. I tried sending EnterLong/ExitLong with quantity 0 but that defaults to quantity 1.

                Another thing I'm thinking about trying - if I send ExitLong within the OnBarUpdate() method, but then cancel it within the OnOrderUpdate() method, will my strategy still go to a flat position or will it realize that the order was cancelled and thus stay long?
                In which case, either issue manually an order to bring the account into sync with the strategy, or else arrange a bool or enum parameter which will allow you to use the Historical property to force realtime operation only for the session. You can then reset the strategy to normal once the day is over.
                Last edited by koganam; 05-06-2013, 04:40 PM.

                Comment


                  #9
                  Originally posted by koganam View Post
                  In which case, either issue manually an order to bring the account into sync with the strategy, or else arrange a bool or enum parameter whcih will allow you to use the Historical property to force realtime operation only for the session. You can then reset the strategy to normal once the day is over.
                  Thanks for the advice, but issuing a manual order to sync the account to the strategy is the same as setting sync account = true, which is not what I want. I don't want my account to enter a market late, so I don't want to change my account at all upon strategy startup (unless it happens to be the right time to buy).

                  I've thought about filtering based on real-time vs. historical before, and I'm not sure if this will work for me. Here's a step-by-step scenario where I think it doesn't work (correct me if I'm wrong):

                  1) I have my strategy filter out historical bars so it uses real-time data only

                  2) I enter the market as expected. My strategy is long and the account is long and I hold the security overnight.

                  3) I have to restart NT overnight for maintenance. When I restart the strategy, the account is long and the strategy should hypothetically be long, but it's flat because I've filtered out historical bars. The two are now out of sync and the next sell signal will be missed because the strategy thinks it's flat.

                  4) As a result, I edit my strategy to use historical bars upon startup only (using time stamps or some other method)

                  5) Now, I enable a strategy when the strategy is long but the account is flat. The two are again out of sync and the next exit long signal will force my account short

                  If I'm missing something, please let me know. I'm still curious about what would happen if I send a buy signal in the OnBarUpdate() method but cancel the order in the OnOrderUpdate() method...could this work?

                  Comment


                    #10
                    Sorry for the double post, but as I finished typing my response I realized how koganam's solution could work for me. If I configured my strategy to filter out historical bars only when my account is flat, I believe this will accomplish what I want.

                    Thanks everyone for your help!
                    Last edited by sjwelch; 05-06-2013, 04:23 PM.

                    Comment


                      #11
                      Originally posted by sjwelch View Post
                      Sorry for the double post, but as I finished typing my response I realized how koganam's solution could work for me. If I configured my strategy to filter out historical bars only when my account is flat, I believe this will accomplish what I want.

                      Thanks everyone for your help!
                      That is one way.

                      However, being a control freak, and not wanting NT to do anything outside my control, I have the somewhat more cumbersome method that I described.

                      1. I have a bool to trade realtime only. It is usually false.
                      2. I start my strategy, and it claims to be long, when the account is flat, because I always go home flat: never hold overnight.
                      3. I stop the strategy; enable the bool to realtime only; reenable the strategy. So the strategy will be flat, and take the next signal.
                      4. I let the strategy ride until it exits at end of day, or at target.
                      5. I stop the strategy, disable realtime only, ...

                      Comment


                        #12
                        Originally posted by sjwelch View Post
                        Sorry for the double post, but as I finished typing my response I realized how koganam's solution could work for me. If I configured my strategy to filter out historical bars only when my account is flat, I believe this will accomplish what I want.

                        Thanks everyone for your help!
                        Ok, so I tried this way but that didn't quite work out. Here's what happened:

                        Real-time data missed a sell signal that's present on historical data, so my position is long despite historical being flat. Now, when I restart the strategy, the two are out of sync.

                        So now I'm back to square one. Any ideas? The most robust solution is finding a method to set strategy position without changing account position. If I call an EnterLong signal in my OnBarUpdate method and then cancel the order in the OnOrderUpdate method, would that update the strategy position or would the strategy position "think" it's long?

                        Comment


                          #13
                          Originally posted by sjwelch View Post
                          Ok, so I tried this way but that didn't quite work out. Here's what happened:

                          Real-time data missed a sell signal that's present on historical data, so my position is long despite historical being flat. Now, when I restart the strategy, the two are out of sync.

                          So now I'm back to square one. Any ideas? The most robust solution is finding a method to set strategy position without changing account position. If I call an EnterLong signal in my OnBarUpdate method and then cancel the order in the OnOrderUpdate method, would that update the strategy position or would the strategy position "think" it's long?
                          Then I must not have communicated correctly to you. In that case, you turn realtime off. The whole point is to turn realtime off or on as necessary to ensure that the strategy position matches the account position. If the historical position matches the account position, then there is no need to use a realtime filter. IOW, the only time that a realtime filter is needed is if the account is flat, but the historical position is not, because a realtime postion will always be flat when it is started, or restarted. That is why I said that the method is somewhat cumbersome, as one has to take specific, manual measures to ensure that the strategy position is made to match the account position.

                          That is the method that I use, because I understand the logic behind it. If you are looking for an automated method to read your account position, that is a different kettle of fish.

                          Comment


                            #14
                            koganam,

                            I'm assuming you mean turn historical on/off (not real-time)? My strategy automatically does this when my account is flat, essentially per the method you described. I should have explained the problem a little better. Here's the scenario:
                            -I start up a strategy. The account is flat, historical position is irrelevant (it's filtered out)
                            -Real-time buy signal happens. My account and strategy position are both long. Everything's going well up to this point.
                            -I restart NT overnight (while still holding on to long position)
                            -When I restart, historical position is flat yet my account position is long. I believe this happened because the sell signal didn't trigger on real-time data, but it triggered for historical data (the two are slightly different). Now the two are out of sync (account is long, strategy is flat).

                            Ideally I would like the strategy to think it's long because my account position is long. Another possible solution is if there's a way to retain real-time data after restarting NT, and using that data instead of historical when starting a strategy. Not sure if this is possible though...

                            Comment


                              #15
                              Originally posted by sjwelch View Post
                              koganam,

                              I'm assuming you mean turn historical on/off (not real-time)?
                              What is the difference? If Historical is false, no historical bars are processed, meaning that only realtime bars are processed. As far as I am concerned, there may be a semantic difference: I fail to see it, and even then that would be irrelevant to me.

                              My strategy automatically does this when my account is flat, essentially per the method you described. I should have explained the problem a little better. Here's the scenario:
                              -I start up a strategy. The account is flat, historical position is irrelevant (it's filtered out)
                              -Real-time buy signal happens. My account and strategy position are both long. Everything's going well up to this point.
                              -I restart NT overnight (while still holding on to long position)
                              -When I restart, historical position is flat yet my account position is long. I believe this happened because the sell signal didn't trigger on real-time data, but it triggered for historical data (the two are slightly different). Now the two are out of sync (account is long, strategy is flat).

                              Ideally I would like the strategy to think it's long because my account position is long. Another possible solution is if there's a way to retain real-time data after restarting NT, and using that data instead of historical when starting a strategy. Not sure if this is possible though...
                              I do not know how else to say it. That is what I said. If the account is not flat, then process Historical data, so that when the Strategy starts it is going to sync to the Acoount position. That is why I use a parameter that I can specify as needed.

                              If I cannot communicate the below to you, then I have failed, and I will bow out of this discussion.
                              Code:
                              //pseudocode:
                              If account position is not flat, process [I]Historical [/I]bars.
                              If account position is flat, but processing [I]Historical[/I] bars makes the Strategy inMarket, then do not process Historical bars.




                              In order to make that happen, I have a parameter that determines whether I process Historical bars or not. That parameter is manually specified through the PropertyGrid, and may mean that I have to:
                              1. Stop the Strategy;
                              2. Set or unset the parameter,
                              3. Restart the Strategy in order to bring things into sync.
                              There may be a way to handle it all automatically through code. I have not explored it, and do not intend to. I prefer to make the decision after seeing what happens. Just my preference. Then again, maybe I am just a control freak?

                              To put it simply, you are not doing what I described.
                              Last edited by koganam; 05-13-2013, 09:42 AM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by ETFVoyageur, 05-07-2024, 07:05 PM
                              17 responses
                              133 views
                              0 likes
                              Last Post ETFVoyageur  
                              Started by ETFVoyageur, Yesterday, 10:13 PM
                              1 response
                              7 views
                              0 likes
                              Last Post ETFVoyageur  
                              Started by somethingcomplex, Yesterday, 10:36 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post somethingcomplex  
                              Started by sofortune, 05-10-2024, 10:28 AM
                              5 responses
                              22 views
                              0 likes
                              Last Post sofortune  
                              Started by guyonabuffalo, Yesterday, 10:01 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post guyonabuffalo  
                              Working...
                              X