Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy error when converting current days, month and year into whole numbers.

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

    Strategy error when converting current days, month and year into whole numbers.

    Hello, I have a strategy to which I must manually enter the time I want it to run, but I have problems to automatically set the current day. I am sending you what the code I have would be like.
    And it tells me that I should have 4 or 5 bars, I don't quite understand how to do it.
    The error is given when I put in the strategy day1, month1, year1. (these are "int")





    protected override void OnBarUpdate()
    {
    if (State == State.Historical)
    return;


    day1=Time[0].Day;
    month1=Time[0].Month ;
    year1=Time[0].Year ;

    }


    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
    {
    if (marketDataUpdate.MarketDataType != MarketDataType.Last || marketDataUpdate.Volume < BlockSize)
    return;

    var date1 = new DateTime(year1, month1, day1, 2, 0, 0);
    var date2 = new DateTime(year1, month1, day1, 3, 5, 25);

    if ( marketDataUpdate.Time> date1) && ( marketDataUpdate.Time< date2)
    Print("");


    }




    Regards.

    #2
    Hello iradielt,

    Thank you for the post.

    From the snippet I can't tell what error you are seeing however it looks like you are trying to compare the current day and a time within the day. If so you can just use ToTime for that purpose, there is a sample condition here: https://ninjatrader.com/support/help...ightsub=totime

    If you can otherwise provide the error you are seeing that would help provide more context about the problem.

    If you are seeing a BarsAgo error that generally means you need to add a condition to check if enough bars are available. From the given sample I don't see what that would be. I would suggest to retest the script and locate the specific code which relates to that error and then reply back with that. We could take a look at why you may be seeing a BarsAgo error based on that code.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi, thank you for you answer.
      The problem I have is that I have to compare it within OnMarketdepth with marketDataUpdate.Time and this has year, days, minutes .....
      Anyway, I need to buy a time in seconds, could I only do this if I have a timeframe of 1 second?

      So how do I do it, could it work?


      protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
      {
      if (marketDataUpdate.MarketDataType != MarketDataType.Last || marketDataUpdate.Volume < BlockSize)
      return;


      if (ToTime(Time[0]) >= 74500 && ToTime(Time[0]) <= 134500)
      Print("");


      }




      Comment


        #4
        Hello iradielt,

        Thank you for the reply.

        In what you provided you would replace Time[0] with the marketDataUpdate.Time if the OnMarketData time is what you intended to use.



        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello, that is correct I intend to use the time of marketDataUpdate.Time, which is not like automatically putting the year, months and days in integers..
          How can I automatically assign the day, month and year? . Can you help me with this.


          " var date1 = new DateTime(year1, month1, day1, 2, 0, 0); "


          If I put this in "OnBarUpdate ()" my strategy gives me an error and it is not enabled. I am working in 1 second timeframe.



          protected override void OnBarUpdate()
          {
          if (State == State.Historical)
          return;


          day1=Time[0].Day;
          month1=Time[0].Month ;
          year1=Time[0].Year ;

          }






          Comment


            #6
            Hello iradielt,

            ToTime can be used with DateTime objects so you don't need to create a custom DateTime like you are doing.

            If you wanted to check a span of time in OnMarketData using the passed in times you could use:

            Code:
            if (ToTime(marketDataUpdate.Time) >= 74500 && ToTime(marketDataUpdate.Time) <= 134500)
            That would check that the ticks time is greater than 7:45 and less than 1:45.

            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Thank you very much for your help, I have already solved it.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by RT001, Yesterday, 03:47 PM
              3 responses
              25 views
              0 likes
              Last Post NinjaTrader_Jim  
              Started by johnMoss, Yesterday, 10:40 PM
              1 response
              10 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by torch2k, Today, 06:24 AM
              1 response
              6 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by WalterSkinner, Yesterday, 02:37 PM
              4 responses
              22 views
              0 likes
              Last Post WalterSkinner  
              Started by Fer123, Yesterday, 04:57 PM
              1 response
              9 views
              0 likes
              Last Post NinjaTrader_Jim  
              Working...
              X