Guest User

MultiStepBreakEvenLongTest

a guest
Jul 29th, 2021
60
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 MultiStepBreakEvenLongTest : Strategy
  29.     {
  30.         private int StopLossModeLong;
  31.         private int StopLossModeShort;
  32.  
  33.         private Order entryOrder = null;
  34.         private Order initialExitOrderLong = null;
  35.         private string oco;
  36.         //private Order firstMoveExitOrderLong = null;
  37.  
  38.         // private Order initialExitOrderShort = null;
  39.         // private Order firstMoveExitOrderShort = null;
  40.         protected override void OnStateChange()
  41.         {
  42.             if (State == State.SetDefaults)
  43.             {
  44.                 Description = @"Enter the description for your new custom Strategy here.";
  45.                 Name = "MultiStepBreakEvenLongTest";
  46.                 Calculate = Calculate.OnEachTick;
  47.                 EntriesPerDirection = 1;
  48.                 EntryHandling = EntryHandling.AllEntries;
  49.                 IsExitOnSessionCloseStrategy = true;
  50.                 ExitOnSessionCloseSeconds = 30;
  51.                 IsFillLimitOnTouch = false;
  52.                 MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
  53.                 OrderFillResolution = OrderFillResolution.Standard;
  54.                 Slippage = 0;
  55.                 StartBehavior = StartBehavior.WaitUntilFlat;
  56.                 TimeInForce = TimeInForce.Gtc;
  57.                 TraceOrders = false;
  58.                 RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
  59.                 StopTargetHandling = StopTargetHandling.PerEntryExecution;
  60.                 BarsRequiredToTrade = 20;
  61.                 // Disable this property for performance gains in Strategy Analyzer optimizations
  62.                 // See the Help Guide for additional information
  63.                 IsInstantiatedOnEachOptimizationIteration = true;
  64.                 IsAdoptAccountPositionAware = true;
  65.                 StartBehavior = StartBehavior.AdoptAccountPosition;
  66.                 IsUnmanaged = true;
  67.                 StopLossModeLong = 0;
  68.                 // StopLossModeShort = 0;
  69.             }
  70.             else if (State == State.Configure)
  71.             {
  72.             }
  73.             else if (State == State.Realtime)
  74.             {
  75.                 if (initialExitOrderLong != null)
  76.                     initialExitOrderLong = GetRealtimeOrder(initialExitOrderLong);
  77.                 // if (initialExitOrderShort != null)
  78.                 // initialExitOrderShort = GetRealtimeOrder(initialExitOrderShort);
  79.             }
  80.         }
  81.  
  82.         protected override void OnBarUpdate()
  83.         {
  84.             if (CurrentBar < BarsRequiredToTrade)
  85.                 return;
  86.             if (State == State.Historical)
  87.                 return;
  88.  
  89.             if (BarsInProgress != 0)
  90.                 return;
  91.             if (Position.MarketPosition == MarketPosition.Flat && entryOrder == null)
  92.             {
  93.                 oco = GetAtmStrategyUniqueId() + "entry";
  94.                
  95.                 SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Market, 1, 0, 0,  oco, "EntryOrder");
  96.             }
  97.  
  98.             if (PositionAccount.MarketPosition == MarketPosition.Long)
  99.             {
  100.                 StopLossModeLong = 0;
  101.             }
  102.  
  103.  
  104.             if ((PositionAccount.MarketPosition == MarketPosition.Long)
  105.                 && (initialExitOrderLong == null)
  106.                 && (StopLossModeLong == 0))
  107.             {
  108.                 initialExitOrderLong = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, 1, 0, Position.AveragePrice - 30*TickSize, "", @"Exit Long FULL STOP" );
  109.             }
  110.  
  111.  
  112.                 // Set 3
  113.             if ((PositionAccount.MarketPosition == MarketPosition.Long)
  114.                 && (initialExitOrderLong != null)
  115.                 && (StopLossModeLong == 1))
  116.             {
  117.                 ChangeOrder(initialExitOrderLong, initialExitOrderLong.Quantity, 0, Position.AveragePrice + 10*TickSize);
  118.             }
  119.                 // Set 4
  120.             if ((PositionAccount.MarketPosition == MarketPosition.Long)
  121.                 && (initialExitOrderLong != null)
  122.                 && (StopLossModeLong == 0)
  123.                 && (Close[0] >= (Position.AveragePrice + (5 * TickSize / 2)) ))
  124.             {
  125.                 StopLossModeLong = 1;
  126.             }
  127.            
  128.            
  129.         }
  130.        
  131.         protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
  132.         {
  133.             if (order.Name == "EntryOrder")
  134.             {
  135.                 entryOrder = order;
  136.             }
  137.             if (order.Name == "Exit Long FULL STOP")
  138.             {
  139.                 initialExitOrderLong = order;
  140.             }
  141.  
  142.             if (entryOrder != null && entryOrder == order)
  143.             {
  144.  
  145.                 if (entryOrder.OrderState == OrderState.Cancelled || entryOrder.OrderState == OrderState.Filled)
  146.                 {
  147.                     entryOrder = null;
  148.                 }
  149.             }
  150.            
  151.             if (initialExitOrderLong != null && initialExitOrderLong == order)
  152.             {
  153.  
  154.                 if (initialExitOrderLong.OrderState == OrderState.Cancelled || initialExitOrderLong.OrderState == OrderState.Filled)
  155.                 {
  156.                     initialExitOrderLong = null;
  157.                 }
  158.             }
  159.         }
  160.     }
  161. }
  162.  
RAW Paste Data