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

Proper use of Position.MarketPosition

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

    Proper use of Position.MarketPosition

    Hi,

    a quick (and probably stupid question), how would this strategy behave? would it switch from long to short, and viceverse also if I use the Position.MarketPosition check?

    Code:
    if (Position.MarketPosition == MarketPosition.Flat && CrossAbove(Close,SMA(20),1)
    {
    EnterLong();
    }
    if (Position.MarketPosition == MarketPosition.Flat && CrossBelow(Close,SMA(20),1)
    {
    EnterShort();
    }
    thanks

    #2
    Hi Sburtt.
    I think if you re-write you code as below, it becomes a bit more obvious what will happen.
    1) It will do nothing until you are flat.
    2) It will then do nothing until one of the Cross conditions is true.
    3) When one of them becomes true it will enter in the corresponding direction.
    4) It will do nothing =more till you are flat again


    if (Position.MarketPosition == MarketPosition.Flat)
    {
    if (CrossAbove(Close,SMA(20),1)
    {
    EnterLong();
    }
    if (CrossBelow(Close,SMA(20),1)
    {
    EnterShort();
    }
    }

    Comment


      #3
      Originally posted by DaveS View Post
      Hi Sburtt.
      I think if you re-write you code as below, it becomes a bit more obvious what will happen.
      1) It will do nothing until you are flat.
      2) It will then do nothing until one of the Cross conditions is true.
      3) When one of them becomes true it will enter in the corresponding direction.
      4) It will do nothing =more till you are flat again


      if (Position.MarketPosition == MarketPosition.Flat)
      {
      if (CrossAbove(Close,SMA(20),1)
      {
      EnterLong();
      }
      if (CrossBelow(Close,SMA(20),1)
      {
      EnterShort();
      }
      }
      Dave thanks for your reply, this confirms what i was thinking, basically if I want is to switch from long to short on the SMA cross over, I don't think this will happen with the script you mentioned above. thanks

      Comment


        #4
        What you have written -

        if (Position.MarketPosition == MarketPosition.Flat)
        {
        if (CrossAbove(Close,SMA(20),1)
        {
        EnterLong();
        }
        if (CrossBelow(Close,SMA(20),1)
        {
        EnterShort();
        }
        }

        Will get you in initally.
        If you want to reverse at the crossovers then add -

        if (Position.MarketPosition == MarketPosition.Short)
        {
        if (CrossAbove(Close,SMA(20),1)
        {
        EnterLong();
        }
        }

        if (Position.MarketPosition == MarketPosition.Long)
        {
        if (CrossBelow(Close,SMA(20),1)
        {
        EnterShort();
        }
        }


        NT does a lotot of the work for you, you don't need to close positions to reverse, just enter in the opposite direction and NT will close the old position for you

        What you must do next is decide how to do the final exit.

        Comment


          #5
          Wouldn't it be easier to use simply:
          Code:
          if (CrossAbove(Close,SMA(20),1)
          {
          EnterLong();
          }
          if (CrossBelow(Close,SMA(20),1)
          {
          EnterShort();
          }
          }
          Without checking the maketposition? What arenthe risks with doing this?

          Comment


            #6
            Originally posted by sburtt View Post
            Wouldn't it be easier to use simply:
            Code:
            if (CrossAbove(Close,SMA(20),1)
            {
            EnterLong();
            }
            if (CrossBelow(Close,SMA(20),1)
            {
            EnterShort();
            }
            }
            Without checking the maketposition? What arenthe risks with doing this?
            No risks that I can think of, MarketPosition is often useful in more complcated scripts, but you don't really need it at all. The code in the quote would do the whole job I think.

            Comment


              #7
              Thanks dave

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by alifarahani, Today, 09:40 AM
              6 responses
              34 views
              0 likes
              Last Post alifarahani  
              Started by Waxavi, Today, 02:10 AM
              1 response
              17 views
              0 likes
              Last Post NinjaTrader_LuisH  
              Started by Kaledus, Today, 01:29 PM
              5 responses
              14 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by Waxavi, Today, 02:00 AM
              1 response
              12 views
              0 likes
              Last Post NinjaTrader_LuisH  
              Started by gentlebenthebear, Today, 01:30 AM
              3 responses
              17 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Working...
              X