Advertisement
Guest User

Untitled

a guest
May 29th, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.27 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.             if (CurrentBars[0] < 1)
  47.                 return;
  48.  
  49.              // Set 1
  50.             if ((Position.MarketPosition == MarketPosition.Flat)
  51.                  && (Close[0] < Open[0]))
  52.             {
  53.                 EnterShortLimit(Convert.ToInt32(DefaultQuantity), 0, "");
  54.                 StopLossModeShort = 0;
  55.             }
  56.            
  57.              // Set 2
  58.             if ((Position.MarketPosition == MarketPosition.Flat)
  59.                  && (Close[0] > Open[0]))
  60.             {
  61.                 EnterLongLimit(Convert.ToInt32(DefaultQuantity), 0, "");
  62.                 StopLossModeLong = 0;
  63.             }
  64.            
  65.              // Set 3
  66.             if ((Position.MarketPosition == MarketPosition.Short)
  67.                  && (StopLossModeShort == 0))
  68.             {
  69.                 ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (10 * TickSize)) , "", "");
  70.             }
  71.            
  72.              // Set 4
  73.             if ((Position.MarketPosition == MarketPosition.Short)
  74.                  && (Close[0] <= (Position.AveragePrice + (-10 * TickSize)) )
  75.                  && (StopLossModeShort == 0))
  76.             {
  77.                 StopLossModeShort = 1;
  78.             }
  79.            
  80.              // Set 5
  81.             if ((Position.MarketPosition == MarketPosition.Short)
  82.                  && (StopLossModeShort == 1))
  83.             {
  84.                 ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), Position.AveragePrice, "", "");
  85.             }
  86.            
  87.              // Set 6
  88.             if ((Position.MarketPosition == MarketPosition.Short)
  89.                  && (Close[0] <= (Position.AveragePrice + (-20 * TickSize)) )
  90.                  && (StopLossModeShort == 1))
  91.             {
  92.                 StopLossModeShort = 2;
  93.             }
  94.            
  95.              // Set 7
  96.             if ((Position.MarketPosition == MarketPosition.Short)
  97.                  && (StopLossModeShort == 2))
  98.             {
  99.                 ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (-10 * TickSize)) , "", "");
  100.             }
  101.            
  102.              // Set 8
  103.             if ((Position.MarketPosition == MarketPosition.Long)
  104.                  && (StopLossModeLong == 0))
  105.             {
  106.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (-10 * TickSize)) , "", "");
  107.             }
  108.            
  109.              // Set 9
  110.             if ((Position.MarketPosition == MarketPosition.Long)
  111.                  && (Close[0] >= (Position.AveragePrice + (10 * TickSize)) )
  112.                  && (StopLossModeLong == 0))
  113.             {
  114.                 StopLossModeLong = 1;
  115.             }
  116.            
  117.              // Set 10
  118.             if ((Position.MarketPosition == MarketPosition.Long)
  119.                  && (StopLossModeLong == 1))
  120.             {
  121.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), Position.AveragePrice, "", "");
  122.             }
  123.            
  124.              // Set 11
  125.             if ((Position.MarketPosition == MarketPosition.Long)
  126.                  && (Close[0] >= (Position.AveragePrice + (20 * TickSize)) )
  127.                  && (StopLossModeLong == 1))
  128.             {
  129.                 StopLossModeLong = 2;
  130.             }
  131.            
  132.              // Set 12
  133.             if ((Position.MarketPosition == MarketPosition.Long)
  134.                  && (StopLossModeLong == 2))
  135.             {
  136.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), (Position.AveragePrice + (10 * TickSize)) , "", "");
  137.             }
  138.            
  139.             Print(@"Time/Date " + Convert.ToString(Times[0][0]) + @" Long Orders = " + Convert.ToString(StopLossModeLong));
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement