Announcement

Collapse
No announcement yet.

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.

    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.

        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.

              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.

                    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.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by NullPointStrategies, Yesterday, 05:17 AM
                        0 responses
                        70 views
                        0 likes
                        Last Post NullPointStrategies  
                        Started by argusthome, 03-08-2026, 10:06 AM
                        0 responses
                        143 views
                        0 likes
                        Last Post argusthome  
                        Started by NabilKhattabi, 03-06-2026, 11:18 AM
                        0 responses
                        76 views
                        0 likes
                        Last Post NabilKhattabi  
                        Started by Deep42, 03-06-2026, 12:28 AM
                        0 responses
                        47 views
                        0 likes
                        Last Post Deep42
                        by Deep42
                         
                        Started by TheRealMorford, 03-05-2026, 06:15 PM
                        0 responses
                        51 views
                        0 likes
                        Last Post TheRealMorford  
                        Working...
                        X