Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop-And-Reverse with Parabolic SAR

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Stop-And-Reverse with Parabolic SAR

    Hi,

    I would like to design a strategy which stops and reverses based on the Parabolic SAR (orange dots in attached screenshot).

    I am not sure how to achieve a position reversal in the same bar. The strategy "buys to cover" and "sells" in the correct bar, but opens the next position a bar later.

    Can anyone please help? Bear in mind, I use NinjaTrader 8, and my C# skills still leave much to be desired.

    Thanks in advance,

    Cyberjoe



    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    	public class StopAndReverse20160919 : Strategy
    	{
    		private ParabolicSAR ParabolicSAR1;
    		double expectedParabolicSARValue;
    
    		protected override void OnStateChange()
    		{
    			if (State == State.SetDefaults)
    			{
    				Description									= @"Explore various indicators...";
    				Name										= "StopAndReverse20160919";
    				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;
    			}
    			else if (State == State.Configure)
    			{
    				ParabolicSAR1				= ParabolicSAR(0.02, 0.2, 0.02);
    				AddChartIndicator(ParabolicSAR1);
    			}
    			else if (State == State.DataLoaded)
    			{		
    				ClearOutputWindow();
    			}
    		}
    
    		protected override void OnBarUpdate()
    		{
    			if (CurrentBars[0] < 1)
    			return;
    
    			// Pick initial long/short position
    			if (Position.MarketPosition == MarketPosition.Flat)
    			{
    				if (Low[0] > ParabolicSAR1[0])
    				{
    					EnterLong();
    				}
    				else if (High[0] < ParabolicSAR1[0])
    				{
    					EnterShort();
    				}
    			}	
    			
    			// Update parabolic SAR for more accurate reversal during upcoming bar
    						
    			if (BarsSinceEntryExecution() == 0)
    			{
    				expectedParabolicSARValue = ParabolicSAR1[0];
    			}
    			
    			else if (BarsSinceEntryExecution() > 0)
    			{
    				expectedParabolicSARValue = ParabolicSAR1[0] - (ParabolicSAR1[1] - ParabolicSAR1[0]);
    			}
    						
    			// Long position reversal
    			if (Position.MarketPosition == MarketPosition.Long)
    			{
    				BackBrushAll = Brushes.PaleGreen;
    				ExitLongStopMarket(this.expectedParabolicSARValue);
    			}
    						
    			// Short position reversal
    			if (Position.MarketPosition == MarketPosition.Short)
    			{
    				BackBrushAll = Brushes.Pink;
    				ExitShortStopMarket(this.expectedParabolicSARValue);				
    			}			
    
    
    			Print(string.Format("{0};  {1};  {2};  {3};  {4};  {5};  {6}", Time[0], Close[0], High[0], Low[0], ParabolicSAR1[0], ParabolicSAR1[1], this.expectedParabolicSARValue));
    		}
    	}
    }
    Attached Files

    #2
    Hello Cyberjoe,

    Thanks for your post and welcome to the forums.

    Your code is executing as expected and could not be improved to permit an earlier entry. When using Calculate.OnBarClose all of the strategy calculation are based on completed and unchanging values of the bar itself and of the indicator being. The strategy code runs once at the end of the bar and any entry orders would not be filled until the next bar which is as you have observed.

    You could run your strategy with Calculate.OnEachTick or Calculate.OnPriceChange but there is risk that you may have oscillating conditions where multiple entry/exits may occur. In these calculation modes, the strategy code is run on each tick or price change and so the indicators are also recalculated and may or may not be the end value that they will be when the bar is finished. Depending on price movement it is possible that on one tick (or price change) you may have a reversal and on another tick (or price change) you may not, leading to the potential for oscillation of entry/exit conditions. You can certainly test this out on sim101 account to understand the potentials. You can also add code (using bools) so that if you get a reversal condition that you only place one order and then prevent any further orders until the next bar, risking a small loss if the indicator does not reverse at the end.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi Paul!

      Thanks your explanations and advice!

      Cyberjoe

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by DavidHP, Today, 07:56 AM
      1 response
      5 views
      0 likes
      Last Post NinjaTrader_Erick  
      Started by kujista, Today, 06:23 AM
      3 responses
      9 views
      0 likes
      Last Post kujista
      by kujista
       
      Started by Mindset, Yesterday, 02:04 AM
      2 responses
      18 views
      0 likes
      Last Post NinjaTrader_RyanS  
      Started by f.saeidi, Today, 08:03 AM
      1 response
      5 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by samish18, 04-17-2024, 08:57 AM
      15 responses
      53 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Working...
      X