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

Move stop based on close[0]

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

    Move stop based on close[0]

    Hi

    I have the following code which enters a short trade and sets an integer "stopShort" at the time of entering the trade.

    I then want to use "stopShort" in a later if formula. Basically so I don't keep re-writing the calculation of "stopShort" i.e. Math.Max.............. because it's ugly and I'm sure it's not the most efficient way.

    Only problem is, it won't let me do this- error definition highlights I'm creating a conflict with the earlier definition.

    Any idea how I can do this? Just trying to be more efficient with the code. I can only define stopShort at the part where I enter the trade, I can't do it earlier like in the State.Historical part (at least I didn't think I could!)

    Thanks!

    // OPEN: Look for a retest of structure
    if (
    Conditions are met
    )
    {

    int stopShort=Math.Max(150,((Convert.ToInt32(Math.Roun d((MAXHigh,BarCountOverbought)[0]-Close[0])/TickSize)))+AvTR));

    EnterShort "GoShort");

    RolA=true;
    }

    In the next part of the formula

    if (RolA && Close[0]<Position.AveragePrice-(1*TickSize*StopShort)

    {
    Move the stop loss to a new level
    }

    #2
    Hello lancasterstephen,

    Thanks for your post.

    NinjaTrader uses C# as the base language for Ninjascript. C# syntax differentiates between capitalization so you would need to be consistent when you name a variable. In your code segment I see "stopShort" and "StopShort" which would be considered two different variables.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi

      Sorry, there was a typo in my post, they should both read "stopShort", so they are the same variable.

      Thanks
      STeve

      Comment


        #4
        Hi Steve,

        Thanks for your reply.

        Taking a closer look at your original post and reducing down to the error you have:

        if (condition)
        {
        int stopShort=Math.Max(150,((...
        }


        You have defined stopShort locally as an int within the curly braces so it will only work within the braces.

        To use the definition as an int outside of the braces we recommend you create the definition in the #region Variables at the class level.

        Example: private int stopShort; // create an integer called stopShort.

        Creating it at the class level lets you use the variable in any method used (OnBarUpdate(), OnExecution(), etc. etc.).

        Once created you can then use it like:

        if (condition)
        {
        stopShort=Math.Max(150,((...
        }

        .
        ..
        if (RolA && Close[0]<Position.AveragePrice-(1*TickSize*stopShort)
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hi Paul

          Thanks- I'd already created it in the region variables.

          Just that the value of this int can only be set based on conditions at the time I enter a short trade.

          So, I can only define the value of stopShort which I'd like to use in the rest of my code once it opens a short trade, at the point I enter a short trade.


          If I define in the region variables something like
          private int stopShort=Math.Max(150,((Convert.ToInt32(Math.Roun d((MAXHigh,BarCountOverbought)[BarsSinceEntryExecution]-Close[BarsSinceEntryExecution])/TickSize)))+AvTR));

          Is it possible to do this?


          Many thanks,
          Steve

          Comment


            #6
            Hi Steve,

            Thanks for your reply.

            Just to check, you are posting in the NinjaTrader7 strategy development forum and i see the use of "BarsSinceEntryExecution" which is an NT8 type name.

            In either case, to reduce confusion, it may be better if you can post your entire code as something is amiss and it is difficult to provide answers based on a limited view.

            If you do not wish to post your code, please feel free to send it to PlatformSupport[at]NinjaTrader[dot]Com Mark the e-mail Atten Paul and include a link to this thread.
            Paul H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by selu72, Today, 02:01 PM
            1 response
            4 views
            0 likes
            Last Post NinjaTrader_Zachary  
            Started by WHICKED, Today, 02:02 PM
            2 responses
            10 views
            0 likes
            Last Post WHICKED
            by WHICKED
             
            Started by f.saeidi, Today, 12:14 PM
            8 responses
            21 views
            0 likes
            Last Post f.saeidi  
            Started by Mikey_, 03-23-2024, 05:59 PM
            3 responses
            51 views
            0 likes
            Last Post Sam2515
            by Sam2515
             
            Started by Russ Moreland, Today, 12:54 PM
            1 response
            8 views
            0 likes
            Last Post NinjaTrader_Erick  
            Working...
            X