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

Reversal after Stop

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

    Reversal after Stop

    I am trying to make a reversal if the position stops out ONLY if the stop happens in the same bar the position was filled. The reversal is working but it works at anytime the positions stops out. I cannot find the mistake. Would you please help me?



    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {

    if (entryOrder01 != null && entryOrder01 == execution.Order)
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {
    // We sum the quantities of each execution making up the entry order
    sumFilled01 += execution.Quantity;

    // Submit exit orders for partial fills
    if (execution.Order.OrderState == OrderState.PartFilled)
    {
    PlaceBracketExits(01, execution, marketPosition);
    barentryonelong = CurrentBar; Print(string.Format("Bar entry {0} ", barentryonelong));

    }
    // Update our exit order quantities once orderstate turns to filled and we have seen execution quantities match order quantities
    else if (execution.Order.OrderState == OrderState.Filled && sumFilled01 == execution.Order.Filled)
    {
    PlaceBracketExits(01, execution, marketPosition);
    barentryonelong = CurrentBar; Print(string.Format("Bar entry {0} ", barentryonelong));

    }

    // Resets the entryOrder object and the sumFilled counter to null / 0 after the order has been filled
    if (execution.Order.OrderState != OrderState.PartFilled && sumFilled01 == execution.Order.Filled)
    {
    entryOrder01 = null;
    sumFilled01 = 0;
    }
    }
    }

    //space for more orders

    if (entryOrder11 != null && entryOrder11 == execution.Order)
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {
    // We sum the quantities of each execution making up the entry order
    sumFilled11 += execution.Quantity;

    // Submit exit orders for partial fills
    if (execution.Order.OrderState == OrderState.PartFilled)
    {

    }
    // Update our exit order quantities once orderstate turns to filled and we have seen execution quantities match order quantities
    else if (execution.Order.OrderState == OrderState.Filled && sumFilled11 == execution.Order.Filled)
    {
    //if (CurrentBar == barNumberOfLongEntries) Print("L reverse senal 2");
    if ( CurrentBar == barentryonelong) Print("L reverse senal 1"); Print(string.Format("Bar entry {0} ", CurrentBar));

    {
    CancelOpenEntries();
    CancelBrackets();
    EnterShort(Convert.ToInt32(Order01LotSize * 2), "SS11"); Print("L entrada reverse 1");
    SetStopLoss("SS11", CalculationMode.Ticks, Order01SL/2, false); Print("L reverse stop 1");
    SetProfitTarget("SS11", CalculationMode.Ticks, Order01PT/2, false); Print("L reverse Limit 1");

    }
    }

    #2
    Hello Jorge.andres.o,

    Thank you for your post.

    To confirm, the last code block in your post is where the reversal is occurring, and you've used prints to verify where the reversal is happening?

    Do you see barentryonelong get modified when you maintain a position longer than one bar? You may want to add a print for just that outside of any if/else blocks but inside of OnExecutionUpdate.

    We'd expect that this would be getting modified unintentionally at some point, and where that happens would be the issue you're experiencing.

    If you're not sure where this is getting modified, we would want to see a reduced export of the code that's a bit more legible than the forum formatting - makes it a little hard to follow.

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

    Comment


      #3
      Hi Jorge.andres.o,
      Quote: "I am trying to make a reversal if the position stops out ONLY if the stop happens in the same bar the position was filled. The reversal is working but it works at anytime the positions stops out."

      Perhaps I'm completely misunderstanding the code snippet and what you are trying to do, but ... where do yo check that OnExecutionUpdate was really called by a STOP?
      I would have expected something like this:

      if (execution.Order.OrderState == OrderState.Filled && sumFilled11 == execution.Order.Filled && order.Name == "Stop loss" && order.OrderAction == OrderAction.Sell)
      {
      if ( CurrentBar == barentryonelong) Print("L reverse senal 1"); Print(string.Format("Bar entry {0} ", CurrentBar));
      {
      CancelOpenEntries();
      CancelBrackets();
      EnterShort(Convert.ToInt32(Order01LotSize * 2), "SS11"); Print("L entrada reverse 1");
      SetStopLoss("SS11", CalculationMode.Ticks, Order01SL/2, false); Print("L reverse stop 1");
      SetProfitTarget("SS11", CalculationMode.Ticks, Order01PT/2, false); Print("L reverse Limit 1");

      NT-Roland

      Comment


        #4
        Thank you NT Roland

        Comment


          #5
          Hello Kate,

          I have not been able to find the error. How do you want me to send you the short version of the code. Also, I cannot show the code in the forum because the code is protected.

          Comment


            #6
            Hello Jorge.andres.o,

            Thank you for your reply.

            We would need a reduced example that is not exported as a compiled assembly for review. Any code unnecessary to reproduce should be omitted (drawing tools, etc). Please export the strategy and email it to us directly as an attachment at platformsupport [at] ninjatrader [dot] com. In the subject line, please include 2812458 ATTN Kate W. so it gets to me specifically.

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

            Comment


              #7
              Thank you Kate,
              I think I have found a way around it

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by maybeimnotrader, Yesterday, 05:46 PM
              1 response
              18 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by Perr0Grande, Yesterday, 08:16 PM
              1 response
              7 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by f.saeidi, Yesterday, 08:12 AM
              3 responses
              25 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by algospoke, Yesterday, 06:40 PM
              1 response
              15 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by quantismo, Yesterday, 05:13 PM
              1 response
              14 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Working...
              X