Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop trace failing

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

    Stop trace failing

    This is a snippet from my script that sets a reference as a stop on orders. It works fine on following the price when it should... but it also follows it the other way! I have stared at this code for hours now... and I just can't see it! Notice the code in red, "Order = true" indicates a long, while "Order = false" indicates a short... null indicates no order.

    Code:
     
    [COLOR=red]if (Order == true)[/COLOR]
    {
    if (Input[0] > MonitorPrice + 1);
    {
    MonitorPrice = Input[0] - 1;
    Print("Long Support @ " + MonitorPrice);
    }
    if (Input[0] <= MonitorPrice || (Input[0] - EntryPrice) >= (double)((Input[0] - PeakValue) * .5))
    {
    ExitLong();
    Order = null;
    if (Input[0] < PeakValue)
    {
    PeakValue = Input[0];
    Print("Peak = " + Input[0]);
    }
    }
    }
    [COLOR=red]if (Order == false)[/COLOR]
    {
    if (Input[0] < MonitorPrice - 1);
    {
    MonitorPrice = Input[0] + 1;
    Print("Short Support @ "+MonitorPrice);
    }
    if (Input[0] > MonitorPrice || (EntryPrice - Input[0]) >= (double)((Input[0] - PeakValue) * .5))
    {
    ExitShort();
    Order = null;
    if (Input[0] > PeakValue)
    {
    PeakValue = Input[0];
    Print("Peak = " + Input[0]);
    }
    }
    }

    #2
    Sleeping Troll, I'm not exactly sure what I'm looking at. Can you please explain what is going wrong ("but it also follows it the other way")?
    AustinNinjaTrader Customer Service

    Comment


      #3
      The Highlighted area moves my stop reference to one point behind the current price. It's counterpart does the same for short orders. When in a long deal the stop follows the price up, but also follows it down. It does the same for a short.


      Code:
       
      [COLOR=lime]if (Order == true){if (Input[0] > MonitorPrice + 1);[/COLOR]
      [COLOR=lime]{[/COLOR]
      [COLOR=lime]MonitorPrice = Input[0] - 1;[/COLOR]
      Print("Long Support @ " + MonitorPrice);
      }
      if (Input[0] <= MonitorPrice || (Input[0] - EntryPrice) >= (double)((Input[0] - PeakValue) * .5))
      {
      ExitLong();
      Order = null;
      if (Input[0] < PeakValue)
      {
      PeakValue = Input[0];
      Print("Peak = " + Input[0]);
      }
      }
      }
      if (Order == false)
      {
      if (Input[0] < MonitorPrice - 1);
      {
      MonitorPrice = Input[0] + 1;Print("Short Support @ "+MonitorPrice);
      }
      if (Input[0] > MonitorPrice || (EntryPrice - Input[0]) >= (double)((Input[0] - PeakValue) * .5)){ExitShort();
      Order = null;
      if (Input[0] > PeakValue){PeakValue = Input[0];
      Print("Peak = " + Input[0]);
      }
      }
      }
      Last edited by Sleeping Troll; 03-30-2010, 03:29 PM.

      Comment


        #4
        Sorry, but I still can't see the issue here. Do you mean that the 'MonitorPrice' moves both up and down for each direction? If so, you'll have to code in some logic that will only change the variable if it is going in the right direction, just like a trailing stop.
        AustinNinjaTrader Customer Service

        Comment


          #5
          That is correct, it moves in both directions... The Conditional:
          "if (Input[0] > MonitorPrice + 1);"
          Should only allow this to happen if Input[0] is > Monitor price +1, yet it stays ahead of a falling price just as it trails a rising price!

          Only thing I can figure is that the conditionals:
          if(Order == true)
          and
          if(Order == false)
          are not being respected.

          Which indicate long and short orders (Order is nullable) it does however respect the "null" as there is no output when Order is null.
          Last edited by Sleeping Troll; 03-30-2010, 03:54 PM.

          Comment


            #6
            Sleeping Troll, using code from your script, I whipped up my own version to show how the trailing stop would trail for a long position:
            Code:
            protected override void OnBarUpdate()
                    {
                        if (Historical) return;
            
                        if (Close[0] > stop + 1)
                        {
                            stop = Close[0] - 1;
                        }
                        Plot0.Set(stop);
                    }
            And everything looks like it should. The stop doesn't go down when the price goes down. I suggest you start with a as-simple-as-possible version of what you're trying to do that works correctly and then slowly layer on complexity until you figure out where it breaks.
            Attached Files
            AustinNinjaTrader Customer Service

            Comment


              #7
              Your code:

              if (Close[0] > stop + 1)
              {
              stop = Close[0] - 1;

              My code:
              if (Close[0] > MonitorPrice + 1);
              {
              MonitorPrice = Close[
              0] - 1;

              ";" DOH! (slap) thx for letting me bounce that one off of you!

              Comment


                #8
                Great to hear you've got it worked out.
                AustinNinjaTrader Customer Service

                Comment


                  #9
                  I take all that back, I removed the ";" and I get the same results! Only thing that I can figure is that your compiler is doing something strange with nullables?

                  Comment


                    #10
                    Are you sure you re-compiled? I do not have any references to any null value in my code. It is very straight-forward. I've attached it so you can see how it works.
                    Attached Files
                    AustinNinjaTrader Customer Service

                    Comment


                      #11
                      #region Variables
                      private int CanCount;
                      private double? macd;
                      private double? macdAvg;
                      private bool? Order;
                      private bool? Study;
                      private double MonitorLong;
                      private double MonitorShort;
                      private int HighBar;
                      private int LowBar;
                      private double EntryPrice;
                      private double? Support;
                      private bool? BollFlag;
                      private bool? MACDCrossAboveFlag;
                      private bool? MACDCrossBelowFlag;
                      private double PeakValue;
                      #endregion

                      Do you understand what the "?" means?
                      Last edited by Sleeping Troll; 03-30-2010, 04:54 PM.

                      Comment


                        #12
                        The "?" means that those variables are declared nullable. They can be assigned a null value.

                        In the future, please create a new thread for new questions instead of continuing all of your questions on the same thread.
                        AustinNinjaTrader Customer Service

                        Comment


                          #13
                          Hi there, I received your private message but communicating in private doesn't help out the community. Could you please re-post the message you sent here so we can all help you out?
                          AustinNinjaTrader Customer Service

                          Comment


                            #14
                            This is not a new question, I have changed my code (Close[0] instead of Input[0]) and the only difference now is the var name and that it is a nullable type... I do not think the name is the problem... so it must be the nullable type.

                            I have since replaced my bool?'s (true, false, null) with String's ("Long", "Short", "none") and all is well... I guess...

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            657 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            373 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
                            579 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X