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

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.


    Paul H.NinjaTrader Customer Service

    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

        Paul H.NinjaTrader Customer Service

        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



              Paul H.NinjaTrader Customer Service

              Comment


                #8
                That's perfect. Thank you.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by TraderBCL, Today, 04:38 AM
                3 responses
                23 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by WeyldFalcon, 08-07-2020, 06:13 AM
                11 responses
                1,423 views
                0 likes
                Last Post jculp
                by jculp
                 
                Started by RubenCazorla, Today, 09:07 AM
                0 responses
                4 views
                0 likes
                Last Post RubenCazorla  
                Started by BarzTrading, Today, 07:25 AM
                2 responses
                29 views
                1 like
                Last Post BarzTrading  
                Started by devatechnologies, 04-14-2024, 02:58 PM
                3 responses
                21 views
                0 likes
                Last Post NinjaTrader_BrandonH  
                Working...
                X