Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trade limit per day fix my code.

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

    Trade limit per day fix my code.

    I am trying to limit my automated trading system to 1 complete trade per day. Meaning it will stop the strategy after a buy and then a sell OR a sell then a buy. (its a basic crossover strategy at the moment) I searched and found some sample code but I do not know where to actually put it in my strategy. Here it is. My strategy does limit the time It makes trades now where do I stick the code to limit the number of trades. Within which brackets is what I'm looking for.

    protected override void OnBarUpdate()
    {

    /* Checks to see if the time is during the busier hours (format is HHMMSS or HMMSS). Only allow trading if current time is during a busy period.
    The timezone used here is (GMT-05:00) EST. */

    if ((ToTime(Time[0]) >= 093000 && ToTime(Time[0]) < 161400))
    {
    // Condition set 1
    if (CrossBelow(ZLEMA(myInput0), SMA(myInput1), 1))
    {
    EnterShort(DefaultQuantity, "");
    }

    // Condition set 2
    if (CrossAbove(ZLEMA(myInput0), SMA(myInput1), 1))
    {
    ExitShort("", "");
    }

    // At the start of a new session
    if (Bars.FirstBarOfSession)
    {
    // Store the strategy's prior number of trades
    priorTradesCount = Performance.AllTrades.Count;

    /* NOTE: Using .AllTrades will include both historical virtual trades as well as real-time trades.
    If you want to only count profits from real-time trades please use .RealtimeTrades. */
    }

    /* prevent trading if 1 trades have already been made in this session. */
    if (Performance.AllTrades.Count - priorTradesCount > 1)

    {
    /* TIP FOR EXPERIENCED CODERS: This only prevents trade logic in the context of the OnBarUpdate() method. If you are utilizing
    other methods like OnOrderUpdate() or OnMarketData() you will need to insert this code segment there as well. */

    // Returns out of the OnBarUpdate() method. This prevents any further evaluation of trade logic in the OnBarUpdate() method.
    return;
    }


    }


    }

    #2
    Hi there, unfortunately we are not able to code out strategies. You'll have to use some sort of a bool flag to limit your trades. Please see the following idea for how to accomplish this:
    Code:
    // in variables
    
    private bool hasEnteredLong = false;
    private bool hasEnteredShort = false;
    
    protected override void OnBarUpdate()
    {
       if (Bars.FirstBarOfSession)
       {
           // this means this is the first bar of the day, reset the flags
           hasEnteredLong = false;
           hasEnteredShort = false;
       }
    
       if (conditions for long entry == true && hasEnteredLong == false)
       {
           hasEnteredLong = true;
           EnterLong(....);
       }
       
       if (conditions for short entry == true && hasEnteredShort == false)
       {
           hasEnteredShort = true;
           EnterShort(....);
       }
    }
    AustinNinjaTrader Customer Service

    Comment


      #3
      Thanks I will give this a try.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by bmartz, 03-12-2024, 06:12 AM
      5 responses
      32 views
      0 likes
      Last Post NinjaTrader_Zachary  
      Started by Aviram Y, Today, 05:29 AM
      4 responses
      13 views
      0 likes
      Last Post Aviram Y  
      Started by algospoke, 04-17-2024, 06:40 PM
      3 responses
      28 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by gentlebenthebear, Today, 01:30 AM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by cls71, Today, 04:45 AM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Working...
      X