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

Max daily loss or profit stop trading

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

    Max daily loss or profit stop trading

    I'm trying to add a max profit and max daily loss to stop trading to my strategy, I have seen every thread here and youtube video out there on the subject but still can't get it to work properly. The daily profit works but from what I have seen defining the max loss as a negative number seems to be my issue. The example below is where I put the code and basic Idea , not all the code obviously. If I comment out OktoTrade = false; it places trades which indicates I'm going into that code thinking I have reached my Max_profit or Max_loss even though I haven't. If I enter a Max_loss as 0 it makes trades but without the desired behaving stopping at my loss limits.


    public classMyStrategy : Strategy
    {

    private bool OktoTrade = true;
    private double Accumulated = 0;


    if (State == State.SetDefaults)
    {
    Max_Profit = 200;
    Max_Loss = -200;

    protected override void OnBarUpdate()
    {

    if (this.CurrentBar < this.BarsRequiredToPlot || this.CurrentBar < this.BarsRequiredToTrade)
    return;

    if (SystemPerformance.AllTrades.TradesPerformance.Cur rency.CumProfit - Accumulated > Max_Profit
    || SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit - Accumulated < Max_Loss)
    {
    OktoTrade = false;
    }
    if (!TimeToTrade(_startTimeInt, _endTimeInt, Time[0])) // reset at the end of the session
    {
    OktoTrade = true;
    Accumulated = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit;
    }
    The check if OKtoTrade before entering a new trade || !OktoTrade if I'm in a trade and I reach my max loss or max profit to exit the trade.

    // enter my values for the strategy
    [Range(0, int.MaxValue)]
    [Display(Name="Max_Profit", Description="", Order=16, GroupName="Parameters")]
    public double Max_Profit
    { get; set; }

    This doesn't allow me to enter a negative number , if I set this to 0 it doesn't check for Max_loss but does enter trades.
    [NinjaScriptProperty]
    [Range(0, int.MaxValue)]
    [Display(Name="Max_Loss", Description="", Order=17, GroupName="Parameters")]
    public double Max_Loss
    { get; set; }

    #2
    Hi jeffom,
    IF Max_Loss doesn't accept negative numbers, you might perhaps want to try using a positive value for Max_Loss (200 in your example) and then convert it to a negative number in your formulas
    || SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit - Accumulated < Max_Loss*-1.
    NT-Roland

    Comment


      #3
      Nice , it does something , anything different is good thank you .

      Comment


        #4
        Hello jeffom,

        I have an example of a daily loss limit you may find helpful.
        Hello, I've updated the DailyLossLimit and DailyLosLimitMultiTrade examples that were posted on the forum for NinjaTrader 7 for NinjaTrader 8. These are often requested and I felt they are good examples to have for NT8. DailyLossLimitExample_NT7 - http://ninjatrader.com/support/forum...241#post451241 (http://ninjatrader
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thank you , I'll check those out.

          Comment


            #6
            I may have spoken to soon, using the three trade examples I was given I still can't get to work , I am always going into this bit of code and setting Limit = true; What am I missing? The debug print seems to think the unrealized profit is 575 which I don't understand.

            public class Strategy : Strategy
            {
            private double currentPnL;
            protected override void OnStateChange()
            {
            if (State == State.SetDefaults)
            {
            LossLimit = 500;
            ProfitLimit = 500;
            }
            bool Limit = false;

            protected override void OnBarUpdate()
            {
            if (currentPnL + Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]) <= -LossLimit ||
            currentPnL + Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]) >= ProfitLimit)
            {
            Limit = true;
            Print("daily limit hit, no new orders" + Time[0].ToString());
            Print(string.Format("{0} | Unrealized: {1} | DailyLimit: {2}", Time[0], (currentPnL + Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0])), (-LossLimit)));
            }
            Last edited by jeffom; 03-13-2020, 04:17 PM.

            Comment


              #7
              Easily capture screenshots, GIFs, and replays that are ready to share. Download the free app for windows and mac.

              Comment


                #8
                I made some progress just updating this in case others run into the same thing. When running with replay data for testing, the above results was because of historical trades being triggered in the strategy and it already reached the profit limit so when I started to run forward no new trades were taken.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by LawrenHom, Today, 10:45 PM
                0 responses
                3 views
                0 likes
                Last Post LawrenHom  
                Started by love2code2trade, Yesterday, 01:45 PM
                4 responses
                28 views
                0 likes
                Last Post love2code2trade  
                Started by funk10101, Today, 09:43 PM
                0 responses
                7 views
                0 likes
                Last Post funk10101  
                Started by pkefal, 04-11-2024, 07:39 AM
                11 responses
                37 views
                0 likes
                Last Post jeronymite  
                Started by bill2023, Yesterday, 08:51 AM
                8 responses
                44 views
                0 likes
                Last Post bill2023  
                Working...
                X