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

Issue with Iorders

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

    Issue with Iorders

    Hi, i have the following code and i really don't understand why the orders are not triggered properly. The behavior is really weird as there as some entry signal triggered but the exit are not triggered properly.

    Do you have any idea what is wrong with the Iorders below??


    Code:
    private IOrder stopLongOrder = null;
    private IOrder stopShortOrder = null;
    private IOrder entryLongOrder = null;
     private IOrder entryShortOrder = null;
    public int contracts;
    
     protected override void Initialize()
    {
        CalculateOnBarClose = true;
        ExitOnClose = false;
        RealtimeErrorHandling = RealtimeErrorHandling.TakeNoAction;
    }
    private void Bracket()
    {
        Stop = 50;
        contracts = (int)(10000 * (0.02 * 10000) / Stop);
    }
    private void LongEntry()
    {
        Bracket();
        barNumberOfOrder = CurrentBar;
    
        entryLongOrder = EnterLongStop(contracts, Close[0] + 1 * TickSize, "LongEntry");
    }
    private void ShortEntry()
    {
        Bracket();
        barNumberOfOrder = CurrentBar;
    
        entryShortOrder = EnterShortStop(contracts, Close[0] - 1 * TickSize, "ShortEntry");
    }
    private void ExitPosition()
    {
        int NbBars = CurrentBar - barNumberOfOrder;
        if (NbBars >= 20)                 
        {
             if (stopLongOrder == null && stopShortOrder == null 
    
                 && Position.MarketPosition == MarketPosition.Long)
             {
                stopLongOrder = ExitLongStop(Close[0] - 1 * TickSize, "LongEntry");
            }
             if (stopShortOrder == null && stopLongOrder == null 
    
               && Position.MarketPosition == MarketPosition.Short)
             {
                stopShortOrder = ExitShortStop(Close[0] + 1 * TickSize, "ShortEntry");
            }
        }
    }
    protected override void OnBarUpdate()
    {
        ExitPosition();
    
             if (entryLongOrder == null && entryShortOrder == null 
    
               && Position.MarketPosition == MarketPosition.Flat 
    
                && Close[0] < Close[1])
             {
                LongEntry();
            }
             if (entryShortOrder == null && entryShortOrder == null 
    
                && Position.MarketPosition == MarketPosition.Flat `
                && Close[0] > Close[1])                                
             {
                ShortEntry();           
            }    
    }
    protected override void OnOrderUpdate(IOrder order)
    {
        if (entryLongOrder != null && entryLongOrder == order)
        {
            if (order.OrderState == OrderState.Rejected)
            {
                entryLongOrder = EnterLong(contracts, "LongEntry");
            }
            if (order.OrderState == OrderState.Filled)
            {
                entryLongOrder = null;
            }
        }
        if (entryShortOrder != null && entryShortOrder == order)
        {
            if (order.OrderState == OrderState.Rejected)
            {
                entryShortOrder = EnterShort(contracts, "ShortEntry");
            }
            if (order.OrderState == OrderState.Filled)
            {
                entryShortOrder = null;
            }
        }
        if (stopLongOrder != null && stopLongOrder == order)
        {
            if (order.OrderState == OrderState.Rejected)
            {
                stopLongOrder = ExitLong("LongEntry");
            }
            if (order.OrderState == OrderState.Filled)
            {
                stopLongOrder = null;
            }
        }
        if (stopShortOrder != null && stopShortOrder == order)
        {
            if (order.OrderState == OrderState.Rejected)
            {
                stopShortOrder = ExitShort("ShortEntry");
            }
            if (order.OrderState == OrderState.Filled)
            {
                stopShortOrder = null;
            }
        }
    }

    #2
    Hello cbadr,

    Have you used prints to ensure the condition that places the exit is evaluating as true when you expect it?

    Below is a public link to a post that demonstrates using prints to understand behavior.


    If so, are you printing the order object in OnOrderUpdate?
    Is the order entering an accepted state?
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by samish18, Today, 01:01 PM
    0 responses
    5 views
    0 likes
    Last Post samish18  
    Started by WHICKED, Today, 12:56 PM
    0 responses
    7 views
    0 likes
    Last Post WHICKED
    by WHICKED
     
    Started by Spiderbird, Today, 12:15 PM
    2 responses
    11 views
    0 likes
    Last Post Spiderbird  
    Started by WHICKED, Today, 12:45 PM
    1 response
    8 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by FrazMann, Today, 11:21 AM
    2 responses
    8 views
    0 likes
    Last Post NinjaTrader_ChristopherJ  
    Working...
    X