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

ChangeOrder() error

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

    ChangeOrder() error

    Hello

    I am working in an strategy with two data series running OnEachTick. It has also breakeven conditions that works.

    All stuff works perfect with just one contract. However, when entries have more than one, all works ok until the script uses breakeven one time. Then, in next entry, error occurs and strategy stops just in the moment of entry

    "Stop price can´t be changed below the market"

    So, breakeven conditions try to change stop before conditions required. I have made a lot of changes, prints and nothing works... I suppose there is a problem resetting any variable but I don´t find the solution. Can you please give me a clue?

    These are the lines of code

    #region Strategy Variables


    private double stopShortPrice;
    private double targetShortPrice;
    private double entryShortPrice;
    private double entryShortObj;

    private int NroShorts;

    #endregion

    else if (State == State.Realtime)

    {

    if (entryShortOrder != null)
    entryShortOrder = GetRealtimeOrder(entryShortOrder);
    if (stopShortOrder != null)
    stopShortOrder = GetRealtimeOrder(stopShortOrder);
    if (targetShortOrder != null)
    targetShortOrder = GetRealtimeOrder(targetShortOrder);
    }

    OnBarUpdate

    if IsFirstTickOfBar
    && Conditions[1]
    {
    entryShortPrice =...
    NroShorts=

    entryShortOrder = (EnterShortLimit(0, true, NroShorts, entryShortPrice, @"S1"));
    }


    #region BREAKEVEN


    if (BarsInProgress == 0)
    {
    if (CurrentBar > barNumberOfEntryShort + 1

    && stopShortOrder != null
    && stopShortPrice > Position.AveragePrice)
    {
    if (Close[0] <= (Position.AveragePrice - ((Position.AveragePrice - targetShortPrice) * 0.5))
    {
    ChangeOrder(stopShortOrder, stopShortOrder.Quantity, 0, Position.AveragePrice);
    barNumberOfEntryShort = 0;
    }
    }
    }
    #endregion

    OnExecutionUpdate

    if (entryShortOrder != null && entryShortOrder == execution.Order)
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {

    sumShortFilled += execution.Quantity;

    if (execution.Order.OrderState == OrderState.PartFilled)
    {
    stopShortOrder = ...
    targetShortOrder = ...
    }

    else if (execution.Order.OrderState == OrderState.Filled && sumShortFilled == execution.Order.Filled)

    {
    stopShortPrice = ...
    targetShortPrice = ...

    stopShortOrder = ExitShortStopMarket(0, true, execution.Order.Filled, stopShortPrice, "Stop S1", "S1");
    targetShortOrder = ExitShortLimit(0, true, execution.Order.Filled, targetShortPrice, "Target S1", "S1");
    barNumberOfEntryShort = CurrentBar;

    }
    if (execution.Order.OrderState != OrderState.PartFilled && sumShortFilled == execution.Order.Filled)
    {
    entryShortOrder = null;
    sumShortFilled = 0;
    }
    }


    #2
    Hello Impeesa, thanks for your post.

    Have you added Prints around the ChangeOrder method? Try printing out the value of Position.AveragePrice and also (Position.AveragePrice - ((Position.AveragePrice - targetShortPrice) * 0.5) before submitting ChangeOrder and compare it with the Close[0] value. The Stop price is being changed to an illegal price level below the bid price of the market. You would need to find exactly where that is happening in this code to fix the issue, the best way to do that is with Prints to the output window. Also, if you have multiple positions in different instruments, use the Positions[] array to access the average entry price of each instrument:



    Please let me know if I can assist any further.


    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hello ChrisL

      Yes, I add prints whenever I program. And there are two data series but it only takes posotions in one of them. I have run the script in market replay for several days...

      I can be wrong, but as I said in post #1, it works ok if it uses just one contract and it also works ok with two or more contracts in the first trade of session...

      Comment


        #4
        Hello Impeesa, thanks for your reply.

        Please turn on the TraceOrders property and observe the output window. Do you get any ignored orders? If you are getting orders ignored by exceeding the Entries Per Direction property, you would need to increase the Entries Per Direction setting. At what point in the code do the Prints stop? What is the last line of code to run after the first trade of the session has taken place?

        I look forward to assisting.
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by bortz, 11-06-2023, 08:04 AM
        47 responses
        1,606 views
        0 likes
        Last Post aligator  
        Started by jaybedreamin, Today, 05:56 PM
        0 responses
        9 views
        0 likes
        Last Post jaybedreamin  
        Started by DJ888, 04-16-2024, 06:09 PM
        6 responses
        19 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by Jon17, Today, 04:33 PM
        0 responses
        6 views
        0 likes
        Last Post Jon17
        by Jon17
         
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        15 views
        0 likes
        Last Post Javierw.ok  
        Working...
        X