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

Algo won't buy and sell at same price of same candle

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

    Algo won't buy and sell at same price of same candle

    Hi,

    I have an algo that buys/sells and then exits at the end of the candle, or rather the open of the next candle after the exit signal is issued as I am trading with CalculateOnBarClose = True. On rare occasions, the algo may get several entry signals in a row.

    If the second trade signal is in the same direction as the first then I would get a sequence of events like:

    @12:00 close of candle: go long signal generated
    @12:01 open of candle: long actioned
    @12:01 close of candle: exitlong is declared, followed by a go long signal again
    @12:02 open of candle: only the first long is exited (i.e a short trade is made) with no second long entered at this same open price

    If the second signal is a trade in the opposite direction there is no issue and I could get a long to exit a short in conjunction with a long entry at the same open price. Is this by design in NinjaTrader? Is this due to NinjaTrader not wanting to appear to wash trade? Is the simplest solution to simply not exit at all if a second long entry in a row is signalled?

    Cheers,
    darmbk.

    #2
    Hello,

    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 Position.MarketPosition will not have known that your position is closed and will combine your orders 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
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the response Chelsea,

      I have a follow up question however. Originally I was also trying to calculate the PnL by using the entry price and the stop's exit price before moving on to the next entry at the same price in the same candle as the stop. Now I have no stop, only a reversal entry by using an entry function in the opposite direction of the prior entry. So how do I calculate the PnL in this case?

      Thanks,
      darmbk.

      Comment


        #4
        Hi darmbk,

        In this case NinjaTrader will submit an order for you with the name 'Close position' to close your order.

        This order does trigger OnOrderUpdate and you can detect the order with order.Name == "Close position".

        (edit)
        Also, you can get the PnL from the last completed trade with:
        Code:
        Performance.AllTrades[Performance.AllTrades.Count-1].ProfitCurrency;
        http://www.ninjatrader.com/support/h.../alltrades.htm
        Last edited by NinjaTrader_ChelseaB; 06-05-2014, 07:16 AM.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Chelsea,

          I presume I can use this for ticks as well? Like this...

          Performance.AllTrades[Performance.AllTrades.Count-1].ProfitTicks;

          If the ClosePosition is actioned prior to the new Entry then I am sorted. I think calculating the PnL using the exit price from the execution of ClosePosition should work right? I am assuming that the ClosePosition gets actioned prior to the new entry so that my old entry price is still stored in a variable I would have defined as the entry price iteratively.

          Thanks,
          darmbk.
          Last edited by darmbk; 06-06-2014, 02:07 AM.

          Comment


            #6
            Hi darmbk,

            Yes, that is the way to get the profit in ticks from the last completed trade.

            Also, that is correct. The 'Close position' order is sent before the new entry order.

            When you say "so that my old entry price is still stored in a variable I would have defined as the entry price iteratively." I'm not quite sure what this means.

            If you are saving this to a variable it will be up to your code logic on when this variable is updated..

            If you are referring to the IOrder that is saved in the trades collection, yes this will still have the entry price..
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by aussugardefender, Today, 01:07 AM
            0 responses
            3 views
            0 likes
            Last Post aussugardefender  
            Started by pvincent, 06-23-2022, 12:53 PM
            14 responses
            238 views
            0 likes
            Last Post Nyman
            by Nyman
             
            Started by TraderG23, 12-08-2023, 07:56 AM
            9 responses
            384 views
            1 like
            Last Post Gavini
            by Gavini
             
            Started by oviejo, Today, 12:28 AM
            0 responses
            4 views
            0 likes
            Last Post oviejo
            by oviejo
             
            Started by pechtri, 06-22-2023, 02:31 AM
            10 responses
            125 views
            0 likes
            Last Post Leeroy_Jenkins  
            Working...
            X