Advertisement
Guest User

Untitled

a guest
Dec 4th, 2020
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 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 TestStrategy : Strategy
  29.     {
  30.         private RSI RSI1;
  31.         Order RSIEntry;
  32.         int OrderBar;
  33.         protected override void OnStateChange()
  34.         {
  35.             if (State == State.SetDefaults)
  36.             {
  37.                 Description                                 = @"Enter the description for your new custom Strategy here.";
  38.                 Name                                        = "TestStrategy";
  39.                 Calculate                                   = Calculate.OnBarClose;
  40.                 EntriesPerDirection                         = 1;
  41.                 EntryHandling                               = EntryHandling.AllEntries;
  42.                 IsExitOnSessionCloseStrategy                = true;
  43.                 ExitOnSessionCloseSeconds                   = 30;
  44.                 IsFillLimitOnTouch                          = false;
  45.                 MaximumBarsLookBack                         = MaximumBarsLookBack.TwoHundredFiftySix;
  46.                 OrderFillResolution                         = OrderFillResolution.Standard;
  47.                 Slippage                                    = 0;
  48.                 StartBehavior                               = StartBehavior.WaitUntilFlat;
  49.                 TimeInForce                                 = TimeInForce.Gtc;
  50.                 TraceOrders                                 = false;
  51.                 RealtimeErrorHandling                       = RealtimeErrorHandling.StopCancelClose;
  52.                 StopTargetHandling                          = StopTargetHandling.PerEntryExecution;
  53.                 BarsRequiredToTrade                         = 20;
  54.                 // Disable this property for performance gains in Strategy Analyzer optimizations
  55.                 // See the Help Guide for additional information
  56.                 IsInstantiatedOnEachOptimizationIteration   = true;
  57.                 TraceOrders = true;
  58.             }
  59.             else if (State == State.Configure)
  60.             {
  61.                
  62.             }
  63.             else if (State == State.DataLoaded)
  64.             {
  65.                 RSI1 = RSI(Close, 14, 3);
  66.                 AddChartIndicator(RSI1);
  67.             }
  68.         }
  69.  
  70.         protected override void OnBarUpdate()
  71.         {
  72.             if(CurrentBar < 2) return;
  73.            
  74.             if (RSIEntry == null && RSI1.Avg[0] > 70 && RSI1.Avg[1] < 70)
  75.             {
  76.                 RSIEntry = EnterShortLimit(0, true, 1, GetCurrentBid() + (100 * TickSize), "S");
  77.                 OrderBar = CurrentBar;
  78.             }
  79.             if(RSIEntry != null && CurrentBar > OrderBar && Position.MarketPosition == MarketPosition.Flat)
  80.             {
  81.                 CancelOrder(RSIEntry);
  82.                 RSIEntry = null;
  83.                 EnterShort("ShortMarket");
  84.                 OrderBar = CurrentBar;
  85.             }
  86.            
  87.             if(Position.MarketPosition == MarketPosition.Short && BarsSinceEntryExecution() > 1)
  88.             {
  89.                 ExitShort("ShortMarket");
  90.             }
  91.                
  92.         }
  93.        
  94.         protected override void OnPositionUpdate(Cbi.Position position, double averagePrice, int quantity, Cbi.MarketPosition marketPosition)
  95.         {
  96.             Print(marketPosition.ToString());
  97.         }
  98.     }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement