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

Increase or Decrease Contracts based on last trade

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

    Increase or Decrease Contracts based on last trade

    Hi all,

    I am looking for the right syntax to increase or decrease my number of contracts based on whether or not I won or lost my last trade or series of trades. I know how to increase or decrease my quantity pretty easily, but I am unsure how to make this a condition based on the results of my last trade or last few trades. For example: If in my last trade I lost maybe on the next trade I decrease my number of contracts from 3 to 2, or if I lost my last 2 trades I increase my number of contracts from 3 to 4. I just don't know the specific syntax to look back at the last series of trades. Previous trade outcome 1, 2,3,4, etc.

    Could anyone fill in the blanks a little bit in terms of what syntax I would use to see the last few trades?

    I apologize if this has been asked previously in another question, but I had no luck finding it, as I don't know the exact wording or syntax that should be used to refer to the outcome of the previous few trades

    Thanks,

    Ian

    #2
    Hello iantg,

    The Performance collection is a collection of the trades.
    http://ninjatrader.com/support/helpG.../alltrades.htm

    To loop through the trades you can do the following:

    for (int i = 0; i < Performance.AllTrades.Count; i++)
    {
    Print(Performance.AllTrades[i].ProfitCurrency);
    }
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Chelsea, thank you for this. I was able to implement my strategy using this, but I have now hit another snag. I often see trade quantities getting split, for example If I have 8 contracts on my last trade, they get executed as multiple pieces (4 and 4). Then when I use this code to see the quantity in the last trade it shows me the last piece of the split contract of 4 and not the entire contract of 8. Is there a way to access the the full quantity instead of the last piece of the contract assuming it gets split when executed?

      Comment


        #4
        Hi Ian,

        If there is a Part Fill, this does cause the part fill to become a separate trade.

        You can check to see if the order part filled in the Log tab of the Control Center.

        You can instead check the entry order's quantity to know what the order was originally submitted for.
        Performance.AllTrades[Performance.AllTrades.Count - 1].Entry.Quantity.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thank you for your prompt response. I tried adding the line of code you provided and it works fine for eliminating double exits paired to a single entries, but when I have double entries it does not work. I have attached an image of the trades executed to you can see the issue: The first series of trades work fine, each trade increments the contracts up by 1 until the contracts start getting split. The first double entry splits an 8 contract trade into 6 and 2, then in the next trade series the code uses the first part of the 8 contracts (6) and increments it by 1 to get 7, whereas it should use the full contract of 8 increment by 1 and get 9. So this is the issue I am trying to work through. Is there any other property I can use to access the full contract for the previous trade and not just the first, or last part of the split?

          Thanks for your help. You have gotten me this far which is great
          Attached Files

          Comment


            #6
            Hello Ian,

            To test to see if the original entry order's quantity is returned I have tested this in a simple script.

            Below is a link to a video I created of the test.
            http://screencast.com/t/65HEXRr8

            Attached is the script I have tested.
            Attached Files
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thanks for the information. This looks promising. I will give it a shot and let you know how it goes.

              Thanks for the help.

              Comment


                #8
                Hello Chelsea, sorry i m not an expert, i hope you can help me.

                I need to increse or decrease the quantity of contract in base to last trade, for exemple i i win i need to add 1 contract and if i lost i have to decrease 1 contract.
                This is the code
                #region Using declarations
                using System;
                using System.ComponentModel;
                using System.Diagnostics;
                using System.Drawing;
                using System.Drawing.Drawing2D;
                using System.Xml.Serialization;
                using NinjaTrader.Cbi;
                using NinjaTrader.Data;
                using NinjaTrader.Indicator;
                using NinjaTrader.Gui.Chart;
                using NinjaTrader.Strategy;
                #endregion

                // This namespace holds all strategies and is required. Do not change it.
                namespace NinjaTrader.Strategy
                {
                /// <summary>
                /// Enter the description of your strategy here
                /// </summary>
                [Description("Enter the description of your strategy here")]
                public class DMcc : Strategy
                {
                #region Variables
                // Wizard generated variables
                private int stoploss = 50; // Default setting for Stoploss
                // User defined variables (add any user defined variables below)
                #endregion

                private bool once = false;
                private IOrder entryOrder;

                /// <summary>
                /// This method is used to configure the strategy and is called once before any strategy method is called.
                /// </summary>
                protected override void Initialize()


                {
                SetStopLoss("", CalculationMode.Ticks, Stoploss, false);

                CalculateOnBarClose = true;
                }

                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()


                {


                // Condition set 1
                if (CrossAbove(DM(14).DiPlus, DM(14).DiMinus, 1)
                && ChaikinVolatility(14)[0] > 0)
                {
                EnterLong(DefaultQuantity, "");
                }

                // Condition set 2
                if (CrossAbove(DM(14).DiMinus, DM(14).DiPlus, 1)
                && ChaikinVolatility(14)[0] > 0)
                {
                EnterShort(DefaultQuantity, "");
                }
                }


                #region Properties
                [Description("")]
                [GridCategory("Parameters")]
                public int Stoploss
                {
                get { return stoploss; }
                set { stoploss = Math.Max(1, value); }
                }
                #endregion
                }
                }

                Comment


                  #9
                  Hello mylanel,

                  First, you will need to start with a variable to track the quantity.

                  I highly recommend that you read over the basic programming concepts in the NinjaTrader Help Guide.
                  http://ninjatrader.com/support/helpG...g_concepts.htm

                  Then you will need a condition that looks at the performance of the last made trade.
                  Performance.AllTrades[Performance.AllTrades.Count-1].ProfitCurrency

                  If the profit is positive, then increment the quantity variable.
                  myVariable++;
                  (which is the same as myVariable = myVariable + 1)

                  If the profit is negative, then decrement the quantity variable.
                  myVariable--;
                  (also the same as myVariable = myVariable - 1)
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Thanks for your reply, i'm not a programmator, can you modify my strategy with your solution please? because i don't know where put the code.
                    thanks again

                    Comment


                      #11
                      mylanel,

                      In the support department at NinjaTrader we do not create, debug, or modify code for our clients. This is so that we can maintain a high level of service for all of our clients as well as our partners.

                      This thread will remain open for any community members who would like to create this script as a convenience to you.

                      You can also contact one of our professional NinjaScript Consultants who would be eager to create or modify this script at your request or assist you with your script. Please let me know if you would like our business development follow up with you with a list of professional NinjaScript Consultants who would be happy to create this script or any others at your request.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        yes i understand, but if you can tell me in which part i can put your code i can solve the problem alone.

                        Comment


                          #13
                          Hello mylanel,

                          The condition that checks the performance could be in OnPositionUpdate after the position updates. You would also need to add a check that an order was closed.
                          http://ninjatrader.com/support/helpG...tionupdate.htm
                          Chelsea B.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by tonynt, Yesterday, 01:48 PM
                          2 responses
                          10 views
                          0 likes
                          Last Post tonynt
                          by tonynt
                           
                          Started by goodknight777, Today, 08:43 AM
                          1 response
                          2 views
                          0 likes
                          Last Post NinjaTrader_ChelseaB  
                          Started by Salahinho99, 05-05-2024, 04:13 AM
                          5 responses
                          47 views
                          0 likes
                          Last Post NinjaTrader_ChelseaB  
                          Started by ttrader23, Yesterday, 09:04 AM
                          7 responses
                          34 views
                          0 likes
                          Last Post ttrader23  
                          Started by damethetrader, Today, 08:31 AM
                          1 response
                          4 views
                          0 likes
                          Last Post NinjaTrader_Clayton  
                          Working...
                          X