NexusFi: Find Your Edge


Home Menu

 





Using IOrders and setting stops problems


Discussion in NinjaTrader

Updated
    1. trending_up 2,688 views
    2. thumb_up 0 thanks given
    3. group 2 followers
    1. forum 4 posts
    2. attach_file 0 attachments




 
Search this Thread

Using IOrders and setting stops problems

  #1 (permalink)
 
spinnybobo's Avatar
 spinnybobo 
Crete, IL/USA
 
Experience: Intermediate
Platform: NinjaTrader, Mt4
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
Posts: 173 since Aug 2009
Thanks Given: 105
Thanks Received: 61

Hello Everyone

I have a Ninjascript question concerning a very simple IOrder strategy.
Basically I have a reason to get into the trade. I enter on an IOrder and then I set the stop.
I don't understand why when I run this very simple code, it always does ExitOnClose even though the price crosses where the stop should be.
The stop is simply not being set for some reason even though I am tagging it with the name "Long"

 
Code
#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.Gui.Chart;
using NinjaTrader.Strategy;
#endregion

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{

    [Description("Basic IOrder Strategy")]
    public class IOrderBasic : Strategy
    {
	#region Variables
		
	private int _stop = 10;
	private int _quantity = 1;
	private SMA _sma;
	private IOrder _entry = null;
	private IOrder _exit = null;
		
	#endregion

        protected override void Initialize()
        {
            CalculateOnBarClose = true;
	    this._sma = SMA(21);
	    Add(_sma);
        }
        protected override void OnBarUpdate()
        {
		//seek an exit
		if (_entry != null && _entry.OrderState == OrderState.Filled && Long)
		{
			if (_exit == null)
			    _exit = ExitLongStop(_quantity, _entry.AvgFillPrice-(_stop*TickSize), "Stop", "Long");
		}
		//seek an entry
		if (Flat)
		{
			if (High[0] >= _sma[0] && High[1] < _sma[1])
			{
				_entry = EnterLong(_quantity, "Long");
			}
		}
        }
	#region Helpers
		
	private bool Long{ get { return Position.MarketPosition == MarketPosition.Long; }}
	private bool Short{ get { return Position.MarketPosition == MarketPosition.Short; }}
	private bool Flat{ get { return Position.MarketPosition == MarketPosition.Flat; }}
		
	#endregion

        #region Properties
        [Description("Stop")]
        [GridCategory("Parameters")]
        public int Stop
        {
            get { return _stop; }
            set { _stop = Math.Max(1, value); }
        }
	[Description("Quantity")]
	[GridCategory("Parameters")]
	public int Quantity
	{
		get { return _quantity; }
		set { _quantity = Math.Max(1, value); }
	}
        #endregion
    }
}
Any help would be much appreciated
thanks
Spencer

Follow me on Twitter Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Exit Strategy
NinjaTrader
Are there any eval firms that allow you to sink to your …
Traders Hideout
Build trailing stop for micro index(s)
Psychology and Money Management
New Micros: Ultra 10-Year & Ultra T-Bond -- Live Now
Treasury Notes and Bonds
Deepmoney LLM
Elite Quantitative GenAI/LLM
 
  #3 (permalink)
 vegasfoster 
las vegas
 
Experience: Intermediate
Platform: Sierra Chart
Broker: Velocity/IB
Trading: 6E
Posts: 1,145 since Feb 2010
Thanks Given: 304
Thanks Received: 844


Maybe...

Reply With Quote
  #4 (permalink)
 
spinnybobo's Avatar
 spinnybobo 
Crete, IL/USA
 
Experience: Intermediate
Platform: NinjaTrader, Mt4
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
Posts: 173 since Aug 2009
Thanks Given: 105
Thanks Received: 61

Hey

This might be working now. Perhaps before the template that I was using was interpreted incorrectly when I rewrote the code. I tried to make it as barbones as possible. At least now when I trace the orders I can see them working.

 
Code
#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.Gui.Chart;
using NinjaTrader.Strategy;
#endregion

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{

    [Description("Basic IOrder Strategy")]
    public class IOrderBasic : Strategy
    {
	#region Variables
		
	private int _stop = 10;
	private int _quantity = 1;
	private SMA _sma;
	private IOrder _entry = null;
	private IOrder _exit = null;
		
	#endregion

        protected override void Initialize()
        {
            CalculateOnBarClose = true;
	    EntryHandling = EntryHandling.UniqueEntries;
	    TraceOrders = true;
	    this._sma = SMA(21);
	    Add(_sma);
        }
        protected override void OnBarUpdate()
        {

		if (CurrentBar < 20) //not enough bars stop
			return;

		if (Flat) //get in a position
		{
			if (High[0] >= _sma[0] && High[1] < _sma[1])
			{
				_entry = EnterLong(_quantity, "Long");
			}
		}
		if (Long) //set stop if in a position
			_exit = ExitLongStop(_quantity, _entry.AvgFillPrice - (_stop*TickSize), "Stop", "Long");
        }
	#region Helpers
		
	private bool Long{ get { return Position.MarketPosition == MarketPosition.Long; }}
	private bool Short{ get { return Position.MarketPosition == MarketPosition.Short; }}
	private bool Flat{ get { return Position.MarketPosition == MarketPosition.Flat; }}
		
	#endregion

        #region Properties
        [Description("Stop")]
        [GridCategory("Parameters")]
        public int Stop
        {
            get { return _stop; }
            set { _stop = Math.Max(1, value); }
        }
	[Description("Quantity")]
	[GridCategory("Parameters")]
	public int Quantity
	{
		get { return _quantity; }
		set { _quantity = Math.Max(1, value); }
	}
        #endregion
    }
}
any other ideas would be appreciated
thanks
Spencer

Follow me on Twitter Started this thread Reply With Quote
  #5 (permalink)
 
spinnybobo's Avatar
 spinnybobo 
Crete, IL/USA
 
Experience: Intermediate
Platform: NinjaTrader, Mt4
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
Posts: 173 since Aug 2009
Thanks Given: 105
Thanks Received: 61

it seems like when I say

 
Code
     if (_entry == null)
         _entry = EnterLong(_quantity, "Long");
it seems to be having a problem when I call to see if it is null first before allowing it to be set.
it seems to be working as long as I don't ask
 
Code
if (_entry == null) 

//or

if(_entry != null)
anyway, it seems to be working now. Now I can at least program trailing stops.

Follow me on Twitter Started this thread Reply With Quote




Last Updated on June 6, 2013


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts