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

Active position not closing with ExitLong / ExitShort

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

    Active position not closing with ExitLong / ExitShort

    Hi,

    I have this strategy in which ExitLong(), ExitShort() aren't working even though I can see by means of a "Print ()" that the portion/set of code is firing.

    private bool myCloseTrade;

    protected override void OnStateChange()
    {

    }

    protected void Button1Click(object sender, RoutedEventArgs e)
    {
    if (Position.MarketPosition != MarketPosition.Flat) {myCloseTrade = true;}
    }

    protected override void OnBarUpdate()
    {
    if (myCloseTrade)
    {
    Print("closing trade");
    if (Position.MarketPosition == MarketPosition.Long) { ExitLong(); }
    if (Position.MarketPosition == MarketPosition.Short) { ExitShort(); }
    }
    }


    What could be the cause ?

    I see "closing trade" in the output window and the trade is active/live, the buttons are being placed on the Chart Trader by means of the code provided on this forum by Chelsea (if I remember well). By the way, Very good piece of code !!

    Thank you.
    Anthony

    #2
    Hello Anthony_0709,

    Thank you for the post.

    To confirm, was the entry submitted from the same strategy and is the strategy in a position when clicking the button? If you are trying to target a manually created position then this would not work for that type of use case.

    One way forward would be to confirm the Position is reporting correctly by using a Print with the position:

    Print(Position.MarketPosition);


    If you are trying to close a position which the strategy started you can also enable TraceOrders to check if the order is being ignored for some reason: https://ninjatrader.com/support/help...ub=traceorders

    If the intention is to close a manual position you would need to use the addon methods for that.


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      Yes, the entry was submitted from the same strategy and the strategy in a position when clicking the button. No orders are created manually. The Position is reporting correctly.

      While waiting for your response, I did some testing and the only way to close the position was with ExitShort() & ExitLong().
      It just won't work using a signalName, even though the signalName is the same throughout the strategy. Any ideas ? I would like to use a signal name.

      Thank you
      Anthony
      ​​​​​​​

      Comment


        #4
        Hello Anthony_0709,

        From the given information there is nothing that sticks out, are you certain you used the same signal name from the entry when calling the exit? If that is being ignored you would need to use TraceOrders to find out why.



        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by arvidvanstaey, Today, 02:19 PM
        4 responses
        10 views
        0 likes
        Last Post arvidvanstaey  
        Started by samish18, 04-17-2024, 08:57 AM
        16 responses
        56 views
        0 likes
        Last Post samish18  
        Started by jordanq2, Today, 03:10 PM
        2 responses
        8 views
        0 likes
        Last Post jordanq2  
        Started by traderqz, Today, 12:06 AM
        10 responses
        18 views
        0 likes
        Last Post traderqz  
        Started by algospoke, 04-17-2024, 06:40 PM
        5 responses
        47 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X