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

LotSize Based on TradePerformance

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

    LotSize Based on TradePerformance

    Hello,

    I have been working on some code to limit Lot Size when the previous trade was a loss. The code is also meant to specify look at the last trade of a Long or a Short.

    If the last (Long or Short) trade was profitable the Maximum Lot Size allowed will be allocated to trade.
    If the last (Long or Short) trade was NOT profitable the 1000 Lot Size allowed will be allocated to trade.

    2 part question:
    1) Is there anyway to get the Trade Count to specifically deliver the number of Long or Short Trades?
    Currently I am bringing in the count of all trades

    2) Is there anything else to consider when trying to asses the profitability of the last trade that I should be aware of?

    Code:
    //Lot Size to Trade
    if (MoneyInAccountToTradeGTB == true)
    && (SystemPerformance.LongTrades.TradesPerformance.ProfitFactor[Performance.AllTrades.Count - 1] >= 1)
    LotSizeToTradeGTB = Math.Floor(((Account.Get(AccountItem.CashValue, Currency.UsDollar) / 100) / Risk.Get("GTB ForexDotCom").ByMasterInstrument[Instrument.MasterInstrument].InitialMargin)) * 1000;
    else if (MoneyInAccountToTradeGTB == true)
    && (SystemPerformance.ShortTrades.TradesPerformance.ProfitFactor[Performance.AllTrades.Count - 1] >= 1)
    LotSizeToTradeGTB = Math.Floor(((Account.Get(AccountItem.CashValue, Currency.UsDollar) / 100) / Risk.Get("GTB ForexDotCom").ByMasterInstrument[Instrument.MasterInstrument].InitialMargin)) * 1000;
    if (MoneyInAccountToTradeGTB == true)
    && (SystemPerformance.LongTrades.TradesPerformance.ProfitFactor[Performance.AllTrades.Count - 1] < 1)
    LotSizeToTradeGTB = 1000;
    else if (MoneyInAccountToTradeGTB == true)
    && (SystemPerformance.ShortTrades.TradesPerformance.ProfitFactor[Performance.AllTrades.Count - 1] < 1)
    LotSizeToTradeGTB = 1000;
    else (MoneyInAccountToTradeGTB == false)
    LotSizeToTradeGTB = 0;
    FYI I typically put GTB within any variable I create. I also left out the calculation I already have in place to check if there is money to trade (MoneyInAccountToTradeGTB).

    #2
    Hello GTBrooks,

    Thanks for the post.

    You actually have part of what you need for the count, you just need to add Count:

    Code:
    SystemPerformance.LongTrades.Count

    For your second question there is nothing specific that comes to mind, are you having trouble in some way or was that just a general question?

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

    Comment


      #3
      Hello Jesse,

      Thank you for the Help. I updated the Script. Would this code run equally well in On Bar Update or on Account Item Update?

      Code:
      //Lot Size to Trade
      if (MoneyInAccountToTrade == true)
      && (SystemPerformance.LongTrades.TradesPerformance.ProfitFactor[SystemPerformance.LongTrades.Count - 1] > 1)
      LotSizeToTrade = Math.Floor(((Account.Get(AccountItem.CashValue, Currency.UsDollar) / 100) / Risk.Get("GTB ForexDotCom").ByMasterInstrument[Instrument.MasterInstrument].InitialMargin)) * 1000;
      else if (MoneyInAccountToTrade == true)
      && (SystemPerformance.ShortTrades.TradesPerformance.ProfitFactor[SystemPerformance.LongTrades.Count - 1] > 1)
      LotSizeToTrade = Math.Floor(((Account.Get(AccountItem.CashValue, Currency.UsDollar) / 100) / Risk.Get("GTB ForexDotCom").ByMasterInstrument[Instrument.MasterInstrument].InitialMargin)) * 1000;
      if (MoneyInAccountToTrade == true)
      && (SystemPerformance.LongTrades.TradesPerformance.ProfitFactor[SystemPerformance.ShortTrades.Count - 1] <= 1)
      LotSizeToTrade = 1000;
      else if (MoneyInAccountToTrade == true)
      && (SystemPerformance.ShortTrades.TradesPerformance.ProfitFactor[SystemPerformance.ShortTrades.Count - 1] <= 1)
      LotSizeToTrade = 1000;
      else (MoneyInAccountToTrade == false)
      LotSizeToTrade = 0;
      the second part of the question was for Best Practices. what would be the Best Practices for this code?
      Last edited by GTBrooks; 06-10-2020, 08:39 PM.

      Comment


        #4
        Hello GTBrooks,

        The OnBarUpdate and OnAccountItemUpdate overrides are for different purposes so that code would not be called at the same frequency. I would suggest using a Print in each override to see how its being called to better understand which you may want to use.

        As for best practices I am not aware of anything specific surrounding these items. I can suggest using Prints to confirm the values you are using outside of that you are pretty free to use these items as needed.

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

        Comment


          #5
          My current logic for Lot Size is in OnBarUpdate. I will put the code there. thanks for the help. I am sure I will be back with more questions later about a subject I have yet to tackle.

          Comment


            #6
            well the first attempt was made with 1 error code.

            Error Code: CS0021
            https://ninjatrader.com/support/help...t8/?cs0021.htm

            It looks like the issue is how I am calling the previous trade
            Code:
             SystemPerformance.LongTrades.TradesPerformance.ProfitFactor[SystemPerformance.LongTrades.Count - 1]
            specifically
            Code:
            [SystemPerformance.LongTrades.Count - 1]
            I tried turning the previous Long / Short Trade Count into a int Variable, and using the created Variable in the middle of the [ ]. the same error occurred.

            is there a way to call the last trades trade performance?

            Comment


              #7
              Hello GTBrooks,

              For a single trade you could look at the trades profit however I don't see a way to specifically get the ProfitFactor as that is part of the trade performance report.

              double cur = SystemPerformance.LongTrades[SystemPerformance.LongTrades.Count - 1].ProfitCurrency;


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

              Comment


                #8
                I am just trying to see if the last Long trade was profitable (net positive), it does not even matter if it is historical or real-time. I am hoping to use this code for delineating between long and short trades.

                is there any code in NinjaTrader 8 that would allow for this lookup.

                Comment


                  #9
                  Hello GTBrooks,

                  The code I last provided would be the profit for a single trade for the Long side. That would be the last trade made and its profit in currency.

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

                  Comment


                    #10
                    I used your code with success, thank you.
                    Last edited by GTBrooks; 06-18-2020, 03:43 PM.

                    Comment


                      #11
                      Hello GTBrooks,

                      The code i provided compiles fine on my end, did you copy/paste it? You would also need to make sure at least 1 trade happened before calling that code.

                      In the code you provided that is not valid so that would not compile, you would need to index the collection which would be the LongTrades collection:

                      SystemPerformance.LongTrades[]


                      Tracking changes in the performance would require the same general concepts, you need to access the performance first before knowing what it would be to calculate a change. Using the trade performance collection is the standard way to gather information about trades you just need to use the indexing on the correct object. There is a sample that works with these collections here: https://ninjatrader.com/support/help...nce_statis.htm


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

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by sidlercom80, 10-28-2023, 08:49 AM
                      168 responses
                      2,262 views
                      0 likes
                      Last Post sidlercom80  
                      Started by Barry Milan, Yesterday, 10:35 PM
                      3 responses
                      10 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by WeyldFalcon, 12-10-2020, 06:48 PM
                      14 responses
                      1,429 views
                      0 likes
                      Last Post Handclap0241  
                      Started by DJ888, 04-16-2024, 06:09 PM
                      2 responses
                      9 views
                      0 likes
                      Last Post DJ888
                      by DJ888
                       
                      Started by jeronymite, 04-12-2024, 04:26 PM
                      3 responses
                      41 views
                      0 likes
                      Last Post jeronymite  
                      Working...
                      X