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

Limiting 1 trade per direction will not limit trade

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

    Limiting 1 trade per direction will not limit trade

    I'm trying to create a strategy that limits 1 trade if the trade is a loss according to the MAE indicator.

    I came across the example code attached & it seems it's optimized for 3 trades per day.

    I tried to change it it to add in both the Long & short side according to my strategy but it still won't limit trades per each side.

    I also tried separating them into Long with Long Logic & Short with Short Logic, & Starting with the short, I'm still not getting a limit entry based upon a trade loss on the short side.

    some code is removed, but all else equal, it still runs, it just doesn't limit trades based upon a trade loss

    Code:
    public class x : Strategy
    {
    ...
    ...
    ...
    
    private int lastLongTrades = 0; // This variable holds our value for how profitable the last three trades were.
    private int priorLongtrades = 0; // This variable holds the number of trades taken prior to each Long session.
    
    private int lastShortTrades = 0; // This variable holds our value for how profitable the last three trades were.
    private int priorShorttrades = 0; // This variable holds the number of trades taken prior to each Short session.
    
    private int priorNumberOfTrades = 0; // This variable holds the number of trades taken. It will be checked every OnBarUpdate() to determine when a trade has closed.
    
    private MAEnvelopes MAE;
    double MiddleMAE = MAE.Middle[0];
    
    public int RM
    RM  = 1;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    ...
    ...
    ...
    }
    else if (State == State.DataLoaded)
    {
    ...
    ...
    ...
    }
    }
    
    
    
    protected override void OnBarUpdate()
    {
    
    if (Close[0] - MiddleMAE < zero
    || MiddleMAE - Close[0] < zero)
    {
    lastLongTrades = 0;
    lastShortTrades = 0;
    priorLongtrades = SystemPerformance.LongTrades.Count;
    priorShorttrades = SystemPerformance.ShortTrades.Count;
    }
    
    // Makes sure there are total x trades for both & long short side
    if ((SystemPerformance.LongTrades.Count - priorLongtrades) >= RM
    || (SystemPerformance.ShortTrades.Count - priorShorttrades) >= RM
    
    //priorNumberOfTrades makes sure this code block only executes when a new trade has finished.
    && SystemPerformance.LongTrades.Count != priorNumberOfTrades
    || SystemPerformance.ShortTrades.Count != priorNumberOfTrades)
    
    {
    // Reset the counter. - Does the counting
    lastLongTrades = 0;
    lastShortTrades = 0;
    
    // Set the new number of completed trades.
    priorNumberOfTrades = SystemPerformance.LongTrades.Count;
    priorNumberOfTrades = SystemPerformance.ShortTrades.Count;
    for (int
    idx = 1; idx <= RM; idx++)
    {
    /* The SystemPerformance.AllTrades array stores the most recent trade at the highest index value. If there are a total of 10 trades,
    this loop will retrieve the 10th trade first (at index position 9), then the 9th trade (at 8), then the 8th trade. */
    Trade Longtrade = SystemPerformance.LongTrades[SystemPerformance.LongTrades.Count - idx];
    Trade Shorttrade = SystemPerformance.ShortTrades[SystemPerformance.ShortTrades.Count - idx];
    
    
    /* If the trade's profit is greater than 0, add one to the counter. If the trade's profit is less than 0, subtract one.
    This logic means break-even trades have no effect on the counter. */
    if (Longtrade.ProfitCurrency > 0
    || Shorttrade.ProfitCurrency > 0)
    {
    lastLongTrades++;
    lastShortTrades++;
    }
    
    else if (Longtrade.ProfitCurrency < 0
    || Shorttrade.ProfitCurrency > 0)
    {
    lastLongTrades--;
    lastShortTrades--;
    }
    }
    // Long Side
    if (lastLongTrades != -RM)
    {
    Entry Logic
    ...
    ...
    ...
    }
    
    if( x happens)
    {
    ExitLong("Long");
    }
    
    //****************************************************************
    
    // ShortSide
    if (ShortLongTrades != -RM)
    {
    Entry Logic
    ...
    ...
    ...
    }
    
    if( x happens)
    {
    ExitShort("Short");
    }
    }
    }
    }
    Click image for larger version

Name:	LimitLoss.PNG
Views:	230
Size:	20.8 KB
ID:	1161099
    What do I need to make this work?

    Thanks in advance.

    #2
    Hello sentient254,

    Thanks for your reply.

    I would suggest taking a different path.

    To stop trading after a loss you could:

    Save the realized PNl to a variable before a trade, perform the trade and if the realizedPNl is less after the trade than the variable then the trade is a loss and you can set a bool variable to prevent further trades.

    or

    You could check the trades collection to see if the last trade profit was a positive value. Reference: https://ninjatrader.com/support/help...collection.htm (see example 1) and then set a bool variable to prevent further trades.

    In terms of the code you posted, I would suggest debugging the code by using print statements to find out why it is not performing as expected. In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    Here is a link to our debugging tips: https://ninjatrader.com/support/help...script_cod.htm
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks, got it. One of my < symbols was reversed on the short side.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by kevinenergy, 02-17-2023, 12:42 PM
      117 responses
      2,766 views
      1 like
      Last Post jculp
      by jculp
       
      Started by Mongo, Today, 11:05 AM
      5 responses
      15 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by SightCareAubetter, Today, 12:55 PM
      0 responses
      3 views
      0 likes
      Last Post SightCareAubetter  
      Started by traderqz, Today, 12:06 AM
      8 responses
      16 views
      0 likes
      Last Post traderqz  
      Started by SightCareAubetter, Today, 12:50 PM
      0 responses
      2 views
      0 likes
      Last Post SightCareAubetter  
      Working...
      X