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

SetProfitTarget and SetStopLoss

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

    SetProfitTarget and SetStopLoss

    Hi,

    Trying to develop 2 time period strategy using build-in functions for profit and loss.


    else if (State == State.Configure)

    {

    AddDataSeries(Data.BarsPeriodType.Tick, 1);

    SetProfitTarget("Long Entry", CalculationMode.Currency, MaxProfitLong);

    SetProfitTarget("Short Entry", CalculationMode.Currency, MaxProfitShort);

    SetStopLoss("Long Entry", CalculationMode.Currency, maxWinLossLong, false);

    SetStopLoss("Short Entry", CalculationMode.Currency, maxWinLossShort, false);

    }

    I'm working with previous hour Hi Low, something like that:

    if (Close[0] >= HLTR1530.HighestHigh[0] &&

    Close[0] < HLTR1530.HighestHigh[0])

    {

    if (myEntryOrder == null)

    {

    EnterLongStopLimit(1, true, 1, HLTR1530.HighestHigh[0] + SizeAboveLong, HLTR1530.HighestHigh[0], "Long Entry");

    barNumberOfOrder = CurrentBars[0];

    Print("LONG HLTR1530.HighestHigh[0] :: " + HLTR1530.HighestHigh[0] + " TS:: " + Time[0].AddMinutes(BarsPeriod.Value).ToString("hh.mm.ss.ffffff"));

    if (myEntryOrder != null )

    Print("Order State :: " +myEntryOrder.OrderState.ToString());

    }

    }
    Based on the documentation I'm using this:

    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled,

    double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)

    {

    // Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.

    // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not gauranteed to be complete if it is referenced immediately after submitting

    if ((order.Name == "Long Entry") || (order.Name == "Short Entry"))

    myEntryOrder = order;



    // Evaluates for all updates to myEntryOrder.

    if (myEntryOrder != null && myEntryOrder == order)

    {

    //Time[0].ToString("hh.mm.ss.ffffff");

    Print("OnOrderUpdate:: State :: " + myEntryOrder.OrderState.ToString() + " TS:: " + Time[0].AddMinutes(BarsPeriod.Value).ToString("hh.mm.ss.ffffff"));

    // Check if myEntryOrder

    if ((myEntryOrder.OrderState == OrderState.Cancelled) ||

    (myEntryOrder.OrderState == OrderState.Filled) ||

    (myEntryOrder.OrderState == OrderState.Rejected)

    )

    {

    // Reset myEntryOrder back to null

    myEntryOrder = null;

    }

    }

    }

    I have 2 questions:
    #1: Log is saying the following:

    OnOrderUpdate:: State :: Submitted TS:: 03.33.00.000000 <-- Seems it shows the order that was submitted before the code submits it: there is 2 minutes difference.
    SHORT HLTR1530.LowestLow[0] :: 2995 TS:: 03.35.43.718000
    Order State :: Submitted
    OnOrderUpdate:: State :: Accepted TS:: 03.33.00.000000
    OnOrderUpdate:: State :: Working TS:: 03.33.00.000000
    OnOrderUpdate:: State :: Filled TS:: 03.33.00.000000

    #2 I can not figure out why the system generates this error (see attached)





    Appreciate any help.

    --PNF

    #2
    Correction

    Comment


      #3
      Hello

      With the Time print you are doing I would suggest to avoid using the bar times if you are trying to get the order time. You can instead use the orders Time property: https://ninjatrader.com/support/help...lightsub=order

      myEntryOrder.Time

      The image you attached did not come through, can you try to update your post or copy the text and paste that?

      I look forward to being of further assistance.
      JesseNinjaTrader Customer Service

      Comment


        #4
        Thank you.
        I will use TS of Order.
        Message:
        Sim101, order '9a26a0827c0a4be089af97ebbb216b61' can't be submitted: The OCO ID 'a2f247a385724304b15ae266ff28e773' ca not be reused. Please use a new OCO ID. affected Order: ByToCover 1 …


        Thank you.
        -PNF

        Comment


          #5
          Hello PageNotFound404,

          This error generally means that one of the orders that was in the OCO group was not submitted at the same time which is something that is not allowed by some brokers. What broker is currently being used?

          One way to avoid this with a strategy or script where you cannot control the OCO would be to use Local OCO if the connection allows for that. You may want to check in the Connection menu -> Edit your connection and see if there is a checkbox for Local OCO.

          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Jesse,
            Thank you for your answer. The question I had is related to the SetProfitTarget and SetStopLoss I set in order not do any custom submission. My connection is Continuum. I do not do any OCO. I'm doing EnterLongStopLimit (short / long) and always waiting when my position is 0, I get this message when I switch to SetProfitTarget and SetStopLoss. Related to your answer is another question. If i'm not using local OCO, does this means that my let say my entry (EnterLong) is pending order at the broker, or it is actually waiting when price hit locally and only after that it submit order to the broker?

            -PNF

            Comment


              #7
              Hello PageNotFound404 ,

              The Set methods are OCO so that error would make sense with the set methods. I did note that you saw this with the sim account, you may want to try and repair the database and reset the sim here to clear the previous orders. It is possible that a OCO was reused from a previous order in some way to generate the error. For a live account the previous comment I made would still apply.

              To clarify OCO means one cancels other so this would not relate to the orders state being pending, this links two orders together so that when one fills the other cancels. Some brokers do this on their end using the OCO id, others do not so there is also local OCO simulation where the platform submits the cancel when the linked order fills.




              Resetting a simulation account will clear all historical trade data from this account. Please follow the instructions below to reset a simulation account:
              • From the NinjaTrader Control Center window select the Accounts tab
              • Right click on your simulation account and select 'Edit Account'
              • Press the "Reset" button
              • Disconnect NinjaTrader from any open connections via Control Center > Connections > Disconnect
              • From the NinjaTrader Control Center window select the menu Tools > Database Management
              • Click on 'Repair DB'
              • Press the "Repair" button


              I look forward to being of further assistance.


              JesseNinjaTrader Customer Service

              Comment


                #8
                OK, I will reset, repair and repeat using build in functions.
                Thank you.
                -PNF

                Comment


                  #9
                  And I did try, and reset and clean and use build-in functions.
                  And - result is the same, unfortunately:
                  Sim101, Order bignumber can't be submited. The OCO ID anotherbignumber cannot be reused. Please use new OCO ID. affected Order: Sell 1 Limit@...
                  How that is working? as it seems to me, build-in function does not?
                  -PNF

                  Comment


                    #10
                    Hello PageNotFound404,

                    Could you attach a sample of the script that you are using and the steps you use to see this? I would like to see if I am also able to generate the error or if this relates to something with your installation/settings.

                    This is controlled internally so we generally wouldn't expect the error for using the Set methods unless there is some logical problem in your code that is creating that situation. I would need to review more about what your logic is doing to better understand why we would see this in this situation.

                    I look forward to being of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Jesse,
                      Thank you for offering help, attached is the code i'm using, I remove repetitive sections, and properties.
                      Thank you.
                      --PNF
                      Attached Files

                      Comment


                        #12
                        Hello PageNotFound404,

                        I tried to import the file but it looks like there is some error in the structure. Could you attach the .cs file instead, you can find the file in the folder: Documents\NinjaTrader 8\bin\Custom\Strategies
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Jesse,
                          Sure, see attached.
                          -PNF
                          Attached Files

                          Comment


                            #14
                            Hello PageNotFound404,

                            Thanks for providing that.

                            I was able to get this imported, the structure looks correct now. I don't have the indicator you have used so I will try replacing that with the SMA in a quick test to see if I can reproduce the error with what is provided. Once I have had more time to test this I will post back here.
                            JesseNinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by bmartz, 03-12-2024, 06:12 AM
                            4 responses
                            32 views
                            0 likes
                            Last Post bmartz
                            by bmartz
                             
                            Started by Aviram Y, Today, 05:29 AM
                            4 responses
                            12 views
                            0 likes
                            Last Post Aviram Y  
                            Started by algospoke, 04-17-2024, 06:40 PM
                            3 responses
                            28 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by gentlebenthebear, Today, 01:30 AM
                            1 response
                            8 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by cls71, Today, 04:45 AM
                            1 response
                            7 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Working...
                            X