Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Auto BE +1 Problem

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

    Auto BE +1 Problem

    Hello there,

    I noticed that my be +1 is not working all the time.... I guess it is because my strategy is calculated at bar close... And that be+1 should be in real time, as soon price is reaching the trigger for the be +1....
    Here's my code :

    Variables :

    Code:
    private int rangePriceLong = 15; // Default setting for RangePrice
    private int rangePriceShort = 15; // Default setting for RangePrice
    		
    // Default setting for StopLoss
    private int stopL = 12; 
    // Default setting for Target 1
    private int target1 = 12;
    // Default setting for position Size
    private int nbcontracts = 1;
    // Default setting for buffer entry
    private int bufferentry = 2;
    // Default setting for break even +1 level
    private int breakeven = 6;
    then,
    Code:
    protected override void Initialize()
            {
                CalculateOnBarClose = true;
            }
    then,

    Code:
    protected override void OnBarUpdate()
    {
    // Resets the stop loss to the original value when all positions are closed			
    					
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, stopL);
    }		
    			
    /////////// Break even part /////////
    // If a long position is open, allow for stop loss modification to breakeven
    			
    else if (Position.MarketPosition == MarketPosition.Long)	
    				
    {
    // Once the price is greater than entry price +x ticks, set stop loss to breakeven +1
    if (High[0] >= Position.AvgPrice + breakeven * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice +1 * TickSize);
    }
    }			
    // If a short position is open, allow for stop loss modification to breakeven
    else if (Position.MarketPosition == MarketPosition.Short)	
    				
    {
    				
    // Once the price is greater than entry price -x ticks, set stop loss to breakeven +1
    				
    if (Low[0] <= Position.AvgPrice - breakeven * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice -1 * TickSize);
    }
    }						
    /////////// End Break even part /////////
    
    /////////////		
    // The Short Signal case
    ////////////		
    if (My conditions	)
    											
    {
                  
    EnterShortStop(nbcontracts, Low[0] - bufferentry * TickSize, "WIISignalShort1"); 
    SetProfitTarget("WIISignalShort1", CalculationMode.Ticks, target1);
    							
    }
    
    /////////////		
    // The Long Signal case
    ////////////		
    if ((Myconditions)
    											
    {
    EnterLongStop(nbcontracts, High[0] + bufferentry * TickSize, "WIISignalLong1"); 
    SetProfitTarget("WIISignalLong1", CalculationMode.Ticks, target1);
    }
    }

    So, is there a problem because my strat is at bar close and I want the auto be +1 intra bar ?
    How can I solve that ?

    Thank you very much for your help !

    #2
    Hi advs108,

    Have you tried CalculateOnBarClose = false?

    If you need it to be set for other parts of you code, you can use the following method to separate parts of your code between the settings
    TimNinjaTrader Customer Service

    Comment


      #3
      My strategy need to place entry order at bar close, only the be+1 as to be intra bar...
      The file you give in the link completely twist my mind but I think the solution is in there !

      I'll come back to you if I get stuck !
      Thank you very much for your help

      Comment


        #4
        Ok, I did my best to implement your solution.... But I'm missing something because my strat does not seems to be working anymore and no orders are submitted...

        Here's what I did :

        Code:
                protected override void Initialize()
                {
                    CalculateOnBarClose = false;
                }
        
                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {
        			
        			
        			
        			// To avoid the"index out of range" :
        			if (CurrentBar < 7)
             				return;
        			
        			//	here, I have different calculations for my strat
        				
        				if  (ThisCondition)
        					{condition1 = false;}
        				
        				else 
        					{condition1 = true;}	
        				
        		
        				if  (ThisCondition2)
        					{condition2 = false;}
        				
        				else 
        					{condition2 = true;}		
        					
        					
        			
        // Return if historical--this sample utilizes tick information, so is necessary to run in real-time.
        if (Historical)
        return;	
        			
        			
        /* FirstTickOfBar specifies that this section of code will only run once per bar, at the close of the bar as indicated by the open of the next bar.
        NinjaTrader decides a bar is closed when the first tick of the new bar comes in, therefore making the close of one bar and the open of the next bar virtually the same event. */
        			
        if (FirstTickOfBar && Position.MarketPosition == MarketPosition.Flat)
        {
        SetStopLoss(CalculationMode.Ticks, stopL);
        				
        /* Since we're technically running calculations at the open of the bar (with open and close being the same event), we need to shift index values back one value, because the open = close = high = low at the first tick. Shifting the values ensures we are using data from the recently closed bar. */		
        /////////////		
        // The Long Signal case
        ////////////		
        if (My conditions for Long )
        											
        {
        EnterLongStop(nbcontracts, High[0] + bufferentry * TickSize, "WIISignalLong1"); 
        SetProfitTarget("WIISignalLong1", CalculationMode.Ticks, target1);
        }
        					
        // Run these calculations (on every tick, because CalculateOnBarClose = false) only if the current position is long.
        if (Position.MarketPosition == MarketPosition.Long)
        {
        /* This condition can and will generate intrabar exits (CalculateOnBarClose = false). Because this logic is run once for every tick recieved, it will provide the quickest exit for your strategy, and will exit as soon as the price reach the be +1 level */
        				
        // Once the price is greater than entry price +x ticks, set stop loss to breakeven +1
        if (High[0] >= Position.AvgPrice + breakeven * TickSize)
        {
        SetStopLoss(CalculationMode.Price, Position.AvgPrice +1 * TickSize);
        }
        }
        			
        /////////////		
        // The Short Signal case
        ////////////		
        if (My conditions for Short )
        {
        EnterShortStop(nbcontracts, Low[0] - bufferentry * TickSize, "WIISignalShort1"); 
        SetProfitTarget("WIISignalShort1", CalculationMode.Ticks, target1);
        }
        // Run these calculations (on every tick, because CalculateOnBarClose = false) only if the current position is short.
        if (Position.MarketPosition == MarketPosition.Short)
        {
        /* This condition can and will generate intrabar exits (CalculateOnBarClose = false). Because this logic is run once for every tick recieved, it will provide the quickest exit for your strategy, and will exit as soon as the price reach the be +1 level */
        
        // Once the price is greater than entry price -x ticks, set stop loss to breakeven +1
        if (Low[0] <= Position.AvgPrice - breakeven * TickSize)
        {
        SetStopLoss(CalculationMode.Price, Position.AvgPrice -1 * TickSize);
        }
        }
        }
        }
        I must have misunderstood something in the logic... Can you help me please ?

        Thank you very much.

        Comment


          #5
          Hi advs108,

          Try using unique Print() statements to ensure your conditions are indeed being met, and that the code is entering in to the condition (if statements) sets.
          More info at - http://www.ninjatrader.com/support/f...ead.php?t=3418

          Also, ensure no orders are being rejected by using TraceOrders
          TimNinjaTrader Customer Service

          Comment


            #6
            I tried to add a Print "short" and Print "long" after my conditions for long and short.
            Nothing happened.. So, obviously the program does not manage to calculate my conditions...

            I tried to comment this :
            Code:
            // Return if historical--this sample utilizes tick information, so is necessary to run in real-time.
            		//	if (Historical)
            		//		return;
            Then my conditions are working on the data from the past, but not for the present ones. My strat is working for the past, but not live datas...

            When I'm calculating my conditions, I'm using this :

            Code:
            // To avoid the"index out of range" :
            if (CurrentBar < 7)
            return;
            Is there a problem between this and
            Code:
            // Return if historical--this sample utilizes tick information, so is necessary to run in real-time.
            		//	if (Historical)
            		//		return;
            Any ideas ?

            Comment


              #7
              Hi advs108,

              I suggest only using the if (Historical) return; since there is a built in CurrentBar check with strategies (CurrentBar check only necessary in indicators in most cases).

              The "Min. bars required" setting from the UI when your apply the strategy takes care of the CurrentBar check.

              Check you log tab for errors and continue to narrow down with Print() statements why the code is not entering the logic.
              TimNinjaTrader Customer Service

              Comment


                #8
                Hey,

                I did all the debug i could do with the print....
                Everything is working well up to..... my entry condition !!!!!

                I'm blocked... Do you mind having a look at the file attached and help me ....

                I don't know how to go further....

                Thank you very much.
                Attached Files

                Comment


                  #9
                  advs108,

                  Please add into Initialize() this line of code:
                  Code:
                  TraceOrders = true;
                  This will allow you to track your orders if they are ignored or rejected for any reason. That should provide you with more feedback as to what is happening with your orders.

                  Please be sure to have your Output Window open so you can see such output.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Ok, my entries are submitted properly, stop loss is working and target too....
                    I'm still fighting with the be +1.....

                    But before going further, I have a question....
                    My strat. is running on one chart and I have another chart with chart trader on. I like to see my orders on the chart.
                    It seems that with this way of doing, "tick by tick", I cannot move my stop nor my target. they come back to the point they were entered... Is there a way to be able to move orders by hand ?

                    If not, I'll have to search in another direction....

                    Thank you for your help

                    Comment


                      #11
                      Hi advs108,

                      You should not manually manipulate orders placed by strategies. Once done, the strategy will adjust it back to the logic in the code on the next OnBarUpdate.

                      If you want to add some discretion, you can instead call on an ATM Strategy
                      More info at - http://www.ninjatrader-support.com/H...gASMStrategies
                      TimNinjaTrader Customer Service

                      Comment


                        #12
                        Yes, that what I had in mind, calling an ATM...

                        But I guess I cannot backtest my strat with the call of an ATM ?

                        Comment


                          #13
                          Hi advs108,

                          Unfortunately not, however, you can use the Market Replay.
                          TimNinjaTrader Customer Service

                          Comment


                            #14
                            Yes, that' what I was thinking, market replay.

                            Anyway, because I'm learning a lot, I'll continue on this track and try to make the be +1 works.... Then, I'll try the ATM call !

                            Thank you very much for all your help !

                            Comment


                              #15
                              I have a problem, the order is submitted too early.....

                              In the sample file, there's this comment :

                              /* Since we're technically running calculations at the open of the bar (with open and close being the same event), we need to shift index values back one value, because the open = close = high = low at the first tick. Shifting the values ensures we are using data from the recently closed bar. */

                              Infact, I want to submit my order at the close of the bar with the good entry conditions + 2 ticks above the high. So, I really need the bar to close before submitting the order....

                              Now, the order is submitted at "the open = close = high = low at the first tick" +2....
                              How can I do that ?
                              Thx

                              Comment

                              Latest Posts

                              Collapse

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