NexusFi: Find Your Edge


Home Menu

 





Need Help with simple strategy on canceling orders


Discussion in NinjaTrader

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




 
Search this Thread

Need Help with simple strategy on canceling orders

  #1 (permalink)
 floyd084 
boston, ma
 
Experience: Intermediate
Platform: NT7
Broker: amp/zenfire
Trading: es
Posts: 10 since Feb 2010
Thanks Given: 3
Thanks Received: 0

So im trying to write a simple strategy that makes a market based on bollinger bands. It's very basic so far and I have it to a point where I can offer or bid at the price I want. What im trying to do is cancel the sitting orders if price crosses the mid point of the bollingers. The strategy so far places an order based on price in relation to the 20 period ma for a bollinger. I'm either offering or bidding at the extreme bands, if price is above the 20 period ma im offering, below and im bidding. What I need to do is cancel the previous order if price doesnt fill me and moves above or below the ma. Heres the code I have so far. It will place a singal order but wont remove it and place a new one if price crosses the middle band. Any help would be greatly appreciated. I've looked at the ninjatrader help for strategies and it doesn't help me much.

protected override void Initialize()
{
Add(Bollinger(2.3, 20));
SetProfitTarget("", CalculationMode.Ticks, 21);
SetStopLoss("", CalculationMode.Ticks, 12, false);

CalculateOnBarClose = false;
}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition set 1

if (GetCurrentBid() < Bollinger(2.3, 20).Middle[0])
{
entryOrder = EnterLongLimit(DefaultQuantity, Bollinger(2.3, 20).Lower[0] + -1 * TickSize, "Long");

}
if (entryOrder != null && GetCurrentBid() > Bollinger(2.3, 20).Middle[0])
{

CancelOrder(entryOrder);
}


// Condition set 2

if (GetCurrentAsk() > Bollinger(2.3, 20).Middle[0])
{
entryOrder = EnterShortLimit(DefaultQuantity, Bollinger(2.3, 20).Upper[0] + 1 * TickSize, "Short");

}
if (entryOrder != null && GetCurrentBid() < Bollinger(2.3, 20).Middle[0])
{

CancelOrder(entryOrder);
}

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
MC PL editor upgrade
MultiCharts
ZombieSqueeze
Platforms and Indicators
REcommedations for programming help
Sierra Chart
Trade idea based off three indicators.
Traders Hideout
Exit Strategy
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Spoo-nalysis ES e-mini futures S&P 500
29 thanks
Just another trading journal: PA, Wyckoff & Trends
25 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
23 thanks
GFIs1 1 DAX trade per day journal
17 thanks
  #3 (permalink)
 pam421 
Florida
 
Experience: Intermediate
Platform: Ninjatrader
Posts: 12 since May 2010
Thanks Given: 34
Thanks Received: 12


You need to be a little more specific on which order gets cancelled when.

Try this...

 
Code
// Bollinger_Band_Strategy

#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

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
	/// <summary>
	/// Simple moving average cross over strategy.
	/// </summary>
	[Description("Bollinger Band Strategy.")]
	public class Bollinger_Band_Strategy : Strategy
	{
		private	IOrder		entryOrder = null;
		private	double		numStdDev	= 2.3;
		private int			period		= 20;


		/// <summary>
		/// This method is used to configure the strategy and is called once before any strategy method is called.
		/// </summary>
		protected override void Initialize()
		{
			Add( Bollinger(numStdDev, period) );
			SetProfitTarget("", CalculationMode.Ticks, 21);
			SetStopLoss("", CalculationMode.Ticks, 12, false);

			CalculateOnBarClose = false;
		}

		/// <summary>
		/// Called on each bar update event (incoming tick).
		/// </summary>
		protected override void OnBarUpdate()
		{
			double bollinger_upper	= Bollinger(numStdDev, period).Upper[0];
			double bollinger_middle	= Bollinger(numStdDev, period).Middle[0];
			double bollinger_lower	= Bollinger(numStdDev, period).Lower[0];

			if (entryOrder == null)
			{
				if (GetCurrentBid() < bollinger_middle)
				{
					double entry_price = bollinger_lower - TickSize;
					entryOrder = EnterLongLimit( DefaultQuantity, entry_price, "Long" );
				}

				if (GetCurrentAsk() > bollinger_middle)
				{
					double entry_price = bollinger_upper + TickSize;
					entryOrder = EnterShortLimit( DefaultQuantity, entry_price, "Short" );
				}
			}
			else
			if (Position.MarketPosition == MarketPosition.Flat)
			{
				if (GetCurrentBid() > bollinger_middle)
				if (entryOrder.OrderAction == OrderAction.Buy)
					CancelOrder(entryOrder);

				if (GetCurrentAsk() < bollinger_middle)
				if (entryOrder.OrderAction == OrderAction.Sell)
					CancelOrder(entryOrder);
			}
		}

		#region Properties
		[Description("Number of standard deviations")]
		[GridCategory("Parameters")]
		[Gui.Design.DisplayNameAttribute("# of std. dev.")]
		public double NumStdDev
		{
			get { return numStdDev; }
			set { numStdDev = Math.Max(0, value); }
		}

		[Description("Numbers of bars used for calculations")]
		[GridCategory("Parameters")]
		public int Period
		{
			get { return period; }
			set { period = Math.Max(1, value); }
		}
		#endregion
	}
}

Reply With Quote
Thanked by:




Last Updated on February 22, 2011


© 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