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

Timing sequence of functions

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

    Timing sequence of functions

    I have this block of code which is supposed to exit a position, and then enter an opposing position on the same criteria. Originally it was causing a double entry which i guessed it was because the code was being run at basically the same time, causing an exit then the entry of the opposing entry was resulting in a "Close position" as well. I added a check to make sure market position is flat, but now the entry is not being triggered which i guess is due to the same reason. What is the best way to address this?

    Code:
    if (Close[0] > emaHigh)
    {
    // Exit any short positions if we get a close above candle high 34ema
    if (Position.MarketPosition == MarketPosition.Short)
    {
    var exitPosition = ExitShort();
    exitPosition.Name = "EMA Above";
    Print(DateTime.Now + ": " + "Exited shorts due to EMA Above");
    }
    if (longsAllowed && longsOn && Position.MarketPosition == MarketPosition.Flat) // Checks if longsOn flag and longsallowed true or not
    {
    // Enter trade
    for (int i = 0; i < EntriesPerTrade; i++)
    {
    // Enter long position with a unique name and set the take profit and stop loss levels
    EnterLong(1, "Long" + i);
    SetProfitTarget("Long" + i, CalculationMode.Ticks, TakeProfitLevels[i]);
    SetStopLoss("Long" + i, CalculationMode.Ticks, InitialStopLoss, isSimulatedStop);
    Print(DateTime.Now + ": " + "onBarUpdate Entered Long" + i + " at price: " + Position.AveragePrice);
    positionNames.Add("Long" + i); // Add entry name to list
    }
    stopLossesModified = false;
    }
    // Do not allow anymore longs until the criteria for short has been met first
    longsAllowed = false;
    shortsAllowed = true;
    }

    #2
    Hello sofortune,

    If your entry is not being summited you would need to use prints to see which part of the condition is not becoming true and modify it.

    Submitting an exit in the same event as an entry will cause a reversal and an entry which will double the position in the opposite direction.

    One other note is that you should always call the Set methods before the entry method:

    SetProfitTarget("Long" + i, CalculationMode.Ticks, TakeProfitLevels[i]);
    SetStopLoss("Long" + i, CalculationMode.Ticks, InitialStopLoss, isSimulatedStop);​​
    EnterLong(1, "Long" + i);
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello sofortune,

      If your entry is not being summited you would need to use prints to see which part of the condition is not becoming true and modify it.

      Submitting an exit in the same event as an entry will cause a reversal and an entry which will double the position in the opposite direction.

      One other note is that you should always call the Set methods before the entry method:

      SetProfitTarget("Long" + i, CalculationMode.Ticks, TakeProfitLevels[i]);
      SetStopLoss("Long" + i, CalculationMode.Ticks, InitialStopLoss, isSimulatedStop);​​
      EnterLong(1, "Long" + i);
      Oh, i am pretty sure the part of the condition which is not true is: Position.MarketPosition == MarketPosition.Flat because when that statement is probably running before the position could fully exit. Is there a way to work around this? I am doing all of this in onBarUpdate, not sure if it is the right place for it.


      Thanks for the advice for the set methods, just curious what is the reason for that?


      Thanks.

      Comment


        #4
        Hello sofortune,

        You would need to reformat your conditions and make a specific condition for your entry. Right now it looks like you are combining the exit and entry conditions which wont work. You would need to make a seperate condition checking if the position is flat for the entry to ensure that condition is not called when you exit the position. If your entry and exit conditions happen at the same time that will lead to reversals and duplication of positions.

        The reason to call the set methods first is to ensure they use the values you supply with the variable. The targets are submitted upon the entry fill and not when you call the Set method so it should have the values you want to use ahead of time. ​
        JesseNinjaTrader Customer Service

        Comment


          #5
          Could you give me a suggestion how to change the existing code?

          The exit and entry conditions are the same basically. Just a seperate if statement with the same criteria, would that do it?
          Last edited by sofortune; 08-09-2023, 09:00 AM.

          Comment


            #6
            Hello sofortune,

            If the conditions are essentially the same you would need to seperate those parts that into another condition which also has those same conditions.

            I would suggest using the strategy builder for this. You can make two sets, one set with your entry conditions and checking for flat and another set with your exit conditions and checking for being in a position. After doing that click View Code and that will show you how the code would be structured for two separate conditions.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Jesse View Post
              Hello sofortune,

              If the conditions are essentially the same you would need to seperate those parts that into another condition which also has those same conditions.

              I would suggest using the strategy builder for this. You can make two sets, one set with your entry conditions and checking for flat and another set with your exit conditions and checking for being in a position. After doing that click View Code and that will show you how the code would be structured for two separate conditions.
              So you mean i basically need to break it up into two different sets of conditions. But if you look at my code above, i've essentially done that as i have two seperate nested if statements with different criteria. Does the onbarupdate process all functions at the same time essentially? even if i have the exit criteria first, before entering long, both are seperate if statements inside the onbarupdate, will this cause them both to trigger before the postions can be updatd?

              Comment


                #8
                I have done this, with two seperate conditions, but it is giving the same thing:
                Code:
                if (Close[0] > emaHigh)
                {
                ExitShortPositionsIfAboveEMAHigh();
                }
                // Close Longs and enter Shorts
                else if (Close[0] < emaLow)
                {
                ExitLongPositionsIfBelowEMALow();
                }
                
                if (Close[0] > emaHigh)
                {
                EnterLongPositionsIfAllowed();
                }
                // Close Longs and enter Shorts
                else if (Close[0] < emaLow)
                {
                
                EnterShortPositionsIfAllowed();
                }

                Comment


                  #9
                  Hello sofortune,

                  OnBarUpdate runs all of the code at once which is why you would need separate conditions for entries and exits. The position won't be updated immediately, it needs time for the order to fill and the position to change. You would only see that update on the following OnBarUpdate event.

                  If your current condition is not becoming true you would have to use prints to see which part is not true when you run it to get a better idea of what needs changed. The alternative would be to completely separate the logic into two sets so that you can be certain that one part of your logic is not effecting the other part.
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Sorry, when you say seperate, do you mean different? Because i've sperated them here.

                    Essentially the logic is

                    When criteria for Long is met:

                    1) exit short
                    2) check marketposition.flat then enter long

                    You confirmed what i suspected that all the code is being run at once which caused this. I know which part is not becoming true, it is the marketposition.flat because the all the code is run at once. So it is not a logic issue from what i can tell. Is there a way you can suggest for me to address this problem?

                    Comment


                      #11
                      Sorry, maybe there is another approach. The main reason i want to exit the position first, is so i can assign a unique name to that exit. Since ninjatrader will automatically exit positions of the opposing direction when entering a new position, is there anyway to assign that a exit a unique name. Currently it shows as "Close Position" Only.

                      Comment


                        #12
                        Hello sofortune,

                        What you are describing is a reversal, in that case you would just call the opposite entry order. That closes the position and enters into the opposite direction. If you want to use exit orders then you need to make a specific condition to exit when the position is short and then another separate condition to enter long when flat. Once the account becomes flat the entry could work, that won't be in the same OnBarUpdate event.

                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Jesse View Post
                          Hello sofortune,

                          What you are describing is a reversal, in that case you would just call the opposite entry order. That closes the position and enters into the opposite direction. If you want to use exit orders then you need to make a specific condition to exit when the position is short and then another separate condition to enter long when flat. Once the account becomes flat the entry could work, that won't be in the same OnBarUpdate event.
                          Ah, let me try that then.

                          Comment


                            #14
                            Originally posted by NinjaTrader_Jesse View Post
                            Hello sofortune,

                            What you are describing is a reversal, in that case you would just call the opposite entry order. That closes the position and enters into the opposite direction. If you want to use exit orders then you need to make a specific condition to exit when the position is short and then another separate condition to enter long when flat. Once the account becomes flat the entry could work, that won't be in the same OnBarUpdate event.
                            Sorry i misunderstood your reply. Yes that's right, just calling the opposite entry order can achieve the same thing but is there a way for me to give the exit of the prior position a custom name through this method?

                            Comment


                              #15
                              Hello sofortune,

                              The exit for a reversal is shown as close position, there is not a way to rename that trade. You would have to use an Exit order if you wanted a custom name.
                              JesseNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by AlgoDreamer, Today, 12:39 PM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by ninza33, Today, 12:31 PM
                              1 response
                              4 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by nleitman, Yesterday, 11:46 AM
                              17 responses
                              45 views
                              0 likes
                              Last Post nleitman  
                              Started by tradingnasdaqprueba, Today, 03:42 AM
                              7 responses
                              32 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by kaywai, Today, 11:59 AM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Working...
                              X