Advertisement
Guest User

Untitled

a guest
May 27th, 2021
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.25 KB | None | 0 0
  1. #region Using declarations
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.ComponentModel.DataAnnotations;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Xml.Serialization;
  13. using NinjaTrader.Cbi;
  14. using NinjaTrader.Gui;
  15. using NinjaTrader.Gui.Chart;
  16. using NinjaTrader.Gui.SuperDom;
  17. using NinjaTrader.Gui.Tools;
  18. using NinjaTrader.Data;
  19. using NinjaTrader.NinjaScript;
  20. using NinjaTrader.Core.FloatingPoint;
  21. using NinjaTrader.NinjaScript.Indicators;
  22. using NinjaTrader.NinjaScript.DrawingTools;
  23. #endregion
  24.  
  25. //This namespace holds Strategies in this folder and is required. Do not change it.
  26. namespace NinjaTrader.NinjaScript.Strategies
  27. {
  28.     public class TrailBuilderExample : Strategy
  29.     {
  30.         private double CurrentTriggerPriceLong;
  31.         private double CurrentStopPriceLong;
  32.  
  33.         private double CurrentTriggerPriceShort;
  34.         private double CurrentStopPriceShort;
  35.  
  36.  
  37.         protected override void OnStateChange()
  38.         {
  39.             if (State == State.SetDefaults)
  40.             {
  41.                 Description                                 = @"Demonstrates changing the price of a Stop Market Order similar to a trailing stop loss";
  42.                 Name                                        = "TrailBuilderExample";
  43.                 Calculate                                   = Calculate.OnEachTick;
  44.                 EntriesPerDirection                         = 1;
  45.                 EntryHandling                               = EntryHandling.AllEntries;
  46.                 IsExitOnSessionCloseStrategy                = true;
  47.                 ExitOnSessionCloseSeconds                   = 30;
  48.                 IsFillLimitOnTouch                          = false;
  49.                 MaximumBarsLookBack                         = MaximumBarsLookBack.TwoHundredFiftySix;
  50.                 OrderFillResolution                         = OrderFillResolution.Standard;
  51.                 Slippage                                    = 0;
  52.                 StartBehavior                               = StartBehavior.WaitUntilFlat;
  53.                 TimeInForce                                 = TimeInForce.Gtc;
  54.                 TraceOrders                                 = false;
  55.                 RealtimeErrorHandling                       = RealtimeErrorHandling.StopCancelClose;
  56.                 StopTargetHandling                          = StopTargetHandling.PerEntryExecution;
  57.                 BarsRequiredToTrade                         = 20;
  58.                 // Disable this property for performance gains in Strategy Analyzer optimizations
  59.                 // See the Help Guide for additional information
  60.                 IsInstantiatedOnEachOptimizationIteration   = true;
  61.                 TrailFrequency                  = 5;
  62.                 TrailStopDistance                   = -5;
  63.                 CurrentTriggerPriceLong   = 0;
  64.                 CurrentStopPriceLong      = 0;
  65.                 CurrentTriggerPriceShort  = 0;
  66.                 CurrentStopPriceShort     = 0;
  67.             }
  68.             else if (State == State.Configure)
  69.             {
  70.             }
  71.         }
  72.  
  73.         protected override void OnBarUpdate()
  74.         {
  75.             if (BarsInProgress != 0)
  76.                 return;
  77.  
  78.               // LONG ORDERS (Sets #1 to #4)
  79.              // Set 1
  80.             if (Position.MarketPosition == MarketPosition.Flat)
  81.             {
  82.                 CurrentStopPriceLong = 0;
  83.             }
  84.            
  85.             if (CurrentBars[0] < 1)
  86.                 return;
  87.  
  88.              // Set 2
  89.             if ((Position.MarketPosition == MarketPosition.Flat)
  90.                  && (State == State.Realtime)
  91.                  && (High[0] > High[1]))
  92.             {
  93.                 EnterLong(Convert.ToInt32(DefaultQuantity), @"longEntry");
  94.                 CurrentTriggerPriceLong = (Close[0] + (TrailFrequency * TickSize)) ;
  95.                 CurrentStopPriceLong= (Close[0] + (TrailStopDistance * TickSize)) ;
  96.             }
  97.            
  98.              // Set 3
  99.             if ((Position.MarketPosition == MarketPosition.Long)
  100.                  && (Close[0] > CurrentTriggerPriceLong))
  101.             {
  102.                 CurrentTriggerPriceLong = (Close[0] + (TrailFrequency * TickSize)) ;
  103.                 CurrentStopPriceLong = (Close[0] + (TrailStopDistance * TickSize)) ;
  104.             }
  105.            
  106.              // Set 4
  107.             if (CurrentStopPriceLong != 0)
  108.             {
  109.                 ExitLongStopMarket(Convert.ToInt32(DefaultQuantity), CurrentStopPriceLong, @"", "");
  110.             }
  111.            
  112.             // SHORT ORDERS (Sets #5 to #8)
  113.            // Set 5
  114.            if (Position.MarketPosition == MarketPosition.Flat)
  115.            {
  116.              CurrentStopPriceShort = 0;
  117.            }
  118.          
  119.            if (CurrentBars[0] < 1)
  120.              return;
  121.  
  122.             // Set 6
  123.            if ((Position.MarketPosition == MarketPosition.Flat)
  124.               && (State == State.Realtime)
  125.               && (Low[0] < Low[1]))
  126.            {
  127.              EnterShort(Convert.ToInt32(DefaultQuantity), @"shortEntry");
  128.              CurrentTriggerPriceShort = (Close[0] - (TrailFrequency * TickSize)) ;
  129.              CurrentStopPriceShort = (Close[0] - (TrailStopDistance * TickSize)) ;
  130.            }
  131.          
  132.            // Set 7
  133.            if ((Position.MarketPosition == MarketPosition.Long)
  134.               && (Close[0] > CurrentTriggerPriceShort))
  135.            {
  136.              CurrentTriggerPriceShort = (Close[0] - (TrailFrequency * TickSize)) ;
  137.              CurrentStopPriceShort = (Close[0] - (TrailStopDistance * TickSize)) ;
  138.            }
  139.          
  140.            // Set 8
  141.            if (CurrentStopPriceShort != 0)
  142.            {
  143.              ExitShortStopMarket(Convert.ToInt32(DefaultQuantity), CurrentStopPriceShort, @"", "");
  144.            }
  145.            
  146.         }
  147.  
  148.         #region Properties
  149.         [NinjaScriptProperty]
  150.         [Range(1, int.MaxValue)]
  151.         [Display(Name="TrailFrequency", Description="This will be how often trail action triggers.", Order=1, GroupName="Parameters")]
  152.         public int TrailFrequency
  153.         { get; set; }
  154.  
  155.         [NinjaScriptProperty]
  156.         [Range(-9999, int.MaxValue)]
  157.         [Display(Name="TrailStopDistance", Description="Distance stop for exit order will be placed. This needs to be less than the TrailLimitDistance to exit a long position.", Order=2, GroupName="Parameters")]
  158.         public int TrailStopDistance
  159.         { get; set; }
  160.         #endregion
  161.  
  162.     }
  163. }
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement