Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Profit Locking, exit for individual instrument in a account, and exit for...

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

    #16
    hehehe.... I hear ya bro...no worries...my too (I always have a cup sitting on the desk)...appreciate ya! Will report back...

    Originally posted by sledge View Post
    OK - I think my caffeine is kicking in.

    You are right. It makes no sense to issue an ExitShort when your Position is Long??



    This might work better for you:



    Code:
    else if (Position.MarketPosition == MarketPosition.Long 
                        && (GetCurrentBid() > Open[3]))
                    {
                       Print ("Exiting Long");
                        ExitLong(); 
                    }
    
                        
                else if (Position.MarketPosition == MarketPosition.Short 
                        && (GetCurrentAsk() < Open[3]))
                    {
                       Print ("Exiting Short");
                        ExitShort();
                    }

    Comment


      #17
      Hey Sledge:

      Had to switch < to > on the Long and Short FYI...This is what I just tested and it showed only one "Exiting Short" in the output window. It did not show anymore or exit any others either...it should have exited multiple times (I monitor the chart action too to visually see when it should be exiting)...any ideas or other commands to make it work?

      Also...I am using the Line Break Bars (does that matter or not)? It shows only the Open and Close on the Bars so I think it should be fine...but, just asking in case in "may matter"?

      Code:
                  else if (Position.MarketPosition == MarketPosition.Long
                          && (GetCurrentBid() < Open[3])) 
                      {
                          Print ("Exiting Short");
                          ExitLong(); 
                      }
      
                  else if (Position.MarketPosition == MarketPosition.Short 
                          && (GetCurrentBid() > Open[3])) 
                      {
                          Print ("Exiting Short");
                          ExitShort();  
                      }
      Last edited by birdog; 02-03-2013, 02:13 PM.

      Comment


        #18
        I ran market replay from 1-31-13 6 pm est to 2-01-13 4:50 pm est...this is the output I got but nothing exited. However, I noticed a BarsSinceEntry() error. I don't think I get this in real time though because it has bars to look back when I initially load. Let me know what your thoughts on this are? See attached.
        Attached Files

        Comment


          #19
          Hello,

          Originally posted by birdog View Post
          I ran market replay from 1-31-13 6 pm est to 2-01-13 4:50 pm est...this is the output I got but nothing exited. However, I noticed a BarsSinceEntry() error. I don't think I get this in real time though because it has bars to look back when I initially load. Let me know what your thoughts on this are? See attached.
          This error means you need to use this overload:

          BarsSinceEntry(int barsInProgressIndex, string signalName, int entriesAgo)

          since you are working with multiple bar series
          LanceNinjaTrader Customer Service

          Comment


            #20
            Can I put that code just one time in the beginning instead of each "if" or "else if" statement?

            For example:

            Code:
            protected override void OnBarUpdate()
                    {
                        if (Historical) return;
                        if (BarsSinceEntry() > 10
                        if (Position.MarketPosition == MarketPosition.Flat)
                        if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired ||            CurrentBars[2] <= BarsRequired)return;
                        if (BarsInProgress == 0
                        if(FirstTickOfBar)
                        {
                            x = 0;
                        }
            etc etc etc...then, my "if" and "else if" individual statements...for example:

            Code:
            if (CrossAbove(SMA(Fast), SMA(Slow), 1) 
                                && x < myTradeCounter
                                && (EMA(BarsArray[1], 1)[0] > EMA(BarsArray[1], 3)[0] 
                                && EMA(BarsArray[2], 1)[0] > EMA(BarsArray[2], 3)[0]))
                            {
                                
                                EnterLong(DefaultQuantity, "BuyMkt");
                                x++;
                            }
                    
                            else if (CrossBelow(SMA(Fast), SMA(Slow), 1) 
                                 && x < myTradeCounter
                                 && (EMA(BarsArray[1], 1)[0] < EMA(BarsArray[1], 3)[0]
                                 && EMA(BarsArray[2], 1)[0] < EMA(BarsArray[2], 3)[0]))
                            {
            etc etc etc etc

            Comment


              #21
              BD - What is your skill level at Programming C# or NinjaScript?

              I admire how far you have gotten... but I am afraid of a few things. Can you start using {} with every IF statement?

              If you are an expert, then that is fine. I'm just worried about making a suggestion, and that not be your intention with the code...

              Based upon what I know, only the first statement after an IF with out brackets is ran... So it appears you are really meaning:

              (and IMHO, this is more readable... the comments at the end of the } aren't really necessary, but can help when nesting gets too deep...)...

              Code:
              protected override void OnBarUpdate()
                      {
                          if (Historical) return;
              
                          if (BarsSinceEntry() > 10
                          {
                              if (Position.MarketPosition == MarketPosition.Flat)
                              {
                                    if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired ||           CurrentBars[2] <= BarsRequired)
                                { 
                                   return;
                                 } // end BarsRequired
                               } // end MarketPosition.Flat           
                          }  //end barssincentry
              
                          if (BarsInProgress == 0)
                          {
                               if(FirstTickOfBar)
                  
                               {
                                  x = 0;
                                } //end FirstTickofBar
                           } //end BIP == 0
              ...


              [QUOTE=birdog;319542]Can I put that code just one time in the beginning instead of each "if" or "else if" statement?

              For example:

              Code:
              protected override void OnBarUpdate()
                      {
                          if (Historical) return;
                          if (BarsSinceEntry() > 10
                          if (Position.MarketPosition == MarketPosition.Flat)
                          if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired ||            CurrentBars[2] <= BarsRequired)return;
                          if (BarsInProgress == 0
                          if(FirstTickOfBar)
                          {
                              x = 0;
                          }

              Comment


                #22
                Beginner to Intermediate in C# (need to really take some video courses and tutorials actually I know)...smile.

                12 year trader, real estate investor (although I like trading a lot more...scaling out at the present time), pilot (non-active), and quasi web developing/marketing etc...entrepreneurial spirit for sure...

                Thanks Sledge.

                I am still getting this:

                **NT** Error on calling 'OnBarUpdate' method for strategy 'Gver6_TrendFlow_AvdSB/20d6368fef01429d89532793ab719a86': You must use the overload that has a 'BarsInProgress' parameter when calling the BarsSinceEntry() method in the context of a multi-time frame and instrument strategy.

                Any other ideas?



                Greg

                Comment


                  #23
                  Hello,

                  You can't use
                  Code:
                  BarsSinceEntry()
                  like this in a multi series script.

                  You have to do something like
                  Code:
                  BarsSinceEntry(0, "mySignalName", 0)
                  LanceNinjaTrader Customer Service

                  Comment


                    #24
                    Lance, do you have a sample Multi series time sample strategy that uses:

                    BarsSinceEntry(0, "mySignalName", 0)


                    that I can look at and compare?

                    The Multi series time sample in NT does not use it.



                    Greg

                    Comment


                      #25
                      Hello,

                      This link is going to be your best explanation of how to use BarsSinceEntry: http://www.ninjatrader.com/support/h...sinceentry.htm

                      BarsSinceEntry(int barsInProgressIndex, string signalName, int entriesAgo)

                      The first is the BIP index the trade was placed on (use 0 for primary)
                      signal name is the name of the signal
                      entries ago will let you check different entries (use 0 for most recent)

                      You will probably want to read through this in great detail before proceeding: http://www.ninjatrader.com/support/h...nstruments.htm
                      LanceNinjaTrader Customer Service

                      Comment


                        #26
                        ok...I will...I read both before but I must be missing something...will again..thanks Lance...

                        Comment


                          #27
                          Lance, Ok, I finally got something to actually compile regarding this. Can you type how you interpret this?

                          Code:
                          protected override void OnBarUpdate()
                                  {
                                      if (BarsSinceEntry(0,"",0) > 20)
                                      if (Historical) return;
                          etc etc etc

                          I only have to put this in one place (in my case immediately below the OnBarUpdate() section (and I do not have to place it in each individual "if" then do something or "else if" then do something statement below it right?

                          Thanks...I think there is a breakthrough...I hope...smile

                          Greg

                          Originally posted by NinjaTrader_Lance View Post
                          Hello,

                          This link is going to be your best explanation of how to use BarsSinceEntry: http://www.ninjatrader.com/support/h...sinceentry.htm

                          BarsSinceEntry(int barsInProgressIndex, string signalName, int entriesAgo)

                          The first is the BIP index the trade was placed on (use 0 for primary)
                          signal name is the name of the signal
                          entries ago will let you check different entries (use 0 for most recent)

                          You will probably want to read through this in great detail before proceeding: http://www.ninjatrader.com/support/h...nstruments.htm

                          Comment


                            #28
                            Hello,

                            Code:
                            protected override void OnBarUpdate()
                                    {
                                        if (BarsSinceEntry(0,"",0) > 20) //if there have been more than  20 bars since entry on the primary series (the chart)
                                        if (Historical) return;//then check to see if the data is historical, if it is return
                            While there might be some advanced case where you would want to do this, likely you'll want to check for historical at the very start of your code

                            This is probably closer to what you're trying to do
                            Code:
                            protected override void OnBarUpdate()
                                    {
                                        if (Historical) 
                                           return;
                            
                                        if (BarsSinceEntry(0,"",0) > 20) 
                                        {
                                             //do something
                                         }
                                     //rest of code
                                    }
                            LanceNinjaTrader Customer Service

                            Comment


                              #29
                              Lance:

                              See attached for error.

                              I put that code in and had to restart Ninja as it would not allow the strategy to go green. After restart, it did. But, I am still actually getting that error code. Here is what I have:

                              Code:
                              protected override void OnBarUpdate()
                                      {
                                          if (Historical)
                                              return;
                                          if (BarsSinceEntry(0,"",0) > 20)
                                          if (Position.MarketPosition == MarketPosition.Flat) 
                                          if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired || CurrentBars[2] <= BarsRequired) 
                                              return; 
                                          if (BarsInProgress == 0) 
                                          {
                                          if(FirstTickOfBar)
                                          {
                                              x = 0;
                                          }
                                                          
                                              if (CrossAbove(SMA(Fast), SMA(Slow), 1) //Bullish Conditions
                                                  && x < myTradeCounter
                                                  // Turn OFF or ON depending on inst. && Account.Positions.Count < 2
                                                  && (EMA(BarsArray[1], 1)[0] > EMA(BarsArray[1], 3)[0] 
                                                  && EMA(BarsArray[2], 1)[0] > EMA(BarsArray[2], 3)[0]
                                                  && EMA(BarsArray[3], 1)[0] > EMA(BarsArray[3], 3)[0]
                                                  && EMA(BarsArray[4], 1)[0] > EMA(BarsArray[4], 3)[0]))
                                              {
                                                  //Greg> Trade Counter attempts to enter long
                                                  EnterLong(DefaultQuantity, "BuyMkt"); 
                                                  //Greg> Trade Counter increments the trade count by 1
                                                  x++;
                                              }
                                      
                                              else if (CrossBelow(SMA(Fast), SMA(Slow), 1)
                              etc etc etc...then, I have 4 or 5 other "else if" conditions...

                              What do I need to do to not get that error?
                              Attached Files
                              Last edited by birdog; 02-04-2013, 02:49 PM.

                              Comment


                                #30
                                Originally posted by sledge View Post
                                I admire how far you have gotten... but I am afraid of a few things. Can you start using {} with every IF statement?

                                If you are an expert, then that is fine. I'm just worried about making a suggestion, and that not be your intention with the code...

                                Based upon what I know, only the first statement after an IF with out brackets is ran... So it appears you are really meaning:

                                (and IMHO, this is more readable... the comments at the end of the } aren't really necessary, but can help when nesting gets too deep...)...
                                Please do this as sledge suggested.

                                Currently you have several if statements that are going to the next section that don't make sense logically.

                                For example, as is you're requiring there to be 20 bars since entry before you check if the market position is flat in order to check if there are enough bars loaded on the chart.

                                Your NinjaScript / C# Code will always be logically processed and evaluate according to your set logic – this can of course lead to unexpected results at times, thus we would suggest to simplify and debug your code to better understand the event sequence it would go through - unfortunately we cannot offer such debug or code modification services here, but please see the provided resources below to help you proceed productively :

                                First of all you would want to use Print() statements to verify values are what you expect - Debugging your NinjaScript code.

                                For strategies add TraceOrders = true to your Initialize() method and you can then view valuable output related to strategy submitted orders through Tools > Output window - TraceOrders

                                It may also help to add drawing objects to your chart for signal and condition confirmation - Drawing Objects.

                                If you would prefer the debug assist of a professional NinjaScript consultant, please check into the following listings - Click here for a list of certified NinjaScript Consultants
                                LanceNinjaTrader Customer Service

                                Comment

                                Latest Posts

                                Collapse

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