Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

ToTime not exiting trades

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

    ToTime not exiting trades

    Hi,

    I have a program which i only want to run during the following hours:
    start at Session1Start (150000)
    end at Session1End (170000)

    My program only takes trades during these times but it will not exit the trade if it is already in the trade at 170000. I believe this is because it is not looking at the program after 170000 when it tells it to exit if still in the trade! Please help! Thank you very much!

    here is the code

    if ((ToTime(Time[0]) <= Session1Start || ToTime(Time[0]) >= Session1End))
    {
    return;
    }

    if ((ToTime(Time[0]) >= Session1Start && ToTime(Time[0]) <= Session1End))
    {
    enter here w certain conditions
    }



    if (Position.MarketPosition == MarketPosition.Short
    && ToTime(Time[0]) <= Session1Start || ToTime(Time[0]) >= Session1End )
    {
    ExitShort("short");
    }
    if (Position.MarketPosition == MarketPosition.Long
    && ToTime(Time[0]) <= Session1Start || ToTime(Time[0]) >= Session1End )
    {
    ExitLong("long");
    }

    #2
    Hello malhome,

    Thank you for your post.

    Your code prevents exits during these times, is this what you intend?

    Can you detail the expected behavior you want?

    Comment


      #3
      I want my trades to take place only during the hours of 150000-170000 and then the strategy to be flat. Currently it only opens trades during these hours, but is a trade starts before 170000 it doesn't exit after. I'd like it to exit as soon as the bar closes after 170000.
      Thanks for your help!

      Comment


        #4
        Hello malhome,

        Thank you for your response.

        You will want to remove the following line of code:
        Code:
        if ((ToTime(Time[0]) <= Session1Start || ToTime(Time[0]) >= Session1End))
        {
        return;
        }

        Comment


          #5
          Thank you works gr8!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by AttiM, 02-14-2024, 05:20 PM
          12 responses
          213 views
          0 likes
          Last Post DrakeiJosh  
          Started by cre8able, 02-11-2023, 05:43 PM
          3 responses
          237 views
          0 likes
          Last Post rhubear
          by rhubear
           
          Started by frslvr, 04-11-2024, 07:26 AM
          8 responses
          116 views
          1 like
          Last Post NinjaTrader_BrandonH  
          Started by stafe, 04-15-2024, 08:34 PM
          10 responses
          47 views
          0 likes
          Last Post stafe
          by stafe
           
          Started by rocketman7, Today, 09:41 AM
          3 responses
          12 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Working...
          X