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

Order Management logic

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

    Order Management logic

    I'm trying to enter a trade with 3 contracts, exit 2 contracts at Profit1 and the 3rd contract at Profit2 - or exit the trade if there is a negative condition for any of the trade. Below is the code snippet.

    The issues I'm seeing is that,
    (1) I only see 2 contracts triggered for the trade and not seeing 3rd contract in the trade at all
    (2) 2nd issue is that I do not see the negative condition triggered. It either exits with a stop loss or exits at 1st target.

    Appreciate your feedback.




    Quantity = 3;
    StopLoss = 40;
    ProfitTarget = 30;
    ExitQuantity = 1;
    ProfitTarget2 = 90;



    protected override void OnBarUpdate()
    {



    if(CurrentBars[0] == 0 && bar0FT == null)
    {
    bar0FT = new FirstTick(CurrentBars[0]);
    }

    int idx = State == State.Historical || Calculate == Calculate.OnBarClose ? 0 : 1;

    bar0FT.Update(CurrentBars[0]);

    if(bar0FT.IsFirstTick &&
    ema9[idx] > ema21[idx] && //<--logic to enter the trade
    Position.MarketPosition == MarketPosition.Flat)
    {
    Entry1IsClosed = false;
    SetStopLoss(CalculationMode.Ticks, StopLoss);
    SetProfitTarget("EnterLong 1", CalculationMode.Ticks, ProfitTarget);

    EnterLong(Quantity - ExitQuantity, "EnterLong 1");

    if(ExitQuantity > 0)
    {
    EnterLong(ExitQuantity, "EnterLong 2");
    SetProfitTarget("EnterLong 2", CalculationMode.Ticks, ProfitTarget2);
    }
    }

    if(bar0FT.IsFirstTick &&
    ema9[idx] < ema21[idx] && //<--logic to enter the trade
    Position.MarketPosition == MarketPosition.Flat)
    {
    Entry1IsClosed = false;
    SetStopLoss(CalculationMode.Ticks, StopLoss);
    SetProfitTarget("EnterShort 1", CalculationMode.Ticks, ProfitTarget);

    EnterShort(Quantity - ExitQuantity, "EnterShort 1");

    if(ExitQuantity > 0)
    {
    EnterShort(ExitQuantity, "EnterShort 2");
    }
    SetProfitTarget("EnterShort 2", CalculationMode.Ticks, ProfitTarget2);
    }


    if(bar0FT.IsFirstTick )
    {
    if(Position.MarketPosition == MarketPosition.Long && ema9[idx] < ema21[idx])
    {
    if(!Entry1IsClosed) ExitLong("EnterLong 1");
    ExitLong("EnterLong 2");
    }
    else if(Position.MarketPosition == MarketPosition.Short && ema9[idx] > ema21[idx] )
    {
    if(!Entry1IsClosed) ExitLong("EnterShort 1");
    ExitShort("EnterShort 2");
    }
    }

    }

    #2
    Hello ark321,

    Thank you for your post.

    What are your settings for Entries Per Direction and Entry Handling? Sounds like the second entry may be getting ignored.

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

    Comment


      #3
      yeah.. It was incorrectly set to 1. Should be 2. right? is rest of the logic correct ?

      Also, how do i get the bar number for entry bar?

      Appreciate your help

      Comment


        #4
        Hello ark321,

        Thank you for your reply.

        I would set it to 1 but make sure that Entry Handling is set to Unique Entries, that way it will allow one of each uniquely named entry before returning to a flat position.

        BarsSinceEntryExecution would get you the number of bars ago an entry was placed. If you then need to get the bar index of that bar you can subtract the value returned from the CurrentBar value:



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

        Comment


          #5
          thank you.

          Comment


            #6
            if(bar0FT.IsFirstTick &&
            ema9[idx] > ema21[idx] && //<--logic to enter the trade

            Any idea what does bar0FT.IsFirstTick exactly checks for? I'm seeing a signal for the bar but the trade is not triggered because it is false.

            Also, the trade seems to be triggering in the next bar and not when I see the signal. Wondering if I'm missing any.

            Comment


              #7
              Hello ark321,

              Thank you for your reply.

              bar0FT looks to be a custom item that references a FirstTick() custom object, and not enough of the code was provided so I don't actually know what value that might provide.

              If you are running the strategy OnBarClose we would expect to see orders being placed on the bar after they are triggered as they would only be placed when the triggering bar closes.

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

              Comment


                #8
                here is the definition for ba0FT

                if(CurrentBars[0] == 0 && bar0FT == null)
                {
                bar0FT = new FirstTick(CurrentBars[0]);
                }

                Comment


                  #9
                  any idea on this?

                  Comment


                    #10
                    Hello ark321,

                    Thank you for your reply.

                    FirstTick() is not a documented NinjaScript method and appears to be custom, so I would not be able to say what that would be expected to do or return.

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

                    Comment


                      #11
                      sorry - here is the complete code for this function

                      public class FirstTick
                      {
                      public bool IsFirstTick{get;set;}
                      public int CurrentBar{get;set;}
                      public int PreviousBar{get;set;}

                      public FirstTick(int CurrentBar)
                      {
                      this.CurrentBar = CurrentBar;
                      PreviousBar = -10;
                      }

                      public void Update(int CurrentBar)
                      {
                      this.CurrentBar = CurrentBar;
                      IsFirstTick = PreviousBar < CurrentBar;
                      PreviousBar = Math.Max(CurrentBar,PreviousBar);
                      }
                      }

                      Comment


                        #12
                        Hello ark321,

                        Thank you for your reply.

                        Looks like this is basically just a fancy way to return either true or false if it's the first tick of the current bar, however, this is unnecessary if you're running OnEachTick or OnBarClose because you could simply use IsFirstTickOfBar:



                        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 Radano, 06-10-2021, 01:40 AM
                        19 responses
                        605 views
                        0 likes
                        Last Post Radano
                        by Radano
                         
                        Started by KenneGaray, Today, 03:48 AM
                        0 responses
                        4 views
                        0 likes
                        Last Post KenneGaray  
                        Started by thanajo, 05-04-2021, 02:11 AM
                        4 responses
                        470 views
                        0 likes
                        Last Post tradingnasdaqprueba  
                        Started by aa731, Today, 02:54 AM
                        0 responses
                        5 views
                        0 likes
                        Last Post aa731
                        by aa731
                         
                        Started by Christopher_R, Today, 12:29 AM
                        0 responses
                        11 views
                        0 likes
                        Last Post Christopher_R  
                        Working...
                        X