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 StopLossModeLong;
- private int StopLossModeShort;
- private int tradeCounter = 0; // This variable represents the number of trades taken per day.
- private int maxTrades = 4; // This variable sets the maximum number of trades to take per day.
- 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;
- StopLossModeLong = 0;
- StopLossModeShort = 0;
- MaxTrades = 4;
- }
- else if (State == State.Configure)
- {
- }
- }
- protected override void OnBarUpdate()
- {
- if (BarsInProgress != 0)
- return;
- if (CurrentBars[0] < 1)
- return;
- if (CurrentBar < 1)
- return;
- // Reset the tradeCounter value at the first tick of the first bar of each session.
- if (Bars.IsFirstBarOfSession && IsFirstTickOfBar)
- {
- Print("resetting tradeCounter");
- tradeCounter = 0;
- }
- // If the amount of trades is less than the permitted value and the position is flat, go on to the next set of conditions.
- if (tradeCounter < MaxTrades && Position.MarketPosition == MarketPosition.Flat)
- {
- // Set 1
- /* If today is Monday or Tuesday from 9:30 to 11:50AM, and the current candle's High is greater than the previous,
- enter long market and increase the trade count by 1.
- In C#, ++ means increment by one. An equilivent would be tradeCounter = tradeCounter + 1; */
- if ((Time[0].DayOfWeek >= DayOfWeek.Monday && Time[0].DayOfWeek <= DayOfWeek.Tuesday)
- && (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0))
- && (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0))
- && High[0] > High[1])
- {
- tradeCounter++;
- EnterLong();
- StopLossModeLong = 0;
- }
- // Set 2
- if ((Position.MarketPosition == MarketPosition.Long)
- && (StopLossModeLong == 0))
- {
- ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (-10 * TickSize)) , @"LONG STOP", "");
- }
- // Set 3
- if ((Position.MarketPosition == MarketPosition.Long)
- && (StopLossModeLong == 0)
- && (Close[0] >= (Position.AveragePrice + (10 * TickSize)) ))
- {
- StopLossModeLong = 1;
- }
- // Set 4
- if ((Position.MarketPosition == MarketPosition.Long)
- && (StopLossModeLong == 1))
- {
- ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), Position.AveragePrice, @"LONG BE", "");
- }
- // Set 5
- if ((Position.MarketPosition == MarketPosition.Long)
- && (StopLossModeLong == 1)
- && (Close[0] >= (Position.AveragePrice + (20 * TickSize)) ))
- {
- StopLossModeLong = 2;
- }
- // Set 6
- if ((Position.MarketPosition == MarketPosition.Long)
- && (StopLossModeLong == 2))
- {
- ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (10 * TickSize)) , @"+10 TICKS PROFIT", "");
- }
- }
- }
- #region Properties
- [Display(GroupName="Parameters", Description="Maximum number of trades to take per day.")]
- public int MaxTrades
- {
- get { return maxTrades; }
- set { maxTrades = Math.Max(1, value); }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement