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

Detect if an opposite trade is running

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

    Detect if an opposite trade is running

    Hi guys

    I have a strategy that execute both Long and Short trades in parallel. So even if a Long trade is already running and the price go in the opposite direction, the strategy execute another short trade while the Long trade is still running.

    I want that when a trade is running, the opposite trade execute with double the normal number of contracts.

    So let's say normally each trade, Long or Short, open with 10 contracts. this is when there is no opposite trading already running.
    But if a Long trade is running, I want the opposite trade, Short trade to open with 20 contracts instead of 10 contracts.
    and the opposite as well, if a Short trade is already running, the Long trade opens with 20 contracts instead of just 10 contracts.

    How can I achieve this?
    Thank you

    #2
    Hello onlinebusiness,

    Thank you for your note.

    You could add a check for a flat, long, or short position in your strategy logic. For example, if you want to open with 10 contracts when there is no position open, it could look something like this:

    if (Position.MarketPosition == MarketPosition.Flat && conditionForEntry)
    {
    // enter 10 long or 10 short
    }

    Then, you could also add in something like the following:

    if (Position.MarketPosition == MarketPosition.Long && conditionsForShortEntry)
    {
    // enter short 20 contracts
    }

    or

    if (Position.MarketPosition == MarketPosition.Short && conditionsForLongEntry)
    {
    // enter long 20 contracts
    }

    If you are using the Managed Approach, Entry() methods automatically reverse a position. For example, if you are in a 10 long position and now call EnterShort(10) -> you will see 2 executions, one to close the long position and the other to get you into the desired 10 contract short position. This is noted in the bullet points on the following page of the help guide:


    Please let me know if I may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      I think the problem is because I am running 2 copies of the same strategy, a copy for Long trades and copy for the Short trades.
      Problem is that this is the only way I can run this strategy, I have to add it twice to the chart, and specify one to for Long trade and the other for Short trades. I can't add just one doing Long and Short at the same time.
      That's why the condition on the Short copy doesn't detect the running trade from the Long copy.


      What do you think, is there anyway to go around this issue?

      Comment


        #4
        Hello onlinebusiness,

        Thank you for your reply.

        Your first condition would not evaluate to true because the position can not be long and flat at the same time
        if ((Position.MarketPosition == MarketPosition.Long) && (PriceBreakoutRectangularBase(upwardBreakout,Volum eMultiple).Plot0[0] == -1 && Position.MarketPosition == MarketPosition.Flat))​

        As for orders not being placed, did you leave the comment "// enter Short 20 contracts" inside of your conditions, or did you use an order entry method? I left that as a comment because the type of order you'd like to use for your entry is up to you and also depends on if you're using the managed approach or an unmanaged strategy. The order methods for both approaches are listed here:


        Another way to better understand a script when it is not behaving as expected is to add debugging prints. This process is explained in-depth at the following link:


        Please let me know if I may be of further assistance.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Nevermind, I figured it out.
          Thankyou

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by futtrader, 04-21-2024, 01:50 AM
          5 responses
          56 views
          0 likes
          Last Post NinjaTrader_Eduardo  
          Started by PeakTry, Today, 10:49 AM
          0 responses
          2 views
          0 likes
          Last Post PeakTry
          by PeakTry
           
          Started by llanqui, Today, 10:32 AM
          0 responses
          5 views
          0 likes
          Last Post llanqui
          by llanqui
           
          Started by StockTrader88, 03-06-2021, 08:58 AM
          45 responses
          3,992 views
          3 likes
          Last Post johntraderuser2  
          Started by TAJTrades, Today, 09:46 AM
          0 responses
          8 views
          0 likes
          Last Post TAJTrades  
          Working...
          X