Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Weird Problem with Position.MarketPosition

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

    Weird Problem with Position.MarketPosition

    I've been testing a strategy for months in market replay where the below code returns correctly as:

    Position.MarketPosition.ToString();
    (In market replay it correctly returns "Long" or "Short" depending if i'm long or short)

    Now that i'm running the strategy Live it only returns "Flat" despite being in a position. See slimmed down version of code below:

    protected override void OnBarUpdate()
    {
    If (criteriaLong){
    goLong();
    position = Position.MarketPosition.ToString();
    }

    If (criteriaShort){
    goShort();
    position = Position.MarketPosition.ToString();
    }
    }

    Yesterday i also noticed that my exit strategy below did not work either. So i now suspect Position.MarketPosition is not working as expected like in Market Replay.

    // Exit wining positions
    if ( (Position.MarketPosition == MarketPosition.Short) && exitShortCriteria )
    {
    ExitShort("one");
    }
    if ( (Position.MarketPosition == MarketPosition.Long) && exitLongCriteria )
    {
    ExitLong("one");
    }

    Any ideas / help would be much appreciated.

    ​​​​​​​Thanks,



    #2
    Hello bc24fl,

    Thanks for your post.

    In your entry blocks, you are reading the current position and placing the current position into I assume a string variable named position. I would not expect this to provide the correct status because the code is occurring immediately after entering the order. It is important to understand that the order and position updates happen asynchronously to your code and take physical time to occur, meanwhile the next line of your code is executed well before the order has been transmitted, received, placed, filled and acknowledged back and then Position is updated. Since you are checking for position inside of an entry condition, that entry condition would have to continue to be true in order for the string position to eventually be updated and that may not occur.

    I am not sure what you are doing with the variable string position but if you want it to be correct, I would suggest placing outside of any conditions. You may also want to debug by printing the marketPosition on each bar, for example: Print (Time[0]+" Current Market position = "+Position.MarketPosition);

    Your exit blocks look as if they should work.


    Comment


      #3
      So what is the best option? My goal is to print out stats to the output windows so i can copy/paste to excel for logging purposes. I don't only get the position. I also get the Open, Close, High, etc....

      In javascript there is a return block when making async calls. Does ninjascript have the same? What is the best way to record stats on my entries?

      Thanks,

      Comment


        #4
        Hello bc24fl,

        Thanks for your reply.

        If all you want to do is to print to the output window then use the Print statement as I advised, it will show when the position updates. In your entry block, you could simply print "Entered long" (or short) if you are looking for a starting location.

        If you would rather write to a file directly, please see our example of using streamwriter. The example shows printing the OHLC and timestamp data per bar but could be adapted to your needs and within a strategy.. https://ninjatrader.com/support/help...o_write_to.htm

        Comment


          #5
          Not sure i understand. What's the difference between these two commands?

          (position is a global variable)
          position = Position.MarketPosition.ToString();
          vs
          Print(Position.MarketPosition.ToString());

          if EnterLong() is an async call then both commands will fail, no?

          Comment


            #6
            I think you're talking about doing this:

            position = "Long";

            Instead of using Position.MarketPosition.

            I'll do that for now but can you point me to documentation regarding these async calls and the entire flow. This way i can handle similar items correctly.

            Thanks,

            Comment


              #7
              Hello bc24fl,

              Thanks for your replies.

              I provided what I thought was a simple solution however if you want more in-depth information please see the links below



              Comment


                #8
                That's perfect. Thank you.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Today, 05:17 AM
                0 responses
                41 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                124 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                64 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                41 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                46 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X