Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

logic if - else if

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

    logic if - else if

    Hello,

    I have a short question about logic with "if" and "else if" please.

    I´m confused now about logic in my scripts with "if" and "else if". I think it shouldn´t trigger the targets as I have done it, but it does.

    if (flat)
    {Variables...
    SetStopLoss()}

    else if (long)
    {Variables..
    SetStopLoss(Breakeven)}

    if(Long && conditions1)
    {ExitLong(1);}

    if(Long && conditions2)
    {ExitLong(2);}

    Why does Exitlong work? Shouldn´t the reading of script in this region stop with executing what is in the "else if" statement? So, shouldnt the script only change setstoploss?

    Thank you
    Tony

    #2
    Hello Tony,

    The ExitLong() is in a separate condition and does not appear to be nested in another condition. It will be evaluated completely separate.

    If the condition in an if evaluates as false, the else if will be evaluated, if the else if's evaluate as false, the else is triggered. This does not prevent any code outside of the if / else if / else from being evaluated or triggered.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello,

      thank you for your reply. Yes, this is how I understand and therefore I´m confused.

      But is the ExitLong here in a seperate condition? Wouldn´t it be only if I would have like

      {
      if (flat)
      {Variables...
      SetStopLoss()}

      else if (long)
      {Variables..
      SetStopLoss(Breakeven)}
      }

      if(Long && conditions1)
      {ExitLong(1);}

      if(Long && conditions2)
      {ExitLong(2);}

      Then the else if would be in a bracket with the if before and then the next if would be in a seperate condition. No?

      Thanks
      Tony

      Comment


        #4
        Hi Tony,

        I'm not quite able to understand your inquiry when yo mention "Then the else if would be in a bracket with the if before and then the next if would be in a seperate condition. No?".

        In your post there is a opening curly brace at the top and no matching curly brace to go with it. This would not compile.

        If there was curly brace at the end, this would compile but would not change the behavior.

        The branching command that calls ExitLong(1); when true, is not nested within any other if statement and will be evaluated completely separate.

        Below is a publicly available link to an educational site dotnetpearls on branching commands. Please see the section about nesting.
        http://www.dotnetperls.com/if
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello,

          thank you for your reply and the link. Please let me do another question about logic in the script:

          I´m using different dataseries in my script-strategies and generally all is working fine. But now I´m confused about time of triggering.

          Eg I have a script-strategy enabled in a 8 Range Chart with COBC=true because of CPU.

          In onbarupdate I have

          if (Historical)return;

          When the condition for exit simply is lets say

          If(GetCurrentBid() > SMA(20)[1]) ExitLong();

          will then be the exit immediately when Bid > SMA(20) even when the current 8RangeBar is not closed yet this moment? Or will the exit be only with Close[0] of the 8 Range Bar because the script-strategy runs in an 8 Range chart?

          If the exit will be only with close of 8 Range Bar would it be possible to have the condition with

          if (GetCurrentBid(2)>SMA(20)[1]) ExitLong();

          to have an exit after 2 ticks when [2] I have for dataseries 2 Range in Initialize? (and not only after close of 8 RangeBar)


          (I know the help-guide with working with BarsArray, but I´m not sure now about this)


          Thank you
          Tony

          Comment


            #6
            Hello Tony,

            If a strategy is enabled with Calculate on bar close set to true, OnBarUpdate will only trigger when a bar closes.

            Below is a link to the help guide on Calculate on bar close.


            If the chart is an 8 range chart, and Calculate on bar close is true, OnBarUpdate will trigger when the 8 range bar closes.

            Calling GetCurrentBid(2) would get the bid of the 3rd added data series. This would not add or subtract ticks from a price.
            Below is a link to the help guide on GetCurrentBid().


            To add or subtract ticks use the tick size.
            myValue = myValue + 3 * TickSize.
            This would add 3 ticks to whatever value myValue already had.

            I am having difficulty understanding your inquiry when you mention "to have an exit after 2 ticks when [2] I have for dataseries 2 Range in Initialize? (and not only after close of 8 RangeBar)".

            Please let me know if I have not understood your inquiry correctly.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello,

              thank you for your reply. To understand correctly - sorry if I cause confusion with inaccurate translation - I write my question more shortly and in a different way:

              I run the script in a 8 Range Chart and I want to have an exit when price is above SMA(20[1] "as soon as possible" and not only with close of 8Range Bar. When my 3rd dataseries is a 2 Range would the exit be at the close of the 2 Range Bar with if (GetCurrentBid(2)>SMA(20)[1]? Or what would I have to do so that the exit is executed with close of 2 RangeBar. (so that I have the exit 6 Ticks earlier then with the Close of the basic dataseries 8 Range)

              Thanks
              Tony

              Comment


                #8
                Hello Tony,

                If you want actions to happen before the bar closes, then you need to either use Calculate on bar close as false so that OnBarUpdate calculates for each tick, or add a secondary series with a smaller granularity and process when that data series BarsInProgress is processing, or put the code in OnMarketData so that it is evaluated whenever market data is received in real time.
                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by burtoninlondon, Today, 12:38 AM
                0 responses
                4 views
                0 likes
                Last Post burtoninlondon  
                Started by AaronKoRn, Yesterday, 09:49 PM
                0 responses
                12 views
                0 likes
                Last Post AaronKoRn  
                Started by carnitron, Yesterday, 08:42 PM
                0 responses
                11 views
                0 likes
                Last Post carnitron  
                Started by strategist007, Yesterday, 07:51 PM
                0 responses
                13 views
                0 likes
                Last Post strategist007  
                Started by StockTrader88, 03-06-2021, 08:58 AM
                44 responses
                3,982 views
                3 likes
                Last Post jhudas88  
                Working...
                X