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 MultiStepBreakeven : Strategy
- {
- private int StopLossMode;
- protected override void OnStateChange()
- {
- if (State == State.SetDefaults)
- {
- Description = @"Enter the description for your new custom Strategy here.";
- Name = "MultiStepBreakeven";
- Calculate = Calculate.OnEachTick;
- 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] < 1)
- return;
- //LONG ORDERS
- // Set 1
- //When no trade is live yet (Flat Position),
- // and the current day is either Monday or Tuesday,
- // and the current bar's High Price is greater tha the previous bar's High Price, and the previous bar's High Price is also greater than the High Price 2 bars ago,
- // and the current time is between 9:30 AM and 11:50 AM, then
- // Place a LONG LIMIT order of default lot size (DefaultQuantity)
- 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/When The Short position gets trigger (= Set 1 conditions get TRUE), then
- // ADD the INITIAL STOP to Breakeven + 10 ticks (= Position.AveragePrice (+ 10 ticks))
- if ((Position.MarketPosition == MarketPosition.Long)
- && (StopLossMode == 0))
- {
- ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (-10 * TickSize)) , @"INITIAL STOP", "");
- }
- // Set 3
- // If/When The Short position gets profitable by (Set 5 #/10) of ticks, then
- // MOVE the STOP to Breakeven (=Position.AveragePrice (*1/+ 0 ticks))
- if ((Position.MarketPosition == MarketPosition.Long)
- && (StopLossMode == 1))
- {
- ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), Position.AveragePrice, @"STOP MOVED FROM -10 TICKS (INITIAL STOP) TO BREAKEVEN", "");
- }
- // Set 4
- // If/When The Short position gets profitable by (Set 6 #/20) of ticks, then
- // MOVE the STOP to Breakeven + 10 ticks (=Position.AveragePrice (+ 10 ticks))
- if ((Position.MarketPosition == MarketPosition.Long)
- && (StopLossMode == 2))
- {
- ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (10 * TickSize)) , @"STOP MOVED FROM BREAKEVEN TO +10 TICKS PROFIT", "");
- }
- // Set 5
- // Once a/any Short Order is live (Set 2)
- //and the current/last price (Close[0]) gets 10 or more ticks in profit (Short entry - 10 ticks or more), then
- // ENABLE the Stop to be moved to Breakeven (StopLossMode = 1;) / but don't move it yet, just unlock it
- // Set 9 will move the Stop from -10 ticks to Breakeven
- if ((Position.MarketPosition == MarketPosition.Long)
- && (StopLossMode == 0)
- && (Close[0] >= (Position.AveragePrice + (10 * TickSize)) ))
- {
- StopLossMode = 1;
- }
- // Set 6
- // Once a/any Short Order has been MOVED to Breakeven (Set 3)
- //and the current/last price (Close[0]) gets 20 or more ticks in profit (Short entry - 20 ticks or more), then
- // ENABLE the Stop to be moved to Breakeven + 10 ticks (StopLossMode = 2;) / but don't move it yet, just unlock it
- // Set 10 will move the Stop from Breakeven to Breakeven + 10 ticks
- if ((Position.MarketPosition == MarketPosition.Long)
- && (StopLossMode == 1)
- && (Close[0] >= (Position.AveragePrice + (20 * TickSize)) ))
- {
- StopLossMode = 2;
- }
- //SHORT ORDERS
- // Set 7
- //When no trade is live yet (Flat Position),
- // and the current day is either Monday or Tuesday,
- // and the current bar's Low Price is lesser than the previous bar's Low Price, and the previous bar's Low Price is also lesser than the Low Price 2 bars ago,
- // and the current time is between 9:30 AM and 11:50 AM, then
- // Place a SHORT LIMIT order of default lot size (DefaultQuantity)
- 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/When The Short position gets trigger (= Set 7 conditions get TRUE), then
- // ADD the INITIAL STOP to Breakeven + 10 ticks (= Position.AveragePrice (+ 10 ticks))
- if ((Position.MarketPosition == MarketPosition.Short)
- && (StopLossMode == 0))
- {
- ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (10 * TickSize)) , @"INITIAL STOP", "");
- }
- // Set 9
- // If/When The Short position gets profitable by (Set 11 #/10) of ticks, then
- // MOVE the STOP to Breakeven (=Position.AveragePrice (*1/+ 0 ticks))
- if ((Position.MarketPosition == MarketPosition.Short)
- && (StopLossMode == 1))
- {
- ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), Position.AveragePrice, @"STOP MOVED FROM +10 TICKS (INITIAL STOP) TO BREAKEVEN", "");
- }
- // Set 10
- // If/When The Short position gets profitable by (Set 12 #/20) of ticks, then
- // MOVE the STOP to Breakeven + 10 ticks (=Position.AveragePrice (+ 10 ticks))
- if ((Position.MarketPosition == MarketPosition.Short)
- && (StopLossMode == 2))
- {
- ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (-10 * TickSize)) , @"STOP MOVED FROM BREAKEVEN TO +10 TICKS PROFIT", "");
- }
- // Set 11
- // Once a/any Short Order is live (Set 8)
- //and the current/last price (Close[0]) gets 10 or more ticks in profit (Short entry - 10 ticks or more), then
- // ENABLE the Stop to be moved to Breakeven (StopLossMode = 1;) / but don't move it yet, just unlock it
- // Set 9 will move the Stop from -10 ticks to Breakeven
- if ((Position.MarketPosition == MarketPosition.Short)
- && (StopLossMode == 0)
- && (Close[0] <= (Position.AveragePrice + (-10 * TickSize)) ))
- {
- StopLossMode = 1;
- }
- // Set 12
- // Once a/any Short Order has been MOVED to Breakeven (Set 9)
- //and the current/last price (Close[0]) gets 20 or more ticks in profit (Short entry - 20 ticks or more), then
- // ENABLE the Stop to be moved to Breakeven + 10 ticks (StopLossMode = 2;) / but don't move it yet, just unlock it
- // Set 10 will move the Stop from Breakeven to Breakeven + 10 ticks
- if ((Position.MarketPosition == MarketPosition.Short)
- && (StopLossMode == 1)
- && (Close[0] <= (Position.AveragePrice + (-20 * TickSize)) ))
- {
- StopLossMode = 2;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement