Flatten positions if OCO rejected

This is a Ninjatrader scripting strategy to flatten all positions if any stop or target is rejected.
 avatar
TrendIsYourFriend
csharp
2 years ago
3.7 kB
13
No Index
Never
#region Using declarations
//-
	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.ComponentModel.DataAnnotations;
	using System.Linq;
	using System.Text;
	using System.Threading.Tasks;
	using System.Windows;
	using System.Windows.Input;
	using System.Windows.Media;
	using System.Xml.Serialization;
	using NinjaTrader.Cbi;
	using NinjaTrader.Gui;
	using NinjaTrader.Gui.Chart;
	using NinjaTrader.Gui.SuperDom;
	using NinjaTrader.Data;
	using NinjaTrader.NinjaScript;
	using NinjaTrader.Core.FloatingPoint;
	using NinjaTrader.NinjaScript.Indicators;
	using NinjaTrader.NinjaScript.DrawingTools;
	//-
#endregion

//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies.TrendIsYourFriend
{
	public class flattenAccountIfOCORejected : Strategy
	{
		
		#region PROPERTIES
		//-
		[NinjaScriptProperty]
		[Display(Name = "Tag", Description = "Label given to an instance of this strategy for the purpose of identification.\n Helpful when the 'Parameters' column is displayed in the 'Control Center/Strategies' window.", Order = 0)]
		public string tag
		{ get; set; }
		//-
		#endregion

		#region CLASS_INSTANCE_VARIABLES
		//-
		private Account		myAccount				= null;
		private string 		myInstrument 			= String.Empty;
		private bool		stopOrTargetRejected;
		//-
		#endregion

		protected override void OnStateChange()
		{
			#region ON_STATE_CHANGE
			//-
			if (State == State.SetDefaults)
			{
				PrintTo = PrintTo.OutputTab2;
				ClearOutputWindow();
				PrintTo = PrintTo.OutputTab1;
				ClearOutputWindow();
				
				Description	= "This strategy flattens all positions if any stop or target is rejected.";
				Name		= "flattenAccountIfOCORejected";
				Calculate	= Calculate.OnEachTick;
				// This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
				// See the Help Guide for additional information
				IsInstantiatedOnEachOptimizationIteration = false;
				
				tag = "chart 1";
								
			}
			else if (State == State.Transition)
			{
				stopOrTargetRejected = false;
				myInstrument = Instrument.FullName;
				lock (Account.All)
				{
					 // Get the real world or simulated account configured for the strategy
              		myAccount = Account.All.FirstOrDefault(a => a.Name == Account.Name);
				}
				// Subscribe to account order updates
				if (myAccount != null)
				{
//					Print( String.Format("myAccount: {0}, myInstrument: {1}", myAccount.Name, myInstrument) );
					myAccount.OrderUpdate += OnOrderUpdate;
				}
				else
					SetState(State.Terminated);
			}
			else if (State == State.Terminated)
			{
				if (myAccount != null)
				{
					myAccount.OrderUpdate -= OnOrderUpdate;
					myAccount = null;
				}
				myInstrument = String.Empty;
			}
			//-
			#endregion
		} // OnStateChange()

		protected override void OnBarUpdate()
		{
			if(State != State.Realtime)
				return;
			
			if (stopOrTargetRejected)
			{
//				Print("Flattening instrument: " + myInstrument);
				myAccount.Flatten(new [] { Instrument.GetInstrument( myInstrument ) });
				stopOrTargetRejected = false;
			}
		}

		private void OnOrderUpdate(object sender, OrderEventArgs e)
		{
//			Print( String.Format("{0} {1}", tag, e.Order.Instrument.FullName) );
			if (e.Order.Name.ToUpper().Contains("STOP") || e.Order.Name.ToUpper().Contains("TARGET"))
				if (e.OrderState == OrderState.Rejected)
				{
					if (e.Order.Instrument.FullName == myInstrument)
						stopOrTargetRejected = true;
				}
		}

	}
}