Advertisement
Guest User

Untitled

a guest
May 25th, 2021
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.10 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 MultiStepTrailStop : Strategy
  29.     {
  30.         private int StopLossMode;
  31.  
  32.  
  33.         protected override void OnStateChange()
  34.         {
  35.             if (State == State.SetDefaults)
  36.             {
  37.                 Description                                 = @"Enter the description for your new custom Strategy here.";
  38.                 Name                                        = "MultiStepTrailStop";
  39.                 Calculate                                   = Calculate.OnBarClose;
  40.                 EntriesPerDirection                         = 1;
  41.                 EntryHandling                               = EntryHandling.AllEntries;
  42.                 IsExitOnSessionCloseStrategy                = true;
  43.                 ExitOnSessionCloseSeconds                   = 30;
  44.                 IsFillLimitOnTouch                          = false;
  45.                 MaximumBarsLookBack                         = MaximumBarsLookBack.TwoHundredFiftySix;
  46.                 OrderFillResolution                         = OrderFillResolution.Standard;
  47.                 Slippage                                    = 0;
  48.                 StartBehavior                               = StartBehavior.WaitUntilFlat;
  49.                 TimeInForce                                 = TimeInForce.Gtc;
  50.                 TraceOrders                                 = false;
  51.                 RealtimeErrorHandling                       = RealtimeErrorHandling.StopCancelClose;
  52.                 StopTargetHandling                          = StopTargetHandling.PerEntryExecution;
  53.                 BarsRequiredToTrade                         = 20;
  54.                 // Disable this property for performance gains in Strategy Analyzer optimizations
  55.                 // See the Help Guide for additional information
  56.                 IsInstantiatedOnEachOptimizationIteration   = true;
  57.                 StopLossMode                    = 0;
  58.             }
  59.             else if (State == State.Configure)
  60.             {
  61.             }
  62.         }
  63.  
  64.         protected override void OnBarUpdate()
  65.         {
  66.             if (BarsInProgress != 0)
  67.                 return;
  68.  
  69.             if (CurrentBars[0] < 2)
  70.                 return;
  71.  
  72.              //LONG ORDERS
  73.              // Set 1
  74.             if ((Position.MarketPosition == MarketPosition.Flat)
  75.                  && ((Time[0].DayOfWeek == DayOfWeek.Monday)
  76.                  || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
  77.                  && (High[0] > High[1])
  78.                  && (High[1] > High[2])
  79.                  && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0))
  80.                  && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0)))
  81.             {
  82.                 EnterLongLimit(Convert.ToInt32(DefaultQuantity), 0, "");
  83.                 StopLossMode = 0;              
  84.             }
  85.            
  86.              // Set 2
  87.             if ((Position.MarketPosition == MarketPosition.Long)
  88.                  && ((Time[0].DayOfWeek == DayOfWeek.Monday)
  89.                  || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
  90.                  && (StopLossMode == 0)
  91.                  && (High[1] > High[2])
  92.                  && (High[0] > High[1])
  93.                  && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
  94.                  && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
  95.             {
  96.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (-10 * TickSize)) , "", "");
  97.             }
  98.  
  99.              // Set 3
  100.             if ((Position.MarketPosition == MarketPosition.Long)
  101.                  && ((Time[0].DayOfWeek == DayOfWeek.Monday)
  102.                  || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
  103.                  && (StopLossMode == 1)
  104.                  && (High[1] > High[2])
  105.                  && (High[0] > High[1])
  106.                  && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
  107.                  && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
  108.             {
  109.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), Position.AveragePrice, "", "");
  110.             }
  111.  
  112.              // Set 4
  113.             if ((Position.MarketPosition == MarketPosition.Long)
  114.                  && ((Time[0].DayOfWeek == DayOfWeek.Monday)
  115.                  || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
  116.                  && (StopLossMode == 2)
  117.                  && (High[1] > High[2])
  118.                  && (High[0] > High[1])
  119.                  && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
  120.                  && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
  121.             {
  122.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (10 * TickSize)) , "", "");
  123.             }
  124.  
  125.  
  126.              // Set 5
  127.             if ((Position.MarketPosition == MarketPosition.Long)
  128.                  && ((Time[0].DayOfWeek == DayOfWeek.Monday)
  129.                  || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
  130.                  && (StopLossMode == 0)
  131.                  && (Close[0] >= (Position.AveragePrice + (10 * TickSize)) )
  132.                  && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
  133.                  && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
  134.             {
  135.                 StopLossMode = 1;
  136.             }
  137.  
  138.  
  139.              // Set 6
  140.             if ((Position.MarketPosition == MarketPosition.Long)
  141.                  && ((Time[0].DayOfWeek == DayOfWeek.Monday)
  142.                  || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
  143.                  && (StopLossMode == 1)
  144.                  && (Close[0] >= (Position.AveragePrice + (20 * TickSize)) )
  145.                  && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
  146.                  && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
  147.             {
  148.                 StopLossMode = 2;
  149.             }
  150.  
  151.  
  152.              //SHORT ORDERS
  153.              // Set 7
  154.             if ((Position.MarketPosition == MarketPosition.Flat)
  155.                  && ((Time[0].DayOfWeek == DayOfWeek.Monday)
  156.                  || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
  157.                  && (Low[0] < Low[1])
  158.                  && (Low[1] < Low[2])
  159.                  && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0))
  160.                  && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0)))
  161.             {
  162.                 EnterShortLimit(Convert.ToInt32(DefaultQuantity), 0, "");
  163.                 StopLossMode = 0;              
  164.             }
  165.            
  166.              // Set 8
  167.             if ((Position.MarketPosition == MarketPosition.Short)
  168.                  && ((Time[0].DayOfWeek == DayOfWeek.Monday)
  169.                  || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
  170.                  && (StopLossMode == 0)
  171.                  && (Low[0] < Low[1])
  172.                  && (Low[1] < Low[2])
  173.                  && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
  174.                  && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
  175.             {
  176.                 ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (10 * TickSize)) , "", "");
  177.             }
  178.  
  179.              // Set 9
  180.             if ((Position.MarketPosition == MarketPosition.Short)
  181.                  && ((Time[0].DayOfWeek == DayOfWeek.Monday)
  182.                  || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
  183.                  && (StopLossMode == 1)
  184.                  && (Low[0] < Low[1])
  185.                  && (Low[1] < Low[2])
  186.                  && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
  187.                  && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
  188.             {
  189.                 ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), Position.AveragePrice, "", "");
  190.             }
  191.  
  192.              // Set 10
  193.             if ((Position.MarketPosition == MarketPosition.Short)
  194.                  && ((Time[0].DayOfWeek == DayOfWeek.Monday)
  195.                  || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
  196.                  && (StopLossMode == 2)
  197.                  && (Low[0] < Low[1])
  198.                  && (Low[1] < Low[2])
  199.                  && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
  200.                  && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
  201.             {
  202.                 ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (-10 * TickSize)) , "", "");
  203.             }
  204.  
  205.  
  206.              // Set 11
  207.             if ((Position.MarketPosition == MarketPosition.Short)
  208.                  && ((Time[0].DayOfWeek == DayOfWeek.Monday)
  209.                  || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
  210.                  && (StopLossMode == 0)
  211.                  && (Close[0] >= (Position.AveragePrice + (-10 * TickSize)) )
  212.                  && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
  213.                  && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
  214.             {
  215.                 StopLossMode = 1;
  216.             }
  217.  
  218.  
  219.              // Set 12
  220.             if ((Position.MarketPosition == MarketPosition.Short)
  221.                  && ((Time[0].DayOfWeek == DayOfWeek.Monday)
  222.                  || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
  223.                  && (StopLossMode == 1)
  224.                  && (Close[0] >= (Position.AveragePrice + (-20 * TickSize)) )
  225.                  && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
  226.                  && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
  227.             {
  228.                 StopLossMode = 2;
  229.             }
  230.            
  231.         }
  232.     }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement