Guest User

MultiStepBreakEvenLongTest

a guest
Aug 2nd, 2021
46
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, PositionAccount.AveragePrice - 30*TickSize, "", @"Exit Long FULL STOP" );
  109.             }
  110.  
  111.             Print("ChangeOrder P " + initialExitOrderLong);
  112.            
  113.  
  114.             Print("ChangeOrder Q " + initialExitOrderLong);
  115.                 // Set 4
  116.             if ((PositionAccount.MarketPosition == MarketPosition.Long)
  117.                 && (initialExitOrderLong != null)
  118.                 && (StopLossModeLong == 0)
  119.                 && (Close[0] >= (PositionAccount.AveragePrice + (3 * TickSize / 2)) ))
  120.             {
  121.                 StopLossModeLong = 1;
  122.                 Print("ChangeOrder Trig. Pri. Mode " + StopLossModeLong);
  123.                 Print("ChangeOrder Trigger Price Move " + initialExitOrderLong);
  124.             }
  125.             Print("ChangeOrder R " + initialExitOrderLong);
  126.            
  127.              // Set 3
  128.             if ((PositionAccount.MarketPosition == MarketPosition.Long)
  129.                 && (initialExitOrderLong != null)
  130.                 && (StopLossModeLong == 1))
  131.             {
  132.                 ChangeOrder(initialExitOrderLong, initialExitOrderLong.Quantity, 0, PositionAccount.AveragePrice + 1*TickSize);
  133.                 Print("ChangeOrder SLMode " + StopLossModeLong);
  134.                 Print("ChangeOrder values " + initialExitOrderLong);
  135.                
  136.             }
  137.            
  138.         }
  139.        
  140.         protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
  141.         {
  142. //          if (order.Name == "EntryOrder")
  143. //          {
  144. //              entryOrder = order;
  145. //          }
  146.             if (order.Name == "Exit Long FULL STOP")
  147.             {
  148.                 initialExitOrderLong = order;
  149.                
  150.             }
  151.  
  152. //          if (entryOrder != null && entryOrder == order)
  153. //          {
  154.  
  155. //              if (entryOrder.OrderState == OrderState.Cancelled || entryOrder.OrderState == OrderState.Filled)
  156. //              {
  157. //                  entryOrder = null;
  158. //              }
  159. //          }
  160.            
  161.             if (initialExitOrderLong != null && initialExitOrderLong == order)
  162.             {
  163.  
  164.                 if (initialExitOrderLong.OrderState == OrderState.Cancelled || initialExitOrderLong.OrderState == OrderState.Filled)
  165.                 {
  166.                     initialExitOrderLong = null;
  167.                 }
  168.             }
  169.         }
  170.     }
  171. }
  172.  
RAW Paste Data