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

set up condition to exit trade after 10 days

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

    set up condition to exit trade after 10 days


    so i am trying to set up a additional condition that will execute a exit if the trade has been going for more than 10 days, but its not working, could i get some assistance on the correct way to make this happen? any assistance would be wonderful

    this is the on bar update section :

    protected override void OnBarUpdate()

    {

    // Ensure the code is executed only for the primary data series

    if (BarsInProgress != 0)

    return;





    if (CurrentBar < 10)

    return;



    ManageEntry();

    ManageExit();

    CheckTradeDuration(); // Call the new method to check the trade duration






    }


    below is the entrance conditions , i have a separate section that deals with the main exit and that is working properly

    }



    // enter short position based on the trend confirmation

    else if (isPivotConfirmTrend2)

    {

    EnterShort(Contracts,@"myEntryshort");

    Print("Entered Short position"); // Added debug message

    OkToTrade = true;

    entryDate = Time[0]; // Store the entry date





    }


    this is the section for the trade duration exit


    private void CheckTradeDuration()

    {

    if (Position.MarketPosition != MarketPosition.Flat)

    {

    if (Time[0].Date.Subtract(entryDate.Date).TotalDays >= 10)

    {

    if (Position.MarketPosition == MarketPosition.Long)

    {

    ExitLong("Exited after 10 days");

    }

    else if (Position.MarketPosition == MarketPosition.Short)

    {

    ExitShort("Exited after 10 days");

    }

    }

    }

    }

    #2
    Hello thehammer,

    To do something after a period of time you would need to store a DateTime and use math similar to what you have. Have you used a print to try and debug what you created? You could start by printing the value in your condition to check what the math is returning:

    Print(Time[0].Date.Subtract(entryDate.Date).TotalDays);
    JesseNinjaTrader Customer Service

    Comment


      #3
      The "position" object might not be showing the actual position... (which has happened to me)

      Based on my experience, in my strats, I use the "OnExecutionUpdate" event and set a variables to the position and entry time when the trade actually has happened...

      https://​​​​​​​ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?syncing_account_posi tions.htm

      I took this approach as using the "Position.MarketPosition" property did not seem to be 100% accurate.

      Also, looking at your code, your "entryDate" approach will only work if you have only one entry at a time...

      Note: I trade FOREX with sometimes over 50 trades daily, so the "Position.MarketPosition" MAY work just fine for you but take a look at the "onExecutionUpdate".
      .

      Comment


        #4
        so this is my debug statements and code below :


        and the output was this message over and over:

        Exited short position after 10 days

        1037


        so not sure whats happening cause the trade in the back test is still stuck




        private void CheckTradeDuration()

        {



        if (Position.MarketPosition != MarketPosition.Flat)

        {




        if (Time[0].Date.Subtract(entryDate.Date).TotalDays >= 10)



        {

        Print(Time[0].Date.Subtract(entryDate.Date).TotalDays);



        }



        {

        if (Position.MarketPosition == MarketPosition.Long)

        {

        ExitLong(@"Exited after 10 days");

        Print("Exited long position after 10 days"); // Debug statement

        }

        else if (Position.MarketPosition == MarketPosition.Short)

        {

        ExitShort(@"Exited after 10 days");

        Print("Exited short position after 10 days"); // Debug statement

        }

        }

        }

        }

        Comment


          #5
          you said your trade exit was OK; how do you know the exit condition was OK and accepted as a valid exit order?
          This is why I suggested using the "OnExecutionUpdate" which can confirm that the exit order was executed.
          >>Just because you send an exit, it does not insure that the position was actually exited; might not have the the liquidity at the price specified so will not be closed... (and other factors)
          AND, unless you reset the entry date variable when you exit, you will get the message over and over.
          I would set an exit variable to the exit time and look at that to signal, then reset both to get ready for the next trade.
          Also, the functions BarsSinceEntryExecution() and BarsSinceExitExecution() , might be useful... look at the docs...​

          Comment


            #6
            waitFX thanks for your suggestions!


            this ended working as intended with no issues finally,

            the issue was naming the exits initially and and the wrong placement of the exit: if and else if


            private void CheckTradeDuration()

            {



            if (Position.MarketPosition != MarketPosition.Flat)

            {




            if (Time[0].Date.Subtract(entryDate.Date).TotalDays >= 60)



            {

            Print(Time[0].Date.Subtract(entryDate.Date).TotalDays);



            if (Position.MarketPosition == MarketPosition.Long)

            {

            ExitLong();

            Print("Exited long position after 10 days"); // Debug statement

            }



            else if (Position.MarketPosition == MarketPosition.Short)



            {

            ExitShort();

            Print("Exited short position after 10 days"); // Debug statement

            }



            }





            }

            }

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by AaronKoRn, Yesterday, 09:49 PM
            0 responses
            11 views
            0 likes
            Last Post AaronKoRn  
            Started by carnitron, Yesterday, 08:42 PM
            0 responses
            10 views
            0 likes
            Last Post carnitron  
            Started by strategist007, Yesterday, 07:51 PM
            0 responses
            12 views
            0 likes
            Last Post strategist007  
            Started by StockTrader88, 03-06-2021, 08:58 AM
            44 responses
            3,982 views
            3 likes
            Last Post jhudas88  
            Started by rbeckmann05, Yesterday, 06:48 PM
            0 responses
            10 views
            0 likes
            Last Post rbeckmann05  
            Working...
            X