Advertisement
Guest User

Untitled

a guest
Jun 6th, 2021
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.30 KB | None | 0 0
  1. #region Using declarations
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.ComponentModel.DataAnnotations;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Xml.Serialization;
  13. using NinjaTrader.Cbi;
  14. using NinjaTrader.Gui;
  15. using NinjaTrader.Gui.Chart;
  16. using NinjaTrader.Gui.SuperDom;
  17. using NinjaTrader.Gui.Tools;
  18. using NinjaTrader.Data;
  19. using NinjaTrader.NinjaScript;
  20. using NinjaTrader.Core.FloatingPoint;
  21. using NinjaTrader.NinjaScript.Indicators;
  22. using NinjaTrader.NinjaScript.DrawingTools;
  23. #endregion
  24.  
  25. //This namespace holds Strategies in this folder and is required. Do not change it.
  26. namespace NinjaTrader.NinjaScript.Strategies
  27. {
  28.     public class MultiStepBreakeven : Strategy
  29.     {
  30.         private int StopLossModeLong;
  31.         private int StopLossModeShort;
  32.         private int tradeCounter    = 0;  // This variable represents the number of trades taken per day.
  33.         private int maxTrades       = 4;  // This variable sets the maximum number of trades to take per day.
  34.  
  35.  
  36.         protected override void OnStateChange()
  37.         {
  38.             if (State == State.SetDefaults)
  39.             {
  40.                 Description                                 = @"Enter the description for your new custom Strategy here.";
  41.                 Name                                        = "MultiStepBreakeven";
  42.                 Calculate                                   = Calculate.OnEachTick;
  43.                 EntriesPerDirection                         = 1;
  44.                 EntryHandling                               = EntryHandling.AllEntries;
  45.                 IsExitOnSessionCloseStrategy                = true;
  46.                 ExitOnSessionCloseSeconds                   = 30;
  47.                 IsFillLimitOnTouch                          = false;
  48.                 MaximumBarsLookBack                         = MaximumBarsLookBack.TwoHundredFiftySix;
  49.                 OrderFillResolution                         = OrderFillResolution.Standard;
  50.                 Slippage                                    = 0;
  51.                 StartBehavior                               = StartBehavior.WaitUntilFlat;
  52.                 TimeInForce                                 = TimeInForce.Gtc;
  53.                 TraceOrders                                 = false;
  54.                 RealtimeErrorHandling                       = RealtimeErrorHandling.StopCancelClose;
  55.                 StopTargetHandling                          = StopTargetHandling.PerEntryExecution;
  56.                 BarsRequiredToTrade                         = 20;
  57.                 // Disable this property for performance gains in Strategy Analyzer optimizations
  58.                 // See the Help Guide for additional information
  59.                 IsInstantiatedOnEachOptimizationIteration   = true;
  60.                 StopLossModeLong                    = 0;
  61.                 StopLossModeShort                   = 0;
  62.                 MaxTrades                           = 4;
  63.             }
  64.             else if (State == State.Configure)
  65.             {
  66.             }
  67.         }
  68.  
  69.         protected override void OnBarUpdate()
  70.         {
  71.             if (BarsInProgress != 0)
  72.                 return;
  73.  
  74.             if (CurrentBars[0] < 1)
  75.                 return;
  76.            
  77.             if (CurrentBar < 1)
  78.                 return;
  79.            
  80.             // Reset the tradeCounter value at the first tick of the first bar of each session.
  81.             if (Bars.IsFirstBarOfSession && IsFirstTickOfBar)
  82.             {
  83.                 Print("resetting tradeCounter");
  84.                 tradeCounter = 0;  
  85.             }
  86.  
  87.  
  88.              
  89.             // If the amount of trades is less than the permitted value and the position is flat, go on to the next set of conditions.
  90.             if (tradeCounter < MaxTrades && Position.MarketPosition == MarketPosition.Flat)
  91.             {
  92.                  // Set 1
  93.                 /* If today is Monday or Tuesday from 9:30 to 11:50AM, and the current candle's High is greater than the previous,
  94.                 enter long market and increase the trade count by 1.
  95.                 In C#, ++ means increment by one. An equilivent would be tradeCounter = tradeCounter + 1; */
  96.                 if ((Time[0].DayOfWeek >= DayOfWeek.Monday && Time[0].DayOfWeek <= DayOfWeek.Tuesday)
  97.                      && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0))
  98.                      && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
  99.                      && High[0] > High[1])
  100.                 {
  101.                     tradeCounter++;
  102.                     EnterLong();
  103.                     StopLossModeLong = 0;              
  104.                 }
  105.                      
  106.                    
  107.                  // Set 2
  108.                 if ((Position.MarketPosition == MarketPosition.Long)
  109.                      && (StopLossModeLong == 0))
  110.                 {
  111.                     ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (-10 * TickSize)) , @"LONG STOP", "");
  112.                 }
  113.      
  114.      
  115.                  // Set 3
  116.                 if ((Position.MarketPosition == MarketPosition.Long)
  117.                      && (StopLossModeLong == 0)
  118.                      && (Close[0] >= (Position.AveragePrice + (10 * TickSize)) ))
  119.                 {
  120.                     StopLossModeLong = 1;
  121.                 }
  122.      
  123.                  // Set 4
  124.                 if ((Position.MarketPosition == MarketPosition.Long)
  125.                      && (StopLossModeLong == 1))
  126.                 {
  127.                     ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), Position.AveragePrice, @"LONG BE", "");
  128.                 }
  129.      
  130.      
  131.                  // Set 5
  132.                 if ((Position.MarketPosition == MarketPosition.Long)
  133.                      && (StopLossModeLong == 1)
  134.                      && (Close[0] >= (Position.AveragePrice + (20 * TickSize)) ))
  135.                 {
  136.                     StopLossModeLong = 2;
  137.                 }
  138.      
  139.                  // Set 6
  140.                 if ((Position.MarketPosition == MarketPosition.Long)
  141.                      && (StopLossModeLong == 2))
  142.                 {
  143.                     ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (10 * TickSize)) , @"+10 TICKS PROFIT", "");
  144.                 }
  145.             }
  146.            
  147.         }
  148.  
  149.         #region Properties
  150.         [Display(GroupName="Parameters", Description="Maximum number of trades to take per day.")]
  151.         public int MaxTrades
  152.         {
  153.             get { return maxTrades; }
  154.             set { maxTrades = Math.Max(1, value); }
  155.         }
  156.         #endregion
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement