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 attach indicator to entry order in umanaged mode?

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

    how to attach indicator to entry order in umanaged mode?

    how to attach indicator to entry order in umanaged mode? and how to say enter when the condition happens a second time in the last 5 bars

    Thank you

    #2
    Hello rickyblah12,

    Thank you for your note.

    To clarify on the first question, do you mean how to make an entry order trail based on an indicator's values?

    For the second, you'd need to keep track of when your condition occurs. I've attached an example in which I check if my condition was true by assigning true or false to a Series<bool>, then looping through the last 5 bars to see how many times the condition is true. If we see that the condition was true twice, we enter long and exit the loop. This example uses the Managed approach but could be adapted to use the unmanaged instead.

    Thanks in advance; I look forward to assisting you further.
    Attached Files
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hey yea lets say I wanted to do this with a EMA?

      Comment


        #4
        Hello rickyblah12,

        Thank you for your reply.

        You'd need to submit the initial stop at the current EMA price when your order is placed, then update the stop periodically if the EMA is still on the correct side of the market. I'm attaching a simple example strategy that enters long if you're in a flat position, the bar is an up bar, and the EMA is less than 5 ticks below the current price. It will then trail with the EMA as long as it's still lower than the current price.

        Please let us know if we may be of further assistance to you.
        Attached Files
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Hey,

          I had a question about the 2nd entries....

          where does the condition for the strategy go?

          Does the code on the OnBarUpdate go in that same order together?

          What happens to the code below do I need to keep it there or erase it and keep the current bars ago code from the 2nd entries code you provided?

          if (BarsInProgress != 0)
          return;

          if (CurrentBars[0] < 1
          || CurrentBars[1] < 0)
          return;

          Comment


            #6
            Hello rickyblah12,

            Thank you for your reply.

            If you're combining the logic of the two examples, you'd want to keep everything that's above the CurrentBars check in the first one at the beginning of OnBarUpdate() just like it is there. I'd modify the CurrentBar check to the following:

            if (CurrentBar < 5 || CurrentBar < EMAPeriod)
            return;

            You'd then want to use that instead of the code you mentioned (the following should be removed from your script:

            if (BarsInProgress != 0)
            return;

            if (CurrentBars[0] < 1
            || CurrentBars[1] < 0)
            return;

            From there, you'd want to use the entry conditions from ExampleEnter2ndTimeCondition is true, but modify the final bit right before the order is entered to check that the EMA is currently less than the close minus 5 ticks, so something like the following:

            // instantiate a new Count variable on each bar to keep track of how many times our condition was true
            int Count = 0;

            // loop through the last 5 bars and enter long if the condition is true twice
            for(int i = 0; i < 5; i++)
            {
            if(IsTrue[i] == true)
            {
            Count += 1;
            // if our count is 2, we're in a flat position, and the EMA is in a position where we can have it trail from, then EnterLong()
            if(Count == 2 && Position.MarketPosition == MarketPosition.Flat && EMA1[0] < Close[0] - (5*TickSize)
            {
            //set stop loss to the current EMA price
            SetStopLoss(CalculationMode.Price, EMA1[0]);
            EnterLong();
            break;
            }
            }

            }

            Finally, after that you'd want to include the logic for the stop to trail based on the EMA:

            if (Position.MarketPosition == MarketPosition.Long && EMA1[0] < Close[0])
            {
            //update stop loss with current EMA price as long as the EMA is still below the Close price
            SetStopLoss(CalculationMode.Price, EMA1[0]);
            }

            Hopefully that helps to clarify the order the logic should go in when combined.

            Please let us know if we may be of further assistance to you.
            Kate W.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by fx.practic, 10-15-2013, 12:53 AM
            5 responses
            5,406 views
            0 likes
            Last Post Bidder
            by Bidder
             
            Started by Shai Samuel, 07-02-2022, 02:46 PM
            4 responses
            98 views
            0 likes
            Last Post Bidder
            by Bidder
             
            Started by DJ888, Yesterday, 10:57 PM
            0 responses
            8 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by MacDad, 02-25-2024, 11:48 PM
            7 responses
            160 views
            0 likes
            Last Post loganjarosz123  
            Started by Belfortbucks, Yesterday, 09:29 PM
            0 responses
            9 views
            0 likes
            Last Post Belfortbucks  
            Working...
            X