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

Strategy exits position with an extra contract

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

    Strategy exits position with an extra contract

    Hello, I am having an issue with a fairly simple strategy that I have created. The strategy is set up to exit my position upon two successive closes above or below my stop level based on a secondary data series.

    Most of the time it works without any issues but occasionally it sends an extra order to close the position and I end up with one contract in the opposite direction. To clarify, if I am short 2 contracts, and there are two successive closes on the secondary data series above my stop level, it will close the position but 3 contracts will execute putting me in a 1 contract long position. This new position is not managed And therefore has no stop associated with it.

    It is not entering a new position based on any logic that I have created because I have a user input boolean for both long or short trades. In the example above the strategy is set to only trade short because the box for long is not checked.

    Here is a section of the short side logic. The “Short” is the user input bool that must be checked for a short trade to be taken. The “TT” is a user variable bool that stands for trade taken which changes to true once a stop is triggered or a second target is hit. This is to prevent the strategy from executing another entry. It is designed to only take one entry and then stop once the trade is completed.

    if ((Position.MarketPosition == MarketPosition.Flat)

    && (CrossAbove(Close, Entry1 - FrontRun, 1))

    && (Short == true)

    && (TT == false))

    {

    EnterShort(Convert.ToInt32(DefaultQuantity), @"Entry1");

    }



    // Set 8

    if ((Position.MarketPosition == MarketPosition.Short)

    && (Entry2Val != 0)

    && (CrossAbove(Close, Entry2Val - FrontRun, 1)))

    {

    EnterShort(Convert.ToInt32(DefaultQuantity), @"Entry2");

    }



    // Set 9

    if ((Position.MarketPosition == MarketPosition.Short)

    && (CrossAbove(Close, Stop, 1))

    && (Stop != 0))

    {

    ExitShort(Convert.ToInt32(DefaultQuantity), "", "");

    TT = true;

    //CloseStrategy("My Strategy");

    }



    // Set 10

    if ((Position.MarketPosition == MarketPosition.Short)

    && (Closes[1][1] > SoftStop)

    && (Closes[1][2] > SoftStop))

    {

    ExitShort(Convert.ToInt32(DefaultQuantity), "", "");

    TT = true;


    }



    #2
    Hello foxtrott21,

    Thanks for your post.

    Are you using Exit methods and Set methods in your script?

    Do you have multiple Exit order methods for a single entry?

    Have you added debugging prints to the strategy and enabled TraceOrders to see how the strategy's logic is behaving and placing orders?

    To understand why the script is behaving as it is, such as placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.

    Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    ​Below is a link to a forum post that demonstrates how to use prints to understand behavior.

    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hi Brandon,

      Thanks for the response.

      Are you using Exit methods and Set methods in your script?

      No. I only exit using the conditions in my example.

      Do you have multiple Exit order methods for a single entry?

      No. If the stop parameters are triggered it exits the entire position regardless of the entry name. As you can see in the script, I do have entry1 and entry2 for entry names but the exit is to exit the entire position if the soft stop has been triggered. Maybe this is not the best way to do this?

      I do have prints for all the variables, and they do not indicate a problem. I don’t think I have trace orders enabled. I have not done that in previous scripts.

      Comment


        #4
        Hello foxtrott21,

        Thanks for your notes.

        Are you able to reproduce the behavior when testing the script using the Playback connection with Market Replay data?

        Playback: https://ninjatrader.com/support/help...8/playback.htm

        Please provide a reduced test script that has prints added demonstrating the behavior you are reporting and send us the exact steps and settings you are using to reproduce the behavior so we may investigate this matter further.

        Note that a reduced copy refers to a copy of the script that contains the minimum amount of code needed to reproduce the issue. All other code is commented out or removed.

        To export the script to share go to Tools > Export > NinjaScript AddOn.

        We look forward to assisting further.
        Brandon H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by DJ888, Today, 10:57 PM
        0 responses
        2 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by MacDad, 02-25-2024, 11:48 PM
        7 responses
        158 views
        0 likes
        Last Post loganjarosz123  
        Started by Belfortbucks, Today, 09:29 PM
        0 responses
        7 views
        0 likes
        Last Post Belfortbucks  
        Started by zstheorist, Today, 07:52 PM
        0 responses
        7 views
        0 likes
        Last Post zstheorist  
        Started by pmachiraju, 11-01-2023, 04:46 AM
        8 responses
        151 views
        0 likes
        Last Post rehmans
        by rehmans
         
        Working...
        X