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

How to handle Unmanaged BiDirectional AccountPosition

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

    How to handle Unmanaged BiDirectional AccountPosition

    Hello Experts.

    I'm relatively new to NinjaScript, but I have coded before in C# so I know the basics. What I am trying to create is a certain strategy that allows me to go short and long at the same time. If I understood correctly, the only way to do this is to us the Unmanaged approach. For now, my strategy only runs on 1 data serie. Currently, what I do is the following:
    Open Sell Position:
    v_OpenOrders[0] = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Market, Convert.ToInt32(v_LotIncrement[0]));
    Open Buy Position:
    v_OpenOrders[0] = SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Market, Convert.ToInt32(v_LotIncrement[0]));
    WHERE >>> v_OpenOrders[] is an Order[] and v_LotIncrement[] a double[] that contains a predefined set of volumes.

    After a while I might have executed orders (and thus open position=) like
    1000 Long at 1.1300
    2000 Short as 1.1250
    3000 Long at 1.1280

    First of all, am I correct to say that:
    - PositionAccount.AveragePrice will give me the BreakEvenPrice level. In other words, it is the average weighted price of all my orders?
    - How do I close all orders at once by Mar****rder? I saved the orders in the v_OpenOrders array.
    - How do I know in the end, after all orders are closed what was my eventually realized PNL?

    Thanks a lot in advance
    Paulito



    #2
    Hello Paulito123,

    Thanks for your post.

    It would not be possible to have a strategy that is both long and short at the same time. As the orders would be sent to the account, the associated buys and sells would render the position flat or in a different position that would likely not be inline the goal of your strategy. You would need separate accounts to have both long and short positions at the same time.

    - PositionAccount.AveragePrice will give me the BreakEvenPrice level. In other words, it is the average weighted price of all my orders?
    PositionAccount will give information on the actual account position. For consistency, I would recommend using Position.AveragePrice which would be the Average Entry Price for the strategy's current position.

    Position - https://ninjatrader.com/support/help...s/position.htm

    - How do I close all orders at once by Mar****rder? I saved the orders in the v_OpenOrders array.
    Using the Unmanaged Approach, we tell NinjaTrder that we do not want to use any pre-managed behaviors offered in the Managed Approach and we want to do everything ourselves. The order objects would have to be cancelled with CancelOrder and your market order would be used to close the total position quantity.

    CancelOrder - https://ninjatrader.com/support/help...ancelorder.htm

    - How do I know in the end, after all orders are closed what was my eventually realized PNL?
    Strategy performance can be fetched with SystemPerformance.AllTrades.TradesPerformance. PnL can also be fetched from the Trade object from a Trades collection. Sample code for using these items and checking the last Trades PnL can be found in the documentation items linked below. OnPositionUpdate can also be used to print out these values after the strategy has returned to a flat position.

    TradesPerformance - https://ninjatrader.com/support/help...erformance.htm

    Trade Collection - https://ninjatrader.com/support/help...collection.htm

    Trade - https://ninjatrader.com/support/help...n-us/trade.htm

    OnPositionUpdate - https://ninjatrader.com/support/help...tionupdate.htm

    Help guide information is publicly available.

    Please let us know if you have any additional questions.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hello Jim

      First of all, thanks for your quick response.

      I'm very much surprised that it is not possible to take a long and short position at the same time when trading Forex. (I did not mention in my first post that I was going to trade forex and that I'm not a US resident, I hope that makes a difference). This would really kill my strategy. I need to be able to hedge my wrong trades.

      On the website of FXCM, I found this:
      How do I enable hedging?
      Hedging can be turned on by logging into www.myfxcm.com and selecting "Hedging Functionality" from the "Account Settings" drop down menu.
      Also here: https://www.fxcm.com/ph/products/overview/ the hedging capabilities are mentioned for CFD's...

      With the open orders i mentioned in my previous post, are meant filled orders. I didn't describe it correctly, mea culpa. So looking again at SubmitOrderUnmanaged(), i noticed this:
      OrderAction.Buy
      OrderAction.BuyToCover
      OrderAction.Sell
      OrderAction.SellShort

      Could it be that I would have to open a BuyToCover trade for the same quantity as the sum of all the filled Sell orders I have opened? If not, could you then explain what the BuyToCover and SellShort properties are used for?

      The rest is clear, so far...

      Thanks again for the information
      Best regards
      Paul

      Comment


        #4
        Originally posted by NinjaTrader_Jim View Post
        You would need separate accounts to have both long and short positions at the same time.
        Hello experts

        How can this be accomplished? I can only operate one account by strategy, if I recall correctly...

        Thanks in advance
        Paul

        Comment


          #5
          Hello Paul,

          Thanks for clarifying on your intentions.

          Hedging Accounts are not supported. We are tracking interest in supporting them in a future version of NinjaTrader with SFT-1458. I'll add a vote on your behalf.

          BuyToCover is a exit from a short position while SellShort is an entry to a short position.

          Even if you use two strategies or use the Unmanaged Approach to open positions in opposite directions out side of the Managed Approach's rules, the account position will be long, short or flat.

          Some clients have found a workaround by creating subaccounts with FXCM and using separate strategies assigned to each account to handle this. We do not support inter-strategy communication, but if you would like to implement your own ways of having scripts communicate with each other you could add code in the AddOn namespace. An example that outlines this is linked below.

          https://ninjatrader.com/support/foru...956#post733956

          Let me know if I can be of further assistance.
          JimNinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_Jim View Post
            Hello Paul,

            Some clients have found a workaround by creating subaccounts with FXCM and using separate strategies assigned to each account to handle this. We do not support inter-strategy communication, but if you would like to implement your own ways of having scripts communicate with each other you could add code in the AddOn namespace.

            Let me know if I can be of further assistance.
            Could you specify more ? Once I'm connected I may have a "long" strategy "Long" which will operate on sub-account "1" and another short strategy "Short" operating on sub-account "2" ?

            Regarding Oanda connection via continuum as an example - can I have two active connection to my two subaccounts simultaneously , or should I swap between them before posting a "Long" or a "Short" ?

            The same question about FXCM Connection ?

            Thanks ,

            Comment


              #7
              Hello xTrader1,

              Yes, you would apply the Long/Short strategies on separate accounts so they would not compete. "Long" can be on "1" and "Short" can be on "2."

              Continuum based connections do not support multiple connections from NinjaTrader, but FXCM connections would allow you to configure 2 separate connections and connect to them at the same time.

              We look forward to assisting.
              JimNinjaTrader Customer Service

              Comment


                #8
                Well, Thanks.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by CortexZenUSA, Today, 12:53 AM
                0 responses
                1 view
                0 likes
                Last Post CortexZenUSA  
                Started by CortexZenUSA, Today, 12:46 AM
                0 responses
                1 view
                0 likes
                Last Post CortexZenUSA  
                Started by usazencortex, Today, 12:43 AM
                0 responses
                5 views
                0 likes
                Last Post usazencortex  
                Started by sidlercom80, 10-28-2023, 08:49 AM
                168 responses
                2,265 views
                0 likes
                Last Post sidlercom80  
                Started by Barry Milan, Yesterday, 10:35 PM
                3 responses
                11 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Working...
                X