Advertisement
Guest User

Untitled

a guest
May 26th, 2021
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.76 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 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                                        = "MultiStepBreakeven";
  39.                 Calculate                                   = Calculate.OnEachTick;
  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.             }
  60.             else if (State == State.Configure)
  61.             {
  62.             }
  63.         }
  64.  
  65.         protected override void OnBarUpdate()
  66.         {
  67.             if (BarsInProgress != 0)
  68.                 return;
  69.  
  70.             if (CurrentBars[0] < 1)
  71.                 return;
  72.  
  73.            
  74.              //LONG ORDERS
  75.              // Set 1
  76.                 //When no trade is live yet (Flat Position),
  77.                     // and the current day is either Monday or Tuesday,
  78.                         // and the current bar's High Price is greater tha the previous bar's High Price, and the previous bar's High Price is also greater than the High Price 2 bars ago,
  79.                             // and the current time is between 9:30 AM and 11:50 AM, then
  80.                                 // Place a LONG LIMIT order of default lot size (DefaultQuantity)
  81.             if ((Position.MarketPosition == MarketPosition.Flat)
  82.                  && ((Time[0].DayOfWeek == DayOfWeek.Monday)
  83.                  || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
  84.                  && (High[0] < High[1])
  85.                  && (High[1] < High[2])
  86.                  && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0))
  87.                  && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0)))
  88.             {
  89.                 EnterLongLimit(Convert.ToInt32(DefaultQuantity), 0, "");
  90.                 StopLossMode = 0;              
  91.             }
  92.            
  93.              // Set 2
  94.             // If/When The Short position gets trigger (= Set 1 conditions get TRUE), then
  95.                  // ADD the INITIAL STOP to Breakeven + 10 ticks (= Position.AveragePrice (+ 10 ticks))
  96.             if ((Position.MarketPosition == MarketPosition.Long)
  97.                  && (StopLossMode == 0))
  98.             {
  99.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (-10 * TickSize)) , @"INITIAL STOP", "");
  100.             }
  101.  
  102.              // Set 3
  103.              // If/When The Short position gets profitable by (Set 5 #/10) of ticks, then
  104.                   // MOVE the STOP to Breakeven (=Position.AveragePrice (*1/+ 0 ticks))
  105.             if ((Position.MarketPosition == MarketPosition.Long)
  106.                  && (StopLossMode == 1))
  107.             {
  108.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), Position.AveragePrice, @"STOP MOVED FROM -10 TICKS (INITIAL STOP) TO BREAKEVEN", "");
  109.             }
  110.  
  111.              // Set 4
  112.              // If/When The Short position gets profitable by (Set 6 #/20) of ticks, then
  113.                   // MOVE the STOP to Breakeven + 10 ticks (=Position.AveragePrice (+ 10 ticks))
  114.             if ((Position.MarketPosition == MarketPosition.Long)
  115.                  && (StopLossMode == 2))
  116.             {
  117.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (10 * TickSize)) , @"STOP MOVED FROM BREAKEVEN TO +10 TICKS PROFIT", "");
  118.             }
  119.  
  120.  
  121.              // Set 5
  122.              // Once a/any Short Order is live (Set 2)
  123.                    //and the current/last price (Close[0]) gets 10 or more ticks in profit (Short entry - 10 ticks or more), then
  124.                   // ENABLE the Stop to be moved to Breakeven (StopLossMode = 1;) / but don't move it yet, just unlock it
  125.                  // Set 9 will move the Stop from -10 ticks to Breakeven
  126.             if ((Position.MarketPosition == MarketPosition.Long)
  127.                  && (StopLossMode == 0)
  128.                  && (Close[0] >= (Position.AveragePrice + (10 * TickSize)) ))
  129.             {
  130.                 StopLossMode = 1;
  131.             }
  132.  
  133.  
  134.              // Set 6
  135.              // Once a/any Short Order has been MOVED to Breakeven (Set 3)
  136.                    //and the current/last price (Close[0]) gets 20 or more ticks in profit (Short entry - 20 ticks or more), then
  137.                   // ENABLE the Stop to be moved to Breakeven + 10 ticks (StopLossMode = 2;) / but don't move it yet, just unlock it
  138.                  // Set 10 will move the Stop from Breakeven to Breakeven + 10 ticks
  139.             if ((Position.MarketPosition == MarketPosition.Long)
  140.                  && (StopLossMode == 1)
  141.                  && (Close[0] >= (Position.AveragePrice + (20 * TickSize)) ))
  142.             {
  143.                 StopLossMode = 2;
  144.             }
  145.              
  146.            
  147.            
  148.             //SHORT ORDERS
  149.              // Set 7
  150.                 //When no trade is live yet (Flat Position),
  151.                     // and the current day is either Monday or Tuesday,
  152.                         // and the current bar's Low Price is lesser than the previous bar's Low Price, and the previous bar's Low Price is also lesser than the Low Price 2 bars ago,
  153.                             // and the current time is between 9:30 AM and 11:50 AM, then
  154.                                 // Place a SHORT LIMIT order of default lot size (DefaultQuantity)
  155.             if ((Position.MarketPosition == MarketPosition.Flat)
  156.                  && ((Time[0].DayOfWeek == DayOfWeek.Monday)
  157.                  || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
  158.                  && (Low[0] < Low[1])
  159.                  && (Low[1] < Low[2])
  160.                  && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0))
  161.                  && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0)))
  162.             {
  163.                 EnterShortLimit(Convert.ToInt32(DefaultQuantity), 0, "");
  164.                 StopLossMode = 0;              
  165.             }
  166.            
  167.              // Set 8
  168.             // If/When The Short position gets trigger (= Set 7 conditions get TRUE), then
  169.                  // ADD the INITIAL STOP to Breakeven + 10 ticks (= Position.AveragePrice (+ 10 ticks))
  170.             if ((Position.MarketPosition == MarketPosition.Short)
  171.                  && (StopLossMode == 0))
  172.             {
  173.                 ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (10 * TickSize)) , @"INITIAL STOP", "");
  174.             }
  175.  
  176.              // Set 9
  177.              // If/When The Short position gets profitable by (Set 11 #/10) of ticks, then
  178.                   // MOVE the STOP to Breakeven (=Position.AveragePrice (*1/+ 0 ticks))
  179.             if ((Position.MarketPosition == MarketPosition.Short)
  180.                  && (StopLossMode == 1))
  181.             {
  182.                 ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), Position.AveragePrice, @"STOP MOVED FROM +10 TICKS (INITIAL STOP) TO BREAKEVEN", "");
  183.             }
  184.  
  185.              // Set 10
  186.              // If/When The Short position gets profitable by (Set 12 #/20) of ticks, then
  187.                   // MOVE the STOP to Breakeven + 10 ticks (=Position.AveragePrice (+ 10 ticks))
  188.             if ((Position.MarketPosition == MarketPosition.Short)
  189.                  && (StopLossMode == 2))
  190.             {
  191.                 ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (-10 * TickSize)) , @"STOP MOVED FROM BREAKEVEN TO +10 TICKS PROFIT", "");
  192.             }
  193.  
  194.  
  195.              // Set 11
  196.              // Once a/any Short Order is live (Set 8)
  197.                    //and the current/last price (Close[0]) gets 10 or more ticks in profit (Short entry - 10 ticks or more), then
  198.                   // ENABLE the Stop to be moved to Breakeven (StopLossMode = 1;) / but don't move it yet, just unlock it
  199.                  // Set 9 will move the Stop from -10 ticks to Breakeven
  200.             if ((Position.MarketPosition == MarketPosition.Short)
  201.                  && (StopLossMode == 0)
  202.                  && (Close[0] <= (Position.AveragePrice + (-10 * TickSize)) ))
  203.             {
  204.                 StopLossMode = 1;
  205.             }
  206.  
  207.  
  208.              // Set 12
  209.              // Once a/any Short Order has been MOVED to Breakeven (Set 9)
  210.                    //and the current/last price (Close[0]) gets 20 or more ticks in profit (Short entry - 20 ticks or more), then
  211.                   // ENABLE the Stop to be moved to Breakeven + 10 ticks (StopLossMode = 2;) / but don't move it yet, just unlock it
  212.                  // Set 10 will move the Stop from Breakeven to Breakeven + 10 ticks
  213.             if ((Position.MarketPosition == MarketPosition.Short)
  214.                  && (StopLossMode == 1)
  215.                  && (Close[0] <= (Position.AveragePrice + (-20 * TickSize)) ))
  216.             {
  217.                 StopLossMode = 2;
  218.             }
  219.            
  220.         }
  221.     }
  222. }
  223.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement