Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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.Gui.Tools;
- 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
- {
- public class MultiStepTrailStop : Strategy
- {
- private int StopLossMode;
- protected override void OnStateChange()
- {
- if (State == State.SetDefaults)
- {
- Description = @"Enter the description for your new custom Strategy here.";
- Name = "MultiStepTrailStop";
- Calculate = Calculate.OnBarClose;
- EntriesPerDirection = 1;
- EntryHandling = EntryHandling.AllEntries;
- IsExitOnSessionCloseStrategy = true;
- ExitOnSessionCloseSeconds = 30;
- IsFillLimitOnTouch = false;
- MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
- OrderFillResolution = OrderFillResolution.Standard;
- Slippage = 0;
- StartBehavior = StartBehavior.WaitUntilFlat;
- TimeInForce = TimeInForce.Gtc;
- TraceOrders = false;
- RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
- StopTargetHandling = StopTargetHandling.PerEntryExecution;
- BarsRequiredToTrade = 20;
- // Disable this property for performance gains in Strategy Analyzer optimizations
- // See the Help Guide for additional information
- IsInstantiatedOnEachOptimizationIteration = true;
- StopLossMode = 0;
- }
- else if (State == State.Configure)
- {
- }
- }
- protected override void OnBarUpdate()
- {
- if (BarsInProgress != 0)
- return;
- if (CurrentBars[0] < 2)
- return;
- //LONG ORDERS
- // Set 1
- if ((Position.MarketPosition == MarketPosition.Flat)
- && ((Time[0].DayOfWeek == DayOfWeek.Monday)
- || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
- && (High[0] > High[1])
- && (High[1] > High[2])
- && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0))
- && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0)))
- {
- EnterLongLimit(Convert.ToInt32(DefaultQuantity), 0, "");
- StopLossMode = 0;
- }
- // Set 2
- if ((Position.MarketPosition == MarketPosition.Long)
- && ((Time[0].DayOfWeek == DayOfWeek.Monday)
- || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
- && (StopLossMode == 0)
- && (High[1] > High[2])
- && (High[0] > High[1])
- && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
- && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
- {
- ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (-10 * TickSize)) , "", "");
- }
- // Set 3
- if ((Position.MarketPosition == MarketPosition.Long)
- && ((Time[0].DayOfWeek == DayOfWeek.Monday)
- || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
- && (StopLossMode == 1)
- && (High[1] > High[2])
- && (High[0] > High[1])
- && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
- && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
- {
- ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), Position.AveragePrice, "", "");
- }
- // Set 4
- if ((Position.MarketPosition == MarketPosition.Long)
- && ((Time[0].DayOfWeek == DayOfWeek.Monday)
- || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
- && (StopLossMode == 2)
- && (High[1] > High[2])
- && (High[0] > High[1])
- && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
- && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
- {
- ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (10 * TickSize)) , "", "");
- }
- // Set 5
- if ((Position.MarketPosition == MarketPosition.Long)
- && ((Time[0].DayOfWeek == DayOfWeek.Monday)
- || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
- && (StopLossMode == 0)
- && (Close[0] >= (Position.AveragePrice + (10 * TickSize)) )
- && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
- && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
- {
- StopLossMode = 1;
- }
- // Set 6
- if ((Position.MarketPosition == MarketPosition.Long)
- && ((Time[0].DayOfWeek == DayOfWeek.Monday)
- || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
- && (StopLossMode == 1)
- && (Close[0] >= (Position.AveragePrice + (20 * TickSize)) )
- && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
- && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
- {
- StopLossMode = 2;
- }
- //SHORT ORDERS
- // Set 7
- if ((Position.MarketPosition == MarketPosition.Flat)
- && ((Time[0].DayOfWeek == DayOfWeek.Monday)
- || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
- && (Low[0] < Low[1])
- && (Low[1] < Low[2])
- && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0))
- && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0)))
- {
- EnterShortLimit(Convert.ToInt32(DefaultQuantity), 0, "");
- StopLossMode = 0;
- }
- // Set 8
- if ((Position.MarketPosition == MarketPosition.Short)
- && ((Time[0].DayOfWeek == DayOfWeek.Monday)
- || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
- && (StopLossMode == 0)
- && (Low[0] < Low[1])
- && (Low[1] < Low[2])
- && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
- && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
- {
- ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (10 * TickSize)) , "", "");
- }
- // Set 9
- if ((Position.MarketPosition == MarketPosition.Short)
- && ((Time[0].DayOfWeek == DayOfWeek.Monday)
- || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
- && (StopLossMode == 1)
- && (Low[0] < Low[1])
- && (Low[1] < Low[2])
- && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
- && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
- {
- ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), Position.AveragePrice, "", "");
- }
- // Set 10
- if ((Position.MarketPosition == MarketPosition.Short)
- && ((Time[0].DayOfWeek == DayOfWeek.Monday)
- || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
- && (StopLossMode == 2)
- && (Low[0] < Low[1])
- && (Low[1] < Low[2])
- && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
- && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
- {
- ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (-10 * TickSize)) , "", "");
- }
- // Set 11
- if ((Position.MarketPosition == MarketPosition.Short)
- && ((Time[0].DayOfWeek == DayOfWeek.Monday)
- || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
- && (StopLossMode == 0)
- && (Close[0] >= (Position.AveragePrice + (-10 * TickSize)) )
- && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
- && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
- {
- StopLossMode = 1;
- }
- // Set 12
- if ((Position.MarketPosition == MarketPosition.Short)
- && ((Time[0].DayOfWeek == DayOfWeek.Monday)
- || (Time[0].DayOfWeek == DayOfWeek.Tuesday))
- && (StopLossMode == 1)
- && (Close[0] >= (Position.AveragePrice + (-20 * TickSize)) )
- && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
- && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0)))
- {
- StopLossMode = 2;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement