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

About Position.MarketPosition

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

    About Position.MarketPosition

    I have a basic strategy. When I first turn it one and trace out

    Print("Market Position = " + Position.MarketPosition);

    The position will read either Long or Short before anything has even happened. As the strategy instantiates is seems to look at the bars that are loading in as everything is setting up and it "thinks" it's making trades even though it's just a few seconds of loading time.

    So even though I do NOT have an open position Position.MarketPosition will insist I am either Long or Short.

    1. Does this issue ring a bell?

    2. It's making it hard for me to open the first trade of the strategy because the first condition will be if (Position.MarketPosition == MarketPosition.Flat )

    3. Is there a work around? A way to clear this state?

    Thanks,

    Coop

    #2
    HTML Code:
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBars[0] < 1)
    return;
    
    Print("Market Position = " + Position.MarketPosition);
    
    // Set 1
    if (Close[0] > Close[1])
    {
    
    SetStopLoss("MyScalp", CalculationMode.Ticks , 40, false);
    SetProfitTarget("runnerLong", CalculationMode.Ticks, 40);
    EnterLong();
    
    }
    
    if (Close[0] < Close[1])
    {
    
    SetStopLoss("MyScalp", CalculationMode.Ticks , 40, false);
    SetProfitTarget("runnerLong", CalculationMode.Ticks, 40);;
    EnterShort();
    
    }
    
    
    
    }​

    Comment


      #3
      I banged together a little code to show what I'm seeing and so you can reproduce. This image attached shows the output of the Print and it shows that I am long but this is just the strategy loaded in and not even playing yet or in a trade.

      Again, if the strategy thinks that I'm in the market it will never make a real trade as I'm telling it to only open a trade if I'm flat.

      Thanks
      Attached Files
      Last edited by coopgrafik; 11-22-2022, 04:32 PM.

      Comment


        #4
        Hi coopgrafik, thanks for writing in. There is a Strategy Position and an Account Position. As the strategy processes the historical data the Position object will change with the Virtual historical position. You can ignore the historical processing by adding this to the top of OnBarUpdate:

        if(State == State.Historical)
        return;

        Please let me know if this does not resolve.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChrisL View Post

          if(State == State.Historical)
          return;
          This did the trick. I must have written 60 strategies by now and somehow I never ran into this issue or heard about the simple fix. Thank you!


          Comment


            #6
            coopgrafik The approach NinjaTrader_ChrisL offers is fine and will certainly work. Personally, I tend to prefer
            Code:
            if (State != State.Realtime) return;
            simply because it is focussed on what I really want (must be Realtime) rather than what I don't want (avoid Historical), and virtually self-documenting. But again, the approach above will work just fine and both approaches are useful. Just a thought.

            Thanks.
            Multi-Dimensional Managed Trading
            jeronymite
            NinjaTrader Ecosystem Vendor - Mizpah Software

            Comment


              #7
              Actually you will soon find out that the next issue is you cannot run your strategy in the strategy analyzer because it does not run with real time data. It would be wise to include a user settable switch that bypasses the if statement when you want to run in the strategy analyzer.

              Comment


                #8
                Good point, Tasker-182.

                Thanks.
                Multi-Dimensional Managed Trading
                jeronymite
                NinjaTrader Ecosystem Vendor - Mizpah Software

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by JerryWar, 11-19-2016, 08:14 AM
                3 responses
                2,067 views
                0 likes
                Last Post tradertom2012  
                Started by capt34, Today, 03:46 AM
                0 responses
                3 views
                0 likes
                Last Post capt34
                by capt34
                 
                Started by wuannetraam, Today, 12:22 AM
                1 response
                10 views
                0 likes
                Last Post wuannetraam  
                Started by TGOG1, Yesterday, 03:02 PM
                2 responses
                23 views
                0 likes
                Last Post TGOG1
                by TGOG1
                 
                Started by AlgoDreamer, Yesterday, 06:12 PM
                0 responses
                8 views
                0 likes
                Last Post AlgoDreamer  
                Working...
                X