Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need Price to be Reached before Entering

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

    Need Price to be Reached before Entering

    I am trying to get the computer to track the price and then submit a market order. I need the price to reach a certain predefined level before it enters.

    It seems that all of the criteria is working properly, except for the one that checks for the price.

    That is:
    Code:
    if (High[0] >= rangeHigh + pipBuffer)
    {}
    All of the other criteria seem to work just fine. Here is more of the code, if that will help you help me figure this out:

    Code:
    			// Setting overnight range
    			if (ToTime(Time[0]) == ToTime(endRangeHr,endRangeMin,00))
    			{
    				startRange = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 2, 0, 0);
    				theHigh = HighestBar(High, CurrentBar - Bars.GetBar(startRange));
    				rangeHigh = High[theHigh];
    				theLow = LowestBar(Low, CurrentBar - Bars.GetBar(startRange));
    				rangeLow = Low[theLow];
    				//DrawText("RangeHigh" + CurrentBar, true, rangeHigh, rangeHigh + 1 * TickSize, Color.Blue);
                }
    			
    			// Reset Flag
    			if (Time[0] > startRange)
    			{
    				restrictTrade = false;
    			}
    
    			 if (direction == TrendDirection.NoTrend)
    			{
    				if (LongEntryOCO == null
    					&& ShortEntryOCO == null
    					&& ToTime(Time[0]) >= ToTime(endRangeHr, endRangeMin, 00)
    					&& ToTime(Time[0]) < ToTime(removeOrderHr, removeOrderMin,00)
    					&& High[0] >= rangeHigh + pipBuffer
    					&& restrictTrade == false)
    				{
    					LongEntryOCO = EnterLong(positionSize, "BreakoutLongOCO");
    					Print("***On " + Time[0] + ", the Range High + Pip Buffer is " + (rangeHigh + pipBuffer));
    					//Print("On " + Time[0] + ", the Entry Order is " + LongEntryOCO);
    					restrictTrade = true;
    }
    It seems like it would be just a quick fix, but I have been working for about 1.5 hours trying various combinations, Printing statements, commenting stuff out and all that I can figure is that it has to do with the High[0] statement.

    Could you please point me in the right direction for what is going on?

    Thank you very much for your help.

    #2
    Hi jg123,

    What do you get in the Output window with the following code?

    Code:
    Print(Time[0]+" -- "+High[0]+" >= "+(rangeHigh+pipBuffer);
    if (High[0] >= rangeHigh + pipBuffer)
    {
    Print("submit order");
    }
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Here is the Output Window:

      **NT** Enabling NinjaScript strategy 'AABreakout2/2cd572ec21324d36875d056e65abf1c3' : On starting a real-time strategy - StrategySync=SubmitImmediately SyncAccountPosition=False EntryHandling=AllEntries EntriesPerDirection=5 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositio ns ExitOnClose=False Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=False CancelExitOrdersOnDisable=False CalculateOnBarClose=False MaxRestarts=4 in 5 minutes
      3/18/2014 7:00:00 PM Entered internal PlaceOrder() method at 3/18/2014 7:00:00 PM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='BreakoutLongOCO' FromEntrySignal=''
      ***On 3/18/2014 7:00:00 PM, the Range High + Pip Buffer is 0.0001
      submit order
      3/18/2014 7:00:00 PM Entered internal PlaceOrder() method at 3/18/2014 7:00:00 PM: BarsInProgress=0 Action=Sell OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=0 SignalName='LongStopOCO' FromEntrySignal='BreakoutLongOCO'
      3/18/2014 7:00:00 PM Entered internal PlaceOrder() method at 3/18/2014 7:00:00 PM: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=1 LimitPrice=202.84 StopPrice=0 SignalName='LongTargetOCO' FromEntrySignal='BreakoutLongOCO'
      and attached is an image of the chart.
      Attached Files

      Comment


        #4
        Hi jg123,

        Welllll... The condition appears to have triggered as I do see submit order in the output...

        However, I wasn't expecting a trade, and I am not seeing the print that shows time followed by the High[0] and the value you are comparing to.

        (Unless that print was edited and is printing ***On 3/18/2014 7:00:00 PM, the Range High + Pip Buffer is 0.0001.

        In which case the High will pretty much always be above this value and the condition should pretty much always be true.)

        Does getting this print resolve the issue?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          It is kind of a strange one, to be honest.

          First of all, nothing is yet resolved.

          rangeHigh+pipBuffer should not be .0001 and, in fact, it only does this when I have the LongEntryOCO order in place. When I comment that out, I get all of the correct values for that every single day. Here is a snippet of the Output Window with that commented out.

          **NT** Disabling NinjaScript strategy 'AABreakout2/832d3792d9a1456980ceeb52b831ce48'
          **NT** Enabling NinjaScript strategy 'AABreakout2/832d3792d9a1456980ceeb52b831ce48' : On starting a real-time strategy - StrategySync=SubmitImmediately SyncAccountPosition=False EntryHandling=AllEntries EntriesPerDirection=5 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositio ns ExitOnClose=True/ triggering 30 before close Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=False CancelExitOrdersOnDisable=False CalculateOnBarClose=False MaxRestarts=4 in 5 minutes
          ***On 3/18/2014 7:00:00 PM, the Range High + Pip Buffer is 0.0001
          submit order
          ***On 3/19/2014 9:00:00 AM, the Range High + Pip Buffer is 1.5293
          submit order
          ***On 3/19/2014 10:00:00 AM, the Range High + Pip Buffer is 1.5293
          submit order
          ***On 3/19/2014 11:00:00 AM, the Range High + Pip Buffer is 1.5293
          submit order
          ***On 3/19/2014 12:00:00 PM, the Range High + Pip Buffer is 1.5293
          submit order
          ***On 3/19/2014 1:00:00 PM, the Range High + Pip Buffer is 1.5293
          submit order
          ***On 3/19/2014 2:00:00 PM, the Range High + Pip Buffer is 1.5293
          submit order
          ***On 3/19/2014 3:00:00 PM, the Range High + Pip Buffer is 1.5293
          submit order
          ***On 3/19/2014 4:00:00 PM, the Range High + Pip Buffer is 1.5293
          submit order
          ***On 3/19/2014 5:00:00 PM, the Range High + Pip Buffer is 1.5293
          submit order
          ***On 3/19/2014 6:00:00 PM, the Range High + Pip Buffer is 1.5293
          submit order
          ***On 3/19/2014 7:00:00 PM, the Range High + Pip Buffer is 1.5293
          submit order
          ***On 3/19/2014 8:00:00 PM, the Range High + Pip Buffer is 1.5293
          submit order
          ***On 3/19/2014 9:00:00 PM, the Range High + Pip Buffer is 1.5293
          submit order
          ***On 3/20/2014 9:00:00 AM, the Range High + Pip Buffer is 1.5359
          submit order
          ***On 3/20/2014 10:00:00 AM, the Range High + Pip Buffer is 1.5359
          submit order
          ***On 3/20/2014 11:00:00 AM, the Range High + Pip Buffer is 1.5359
          submit order
          ***On 3/20/2014 12:00:00 PM, the Range High + Pip Buffer is 1.5359
          submit order
          ***On 3/20/2014 1:00:00 PM, the Range High + Pip Buffer is 1.5359
          submit order
          ***On 3/20/2014 2:00:00 PM, the Range High + Pip Buffer is 1.5359
          submit order
          ***On 3/20/2014 3:00:00 PM, the Range High + Pip Buffer is 1.5359
          submit order
          ***On 3/20/2014 4:00:00 PM, the Range High + Pip Buffer is 1.5359
          Here is the code that gave that in the Output

          Code:
          			else if (direction == TrendDirection.NoTrend)
          			{
          				if (LongEntryOCO == null
          					&& ShortEntryOCO == null
          					&& ToTime(Time[0]) >= ToTime(endRangeHr, endRangeMin, 00)
          					&& ToTime(Time[0]) < ToTime(removeOrderHr, removeOrderMin,00)
          					//&& High[0] >= rangeHigh + pipBuffer
          					&& restrictTrade == false)
          				{
          					//LongEntryOCO = EnterLong(positionSize, "BreakoutLongOCO");
          					Print("***On " + Time[0] + ", the Range High + Pip Buffer is " + (rangeHigh + pipBuffer));
          					Print("submit order");
          					//Print("On " + Time[0] + ", the Entry Order is " + LongEntryOCO);
          					restrictTrade = true;
          }
          Note that i also have the conditional statement High[0] >= rangeHigh + pipBuffer commented out.

          Comment


            #6
            I am uploading the full code just in case you will find that useful
            Attached Files

            Comment


              #7
              Hi jg123,

              From what I can see the condition is being triggered.

              If the value is not what it should be, I would recommend that you print the values of rangeHigh and pipBuffer after they are set and before they are used to make sure they are what you want.

              If you would like help debugging, please make a script that only has the necessary code to reproduce. This means removing any and all code that does not set these variables or go in the condition statement.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Okay, I have definitely narrowed it down to what is having the issue, but I do not see what is going wrong with this. The error comes as soon as I add in the Buy Market order.

                The reason that calculating the rangeHigh + pipBuffer as .0001 was due to the minimum number of bars I had set in the strategy parameters. I changed that down to 1 bar and it started calculating that correctly.

                It is properly calculating the range, properly setting the bool flag to true and false, and properly printing a "submit order" to the correct times of day, but, as soon as I put in the actual buy order, things start getting messed up.

                Below you will find the various elements of the code

                Creating the Range code:
                Code:
                			// Setting overnight range
                			if (ToTime(Time[0]) == ToTime(endRangeHr,endRangeMin,00))
                			{
                				startRange = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 2, 0, 0);
                				theHigh = HighestBar(High, CurrentBar - Bars.GetBar(startRange));
                				rangeHigh = High[theHigh];
                				theLow = LowestBar(Low, CurrentBar - Bars.GetBar(startRange));
                				rangeLow = Low[theLow];
                				Print(" ");
                				Print(Time[0] + " Range High is " + rangeHigh + " and is " + "at " + Time[theHigh] + ".");
                				Print(Time[0] + " Range Low is " + rangeLow + " and is " + "at " + Time[theLow] + ".");
                				Print("The Pip Buffer is " + pipBuffer + ".");
                				Print("The Range High + the Pip Buffer = " + (rangeHigh + pipBuffer));
                				Print("The Range Low - the Pip Buffer = " + (rangeLow - pipBuffer));
                
                            }
                Flag Reset Code:
                Code:
                			// Reset Flag
                			if (Time[0] > startRange
                				&& ToTime(Time[0]) < ToTime(endRangeHr,endRangeMin,00))
                			{
                				restrictTrade = false;
                				Print(" ");
                				Print(Time[0] + " " + restrictTrade);
                			}
                Order Placement code (with actual order (LongEntryOCO) commented out)
                Code:
                			if (direction == TrendDirection.NoTrend)
                			{
                				if (LongEntryOCO == null
                					&& ShortEntryOCO == null
                					&& ToTime(Time[0]) >= ToTime(endRangeHr, endRangeMin, 00)
                					&& ToTime(Time[0]) < ToTime(removeOrderHr, removeOrderMin,00)
                					&& High[0] >= rangeHigh + pipBuffer
                					&& restrictTrade == false)
                				{
                					//LongEntryOCO = EnterLong(positionSize, "BreakoutLongOCO");
                					Print(Time[0] + " submit order");
                					restrictTrade = true;
                					Print(Time[0] + " " + restrictTrade);
                				}
                        	}
                		}
                OnExecution in order to set stop & target levels
                Code:
                			if (execution.Order != null)
                			{
                				if (LongEntryOCO != null
                					&& LongEntryOCO == execution.Order)
                				{
                					LongStopOCO = ExitLongStop(0, true, positionSize, (rangeLow - pipBuffer),"LongStopOCO","BreakoutLongOCO");
                					LongTargetOCO = ExitLongLimit(0, true, positionSize, LongEntryOCO.AvgFillPrice + (LongEntryOCO.AvgFillPrice - (rangeLow - pipBuffer)), "LongTargetOCO", "BreakoutLongOCO");
                					restrictTrade = true;
                				}
                			}
                When I run this and see what printed, this is what I get:
                6/16/2014 9:00:00 AM Range High is 1.44305 and is at 6/16/2014 3:00:00 AM.
                6/16/2014 9:00:00 AM Range Low is 1.43855 and is at 6/16/2014 9:00:00 AM.
                The Pip Buffer is 0.0001.
                The Range High + the Pip Buffer = 1.44315
                The Range Low - the Pip Buffer = 1.43845
                6/16/2014 1:00:00 PM submit order
                6/16/2014 1:00:00 PM True

                6/17/2014 12:00:00 AM False

                6/17/2014 1:00:00 AM False

                6/17/2014 2:00:00 AM False

                6/17/2014 3:00:00 AM False

                6/17/2014 4:00:00 AM False

                6/17/2014 5:00:00 AM False

                6/17/2014 6:00:00 AM False

                6/17/2014 7:00:00 AM False

                6/17/2014 8:00:00 AM False

                6/17/2014 9:00:00 AM Range High is 1.4508 and is at 6/17/2014 9:00:00 AM.
                6/17/2014 9:00:00 AM Range Low is 1.44535 and is at 6/17/2014 4:00:00 AM.
                The Pip Buffer is 0.0001.
                The Range High + the Pip Buffer = 1.4509
                The Range Low - the Pip Buffer = 1.44525
                6/17/2014 10:00:00 AM submit order
                6/17/2014 10:00:00 AM True

                6/18/2014 12:00:00 AM False

                6/18/2014 1:00:00 AM False

                6/18/2014 2:00:00 AM False

                6/18/2014 3:00:00 AM False

                6/18/2014 4:00:00 AM False

                6/18/2014 5:00:00 AM False

                6/18/2014 6:00:00 AM False

                6/18/2014 7:00:00 AM False
                You can see that an order is submitted on 6/16 & 6/17 (it properly prints a "submit order" on every following day where the criteria is met. But, as soon as I uncomment the LongEntryOCO then it only submits an order on 6/16 and not on 6/17 or any of the following days.

                The flag is changing as it should so that does not seem to be the issue. From what I can see, everything in the OnExecution looks correct and therefore I can not seem to find the problem.

                Comment


                  #9
                  I just had a random idea that seemed to work.

                  At the end of my OnExecution code, I placed this:

                  LongEntryOCO = null;

                  Honestly, I just did it on a whim with no idea at all that it would do anything, but now it seems that all of the following trades are starting to be made.

                  I guess as I think through it, it makes sense that this would cause it all to work because, in the OnBarUpdate, I gave LongEntryOCO a value and it will only place another trade if it is set to null.

                  Comment

                  Latest Posts

                  Collapse

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