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

Close position on loss

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

    Close position on loss

    Hello,
    I am trying to setup something that should be simple, however me != programmer and after countless attempts with the "Strategy builder" i finally gave up! Will appreciate if somebody can help me:

    A strategy (or may be addon, please mods move the thread in another section if this is not the correct one) monitoring my position and if a possition goes below XXX usd lost close it without confirmation, together with any Stop/Limit orders available. Like if you press the "Close" button on the chart trader. Basically this has to work like a stop loss order. Yes i can just put stop lost order but sometime i make mistakes and want to be sure my position will be closed whatever happens.

    The only parameter here will be the maximum lost in currency.

    And i should be able to make new orders, from say another chart with the same instrument or superDOM, which will be monitored by the strategy once they get filled.

    I think it can not be done only with strategy builder, because strategy created with strategy builder will monitor orders created only with that strategy (i could be wrong here but after so many attempts i gave up).

    I have found on the web what i am looking for here :http://www.thetraderisk.com/ninjatrader-risk-analyzer/ but so far no response from the site.

    Thanks in advance for any help!

    #2
    Hello icog111, and thank you for your question. ExitLong and ExitShort can be called safely whether or not you are actually in a position. We can take advantage of this fact when we want to close a position; calling both of these back-to-back without arguments will close your position. We can check your position's PnL with Strategy -> UnrealizedPnL. Positive numbers are gain, negative numbers are loss. We'll want to check currency, and 0 bars ago. We can compare this to see if it is less than a Misc -> Numeric Value to check to see if your loss has exceeded a specified amount.

    I have included a picture where both these are set. Please let us know if there are any other ways we can help.
    Attached Files
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Thank you Jessica,
      After upgrading to 8.0.5.2 the syntax of my strategy looks 1:1 like the one you posted. However i am still unable to make it work correctly. Does it have something to do with the fact, that the initial orders are not submited by the strategy itself, i do them manually? I tried both:

      Submit order - apply strategy
      Apply strategy - submit order

      Both cases the strategy is Enabled in the log, however nothing happens when the loss limit is exceeded.

      If i do the same setup like Alert, it works correctly, but there i have only "Buy" or "Sell" options, not "Close".

      Comment


        #4
        Thank you for this additional information.

        Does it have something to do with the fact, that the initial orders are not submited by the strategy itself, i do them manually
        This could be yes. Strategies can only manage their own orders.

        I tried both
        One thing that we are missing is your order entry logic. Could you send a screen shot with the code you are using in your strategy to get into a position, similar to the one I sent you?

        To send a screenshot with Windows 7 or newer I would recommend using Window's Snipping Tool.
        Click here for instructions
        Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save as a jpeg file and send the file as an attachment.
        Click here for detailed instruction



        If i do the same setup like Alert, it works correctly, but there i have only "Buy" or "Sell" options, not "Close"

        To clarify, are you looking to actually close your strategy, or are you wanting to make an option called close to appear? If the latter, can you also send a picture of the screen that is missing an option called close?


        I look forward to assisting further.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Attached is the "alert" setup window if Alert setup is done from Market analyzer. I can only Buy or Sell, not Close.

          I dont have any other code for order entry, all my orders i enter Manually. The strategy only has to monitor the profit/loss and close based on that.

          I read some other topics and now i run the strategy with option "Adopt account position". That works, but only if the strategy is ran After the order is made and only if the order is without "ATM Strategy".

          Code:
          if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= -55)
          			{
          			if (PositionAccount.MarketPosition == MarketPosition.Long)
          			{
          				SubmitOrderUnmanaged(0,OrderAction.Sell, OrderType.Market, (DefaultQuantity));
          				
          			}
          				else  if 	(PositionAccount.MarketPosition == MarketPosition.Short)
          				{
          				SubmitOrderUnmanaged(0,OrderAction.Buy, OrderType.Market, (DefaultQuantity));
          So is there a way to monitor the profit/loss on my current position on a single chart (i always have only one open position, only the size changes) without the obligation to re-enable the strategy after each purchase?
          Attached Files

          Comment


            #6
            As we are using NinjaScript strategies in this way, this will be the only way this will be able to work for you. Strategies are designed for full automation, with the understanding that advanced users such as yourself who would prefer to interact with the strategy directly would be able to code this capability in on their own. The path most users with your use case take is to create screen elements such as buttons, which are fully controlled by the strategy and thus able to place strategy managed orders, but which users can interact with directly. The documentation for UserControlCollection,



            is a good place to start if you decide to take this approach.
            Jessica P.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by PaulMohn, Today, 12:36 PM
            0 responses
            2 views
            0 likes
            Last Post PaulMohn  
            Started by love2code2trade, 04-17-2024, 01:45 PM
            4 responses
            38 views
            0 likes
            Last Post love2code2trade  
            Started by alifarahani, Today, 09:40 AM
            2 responses
            14 views
            0 likes
            Last Post alifarahani  
            Started by junkone, Today, 11:37 AM
            3 responses
            21 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by frankthearm, Yesterday, 09:08 AM
            12 responses
            44 views
            0 likes
            Last Post NinjaTrader_Clayton  
            Working...
            X