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

Enter trade only if not already in one?

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

    Enter trade only if not already in one?

    Hi all,

    I'm trying to implement some simple logic that does not submit a new order until all existing positions have been closed and the user is flat

    I thought this would work

    if(Position.MarketPosition == MarketPosition.Flat){

    Print("Market Position is FLAT, going LONG -> " + Position.MarketPosition);

    enterLongTrade(High[0]);

    }

    However, I'm still noticing new orders being executed right after each other, which shouldn't happen. What could I be missing here?
    jaybedreamin
    NinjaTrader Ecosystem Vendor - Zion Trading Algos

    #2
    Hello jaybedreamin,

    Thanks for your post.

    Position.MarketPosition == MarketPosition.Flat would be the correct way to detect if the strategy position is flat before placing an order. When this method is used in a condition, the strategy would only enter an order when the strategy position is flat (Strategy positions are separate from actual Account positions).

    Position.MarketPosition: https://ninjatrader.com/support/help...etposition.htm
    Strategy vs. Account Position: https://ninjatrader.com/support/help..._account_p.htm

    Please view the attached simple example script demonstrating entering a Long position when the strategy is in a Flat position.

    When the example script is enabled on a chart, we see that an Entry order is only submitted when the strategy position is flat. If the strategy position is not flat, an order is not placed.

    Note that enterLongTrade() is not a default NinjaScript method. See the Managed Approach order methods below.


    To understand why the script is behaving as it is, such as placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.

    https://ninjatrader.com/support/foru...121#post791121​​

    Let me know if I may assist further.​
    Attached Files
    Last edited by NinjaTrader_BrandonH; 02-08-2023, 08:10 AM.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      What kind of Order Method is this : enterLongTrade(High[0]); which you are using ?
      Is this a valid method ?

      Comment


        #4
        Originally posted by NinjaTrader_BrandonH View Post

        Position.MarketPosition == MarketPosition.Flat would be the correct way to detect if the strategy position is flat before placing an order. When this method is used in a condition, the strategy would only enter an order when the strategy position is flat (Strategy positions are separate from actual Account positions).
        This has always been something I've wanted to know. Let's say I have two charts and a separate strategy running on each one. I only want to be in one order at a time overall. If strategy A fires off, and while that one is still going strategy B meets the requirements to fire off, I want strategy B to NOT fire because I'm already in a trade.

        Is there a way to set up a condition taking into account the account position(s)? That is, not just "fire only if this strategy doesn't already have me in a trade", but "fire only if I'm entirely flat across the board in my account."

        Thank you in advance!

        Comment


          #5
          Hello LunaKai,

          Thanks for your note.

          PositionAccount.MarketPosition could be used to detect if an account's position is flat before placing an order in a strategy.
          Code:
          if (PositionAccount.MarketPosition == MarketPosition.Flat)
          {
              //do something here.
          }
          See this help guide page for more information about PositionAccount.MarketPosition and sample code: https://ninjatrader.com/support/help...etposition.htm

          Please let me know if I may assist further.
          Brandon H.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by ClarkInnissy, Today, 11:27 PM
          0 responses
          1 view
          0 likes
          Last Post ClarkInnissy  
          Started by ETFVoyageur, Today, 08:42 PM
          0 responses
          13 views
          0 likes
          Last Post ETFVoyageur  
          Started by kaseipds, Today, 06:15 AM
          3 responses
          47 views
          0 likes
          Last Post NinjaTrader_Eduardo  
          Started by ZimmeMark, Today, 05:09 PM
          0 responses
          24 views
          0 likes
          Last Post ZimmeMark  
          Started by ETFVoyageur, Today, 05:02 PM
          0 responses
          7 views
          0 likes
          Last Post ETFVoyageur  
          Working...
          X