Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using formulas to create variables & variable reset

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

    Using formulas to create variables & variable reset

    Hello,

    Below is a snip of a section of my automated strategy.




    This bot focuses on entering short trades only.

    "Benchmark" - is used as a level that is set manually from technical/fundamental analysis, while all other inputs are essentially offsets.
    The Profit Target is set to One point away while the Stop Loss is also set to One point away from the "ShortEntry".

    What I want is:

    When the bot enters a trade and takes its One point profit target, then great, we take profit and the strategy will place another order when my Benchmark is reached again.

    and

    After we have our first realized loss, before placing the next order, the bot will look at the AccountItem.GrossRealizedProfit and divide it by 100 (100 is derived from the currency amount that one /ES point is worth based on how many contracts you trade, in this case: OrderQuantity = 2 or $100 per point.). The result from this math is essentially the amount of points lost during the life of the strategy. Now, the points lost from that trade, or "LossInPoints" should then be added to the profit target on the next trade.

    It seems to only work for one loop, so when we have one loss before reaching profit it will add one extra point on the next trade. Then after that if we have three losses before reaching profit it should add three extra points but it adds only one point again.

    How can I have the profit target update after the first loop and so on?

    #2
    Hello Don22Trader1,

    Thanks for posting, but there seems to be an issue with the attachment. Could you try sharing it again, or try providing a text file attachment?

    Comment


      #3
      Click image for larger version

Name:	choppybot.png
Views:	352
Size:	64.0 KB
ID:	1178996

      Comment


        #4
        Don22Trader1,

        Thanks for your message.

        As you are using CalculationMode.Price, the target and stop will always be at the same price levels. If you want to update the Profit Target or the Stop Loss to a new level, you can do so in OnBarUpdate and call the Set method there. Be sure to set the Set method to an initial level before the next entry order is submitted to ensure the Stop Loss and Profit Target are submitted to valid levels once the entry order fills.

        An example that demonstrates updating a stop loss with SetStopLoss in OnBarUpdate, and resetting SetStopLoss to an initial level before the next entry is submitted can be found below.



        As a side note, I see that you are accessing Account Realized PnL at the beginning of the scripts life and in OnBarUpdate. I would suggest only using Account level information during realtime processing, or to skip processing historcial data in the script, because the Account Realized PnL may not be relevant for the backtest/historical processing protion of the strategy.

        If you want the strategy to skip processing historical data and always start flat, you can add:

        if (State == State.Historical) return;

        to the top of the OnBarUpdate method.

        Comment


          #5
          If using CalculationMode.Price always keeps the profit target in the same level, what other CalculationMode method will allow the profit target to move after my position goes flat and enters again?



          Also, after implementing Lines 68 - 82 from the "SamplePriceModification" code that you sent a link to, the strategy takes around 9 to 10 minutes, or more, to connect to the simulation101 account market data after being enabled. Is there a reason for this?
          Last edited by Don22Trader1; 11-19-2021, 02:31 PM.

          Comment


            #6
            Hello Don22Trader,

            If using CalculationMode.Price always keeps the profit target in the same level, what other CalculationMode method will allow the profit target to move after my position goes flat and enters again?
            The CalculationMode determines how you want the target/stop order offset from the entry price. For example, Ticks would be ticks from the average entry price, Price means a definite price.

            If you want to update a Stop Loss or Profit Target while in a position, you can call SetProfitTarget or SetStopLoss with a CalculationMode of Price in OnBarUpdate when Position.MarketPosition == MarketPosition.Long (and other logic that would be used to trigger the update of the target/stop)

            You would just have to be sure to call the Set method with an initial level before the next entry is submitted because Set methods prep NinjaTrader to submit target/stop when the entry order fills. If the Set method is not reset before the next entry, it would be using a previous level, and may be rejected/ignored.

            Also, after implementing Lines 68 - 82 from the "SamplePriceModification" code that you sent a link to, the strategy takes around 8 to 9 minutes to connect to the simulation101 account market data once enabled. Is there a reason for this?
            This does not tell me enough about what exactly is programmed in the strategy and what is causing the issue. You can comment/uncomment parts of your code to identify which specific parts are causing specific behaviors. If you have a specific question, please demonstrate with a small test script. We will not be able to provide code review for a full strategy, but if you implement just the part you are having trouble with in a small test script and share how you are testing to to hit a symptom, we can comment further.

            Exporting as source code - https://ninjatrader.com/support/help...tAsSourceFiles

            Comment


              #7
              Thanks for clarifying on the CalculationMode Method.

              As for updating the profit target, I do not want to update the profit target during a position. I wish to update the profit target after realizing a loss and the bot is not in a position. Essentially, like how a trailing drawdown works, it will have to follow the highest point my account reached at any time of the trading session. The next time it enters a position, any dollar loss accumulated (from gross realized pnl) will be calculated into "points" that will be added to the profit target on the next trade. Once my account reaches a new high, the profit target will be reset to its default value.

              Should I compare the real-time account size to the historical account size, and if the real-time is less than the latter, then add the real-time gross pnl?

              and if greater than...

              the real time gross pnl will be set to Zero?




              Comment


                #8
                Hello Don22Trader1,

                If you want to change the Profit Target for the next entry, call SetProfitTarget in OnBarUpdate with your intended level when the strategy is flat, and before you call the entry order method.

                Should I compare the real-time account size to the historical account size, and if the real-time is less than the latter, then add the real-time gross pnl?
                This is going to be subjective to your strategy, but the real Account information would not be relevant for the historical portion. I would suggest only using information from Account.Get when the strategy is processing realtime data. I also suggest using debugging prints to check the values being calculated that you want to use for the Stop and Target levels.

                You can check State in OnBarUpdate to see if the strategy is processing historical data if (State == State.Historical) or is processing realtime data with if (State == State.Realtime)

                Comment


                  #9
                  Ninjatrader_Jim,

                  Thanks for all the help, I figured out how to add the loss in points from the previous trade on to my next trade. Initially I had the SetProfitTarget to adjust while in a short position instead of having it under MarketPosition.Flat. Moving it under MarketPosition.Flat fixed that.

                  Also, for the simple division formula that creates the variable "LossInPoints", I moved it out of State.Configure and placed it in the OnBarUpdate right after "If (CurrentBars[0] < 1 || CurrentBars[1] < 0)". This allows for "LossInPoints" to update constantly according to the Account Gross Realized Profit Loss.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by NullPointStrategies, Today, 05:17 AM
                  0 responses
                  50 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  126 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  69 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  42 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  46 views
                  0 likes
                  Last Post TheRealMorford  
                  Working...
                  X