Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Exit orders not sent at specific price

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

    Exit orders not sent at specific price

    Hello,

    I am struggling to get the code properly written to send orders at the specific prices. The exit prices were off the scale. Also, I got error message saying the order price is above/below the market price and caused my strategy to be disabled. I am using MACrossover to help me to understand how those exits work. What did I miss something?

    Thanks for your help,
    -traderjh

    Code:
    // 
    // Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
    //
    
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Strategy;
    #endregion
    namespace NinjaTrader.Strategy
    {
    	public class myMACrossOver : Strategy
    	{
    		#region Variables
    		private int	fast = 10;
    		private int	slow = 25;
    		private double targetPips = 0.00150;
    		private double lossPips = 0.00100;
    		#endregion
    		/// <summary>
    		/// This method is used to configure the strategy and is called once before any strategy method is called.
    		/// </summary>
    		protected override void Initialize()
    		{
    			BarsRequired = 0;
    			SMA(Fast).Plots[0].Pen.Color = Color.Yellow;
    			SMA(Slow).Plots[0].Pen.Color = Color.Red;
                Add(SMA(Fast));
                Add(SMA(Slow));
    			CalculateOnBarClose	= false;
    		}
    		protected override void OnBarUpdate()
    		{
    			double fastPrice = (SMA(Fast)[0]);  
    			double slowPrice = (SMA(Slow)[0]); 
    
    			if (Position.MarketPosition == MarketPosition.Flat)
    			{
    				if (CrossAbove(SMA(Fast), SMA(Slow), 10))
    					EnterLong();
    				else if (CrossBelow(SMA(Fast), SMA(Slow), 10))
    					EnterShort();
    			}
    			if (Position.MarketPosition == MarketPosition.Long)
    			{
    				if ((Close[0] <= Position.AvgPrice + targetPips) && (High[0] > (Position.AvgPrice + targetPips)))
    				{
    					ExitLongStop((Position.AvgPrice + targetPips));
    				}
    				else if (Close[0] <= Position.AvgPrice + lossPips)
    				{
    					ExitLongStop((Position.AvgPrice - lossPips));
    				}
    			}
    			if (Position.MarketPosition == MarketPosition.Short)
    			{
    				if ((Close[0] >= (Position.AvgPrice - targetPips)) && (Low[0] < (Position.AvgPrice - targetPips)))
    				{		
    					ExitShortStop((Position.AvgPrice - targetPips));			
    				}
    				else if (Close[0] >= Position.AvgPrice + lossPips)
    				{
    					ExitShortStop((Position.AvgPrice + lossPips));
    				}
    			}
    		}
    		#region Properties
    		public int Fast
    		{
    			get { return fast; }
    			set { fast = Math.Max(1, value); }
    		}
    		public int Slow
    		{
    			get { return slow; }
    			set { slow = Math.Max(1, value); }
    		}
    		public double TargetPips
    		{
    			get { return targetPips; }
    			set { targetPips = Math.Max(0.0, value); }
    		}
    		public double LossPips
    		{
    			get { return lossPips; }
    			set { lossPips = Math.Max(0.0, value); }
    		}
    		#endregion
    	}
    }
    Attached Files

    #2
    Hello,

    It looks like you have some cases in which you're trying to place a stop exit long order above the current market price

    Specifically look at this:

    Code:
    if ((Close[0] <= Position.AvgPrice + targetPips) && (High[0] > (Position.AvgPrice + targetPips)))
        {
         ExitLongStop((Position.AvgPrice + targetPips));
    
    .....................
    Here you are checking to see if the current price is less than your avg price AND if the high of the current bar is greater than the avg price

    You're then trying to exit at a price that you know will be equal to or greater than the current price.

    If this is your goal then simply call ExitLong();

    Your NinjaScript / C# Code will always be logically processed and evaluate according to your set logic – this can of course lead to unexpected results at times, thus we would suggest to simplify and debug your code to better understand the event sequence it would go through - unfortunately we cannot offer such debug or code modification services here, but please see the provided resources below to help you proceed productively :


    First of all you would want to use Print() statements to verify values are what you expect - Debugging your NinjaScript code.


    For strategies add TraceOrders = true to your Initialize() method and you can then view valuable output related to strategy submitted orders through Tools > Output window - TraceOrders


    It may also help to add drawing objects to your chart for signal and condition confirmation - Drawing Objects.


    If you would prefer the debug assist of a professional NinjaScript consultant, please check into the following listings - Click here for a list of certified NinjaScript Consultants
    LanceNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by haas88, 03-21-2024, 02:22 AM
    19 responses
    220 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by ZeroKuhl, Yesterday, 04:31 PM
    4 responses
    31 views
    0 likes
    Last Post ZeroKuhl  
    Started by cupir2, Yesterday, 07:44 PM
    3 responses
    22 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by reynoldsn, Yesterday, 07:26 PM
    2 responses
    16 views
    0 likes
    Last Post reynoldsn  
    Started by MartinT, 05-17-2023, 06:00 AM
    18 responses
    175 views
    0 likes
    Last Post flybuzz
    by flybuzz
     
    Working...
    X