Advertisement
Guest User

Untitled

a guest
May 31st, 2021
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.00 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.  
  33.  
  34.         protected override void OnStateChange()
  35.         {
  36.             if (State == State.SetDefaults)
  37.             {
  38.                 Description                                 = @"Enter the description for your new custom Strategy here.";
  39.                 Name                                        = "MultiStepBreakeven";
  40.                 Calculate                                   = Calculate.OnEachTick;
  41.                 EntriesPerDirection                         = 1;
  42.                 EntryHandling                               = EntryHandling.AllEntries;
  43.                 IsExitOnSessionCloseStrategy                = true;
  44.                 ExitOnSessionCloseSeconds                   = 30;
  45.                 IsFillLimitOnTouch                          = false;
  46.                 MaximumBarsLookBack                         = MaximumBarsLookBack.TwoHundredFiftySix;
  47.                 OrderFillResolution                         = OrderFillResolution.Standard;
  48.                 Slippage                                    = 0;
  49.                 StartBehavior                               = StartBehavior.WaitUntilFlat;
  50.                 TimeInForce                                 = TimeInForce.Gtc;
  51.                 TraceOrders                                 = false;
  52.                 RealtimeErrorHandling                       = RealtimeErrorHandling.StopCancelClose;
  53.                 StopTargetHandling                          = StopTargetHandling.PerEntryExecution;
  54.                 BarsRequiredToTrade                         = 20;
  55.                 // Disable this property for performance gains in Strategy Analyzer optimizations
  56.                 // See the Help Guide for additional information
  57.                 IsInstantiatedOnEachOptimizationIteration   = true;
  58.                 StopLossModeLong                            = 0;
  59.                 StopLossModeShort                           = 0;
  60.             }
  61.             else if (State == State.Configure)
  62.             {
  63.             }
  64.         }
  65.  
  66.         protected override void OnBarUpdate()
  67.         {
  68.             if (BarsInProgress != 0)
  69.                 return;
  70.  
  71.              // Set 1
  72.             if ((Position.MarketPosition == MarketPosition.Flat)
  73.                  && (Close[0] > Open[0]))
  74.             {
  75.                 EnterLongLimit(Convert.ToInt32(DefaultQuantity), GetCurrentBid(), "");
  76.                 StopLossModeLong = 0;
  77.             }
  78.            
  79.             if (CurrentBars[0] < 1)
  80.                 return;
  81.                        
  82.        
  83.              // Set 2
  84.             if ((Position.MarketPosition == MarketPosition.Long)
  85.                  && (StopLossModeLong == 0))
  86.             {
  87.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (-10 * TickSize)) , "", "");
  88.             }
  89.            
  90.              // Set 3
  91.             if ((Position.MarketPosition == MarketPosition.Long)
  92.                  && (Close[0] >= (Position.AveragePrice + (10 * TickSize)) )
  93.                  && (StopLossModeLong == 0))
  94.             {
  95.                 StopLossModeLong = 1;
  96.             }
  97.            
  98.              // Set 4
  99.             if ((Position.MarketPosition == MarketPosition.Long)
  100.                  && (StopLossModeLong == 1))
  101.             {
  102.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), Position.AveragePrice, "", "");
  103.             }
  104.            
  105.              // Set 5
  106.             if ((Position.MarketPosition == MarketPosition.Long)
  107.                  && (Close[0] >= (Position.AveragePrice + (20 * TickSize)) )
  108.                  && (StopLossModeLong == 1))
  109.             {
  110.                 StopLossModeLong = 2;
  111.             }
  112.                        
  113.              // Set 6
  114.             if ((Position.MarketPosition == MarketPosition.Long)
  115.                  && (StopLossModeLong == 2))
  116.             {
  117.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (10 * TickSize)) , "", "");
  118.             }
  119.            
  120.  
  121.              // Set 7
  122.             if ((Position.MarketPosition == MarketPosition.Flat)
  123.                  && (Close[0] < Open[0]))
  124.             {
  125.                 EnterShortLimit(Convert.ToInt32(DefaultQuantity), GetCurrentAsk(), "");
  126.                 StopLossModeShort = 0;
  127.             }
  128.            
  129.             if (CurrentBars[0] < 1)
  130.                 return;
  131.                        
  132.        
  133.              // Set 8
  134.             if ((Position.MarketPosition == MarketPosition.Short)
  135.                  && (StopLossModeShort == 0))
  136.             {
  137.                 ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (10 * TickSize)) , "", "");
  138.             }
  139.            
  140.              // Set 9
  141.             if ((Position.MarketPosition == MarketPosition.Short)
  142.                  && (Close[0] <= (Position.AveragePrice + (-10 * TickSize)) )
  143.                  && (StopLossModeShort == 0))
  144.             {
  145.                 StopLossModeShort = 1;
  146.             }
  147.            
  148.              // Set 10
  149.             if ((Position.MarketPosition == MarketPosition.Short)
  150.                  && (StopLossModeShort == 1))
  151.             {
  152.                 ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), Position.AveragePrice, "", "");
  153.             }
  154.            
  155.              // Set 11
  156.             if ((Position.MarketPosition == MarketPosition.Short)
  157.                  && (Close[0] <= (Position.AveragePrice + (-20 * TickSize)) )
  158.                  && (StopLossModeShort == 1))
  159.             {
  160.                 StopLossModeShort = 2;
  161.             }
  162.                        
  163.              // Set 12
  164.             if ((Position.MarketPosition == MarketPosition.Short)
  165.                  && (StopLossModeShort == 2))
  166.             {
  167.                 ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (-10 * TickSize)) , "", "");
  168.             }
  169.            
  170.         }
  171.     }
  172. }
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement