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

Trades per session

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

    Trades per session

    Hello,

    Im trying to set a limit mumber of trades per session and not per day.
    I have the following code wich is limiting trades per day and not by session.
    Please assist.

    Thanks

    Code:
    protected override void OnBarUpdate()
    
    if (base.Bars.FirstBarOfSession)
    				
    				
                                    if (Performance.AllTrades.Count >= MaxTradesPerSession && MaxTradesPerSession != 0)
    				{return;}
            {

    #2
    hliboi, I would suggest checking into those two references here with alternate approaches -





    The AllTrades count would always be all your your trade done, historical as well as realtime - it would per se not reset at breaks, thus you would either need to custom count your trade entries per session piece or calcuate the trades done via first storing the all trades values at the session break.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      no go

      Hello Bertrand,

      Thanks but its not working.
      Here is my OnBarUpdate





      Code:
      		
      [SIZE="3"][SIZE="2"]protected override void OnBarUpdate()
              {
      			EntryHandling		= EntryHandling.UniqueEntries;
      		
      			
      //	Daily Loss
                  if (base.Bars.FirstBarOfSession && FirstTickOfBar)
      				priorCumProfit = Performance.AllTrades.TradesPerformance.Currency.CumProfit;
      //				tradeCounter = 0;
      				
      
      			// *** Calculate the total profit (cumulative profit minus prior profit plus the current position profit 
      			double myMaxProfit = maxprofit;
      			double myMaxLoss = maxloss;
      			double cumProfit = (double) Performance.AllTrades.TradesPerformance.Currency.CumProfit;
      			double curPosition = (double) Position.GetProfitLoss(Close[0], PerformanceUnit.Currency);
      			double totCumProfit = (double) (cumProfit - priorCumProfit) + curPosition ;
      					
      	
      			// *** STOP the strategy! if a total profit or loss exceeds the max
      				if (totCumProfit <= myMaxLoss || totCumProfit >= myMaxProfit)
      				{
      				if (Position.MarketPosition == MarketPosition.Long) {ExitLong("DMA: Exit Long ", "");}
      				if (Position.MarketPosition == MarketPosition.Short) {ExitShort("DMA: Exit Short", "");}
      				return;
      				}
      				
      
      //working with maxtradesperday
      
      				if (Performance.AllTrades.Count >= maxTradesPerDay && maxTradesPerDay != 0)
      				{return;} 
             
               
      			if (base.ToTime(base.Time[0]) < this.startTimeInt * 100 && this.startTimeInt > 0)
                 		 {
                   	   return;
                		  }
      			
      			if (base.ToTime(base.Time[0]) >= this.exitTimeInt * 100 && this.exitTimeInt > 0)
                  {
      				if (base.Position.MarketPosition == MarketPosition.Long)
                      {
                          base.ExitLong();
                      }
                      if (base.Position.MarketPosition == MarketPosition.Short)
                      {
                          base.ExitShort();
                      }
                      return;
                  }
      
      		if (base.ToTime(base.Time[0]) <= this.lastEntryTimeInt * 100 || this.lastEntryTimeInt == 0)
      			{	
      			
      			ManageOrders();
      	
      // Not Working maxtradespersession
      			
      		if (tradeCounter < MaxTradesSession && Position.MarketPosition == MarketPosition.Flat)				
      				{
                      
      			
      			if (Position.MarketPosition != MarketPosition.Flat) return;
      			
      			if (Long logic)
      				
      			{GoLong();
      				tradeCounter++;}
      			
      			else if (Short Logic)
      			
      				{GoShort();
      				tradeCounter++;}
      				
                    }
             		 }
      	
      			
      		}[/SIZE][/SIZE]

      Comment


        #4
        hliboi, so what outcome do you exactly see with the tradeCounter approach used? You would need to keep in mind here the counter would be increased if your logic to place an orders triggers, here you would not take into consideration if you were for example actually getting a fill or not. So results could differ in this respect from the performance class approach.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          I'm getting a max of XX trades per day as determined.
          But trades per session is not limiting anything. It continues to ignore it making more that XX trades per session.

          My session template has 4 sessions in one day. I would like to limit the trades per session that would create the possibility to have a maximum amount of trades per day but also limit the amount of trades per session.

          Thanks

          Comment


            #6
            Thanks for clarifying, seems I missed something about your snippet provided earlier -

            Code:
            if (base.Bars.FirstBarOfSession && FirstTickOfBar)
            				priorCumProfit = Performance.AllTrades.TradesPerformance.Currency.CumProfit;
            //				tradeCounter = 0;
            please change this to -

            Code:
             if (base.Bars.FirstBarOfSession && FirstTickOfBar)
            {
                priorCumProfit = Performance.AllTrades.TradesPerformance.Currency.CumProfit;
                tradeCounter = 0;
            }
            As otherwise you would never reset your counter or reset on every bar (if you just had your outcomment // removed and not changed the bracing).
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Sorry to bother you again Bertrand,

              Its still not working. Getting my maximum number of trades per day but its ignoring the max trades per session. I've put 3 trades per session so at maximum one should have 12 trades per day. I've put the maximum trades per day at 21 for instance and sure enough it only makes 21 trades when it should only make 12.

              Thank You

              Comment


                #8
                No worries, we will get to the core of it. What session template do you exactly use and how do you judge it's doing too many trades per session segment defined? If you print the tradeCounter from OnBarUpdate() would it actually exceed then your set MaxTradesPerSession #?

                To debug further why this occurs I would

                a) print all variables in your code that would affect this part of logic
                b) leave the max trades per day logic out
                c) just set for example 1 entry per session for now and work to get this handled correctly, as will be easier to oversee things with a lower trade # overall
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Bertrand, I got it working.
                  Ive moved the session code to the top and did the logic like MaxTradesPerDay.

                  Posting the code below.

                  Thanks for your help and hints.

                  Code:
                    if (Bars.FirstBarOfSession )
                  			{
                    			  priorCumProfit = Performance.AllTrades.TradesPerformance.Currency.CumProfit;
                    			  tradeCounter = 0;
                  			}
                  			if (tradeCounter >= MaxTradesSession && MaxTradesSession != 0
                  				)
                                  {
                  				   return;
                             		}

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by mintos, 04-02-2024, 08:22 PM
                  4 responses
                  29 views
                  0 likes
                  Last Post mintos
                  by mintos
                   
                  Started by Felix Reichert, 04-26-2024, 02:12 PM
                  10 responses
                  69 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by PaulMohn, 04-24-2024, 03:49 AM
                  4 responses
                  36 views
                  0 likes
                  Last Post PaulMohn  
                  Started by lightsun47, Today, 11:37 AM
                  1 response
                  9 views
                  0 likes
                  Last Post NinjaTrader_Zachary  
                  Started by vitaly_p, Yesterday, 05:09 PM
                  4 responses
                  36 views
                  0 likes
                  Last Post vitaly_p  
                  Working...
                  X