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

Total number of positions for given signal

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

    Total number of positions for given signal

    I have a strategy that places different orders at different times using different signals. For simplicity lets say Signal1,Signal2, and Signal3.

    How would I get the total number of positions that each specific Signal as placed and been filled? The reason is because Signal1 is allowed to put in up to 2 positions but Signal 3 is only allowed to plane 1 position but Signal2 can put in as many positions as it wants.

    I have tried something like this to loop through all the orders but it doesnt seem to be working

    Code:
    int sig1 = 0
    
     private void OnAccountItemUpdate(object sender, AccountItemEventArgs e)
    {
    foreach (Order order in myAccount.Orders)
        {
         if(order.Name == "Signal1") sig1+=1
        }
    }
    Last edited by cutzpr; 06-08-2018, 03:24 PM.

    #2
    Hello cutzpr,

    Thank you for the post.

    For this, I would likely suggest using the OnExecutionUpdate override as you want to know this about filled orders. Using OnExecutionUpdate you could watch for the various different orders by name, and then accumulate counters based on what those conditions see.



    In the first example from the above page, the execution.Order.Name is used to delegate logic. You could also do this but accumulate your counts based on the orders coming in. you can also see if the order was partially filled and the quantity of the fill to accumulate accurately.

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

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello cutzpr,

      Thank you for the post.

      For this, I would likely suggest using the OnExecutionUpdate override as you want to know this about filled orders. Using OnExecutionUpdate you could watch for the various different orders by name, and then accumulate counters based on what those conditions see.



      In the first example from the above page, the execution.Order.Name is used to delegate logic. You could also do this but accumulate your counts based on the orders coming in. you can also see if the order was partially filled and the quantity of the fill to accumulate accurately.

      I look forward to being of further assistance.
      Thank you Jesse. I was able to get the total number of executions of said signal but how would I go about getting only the ones that are currently active/open.

      Comment


        #4
        I cant seem to get the number of positions that are open for a given signal. I thought this should be easy. I tried doing a counter if the position was filled but you can see from the log output, it states there are well over the number of positions that are allowed by the EntriesPerDirection = 3. As you can see from the log there are lot of Canceled and Ignored Orders due to RealtimeErrorHandling.IgnoreAllErrors.

        just fyi My strategy is always in the market. It does not submit close or exit orders but submits opposite side trades instead to close positions. That is why you will see pos+=1 and pos-=1 to keep track of the positions. I am guessing this is why I am getting "Internal Order Handling Rules" Error in my log output. How would I go about putting in stop and reversals without getting the "Internal Order Handling Rules" Error ?

        Do I have to create a counter for the Canceled and Ignored orders aswell and subtract them? There has to be a better way. I see there is an OrderState.Cancelled state but not an Order Ignored state.

        On a side but related not, how can I stop the strategy from placing a trade that exceeds the max "EntriesPerDirection". I would like to build this into my trade loginc. Although this functionality is already built into NinjaTrader, it prints to the output window. I rather have a clean output window.


        PHP Code:
        RealtimeErrorHandling    RealtimeErrorHandling.IgnoreAllErrors
        PHP Code:
        EntriesPerDirection    3
        PHP Code:
        #region OnExecutionUpdate
                
                
        protected override void OnExecutionUpdate(Execution executionstring executionIddouble priceint quantityMarketPosition marketPositionstring orderIdDateTime time)
                {

                    
                    if (
        execution.Order.Name == "LongEMA")  entryOrder execution.Order;
                        if (
        entryOrder != null && entryOrder == execution.Order)
                          {
                              Print(
        entryOrder.ToString());
                              if (
        entryOrder.OrderState == OrderState.Filled)
                              {
                              
        pos+=1;
                              Print(
        pos);
                              }
                              
        entryOrder null;
                          }
                    
                    if (
        execution.Order.Name == "ShortEMA")  entryOrder execution.Order;
                        if (
        entryOrder != null && entryOrder == execution.Order)
                          {
                              Print(
        entryOrder.ToString());
                              if (
        entryOrder.OrderState == OrderState.Filled)
                              {
                              
        pos-=1;
                              Print(
        pos);
                              }
                              
        entryOrder null;
                          }
                    
                    if (
        execution.Order.Name == "LongHisto")  entryOrder execution.Order;
                        if (
        entryOrder != null && entryOrder == execution.Order)
                          {
                              Print(
        entryOrder.ToString());
                              if (
        entryOrder.OrderState == OrderState.Filled)
                              {
                              
        pos+=1;
                              Print(
        pos);
                              }
                              
        entryOrder null;
                          }    
                    
                    if (
        execution.Order.Name == "ShortHisto")  entryOrder execution.Order;
                        if (
        entryOrder != null && entryOrder == execution.Order)
                          {
                              Print(
        entryOrder.ToString());
                              if (
        entryOrder.OrderState == OrderState.Filled)
                              {
                              
        pos-=1;
                              Print(
        pos);
                              }
                              
        entryOrder null;
                          }
                
                }
        #endregion 
        Attached Files
        Last edited by cutzpr; 06-09-2018, 11:38 PM.

        Comment


          #5
          Hello cutzpr,

          Thank you for the reply.

          The Internal Order Handling Rules errors you are getting are not specifically related to RealtimeErrorHandling, I would suggest removing RealtimeErrorHandling from your script until you fully understand how the order handling rules apply to the Managed approach. RealtimeErrorHandling is an advanced property which allows you to take over when rejections occur but also puts your script at risk because you have to manage everything in that case.

          To learn about the Internal order handling rules that apply to the managed approach, please see the following page: https://ninjatrader.com/support/help...antedPositions

          As there are a few rules, you will need to review them all and see which applies to the situation where you are seeing the error in contrast to debugging or testing your script.

          As far as tracking goes, I would suggest taking a look at the following example which demonstrates tracking orders through their lifecycle: https://ninjatrader.com/support/help...and_onexec.htm

          My prior reply only had covered Filled orders as that is all you had originally asked for. To track orders from their initial states, you could also use OnOrderUpdate to find working orders and see when order states change. OnExecution can be used for knowing when fills occur and OnOrderUpdate for everything else.

          Please let me know if I may be of further assistance.
          JesseNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Aviram Y, Today, 05:29 AM
          0 responses
          1 view
          0 likes
          Last Post Aviram Y  
          Started by quantismo, 04-17-2024, 05:13 PM
          3 responses
          25 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by ScottWalsh, 04-16-2024, 04:29 PM
          7 responses
          34 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by cls71, Today, 04:45 AM
          0 responses
          6 views
          0 likes
          Last Post cls71
          by cls71
           
          Started by mjairg, 07-20-2023, 11:57 PM
          3 responses
          216 views
          1 like
          Last Post PaulMohn  
          Working...
          X