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

Stop Limit order triggered by price level cross

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

    Stop Limit order triggered by price level cross

    Hello
    Please help to generate a logic that makes a stop limit order to be placed on LEVEL X if the price hits LEVEL Y.
    As far as I understand to keep fire a stop order while condition is not true anymore I require to set a variable which is resided between the condition itself and SL order action. However, if my trigger condition is price and it steps away from the conditional level, the variable turns back to false as well. Hence order dies next bar.
    The algorithm that I require seems extremely simple, but in reality I have no idea how to realize it...

    I appreciate if you propose any operable logic. Pls see the illustration for better understanding what needs to be done.

    Thnx

    #2
    Hello Aporshnev,

    Thank you for the inquiry.

    What you are explaining is true and is also possible. To submit a stop or limit type order and keep it active would require that the condition remains true.

    One way to do this would be to use a variable and some conditions to toggle the variable. A separate condition could be used to check the state of the variable to determine if the order should or should not be placed. Because you want to toggle the live until cancelled a variable can be used for that purpose because you cannot toggle LiveUntilCancelled. If you choose to use LiveUntilCancelled you will need to cancel the order yourself rather than just not calling the entry method. Here is a quick example of one way to realize that goal:

    Code:
    private bool entryShouldBeActive = false;
    protected override void OnBarUpdate()
    {
         if(State == State.Realtime)
        {
            if(CrossAbove(Close, Open, 1))
            {
                entryShouldBeActive = true;
            } else if(CrossBelow(Close, Open, 1))
            {
                entryShouldBeActive = false;
            }
            if(entryShouldBeActive == true)
            {
                EnterLongStopLimit(1, Close[0] + 5 * TickSize, Close[0] + 1     * TickSize, "TestEntry");
            }
        }
    }
    This sample relies on being in realtime and is just used to demonstrate the toggling of the bool variable. You could expand on this however the main idea to take away from this is how the bool variable is used to enable/disable the entry order. While the variable is true the entry is submitted, once a cross below happens the entry condition is no longer true and will be cancelled on the next bar if not filled. The cross conditions could be replaced with your own conditions checking for prices, again the main take away is how the variable is toggled based on your conditions and how the variable is used in the third condition.



    I look forward to being of further assistance.

    JesseNinjaTrader Customer Service

    Comment


      #3
      It appears that position entry via SL order is too complicated. I decided to test the following: I made 3 user inputs (for test) with price meanings and set EnterLongPosition if Ask price equals user input. Stops and ProfitTakes were set as well, but this is not important so far. During the test I observed that on a few entry events the position was not opened. At least there are no historical execution markers on the chart. Moreover, on different timeframes the strategy reports different performance and it is unclear to me why - the entry condition is price and it has been crossed several times. One of my assumptions is the historical data is inaccurate, or testing on the Sim account is not 100% accurate, however, I would like to ask you how can this be?
      I marked the input prices (BuyTrigger1 etc.) on the test chart for convenience. I expected a long position on the fact of this levels cross (whatever above or below)

      If I set the inappropriate conditions to open the position on the fact of price touch, then how can this be realized differently?
      Attached Files

      Comment


        #4
        Hello Aporshnev,

        Checking if two prices are directly equal will often not work, this is due to how floating point numbers are stored in memory. Using a crossing condition or greater/lesser condition would be advised to be used.

        You could try checking a range of prices instead, for example, if the price is greater than or equal to 9140 and less than 9141. This would allow the condition to return true if the price lands on 9140.


        I look forward to being of further assistance.

        JesseNinjaTrader Customer Service

        Comment


          #5
          Thanks. The task is almost solved.
          What calculation method loads the system more - calculate on each tick of on price change?

          Comment


            #6
            Hello Aporshnev,

            OnEachTick would be the most processor intensive as that calls your script for each tick.


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

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by cre8able, Today, 03:20 PM
            0 responses
            5 views
            0 likes
            Last Post cre8able  
            Started by Fran888, 02-16-2024, 10:48 AM
            3 responses
            47 views
            0 likes
            Last Post Sam2515
            by Sam2515
             
            Started by martin70, 03-24-2023, 04:58 AM
            15 responses
            114 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by The_Sec, Today, 02:29 PM
            1 response
            8 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by jeronymite, 04-12-2024, 04:26 PM
            2 responses
            31 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Working...
            X