NexusFi: Find Your Edge


Home Menu

 





Need Help with simple strategy on canceling orders


Discussion in NinjaTrader

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




 
Search this Thread
  #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?
SEC Chairman Calls for New Golden Age of SEC-CFTC Regula …
Traders Hideout
More Than Capable: Hegseths War Warning Validates $114M …
Prediction Markets & Event Contracts
Day 96 Missiles Hit Kuwait and Bahrain: June 15 Peace at …
Prediction Markets & Event Contracts
Iran War Prediction Markets: Ceasefire 16%, Ground Invas …
Prediction Markets & Event Contracts
Iran Ceasefire Surges to 19.5% on US 15-Point Plan -- 82 …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
21 thanks
2026 Jlab journal
10 thanks
Algo automated / semi-automated trading anyone?
6 thanks
Lady Vols Primer: Trading Volatility Journal
6 thanks
2026 Fire Horse
5 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


© 2026 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 - Downloads - Top
no new posts