Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Please help me to find out why Ninja doesnt want to execute the EnterLong method

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

    Please help me to find out why Ninja doesnt want to execute the EnterLong method

    Hello

    please could you help me to find out what is wrong with my NinjaTrader?
    I have strategy in attachement.
    The system of the strategy is:

    Strategy will find maximum and minimum price between 14:40 and 15:01 on CL market. This time range is called like timeArray.

    When the price will go over/under this MAX and MIN lines, it will enter to LONG/SHORT
    This first trade has to be done until 16:00 or no another enter signal is accepted this day.
    Strategy can make also second enter but just in case it is to the opposite way like the first trade (if first was long the second has to be short) and the first trade was losing. Another cases are ignored.
    Exit is in stoploss or profittarget.
    It's simple.

    The code is executing correctly but I dont understand why, when my strategy will make EnterLong() the code is freezen.
    I tried this just in the historical data.

    See attachement please for more info.
    There are two different errors.

    Thank you for your assist.


    BTW: I was using ZenFire demo account and now the ZenFire is not providing live data to me - I can see just empty chart when I want to see live data..
    But when I want to see for example from 1.2.2010 - 5.2.2010 it shows me this data fine.
    But I also tried to test that on SimulatedDataFeed and it is also not working.
    The code is executing just until the EnterLong method and after that like the code is not executing anymore
    Attached Files

    #2
    Hello Zooinek,

    Could you translate this line from the log tab:

    Error on calling 'OnBarUpdate' method for strategy 'CLbreakout': Index je mimo rozsah. Index musí být nezáporný a musí být menší než velikost kolekce.

    It may be trying to access a value that doesn't exist. If that's the case make sure you have CurrentBar checks as outlined in the document below:
    Make sure you have enough bars in the data series you are accessing
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Hello, thank you for your response...

      That error message translated to the english:
      Error on calling 'OnBarUpdate' method for strategy 'CLbreakout': Index is out of range. Index has to be nonnegative (bigger than zero, I am not sure how to say it in english) and has to be smaller than length of collection.

      I understand what you mean with this: Make sure you have enough bars in the data series you are accessing
      but that is not reason of my error - see my source code there nothing which could cause this error that I am trying to use older bars than the chart really contains.

      Maybe it could be caused when (in the source code) I am trying to use future bars.
      I mean for example:

      // I am on start of day, for example 8:00 AM and I am trying to use future bar.
      // Bar of 15:30 PM

      int futureBar_hour = 15;
      int futureBar_minute = 30;
      int bars_futureBar = Bars.GetBar(new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, futureBar_hour, futureBar_minute, 0));

      // the bars_futureBar integer tells me how much bars ago stands my futureBar
      // for example I have 1 minute TimeFrame and the current time is 14:15 PM so
      // the futureBar = -75 (because the bar with time=15:30 is -75 bars ago so this)
      // is negative number so it means the bar will be shown after 75 bars.
      // Another example: is 16:41 PM so the futureBar = 71 (because bar with time=15:30 is 71 bars ago)

      And I am using this technique in my source code so maybe this caused the error with the "index is out of range". Maybe the NinjaTrader doesn't have collection so big that I can looking for so far future bars.. ??

      But I have to tell you that I was also using this technique also in another live automated trading strategy and it is working good so I really don't understand what's wrong..??

      PLEASE help me resolve this issue. Thank you very much.

      Comment


        #4
        Hi Zooinek,

        Unfortunately, we don't support referencing future bars through negative index values.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Hello

          I wrote it incorrectly...
          the example should be this:





          namespace NinjaTrader.Strategy {



          public class CLbreakout : Strategy {


          #region Variable
          int tagId;
          int startOfTimeArray = 1440;
          int endOfTimeArray = 1501;
          int lastBarForE1 = 1510; // latest time util I can realize first entry in the day session
          int lastBarForE2 = 1530; // latest time util I can realize second entry in the day session
          int definitlyExit = 2200;
          double stopLossValue = 16;
          double profitTargetValue = 69;

          int startOfTimeArray_hour;
          int startOfTimeArray_minute;
          int endOfTimeArray_hour;
          int endOfTimeArray_minute;
          int lastBarForE1_hour;
          int lastBarForE1_minute;
          int lastBarForE2_hour;
          int lastBarForE2_minute;
          int definitlyExit_hour;
          int definitlyExit_minute;

          int bars_startOfTimeArray = 0;
          int bars_endOfTimeArray = 0;
          int bars_lastBarForE1 = 0;
          int bars_lastBarForE2 = 0;
          int bars_definitlyExit = 0;

          double timeArrayMax;
          double timeArrayMin;

          int countOfEntriesThisDay;

          #endregion


          protected override void Initialize() {
          CalculateOnBarClose = true;
          startOfTimeArray_hour = (int)startOfTimeArray/100;
          startOfTimeArray_minute = (int)startOfTimeArray%100;
          endOfTimeArray_hour = (int)endOfTimeArray/100;
          endOfTimeArray_minute = (int)endOfTimeArray%100;
          lastBarForE1_hour = (int)lastBarForE1/100;
          lastBarForE1_minute = (int)lastBarForE1%100;
          lastBarForE2_hour = (int)lastBarForE2/100;
          lastBarForE2_minute = (int)lastBarForE2%100;
          definitlyExit_hour = (int)definitlyExit/100;
          definitlyExit_minute = (int)definitlyExit%100;
          SetStopLoss(CalculationMode.Ticks, stopLossValue); // set stoploss
          SetProfitTarget(CalculationMode.Ticks, profitTargetValue); // set profittarget
          Add(PeriodType.Tick, 1);
          }


          protected override void OnBarUpdate() {

          #region defaul timeFrame
          if (BarsInProgress==0) {

          #region initialize the time points
          int year = Time[0].Year;
          int month = Time[0].Month;
          int day = Time[0].Day;
          bars_startOfTimeArray = CurrentBar - Bars.GetBar(new DateTime(year, month, day, startOfTimeArray_hour, startOfTimeArray_minute, 0, 0));
          bars_endOfTimeArray = CurrentBar - Bars.GetBar(new DateTime(year, month, day, endOfTimeArray_hour, endOfTimeArray_minute, 0, 0));
          bars_lastBarForE1 = CurrentBar - Bars.GetBar(new DateTime(year, month, day, lastBarForE1_hour, lastBarForE1_minute, 0, 0));
          bars_lastBarForE2 = CurrentBar - Bars.GetBar(new DateTime(year, month, day, lastBarForE2_hour, lastBarForE2_minute, 0, 0));
          bars_definitlyExit = CurrentBar - Bars.GetBar(new DateTime(year, month, day, definitlyExit_hour, definitlyExit_minute, 0, 0));
          #endregion

          #region inside the timeArray
          if (bars_startOfTimeArray>=0 && bars_endOfTimeArray<=0) {
          BackColor = Color.FromArgb(80,255,75,75);
          if (bars_startOfTimeArray==0) {
          timeArrayMax=High[0];
          timeArrayMin=Low[0];
          countOfEntriesThisDay = 0; // this will mare sure that every trading session starts with countOfEntriesThisDay = 0
          }
          if (High[0]>timeArrayMax) { timeArrayMax=High[0]; }
          if (Low[0]<timeArrayMin) { timeArrayMin=Low[0]; }
          if (bars_endOfTimeArray==0) {
          DrawLine("max"+(tagId++), bars_startOfTimeArray, timeArrayMax, 0, timeArrayMax, Color.Black, DashStyle.Solid, 1);
          DrawLine("min"+(tagId++), bars_startOfTimeArray, timeArrayMin, 0, timeArrayMin, Color.Black, DashStyle.Solid, 1);
          }
          }
          #endregion

          #region definitly exit
          if (bars_definitlyExit==0) {
          ExitShort();
          ExitLong();
          }
          #endregion

          }
          #endregion

          #region 1Tick timeFrame
          if (BarsInProgress==1) {

          #region entries
          #region long
          if (Close[0]>=timeArrayMax && Position.MarketPosition==MarketPosition.Flat && bars_endOfTimeArray>=0) {
          if (countOfEntriesThisDay==0 && bars_lastBarForE1<0) {
          EnterLong();
          countOfEntriesThisDay++;
          Print(Time[0].ToString()+"\t First enter today. LONG");
          }
          if (countOfEntriesThisDay==1 && bars_lastBarForE2<0) {
          if (Performance.AllTrades[0].Entry.MarketPosition==MarketPosition.Short && getLastProfit()<0) {
          EnterLong();
          countOfEntriesThisDay++;
          Print(Time[0].ToString()+"\t Second enter today. LONG");
          }
          }
          }
          #endregion

          #region short
          if (Close[0]<=timeArrayMin && Position.MarketPosition==MarketPosition.Flat && bars_endOfTimeArray>=0) {
          if (countOfEntriesThisDay==0 && bars_lastBarForE1<0) {
          EnterShort();
          countOfEntriesThisDay++;
          Print(Time[0].ToString()+"\t First enter today. SHORT");
          }
          if (countOfEntriesThisDay==1 && bars_lastBarForE2<0) {
          if (Performance.AllTrades[0].Entry.MarketPosition==MarketPosition.Long && getLastProfit()<0) {
          EnterShort();
          countOfEntriesThisDay++;
          Print(Time[0].ToString()+"\t Second enter today. SHORT");
          }
          }
          }
          #endregion
          #endregion

          }
          #endregion

          }


          #region another methods

          public double getLastProfit() {
          return Performance.AllTrades[0].ProfitPoints * Performance.AllTrades[0].Quantity;
          }

          #endregion


          }

          }





          This strategy is in the .zip file in my previous post in attachement.
          Please check that, because my code is corret but it is not working on the chart.
          The strategy freeze after EnterLong/EnterShort method and I dont know if its caused by the NinjaTrader or the price data.
          But its also freezing on simulated data feed so I dont this this would be error of price data..

          Maybe you have bug in NinjaTrader so please check this code - try to run this strategy you will see what happen

          Comment


            #6
            zooinek, to clarify the error you get shown in the first screenshot - there's no live data right now on the holiday for the simulation engine to process your order on the simulator.

            For the OnBarUpdate() error, you would need to check with Prints, Try / Catch where you run into an invalid index value in your arrays employed.
            BertrandNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by andrewtrades, Today, 04:57 PM
            1 response
            5 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by chbruno, Today, 04:10 PM
            0 responses
            3 views
            0 likes
            Last Post chbruno
            by chbruno
             
            Started by josh18955, 03-25-2023, 11:16 AM
            6 responses
            436 views
            0 likes
            Last Post Delerium  
            Started by FAQtrader, Today, 03:35 PM
            0 responses
            7 views
            0 likes
            Last Post FAQtrader  
            Started by rocketman7, Today, 09:41 AM
            5 responses
            19 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Working...
            X