Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)) , "", "");
- }
- Print(@"Time/Date " + Convert.ToString(Times[0][0]) + @" Long Orders = " + Convert.ToString(StopLossModeLong) + @" Price ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement