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 cre8able, Yesterday, 07:24 PM
      1 response
      13 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by cocoescala, 10-12-2018, 11:02 PM
      6 responses
      939 views
      0 likes
      Last Post Jquiroz1975  
      Started by gbourque, Today, 06:39 AM
      1 response
      4 views
      0 likes
      Last Post NinjaTrader_Erick  
      Started by cmtjoancolmenero, Yesterday, 03:58 PM
      1 response
      17 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by benmarkal, Yesterday, 12:52 PM
      3 responses
      23 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Working...
      X