Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Dynamic Profit

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

    Dynamic Profit

    Hi,

    I would like to include a logic in my strategy where to keep trading until profits drops to 75% of the max profit reached. For example, if I'm trading and I've reached $100 in profits. I would keep trading until the profits fall 75%.

    So far, I’ve been able to successfully create the logic to get the currentPL for the day where it resets at the beginning of the day. I need help with creating a logic to get 75% of the max profit reached?

    Code:
    #region Using declarations
    
    using System;
    
    using System.Collections.Generic;
    
    using System.ComponentModel;
    
    using System.ComponentModel.DataAnnotations;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    using System.Windows;
    
    using System.Windows.Input;
    
    using System.Windows.Media;
    
    using System.Xml.Serialization;
    
    using NinjaTrader.Cbi;
    
    using NinjaTrader.Gui;
    
    using NinjaTrader.Gui.Chart;
    
    using NinjaTrader.Gui.SuperDom;
    
    using NinjaTrader.Gui.Tools;
    
    using NinjaTrader.Data;
    
    using NinjaTrader.NinjaScript;
    
    using NinjaTrader.Core.FloatingPoint;
    
    using NinjaTrader.NinjaScript.Indicators;
    
    using NinjaTrader.NinjaScript.DrawingTools;
    
    #endregion
    
    
    
    
    //This namespace holds Strategies in this folder and is required. Do not change it.
    
    namespace NinjaTrader.NinjaScript.Strategies
    
    {
    
    public class DynamicPLExample : Strategy
    
    {
    
    private bool stoptrading = false;
    
    private double Accumulated = 0;
    
    private double UnrealizedPL;
    
    private double RealizedPL;
    
    private double CurrentPL;
    
    
    
    protected override void OnStateChange()
    
    {
    
    if (State == State.SetDefaults)
    
    {
    
    Description = @"Enter the description for your new custom Strategy here.";
    
    Name = "DynamicPLExample";
    
    Calculate = Calculate.OnBarClose;
    
    EntriesPerDirection = 1;
    
    EntryHandling = EntryHandling.AllEntries;
    
    IsExitOnSessionCloseStrategy = true;
    
    ExitOnSessionCloseSeconds = 30;
    
    IsFillLimitOnTouch = false;
    
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    
    OrderFillResolution = OrderFillResolution.Standard;
    
    Slippage = 0;
    
    StartBehavior = StartBehavior.WaitUntilFlat;
    
    TimeInForce = TimeInForce.Gtc;
    
    TraceOrders = false;
    
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    
    BarsRequiredToTrade = 20;
    
    // Disable this property for performance gains in Strategy Analyzer optimizations
    
    // See the Help Guide for additional information
    
    IsInstantiatedOnEachOptimizationIteration = true;
    
    }
    
    else if (State == State.Configure)
    
    {
    
    }
    
    }
    
    
    
    
    protected override void OnBarUpdate()
    
    {
    
    if (BarsInProgress != 0)
    
    return;
    
    
    
    
    if (CurrentBars[0] < 1)
    
    return;
    
    
    RealizedPL = SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit - Accumulated;
    
    
    
    UnrealizedPL = Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Closes[0][0]);
    
    
    
    CurrentPL = RealizedPL + UnrealizedPL;
    ​
    
    
    
    if (Bars.IsFirstBarOfSession)
    
    {
    
    Print(Instrument.FullName + "|" + "ProfitLoss:" + "|" + ProfitLoss);
    
    Print(Instrument.FullName + "|" + "CurrentPL:" + "|" + CurrentPL);
    
    Accumulated = SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit;
    
    stoptrading = false;
    
    }
    
    
    
    //Entry Logic
    
    
    
    if ((Position.MarketPosition == MarketPosition.Long) || (Position.MarketPosition == MarketPosition.Short))
    
    {
    
    [COLOR=#e74c3c]if (CurrentPL > {Need Logic Here} If CurrentPL is 75% OffsetTextConverter max profit preached)[/COLOR]
    
    {
    
    stoptrading = true;
    
    }
    
    
    
    }
    
    }
    
    }
    ​
    Last edited by AgriTrdr; 08-07-2024, 12:08 PM.

    #2
    Hello AgriTrdr,

    Thank you for your post.

    You would need to do some math to calculate 75% of the max profit. From the code, it looks like you are storing this max value in a variable called Accumulated and the current pnl in CurrentPL? In that case, you could do something like:

    Code:
    if (CurrentPL <= (Accumulated * 0.75))
    // stop trading
    Please let us know if you have any further quesitons.

    Comment


      #3
      So in the strategy Accumulated is just keeping all the profits and not the net profit. Would I be able to do something like this?

      I tried the below but it's giving me an error CS1501 on MaxProfit = Math.Max(CurrentPL);

      Code:
      private double MaxProfit;
      
      OnBarUpdate()
      {
      
      CurrentPL = RealizedPL + UnrealizedPL;
      ​
      MaxProfit = Math.Max(CurrentPL);
      
      if (CurrentPL <= (MaxProfit * 0.75))
      // stop trading

      Comment


        #4
        Hello AgriTdr,

        Math.Max is supposed to return the larger of two numbers, however you are only supplying one.



        If you want to obtain the net profit just use NetProfit property.



        Please let us know if you have any further questions.

        Comment


          #5
          So I believe NetProfit only gives the current NetProfit and not the max NetProfit that was in the strategy. I think I need to hold the max CurrentPL somehow and do the first logic example that you had provided.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          116 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          61 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          40 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          43 views
          0 likes
          Last Post TheRealMorford  
          Started by Mindset, 02-28-2026, 06:16 AM
          0 responses
          82 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Working...
          X