Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Using declarations
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Xml.Serialization;
- using NinjaTrader.Cbi;
- using NinjaTrader.Gui;
- using NinjaTrader.Gui.Chart;
- using NinjaTrader.Gui.SuperDom;
- using NinjaTrader.Gui.Tools;
- using NinjaTrader.Data;
- using NinjaTrader.NinjaScript;
- using NinjaTrader.Core.FloatingPoint;
- using NinjaTrader.NinjaScript.Indicators;
- using NinjaTrader.NinjaScript.DrawingTools;
- #endregion
- //This namespace holds Strategies in this folder and is required. Do not change it.
- namespace NinjaTrader.NinjaScript.Strategies
- {
- public class TestStrategy : Strategy
- {
- private RSI RSI1;
- Order RSIEntry;
- int OrderBar;
- protected override void OnStateChange()
- {
- if (State == State.SetDefaults)
- {
- Description = @"Enter the description for your new custom Strategy here.";
- Name = "TestStrategy";
- Calculate = Calculate.OnBarClose;
- 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;
- TraceOrders = true;
- }
- else if (State == State.Configure)
- {
- }
- else if (State == State.DataLoaded)
- {
- RSI1 = RSI(Close, 14, 3);
- AddChartIndicator(RSI1);
- }
- }
- protected override void OnBarUpdate()
- {
- if(CurrentBar < 2) return;
- if (RSIEntry == null && RSI1.Avg[0] > 70 && RSI1.Avg[1] < 70)
- {
- RSIEntry = EnterShortLimit(0, true, 1, GetCurrentBid() + (100 * TickSize), "S");
- OrderBar = CurrentBar;
- }
- if(RSIEntry != null && CurrentBar > OrderBar && Position.MarketPosition == MarketPosition.Flat)
- {
- CancelOrder(RSIEntry);
- RSIEntry = null;
- EnterShort("ShortMarket");
- OrderBar = CurrentBar;
- }
- if(Position.MarketPosition == MarketPosition.Short && BarsSinceEntryExecution() > 1)
- {
- ExitShort("ShortMarket");
- }
- }
- protected override void OnPositionUpdate(Cbi.Position position, double averagePrice, int quantity, Cbi.MarketPosition marketPosition)
- {
- Print(marketPosition.ToString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement