Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need help with IOrders

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

    Need help with IOrders

    Hello,

    I am trying to use IOrders to store my entry prices and then use those values to exit after target/stop condition is triggered.

    But when I backtest the below code and when I print the entry1price, entry2price, diff to output: I just get 3 zeros on the output window plus the stop gets trigged immediately on the same bar, but the price never dropped 100 points. What’s causing this?…

    (note: This doesn’t make sense to me, but the code works if I change ‘<’ to ‘>’ in the condition that triggers the stop….also this is a multi instrument strategy)

    Can someone please give me a bit advice on how to fix this
    Thanks for your time in advance


    #region Variables
    private IOrder entryOrder1 = null;
    private IOrder entryOrder2 = null;
    private double entry1Price = 0;
    private double entry2Price = 0;
    #endregion


    protected override void OnBarUpdate()
    {
    currentdiff = closes[1][0] – closes[2][0];

    //this is the entry
    If (condition1
    && entryOrder1 == null
    && entryOrder2 == null
    {
    entryOrder1 = EnterLong(); entryOrder2 = EnterShort();
    }


    //this to calculate the difference between my entry prices on two instruments
    If (entryOrder1 != null && entryOrder2 != null)
    {
    if (entryOrder1.Filled > 0 && entryOrder2.Filled > 0)
    {
    entry1Price = entryOrder1.AvgFillPrice; entry2Price = entryOrder2.AvgFillPrice;
    diff = entry1Price - entry2Price;
    }


    //this is the profit target
    //currentdiff is calculated on each bar
    If(currentdiff >= diff + 100
    && entryOrder1 != null && entryOrder2 != null)
    {
    ExitLong(); ExitShort();
    entryOrder1 = null; entryOrder2 = null;
    Print(entry1Price); Print(entry2Price);
    Print(diff);
    }

    //this is the stop
    If(currentdiff <= diff - 100
    && entryOrder1 != null && entryOrder2 != null)
    {
    ExitLong(); ExitShort();
    entryOrder1 = null; entryOrder2 = null;
    Print(entry1Price); Print(entry2Price);
    Print(diff);
    }
    }
    else {}

    #2
    ImaNinja2, best is you debug this with the TraceOrders feature - this will give insight into the reasons why orders are cancelled, expire etc -



    For code structure to get going, I would suggest working alongside this reference sample below and then retrying -

    The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()

    Comment


      #3
      thanks a lot of your help Bertrand, i traced the orders and i realized that the reason i was getting zeros for entry prices was because i was i printing them without an actual order ever being filled.

      so i added a condition to check if the order is filled and only then print the entry prices - now the entryprices print properly and the stop is triggered at the right time!

      thanks again!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      647 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      368 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      108 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      571 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      573 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X