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 rhyminkevin, Today, 04:58 PM
                0 responses
                12 views
                0 likes
                Last Post rhyminkevin  
                Started by lightsun47, Today, 03:51 PM
                0 responses
                6 views
                0 likes
                Last Post lightsun47  
                Started by 00nevest, Today, 02:27 PM
                1 response
                13 views
                0 likes
                Last Post 00nevest  
                Started by futtrader, 04-21-2024, 01:50 AM
                4 responses
                48 views
                0 likes
                Last Post futtrader  
                Started by Option Whisperer, Today, 09:55 AM
                1 response
                15 views
                0 likes
                Last Post bltdavid  
                Working...
                X