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:

        https://ninjatrader.com/support/help...yexecution.htm

        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

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by KirkHammett, Today, 03:23 AM
                0 responses
                2 views
                0 likes
                Last Post KirkHammett  
                Started by eleven, Yesterday, 10:06 PM
                0 responses
                35 views
                0 likes
                Last Post eleven
                by eleven
                 
                Started by ezrollin, Yesterday, 08:12 PM
                7 responses
                72 views
                0 likes
                Last Post bltdavid  
                Started by kyamato, 11-23-2021, 10:51 PM
                6 responses
                91 views
                0 likes
                Last Post jeronymite  
                Started by fscabrera03, Yesterday, 07:14 PM
                0 responses
                49 views
                0 likes
                Last Post fscabrera03  
                Working...
                X