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

Newbie - close position if setup no longer applies

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

    Newbie - close position if setup no longer applies

    So I'm a newbie coder and trying to program a strategy.

    Let's say I have a set of conditions that trigger an entry. Then say some bars later the trade is still open, but a new setup occurs in the opposite direction. I'd like to close the open position and then possibly open one in the opposite direction, a reversal if you will.

    Here's how I've coded it. However, i'm getting some mixed results with Market Replay. sometimes it works, sometimes it doesn't. what it seems to do is close the open position but doesn't open the new position
    // Condition set 1 short
    if (Falling(MACD()
    {
    DrawDot("" + CurrentBar, false, 0, High[0] + 2 * TickSize, Color.Red);
    ExitLong(); //close open long position
    EnterShortLimit(Contracts, Close[0] + Retrace * TickSize, "BGS");
    SetProfitTarget("BGS", CalculationMode.Ticks, Ttarget);
    SetStopLoss("BGS", CalculationMode.Ticks, Sstop, false);

    also, i put the Target and Stop Loss with the order so i can use different target and or stop loss for other conditions... is that correct also?

    thank you for any help to a novice
    Last edited by rsmtrade; 02-15-2016, 03:10 PM.

    #2
    Hello rsmtrade,

    There is behavior where the strategy will double the opposite direction or result in an overfill if an Exit and an Entry are called and filled on the same bar.

    When calling an entry method in the opposite direction of your position this will cause your position to be reversed. NinjaTrader will automatically submit an order to close your existing position and then enter an order to enter you into the opposite position.
    If you exit and then call an entry method in the same run of OnBarUpdate, the OnPositionUpdate() will not have yet run and NinjaTrader will not have known that your position is closed. This will cause both actions to complete and end up sending 3 orders. The first order is the exit position from your exit method, the second order is to close the position from NinjaTrader automatically reversing your position, the third order is to enter you into the opposite position.
    This is not elaborated on in the help guide but the help guide does say "Entry methods are used to submit orders that create a market position if none exists or to reverse an existing position.". This is in the Entry Methods section in the link below.
    http://www.ninjatrader.com/support/h...d_approach.htm

    Basically, to avoid any issues, you can do one of two things.
    Allow the existing position to remain until the order to enter the opposite direction fills, at which point ninjatrader will automatically reverse your position (placing a order automatically to close the old position).
    All this would require is removing the Exit call.

    Or, detect that the exit has filled, submit the limit in the opposite direction when that is detected in OnOrderUpdate when the IOrder.OrderState == OrderState.Filled.


    Also, do you have prints in your code that are printing the values of the variables used in conditions that place orders? These values would tell you why a script will have an if statement evaluate as true or false.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      thank you.

      i removed the "exit" calls

      what i see happening now is that if a position is open, it stays open until either the target or stoploss is hit. even if on a new bar there is a signal to go the opposite way. i was expecting it to reverse the position.

      Comment


        #4
        Hi rsmtrade,

        Are there messages about orders being ignored in the Output Window?

        Are there messages about orders being cancelled due to the live until cancelled?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          i'm sorry i don't see any messages... but i really don't know about the process of messages so i'm sure i'm missing something. at the moment it seems to work good enough for me. i'll work at finding someone who can help me one on one. appreciate your help

          Comment


            #6
            Hello rsmtrade,

            The messages about an order being ignored will be in the Output window.

            Tools -> Output Window...

            This also requires that TraceOrders = true; be added to the Initialize() method of the script.

            http://ninjatrader.com/support/helpG...tools_menu.htm

            http://ninjatrader.com/support/helpG...raceorders.htm

            Using prints and trace orders (which output to the output window) is the process for debugging any script.

            By removing the exits, the enter long should still placed and should cause the strategy to flip the position as long as the order isn't ignored and as long as the order to enter the new position is not cancelled due to liveUntilCancelled being set to false.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by DJ888, 04-16-2024, 06:09 PM
            3 responses
            10 views
            0 likes
            Last Post NinjaTrader_Erick  
            Started by RookieTrader, Today, 07:41 AM
            0 responses
            2 views
            0 likes
            Last Post RookieTrader  
            Started by maybeimnotrader, Yesterday, 05:46 PM
            1 response
            18 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by Perr0Grande, Yesterday, 08:16 PM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by f.saeidi, Yesterday, 08:12 AM
            3 responses
            25 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Working...
            X