Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Hold Order Value Constant?

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

    Hold Order Value Constant?

    Hi,
    I'm a beginner programmer to say the least but can do fine if I have a good example. I've read the Advanced Order Handling section, but the examples aren't quite clear enough for me to totally understand. I've solved a few other issues using some sample.zip files Josh had posted. I'm hoping someone has an example strat that holds a flagged price until it's hit or cancelled.
    I'm using VStop indicator instead of ATR Trailing. When the price causes the Stop to change over, I want to wait for a retrace and then enter on the Stop value that was broken causing the change over. I can't use NBars since I don't know how many bars for a retrace to come back and hit that price again. In plain english, here's what I want to make the strategy do, for a short entry as an example:
    If price breaking through a stop causes a change over, and the low of any succeeding bar is greater than the stop value at the time of change over (the retrace requirement), then enter at that stop value on a Stop Order. I'll need to cancel the standing order once it's filled.
    I've attached a picture since I'm sure it will explain much better what I'm looking to do. Thanks in advance!
    Joe
    Attached Files

    #2
    Hi Joe,

    One of our NinjaScript trainees will respond to you later today. Thank you for your patience.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thanks Josh, I have all the patience in the world. Also, thank SO MUCH for posting those Sample.zip files I found earlier in the forum (SamplePnL and SamplePriceModification). They sure made life a lot easier!
      Joe

      Comment


        #4
        Hi Joe,

        Thanks for your post.

        The first thing you want to do is store your TradePrice when the price is penetrating the stop value.

        Code:
         if (Close[0] < MyStopValue)
            TradePrice = MyStopValue;
        Next, you want to place ShortStop order when the market trades above this value (retracement condition) -

        Code:
         if (Close[0] > MyStopValue && Position.MarketPosition == Flat)
            EnterShortStop(TradePrice,"JoeGoesShort");
        Next, once filled you reset your TradePrice -

        Code:
         if (Position.MarketPosition == Short)
            TradePrice = 0;
        Please tell me if this works for you!

        Comment


          #5
          Yahoo!! Thanks Bertrand, I'll give it a shot, then let you know how I fared with it.
          Joe

          Comment


            #6
            Hi Bertrand,
            Well, I've tried everything I can think of and can't get it to work. I've searched Help for TradePrice and nothing comes up, so I'm at a loss. Does the TradePrice command keep that price at the time it's issued constant? Since I"m using a trailing stop point to enter (after the retrace) I'm thinking that value keeps changing, and if TradePrice doesn't keep the value at the time it was set then then entry will never happen since the trailing stop changes over once it's broken. I tried using TradePrice in the "close above" part of the code for the retrace (in case TradePrice does stay constant), but that doesn't work either. Here's the latest revision, and now I'm out of ideas. I used ATR Trailing and not VStop since I know you have ATR Trailing indicator. (ATR Upper is the lower dot that prints when the indicator is applied.) I tried 0 and 1 bar ago to set TradePrice.

            protected override void OnBarUpdate()
            {
            // Condition set 1
            if (ToTime(Time[0]) >= ToTime(7, 30, 0)
            && ToTime(Time[0]) <= ToTime(14, 0, 0)
            && Close[0] < ATRTrailing(4, 10, 0.005).Upper[0])
            tradeprice = ATRTrailing(4, 10, 0.005).Upper[1];
            {
            }

            // Condition set 2
            if (Close[0] > tradeprice)
            {
            EnterShortStop(DefaultQuantity, tradeprice, "JoeGoesShort");
            }

            // Condition set 3
            if (Position.MarketPosition == MarketPosition.Short)
            tradeprice = 0;
            {

            }

            I know you can solve this! Thanks,
            Joe

            Comment


              #7
              Hi Joe,

              in this code snippet 'tradePrice' is just a double variable that stores your desired price value which we then use in the order. So you will need to define a

              Code:
               private double tradePrice = 0;
              in the Variables section of your strategy code to use it later. It will then hold your needed price value, because we only store it once Close < JoesTrailingStop.


              Originally posted by Trade1953 View Post
              Hi Bertrand,
              Well, I've tried everything I can think of and can't get it to work. I've searched Help for TradePrice and nothing comes up, so I'm at a loss. Does the TradePrice command keep that price at the time it's issued constant? Since I"m using a trailing stop point to enter (after the retrace) I'm thinking that value keeps changing, and if TradePrice doesn't keep the value at the time it was set then then entry will never happen since the trailing stop changes over once it's broken. I tried using TradePrice in the "close above" part of the code for the retrace (in case TradePrice does stay constant), but that doesn't work either. Here's the latest revision, and now I'm out of ideas. I used ATR Trailing and not VStop since I know you have ATR Trailing indicator. (ATR Upper is the lower dot that prints when the indicator is applied.) I tried 0 and 1 bar ago to set TradePrice.

              protected override void OnBarUpdate()
              {
              // Condition set 1
              if (ToTime(Time[0]) >= ToTime(7, 30, 0)
              && ToTime(Time[0]) <= ToTime(14, 0, 0)
              && Close[0] < ATRTrailing(4, 10, 0.005).Upper[0])
              tradeprice = ATRTrailing(4, 10, 0.005).Upper[1];
              {
              }

              // Condition set 2
              if (Close[0] > tradeprice)
              {
              EnterShortStop(DefaultQuantity, tradeprice, "JoeGoesShort");
              }

              // Condition set 3
              if (Position.MarketPosition == MarketPosition.Short)
              tradeprice = 0;
              {

              }

              I know you can solve this! Thanks,
              Joe

              Comment


                #8
                Hey Bertrand,
                Yes, I had done that. It won't compile without it. Anything obviously wrong here? I also tried putting it under Wizard generated variables but that didn't do anything either. I'm still stumped. It must be SOME dumb thing I'm doing.
                Joe
                {
                #region Variables
                // Wizard generated variables
                private int pT = 12; // Default setting for PT
                private int sL = 12; // Default setting for SL
                // User defined variables (add any user defined variables below)
                private double tradeprice = 0;
                #endregion

                Comment


                  #9
                  Hi Joe,

                  there are some issues with the way paranthesis is used in your code.

                  The code in the OnBarUpdate() section should look this way:

                  Code:
                   
                  protected override void OnBarUpdate()
                  {
                  // Condition set 1
                  if (ToTime(Time[0]) >= ToTime(7, 30, 0)
                  && ToTime(Time[0]) <= ToTime(14, 0, 0)
                  && Close[0] < ATRTrailing(4, 10, 0.005).Upper[0])
                  {
                  tradeprice = ATRTrailing(4, 10, 0.005).Upper[1];
                  }
                   
                  // Condition set 2
                  if (Close[0] > tradeprice)
                  {
                  EnterShortStop(DefaultQuantity, tradeprice, "JoeGoesShort");
                  }
                   
                  // Condition set 3
                  if (Position.MarketPosition == MarketPosition.Short)
                  {
                  tradeprice = 0;
                  }
                   
                  }

                  Originally posted by Trade1953 View Post
                  Hey Bertrand,
                  Yes, I had done that. It won't compile without it. Anything obviously wrong here? I also tried putting it under Wizard generated variables but that didn't do anything either. I'm still stumped. It must be SOME dumb thing I'm doing.
                  Joe
                  {
                  #region Variables
                  // Wizard generated variables
                  private int pT = 12; // Default setting for PT
                  private int sL = 12; // Default setting for SL
                  // User defined variables (add any user defined variables below)
                  private double tradeprice = 0;
                  #endregion

                  Comment


                    #10
                    Thanks Bertrand,
                    I've been gone all day and will try your changes first thing tomorrow. I did try putting the parenthesis and curly brackets in various places (with no luck) but your code looks different. I'll bet it will work now! I'll let you know one way or the other. Thanks SO MUCH for your patience. NinjaScript is a bit different than Easy Language.
                    Joe

                    Comment


                      #11
                      You are welcome Joe, let me know how it works out!

                      Originally posted by Trade1953 View Post
                      Thanks Bertrand,
                      I've been gone all day and will try your changes first thing tomorrow. I did try putting the parenthesis and curly brackets in various places (with no luck) but your code looks different. I'll bet it will work now! I'll let you know one way or the other. Thanks SO MUCH for your patience. NinjaScript is a bit different than Easy Language.
                      Joe

                      Comment


                        #12
                        Hey Bertrand,
                        I've tried everything, still no luck. I even tried to require TradePrice == Zero with condition 1 so it would make sure and see it, but I got an error saying something like you can't have an operator that is double or bool.
                        I have the code EXACTLY as you showed. I built a simple EMA crossover strat to try this TradePrice snippet on something that doesn't move like the trailing stops. Without the TradePrice snippet, strategy works perfectly. I had it enter on a High[0] limit after 3 X 8. It took plenty of trades. I then added TradePrice when the 3 X 8 cross, set it equal to High[0] (just like the plain strategy entry) corrected the remaing code to match your example, and it didn't take any trades at all (just like the other strat, no trades). (Yes, I remembered to put it in the Variables too.
                        It simply does not like the TradePrice snippet. Do you have a strategy that uses TradePrice that works? I'm attaching the simple EMA strat with TradePrice in it. There MUST be something missing in the strat still. If you have a strategy that works with TradePrice in it, I would love to see the whole thing. That would help me troubleshoot much more thoroughly without so much trial and error.
                        Thanks!
                        Joe
                        Attached Files

                        Comment


                          #13
                          Hi Joe,

                          First please check if tradeprice > 0 when sending out the Limitorder in Condition2. Otherwise it can get rejected.

                          Next you can add TradeOrders = True in the Initialize portion of your code. This way all placed orders are displayed in the Output window (Control Center > Tools > Output window) and you can debug them easier.

                          Here is a link to the TraceOrders feature -




                          Originally posted by Trade1953 View Post
                          Hey Bertrand,
                          I've tried everything, still no luck. I even tried to require TradePrice == Zero with condition 1 so it would make sure and see it, but I got an error saying something like you can't have an operator that is double or bool.
                          I have the code EXACTLY as you showed. I built a simple EMA crossover strat to try this TradePrice snippet on something that doesn't move like the trailing stops. Without the TradePrice snippet, strategy works perfectly. I had it enter on a High[0] limit after 3 X 8. It took plenty of trades. I then added TradePrice when the 3 X 8 cross, set it equal to High[0] (just like the plain strategy entry) corrected the remaing code to match your example, and it didn't take any trades at all (just like the other strat, no trades). (Yes, I remembered to put it in the Variables too.
                          It simply does not like the TradePrice snippet. Do you have a strategy that uses TradePrice that works? I'm attaching the simple EMA strat with TradePrice in it. There MUST be something missing in the strat still. If you have a strategy that works with TradePrice in it, I would love to see the whole thing. That would help me troubleshoot much more thoroughly without so much trial and error.
                          Thanks!
                          Joe

                          Comment


                            #14
                            Thanks, I'll give it a shot.
                            Joe
                            PS- an example strat with TradePrice working would still be much appreciated. I think it would save us a lot of time going back and forth.

                            Comment


                              #15
                              TradePrice3 fixed

                              Originally posted by Trade1953 View Post
                              Thanks, I'll give it a shot.
                              Joe
                              PS- an example strat with TradePrice working would still be much appreciated. I think it would save us a lot of time going back and forth.
                              Hi Joe,
                              Please take a look at the attached zip. TradePrice works now well and also TraceOrders are enabled, go to Tools > Output window to see the complete order generation information.

                              Have a good evening!
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              651 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              370 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              577 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X