Advertisement
Guest User

MultiStepBreakevenStrategyBuilder.zip

a guest
Jul 30th, 2021
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.48 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.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] < 1)
  70.                 return;
  71.  
  72.              // Set 1
  73.             if ((Position.MarketPosition == MarketPosition.Flat)
  74.                  && (Close[0] > Open[0]))
  75.             {
  76.                 EnterLong(Convert.ToInt32(DefaultQuantity), "");
  77.                 StopLossMode = 0;
  78.             }
  79.            
  80.              // Set 2
  81.             if ((Position.MarketPosition == MarketPosition.Long)
  82.                  && (StopLossMode == 0))
  83.             {
  84.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (-10 * TickSize)) , "", "");
  85.             }
  86.            
  87.              // Set 3
  88.             if ((Position.MarketPosition == MarketPosition.Long)
  89.                  && (StopLossMode == 1))
  90.             {
  91.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), Position.AveragePrice, "", "");
  92.             }
  93.            
  94.              // Set 4
  95.             if ((Position.MarketPosition == MarketPosition.Long)
  96.                  && (StopLossMode == 2))
  97.             {
  98.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (10 * TickSize)) , "", "");
  99.             }
  100.            
  101.              // Set 5
  102.             if ((Position.MarketPosition == MarketPosition.Long)
  103.                  && (Close[0] >= (Position.AveragePrice + (10 * TickSize)) )
  104.                  && (StopLossMode == 0))
  105.             {
  106.                 StopLossMode = 1;
  107.             }
  108.            
  109.              // Set 6
  110.             if ((Position.MarketPosition == MarketPosition.Long)
  111.                  && (Close[0] >= (Position.AveragePrice + (20 * TickSize)) )
  112.                  && (StopLossMode == 1))
  113.             {
  114.                 StopLossMode = 2;
  115.             }
  116.            
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement