Advertisement
Guest User

Untitled

a guest
Jun 6th, 2021
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1. //
  2. // Copyright (C) 2015, NinjaTrader LLC <www.ninjatrader.com>.
  3. // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
  4. //
  5. #region Using declarations
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.ComponentModel.DataAnnotations;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Xml.Serialization;
  17. using NinjaTrader.Cbi;
  18. using NinjaTrader.Gui;
  19. using NinjaTrader.Gui.Chart;
  20. using NinjaTrader.Gui.SuperDom;
  21. using NinjaTrader.Data;
  22. using NinjaTrader.NinjaScript;
  23. using NinjaTrader.Core.FloatingPoint;
  24. using NinjaTrader.NinjaScript.DrawingTools;
  25. #endregion
  26.  
  27. // This namespace holds all strategies and is required. Do not change it.
  28. namespace NinjaTrader.NinjaScript.Strategies
  29. {
  30.     public class SampleTradeLimiter : Strategy
  31.     {
  32.         private int aDXPeriod       = 14; // Default setting for ADXPeriod
  33.         private int tradeCounter    = 0;  // This variable represents the number of trades taken per day.
  34.         private int maxTrades       = 5;  // This variable sets the maximum number of trades to take per day.
  35.  
  36.         protected override void OnStateChange()
  37.         {
  38.             if(State == State.SetDefaults)
  39.             {
  40.                 ADXPeriod   = 14;
  41.                 MaxTrades   = 5;
  42.                 Calculate   = Calculate.OnBarClose;
  43.                 Name        = "Sample Trade Limiter";
  44.             }
  45.             else if(State == State.Configure)
  46.             {
  47.                 // Add the current day open, high, low indicator to visually see entry conditions.
  48.                 AddChartIndicator(CurrentDayOHL());
  49.             }
  50.         }
  51.  
  52.         protected override void OnBarUpdate()
  53.         {
  54.             // Make sure there are enough bars.
  55.             if (CurrentBar < 1)
  56.                 return;
  57.            
  58.             // Reset the tradeCounter value at the first tick of the first bar of each session.
  59.             if (Bars.IsFirstBarOfSession && IsFirstTickOfBar)
  60.             {
  61.                 Print("resetting tradeCounter");
  62.                 tradeCounter = 0;  
  63.             }
  64.            
  65.             // If the amount of trades is less than the permitted value and the position is flat, go on to the next set of conditions.
  66.             if (tradeCounter < MaxTrades && Position.MarketPosition == MarketPosition.Flat)
  67.             {
  68.                 /* If a new low is made, enter short and increase the trade count by 1.
  69.                 In C#, ++ means increment by one. An equilivent would be tradeCounter = tradeCounter + 1; */
  70.                 if (CurrentDayOHL().CurrentLow[0] < CurrentDayOHL().CurrentLow[1])
  71.                 {
  72.                     tradeCounter++;
  73.                     EnterShort();
  74.                 }
  75.                 // If a new high is made, enter long and increase the trade count by 1.
  76.                 else if (CurrentDayOHL().CurrentHigh[0] > CurrentDayOHL().CurrentHigh[1])
  77.                 {
  78.                     tradeCounter++;
  79.                     EnterLong();
  80.                 }
  81.             }
  82.            
  83.             /* Exit a position if "the trend has ended" as indicated by ADX.
  84.             If the current ADX value is less than the previous ADX value, the trend strength is weakening. */
  85.             if (ADX(ADXPeriod)[0] < ADX(ADXPeriod)[1] && Position.MarketPosition != MarketPosition.Flat)
  86.             {
  87.                 if (Position.MarketPosition == MarketPosition.Long)
  88.                     ExitLong();
  89.                 else if (Position.MarketPosition == MarketPosition.Short)
  90.                     ExitShort();
  91.             }
  92.         }
  93.  
  94.         #region Properties
  95.         [Display(GroupName="Parameters", Description="Period for the ADX indicator")]
  96.         public int ADXPeriod
  97.         {
  98.             get { return aDXPeriod; }
  99.             set { aDXPeriod = Math.Max(1, value); }
  100.         }
  101.  
  102.         [Display(GroupName="Parameters", Description="Maximum number of trades to take per day.")]
  103.         public int MaxTrades
  104.         {
  105.             get { return maxTrades; }
  106.             set { maxTrades = Math.Max(1, value); }
  107.         }
  108.         #endregion
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement