Advertisement
Guest User

Untitled

a guest
May 30th, 2021
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. namespace NinjaTrader.NinjaScript.Strategies
  2. {
  3.     public class MultiStepBreakeven : Strategy
  4.     {
  5.         private int StopLossModeLong;
  6.         private int StopLossModeShort;
  7.  
  8.  
  9.         protected override void OnStateChange()
  10.         {
  11.             if (State == State.SetDefaults)
  12.             {
  13.                 Description                                 = @"Enter the description for your new custom Strategy here.";
  14.                 Name                                        = "MultiStepBreakeven";
  15.                 Calculate                                   = Calculate.OnEachTick;
  16.                 EntriesPerDirection                         = 1;
  17.                 EntryHandling                               = EntryHandling.AllEntries;
  18.                 IsExitOnSessionCloseStrategy                = true;
  19.                 ExitOnSessionCloseSeconds                   = 30;
  20.                 IsFillLimitOnTouch                          = false;
  21.                 MaximumBarsLookBack                         = MaximumBarsLookBack.TwoHundredFiftySix;
  22.                 OrderFillResolution                         = OrderFillResolution.Standard;
  23.                 Slippage                                    = 0;
  24.                 StartBehavior                               = StartBehavior.WaitUntilFlat;
  25.                 TimeInForce                                 = TimeInForce.Gtc;
  26.                 TraceOrders                                 = false;
  27.                 RealtimeErrorHandling                       = RealtimeErrorHandling.StopCancelClose;
  28.                 StopTargetHandling                          = StopTargetHandling.PerEntryExecution;
  29.                 BarsRequiredToTrade                         = 20;
  30.                 // Disable this property for performance gains in Strategy Analyzer optimizations
  31.                 // See the Help Guide for additional information
  32.                 IsInstantiatedOnEachOptimizationIteration   = true;
  33.                 StopLossModeLong                    = 0;
  34.                 StopLossModeShort                   = 0;
  35.             }
  36.             else if (State == State.Configure)
  37.             {
  38.             }
  39.         }
  40.  
  41.         protected override void OnBarUpdate()
  42.         {
  43.             if (BarsInProgress != 0)
  44.                 return;
  45.  
  46.              // Set 1
  47.             if ((Position.MarketPosition == MarketPosition.Flat)
  48.                  && (Close[0] > Open[0]))
  49.             {
  50.                 EnterLongLimit(Convert.ToInt32(DefaultQuantity), GetCurrentBid(), "");
  51.                 StopLossModeLong = 0;
  52.             }
  53.            
  54.             if (CurrentBars[0] < 1)
  55.                 return;
  56.                        
  57.        
  58.              // Set 2
  59.             if ((Position.MarketPosition == MarketPosition.Long)
  60.                  && (StopLossModeLong == 0))
  61.             {
  62.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (-10 * TickSize)) , "", "");
  63.             }
  64.            
  65.              // Set 3
  66.             if ((Position.MarketPosition == MarketPosition.Long)
  67.                  && (Close[0] >= (Position.AveragePrice + (10 * TickSize)) )
  68.                  && (StopLossModeLong == 0))
  69.             {
  70.                 StopLossModeLong = 1;
  71.             }
  72.            
  73.              // Set 4
  74.             if ((Position.MarketPosition == MarketPosition.Long)
  75.                  && (StopLossModeLong == 1))
  76.             {
  77.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), Position.AveragePrice, "", "");
  78.             }
  79.            
  80.              // Set 5
  81.             if ((Position.MarketPosition == MarketPosition.Long)
  82.                  && (Close[0] >= (Position.AveragePrice + (20 * TickSize)) )
  83.                  && (StopLossModeLong == 1))
  84.             {
  85.                 StopLossModeLong = 2;
  86.             }
  87.                        
  88.              // Set 6
  89.             if ((Position.MarketPosition == MarketPosition.Long)
  90.                  && (StopLossModeLong == 2))
  91.             {
  92.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (10 * TickSize)) , "", "");
  93.             }
  94.            
  95.             Print(@"Time/Date " + Convert.ToString(Times[0][0]) + @" Long Orders = " + Convert.ToString(StopLossModeLong) + @" Price       ");
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement