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

Basing share size on previous trade result

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

    Basing share size on previous trade result

    Does anyone know if there is a method to check if the previous trade was a winning or losing trade and using that to determine the number of shares for the following trade.

    As with many strategies losers and winners often come in groups. In my efforts to minimize drawdowns I would like to trade smaller when I'm in a drawdown and perhaps trade larger when I'm on a winning streak. My current strategy has as many as 25 consecutive losers AND winners I would like to "exploit" this if I can.

    Any thoughts?

    #2
    Hello ShruggedAtlas,

    Thank you for your inquiry.

    I would suggest reading this section of the help guide on TradeCollection, which is a collection of Trade objects: http://ninjatrader.com/support/helpG...collection.htm

    This reference sample from our support forum may be of use as well: http://ninjatrader.com/support/forum...ead.php?t=4084

    To obtain the net profit of the last trade, you would do something like the example below:
    Code:
    Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
    double totalProfitOfLastTrade = lastTrade.ProfitCurrency * lastTrade.Quantity;
    You would then know if the trade was a winner or loser based on a negative or positive profit.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Outstanding! Thanks!

      Comment


        #4
        Problems...of course.... with implementation

        I have tried to implement the info you sent me but I'm getting a
        "You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."
        error.

        The problem seems to be with the following code:
        Code:
        Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
        what am I doing wrong?

        The entry code from BarsInProgress 0 is as follows:

        Code:
                    if( BarsInProgress == 0 )
                    {
                        Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
                        
                        if (ToTime(Time[0]) > start && ToTime(Time[0]) < (start + end))
                        {                    
                            if(direction == "down")
                            {
                                Print("direction is *DOWN* @ " + Time[0].ToString());
                                
                                if(CUMRSI(BarsArray[0],triggerPeriod,3)[0] > upper)
                                {                            
                                    if(Performance.AllTrades.Count > 1)
                                    {
                                        Print(lastTrade.ProfitCurrency);
                                        
                                        if(lastTrade.ProfitCurrency < 0)
                                        {                                
                                            EnterShort(shares / 2, "E1");
                                        }
                                        else
                                        {
                                            EnterShort(shares, "E1");
                                        }
                                    }
                                }
                            }
        
                        }
        }

        Comment


          #5
          Hello ShruggedAtlas,

          You need to check if you actually have a trade before that code can be implemented.

          If there are no trades, you would be trying to access an index of -1, which is out of bounds.

          Example:
          Code:
          if (Performance.AllTrades.Count > 0)
          {
               // logic
          }
          Zachary G.NinjaTrader Customer Service

          Comment


            #6
            I'm afraid i'm confused. My trade size depends on the previous trade.

            Performance.AllTrades.Count > 1 prevents any trades from happening if I put my entry logic within that block.

            the following code yields no trades.
            Code:
                        if( BarsInProgress == 0 )
                        {
                            if(Performance.AllTrades.Count > 1)
                            {
                                Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
                            
                                if (ToTime(Time[0]) > start && ToTime(Time[0]) < (start + end))
                                {                    
                                    if(direction == "down")
                                    {
                                        Print("direction is *DOWN* @ " + Time[0].ToString());
                                    
                                        if(CUMRSI(BarsArray[0],triggerPeriod,3)[0] > upper)
                                        {        
                                            EnterShort(shares, "E1");
                                        }
                                    }
                                }                    
            
                            }
            
                        }
            Last edited by ShruggedAtlas; 08-13-2015, 11:38 AM.

            Comment


              #7
              Hello ShruggedAtlas,

              You are getting the "You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart" because your code is trying to access a trade that does not yet exist. Without any trades, the Performance.AllTrades.Count would be 0. 0 - 1 is -1. Performance.AllTrades[-1] is out of bounds.

              You do not want Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1]; to run without a trade in there first. In order to check for this, you would use:
              Code:
              if (Performance.AllTrades.Count > 0)
              {
                   Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
              }
              Zachary G.NinjaTrader Customer Service

              Comment


                #8
                To be sure i restarted NT and reapplied my strategy. I do not get the error you mentioned after changing my code to what is shown in my last post. I believe I'm not getting trades at the moment because my entry code is placed within the ...... if(Performance.AllTrades.Count > 0).....
                block. It seems I have to run my original entry code at least once to get a trade before I can actually run the .......if(Performance.AllTrades.Count > 0)..... block which would contain the .....Trade lastTrade variable assignment etc.

                I'm not trying to contradict you I'm just saying my code seems to run without error and there are no trades taken so this just my analysis of it.

                Unless there is something else I'm doing wrong the only thing I can think of doing right now is creating a conditional using a flag variable that flags after the first trade and then routes me to block containing the trade tracking code.

                Update: I got trades after running the following code which includes a flag conditional.

                Code:
                            if( BarsInProgress == 0 )
                            {
                                if (flag == false)
                                {
                                    if (ToTime(Time[0]) > start && ToTime(Time[0]) < (start + end))
                                    {                    
                                        if(direction == "down")
                                        {                    
                                            if(CUMRSI(BarsArray[0],triggerPeriod,3)[0] > upper)
                                            {        
                                                EnterShort(shares, "E1");
                                                flag = true;
                                            }
                                        }
                                    }
                                }
                                else
                                {                
                                    if(Performance.AllTrades.Count > 0)
                                    {
                                        Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
                                
                                        if (ToTime(Time[0]) > start && ToTime(Time[0]) < (start + end))
                                        {                    
                                            if(direction == "down")
                                            {                    
                                                if(CUMRSI(BarsArray[0],triggerPeriod,3)[0] > upper)
                                                {        
                                                    EnterShort(shares, "E1");
                                                }
                                            }
                                        }                    
                
                                    }
                                }
                
                            }
                Last edited by ShruggedAtlas; 08-13-2015, 01:09 PM.

                Comment


                  #9
                  Hello ShruggedAtlas,

                  You will not get any trades if your entry logic is placed into the if (Performance.AllTrades.Count > 0) block because the if statement will never be true.

                  This check is there so that the line "Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];" will only run as long as you have at least one trade. Without at least one trade, you will get an out of range error.

                  Code:
                  if (Performance.AllTrades.Count > 0)
                  {
                       Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
                  }
                  You will want your initial entry logic to be outside of that if statement.
                  Zachary G.NinjaTrader Customer Service

                  Comment


                    #10
                    Here is what I have so far but unfortunately I'm not getting the correct results. I'm not getting any errors and it does appear to be adjusting position size but unfortunately not correctly :-(
                    Code:
                                if( BarsInProgress == 0 )
                                {
                                    if (flag == false)
                                    {
                                        if (ToTime(Time[0]) > start && ToTime(Time[0]) < (start + end))
                                        {                    
                                            if(direction == "down")
                                            {                    
                                                if(CUMRSI(BarsArray[0],triggerPeriod,3)[0] > upper)
                                                {        
                                                    EnterShort(shares, "E1");
                                                    flag = true;
                                                }
                                            }
                                        }
                                        if (ToTime(Time[0]) > start && ToTime(Time[0]) < (start + end))
                                        {        
                                        
                                            if(direction == "up")
                                            {                    
                                                if(CUMRSI(BarsArray[0],triggerPeriod,3)[0] < lower)
                                                {                            
                                                    EnterLong(shares,"E1");
                                                    flag = true;
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {                
                                        if(Performance.AllTrades.Count > 0)
                                        {
                                            Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
                                    
                                            if (ToTime(Time[0]) > start && ToTime(Time[0]) < (start + end))
                                            {                    
                                                if(direction == "down")
                                                {                    
                                                    if(CUMRSI(BarsArray[0],triggerPeriod,3)[0] > upper)
                                                    {    
                                                        if(lastTrade.ProfitCurrency > 0)
                                                        {
                                                            EnterShort(shares * 2, "E1");
                                                        }
                                                        else
                                                        {
                                                            EnterShort(shares, "E1");
                                                        }
                                                    }
                                                }
                                            }    
                                            if (ToTime(Time[0]) > start && ToTime(Time[0]) < (start + end))
                                            {                    
                                                if(direction == "up")
                                                {                    
                                                    if(CUMRSI(BarsArray[0],triggerPeriod,3)[0] < lower)
                                                    {    
                                                        if(lastTrade.ProfitCurrency < 0)
                                                        {
                                                            EnterLong(shares / 2, "E1");
                                                        }
                                                        else
                                                        {
                                                            EnterLong(shares, "E1");
                                                        }
                                                    }
                                                }
                                            }
                    
                                        }
                                    }
                    
                                }
                    The first trade is 4,000 (default size) and Profit.Currency > 0 so the next trade double to 8,000 shares and the trade results in Profit.Currency > 0 but then the next trade takes a position size of 4000. It should once again take an 8,000 share position size. Obviously I'm doing something wrong here.

                    I've included results in attached image. you can see the incorrect ones vs the correct ones.
                    Attached Files
                    Last edited by ShruggedAtlas; 08-13-2015, 01:44 PM.

                    Comment


                      #11
                      Hello ShruggedAtlas,

                      At this point, I would suggest adding Print statements into your code to see what is going on and why the amount of contracts for each trade is incorrect.

                      Because lastTrade.ProfitCurrency is the determining factor for your amount of contracts, here is an example of what you could use for a Print statement:

                      Code:
                      Print(lastTrade.ProfitCurrency);
                      From here, you could print out other variables if needed to determine more about what's going on and why the logic is running in this way.

                      Please, let us know if we may be of further assistance.
                      Zachary G.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by jaybedreamin, Today, 05:56 PM
                      0 responses
                      3 views
                      0 likes
                      Last Post jaybedreamin  
                      Started by DJ888, 04-16-2024, 06:09 PM
                      6 responses
                      18 views
                      0 likes
                      Last Post DJ888
                      by DJ888
                       
                      Started by Jon17, Today, 04:33 PM
                      0 responses
                      1 view
                      0 likes
                      Last Post Jon17
                      by Jon17
                       
                      Started by Javierw.ok, Today, 04:12 PM
                      0 responses
                      9 views
                      0 likes
                      Last Post Javierw.ok  
                      Started by timmbbo, Today, 08:59 AM
                      2 responses
                      10 views
                      0 likes
                      Last Post bltdavid  
                      Working...
                      X