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;
- 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;
- }
- else if (State == State.Configure)
- {
- }
- }
- protected override void OnBarUpdate()
- {
- if (BarsInProgress != 0)
- return;
- // Set 1
- if ((Position.MarketPosition == MarketPosition.Flat)
- && (Close[0] > Open[0]))
- {
- EnterLongLimit(Convert.ToInt32(DefaultQuantity), GetCurrentBid(), "");
- StopLossModeLong = 0;
- }
- if (CurrentBars[0] < 1)
- return;
- // Set 2
- if ((Position.MarketPosition == MarketPosition.Long)
- && (StopLossModeLong == 0))
- {
- ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (-10 * TickSize)) , "", "");
- }
- // Set 3
- if ((Position.MarketPosition == MarketPosition.Long)
- && (Close[0] >= (Position.AveragePrice + (10 * TickSize)) )
- && (StopLossModeLong == 0))
- {
- StopLossModeLong = 1;
- }
- // Set 4
- if ((Position.MarketPosition == MarketPosition.Long)
- && (StopLossModeLong == 1))
- {
- ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), Position.AveragePrice, "", "");
- }
- // Set 5
- if ((Position.MarketPosition == MarketPosition.Long)
- && (Close[0] >= (Position.AveragePrice + (20 * TickSize)) )
- && (StopLossModeLong == 1))
- {
- StopLossModeLong = 2;
- }
- // Set 6
- if ((Position.MarketPosition == MarketPosition.Long)
- && (StopLossModeLong == 2))
- {
- ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (10 * TickSize)) , "", "");
- }
- // Set 7
- if ((Position.MarketPosition == MarketPosition.Flat)
- && (Close[0] < Open[0]))
- {
- EnterShortLimit(Convert.ToInt32(DefaultQuantity), GetCurrentAsk(), "");
- StopLossModeShort = 0;
- }
- if (CurrentBars[0] < 1)
- return;
- // Set 8
- if ((Position.MarketPosition == MarketPosition.Short)
- && (StopLossModeShort == 0))
- {
- ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (10 * TickSize)) , "", "");
- }
- // Set 9
- if ((Position.MarketPosition == MarketPosition.Short)
- && (Close[0] <= (Position.AveragePrice + (-10 * TickSize)) )
- && (StopLossModeShort == 0))
- {
- StopLossModeShort = 1;
- }
- // Set 10
- if ((Position.MarketPosition == MarketPosition.Short)
- && (StopLossModeShort == 1))
- {
- ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), Position.AveragePrice, "", "");
- }
- // Set 11
- if ((Position.MarketPosition == MarketPosition.Short)
- && (Close[0] <= (Position.AveragePrice + (-20 * TickSize)) )
- && (StopLossModeShort == 1))
- {
- StopLossModeShort = 2;
- }
- // Set 12
- if ((Position.MarketPosition == MarketPosition.Short)
- && (StopLossModeShort == 2))
- {
- ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (-10 * TickSize)) , "", "");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement