Using IOrders and setting stops problems - futures io
futures io futures trading



Using IOrders and setting stops problems


Discussion in NinjaTrader

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




Welcome to futures io: the largest futures trading community on the planet, with well over 125,000 members
  • Genuine reviews from real traders, not fake reviews from stealth vendors
  • Quality education from leading professional traders
  • We are a friendly, helpful, and positive community
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts
  • We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

(If you already have an account, login at the top of the page)

 
Search this Thread
 

Using IOrders and setting stops problems

(login for full post details)
  #1 (permalink)
Crete, IL/USA
 
Experience: Intermediate
Platform: NinjaTrader, Mt4
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
 
spinnybobo's Avatar
 
Posts: 181 since Aug 2009
Thanks: 103 given, 56 received

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 futures io?
Work with historical tcks data on MUltichart NET PowerLa …
MultiCharts
ZigZag with Depth and Backstep
EasyLanguage Programming
Indicators for pair trading MES and M2K
Platforms and Indicators
How is the range of strike prices determined?
Options
Short Interest Ratio indicator?
ThinkOrSwim
 
Best Threads (Most Thanked)
in the last 7 days on futures io
Battlestations: Show us your trading desks!
124 thanks
Big Mike in Ecuador
61 thanks
Want your NinjaTrader indicator created, free?
28 thanks
If you needed one-on-one help with any trading issue, ho …
26 thanks
Selling Options on Futures?
21 thanks
 
(login for full post details)
  #3 (permalink)
las vegas
 
Experience: Intermediate
Platform: Sierra Chart
Broker: Velocity/IB
Trading: 6E
 
Posts: 1,147 since Feb 2010
Thanks: 304 given, 839 received


Maybe...

Reply With Quote
 
(login for full post details)
  #4 (permalink)
Crete, IL/USA
 
Experience: Intermediate
Platform: NinjaTrader, Mt4
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
 
spinnybobo's Avatar
 
Posts: 181 since Aug 2009
Thanks: 103 given, 56 received

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
 
(login for full post details)
  #5 (permalink)
Crete, IL/USA
 
Experience: Intermediate
Platform: NinjaTrader, Mt4
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
 
spinnybobo's Avatar
 
Posts: 181 since Aug 2009
Thanks: 103 given, 56 received

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


futures io Trading Community Platforms and Indicators NinjaTrader > Using IOrders and setting stops problems


Last Updated on June 6, 2013


Upcoming Webinars and Events
 

NinjaTrader Indicator Challenge!

Ongoing
 

Battlestations! Show us your trading desk - $1,500 in prizes!

March
 

Importance of Finding Your Own Way w/Adam Grimes

Elite only
 

Journal Challenge w/Jigsaw

April
     



Copyright © 2021 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts