NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 03-30-2012, 09:31 AM   #1
Zigman
Member
 
Join Date: Apr 2009
Location: Toronto ON
Posts: 31
Thanks: 6
Thanked 1 time in 1 post
Unhappy Error in strategy - incorrect entries and exits on next bar

Hello everyone,

- Hoping someone can help me figure out why my "multiple bollinger fade" strategy is not entering or exiting properly...

Positions should be entered once as price closes above each bollinger band (i.e. max 3 contracts long or short at any time)

The positions (1, 2 or 3 contracts, either all long or all short) should then hold until one of three events takes place:

1. price closes above/below the mid line of 1st bollinger band (close 1, 2 or 3 contracts accordingly)
2. stop loss in $$ is hit (close 1, 2 or 3 contracts accordingly)
3. profit target in $$ is hit (close 1, 2 or 3 contracts accordingly)

It's probably something simple but I can't seem to figure it out.

Complete code below:


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
{
    /// <summary>
    /// Enter the description of your strategy here
    /// </summary>
    [Description("Enter the description of your strategy here")]
    public class MultiFadeAtEachBollingerZiggy2 : Strategy
    {
        #region Variables
 
        private int boLength1a = 100; // Default length for first bolinger band 
        private int boLength2a = 100; // Default length for second bollinger band
        private int boLength3a = 100; // Default length for third bollinger band
        private double boDev1a = 1.500; // Default Std Dev for first bolinger band
        private double boDev2a = 2.000; // Default Std Dev for second bolinger band
        private double boDev3a = 1.000; // Default Std Dev for third bolinger band
        private int trgtInDollars = 2500; // Default setting for profit target in $$
        private int stpInDollars = 5000; // Default setting for stop loss in $$
 
        #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()
        {
            Add(Bollinger(BoDev1a, BoLength1a));
            Add(Bollinger(BoDev2a, BoLength2a));
            Add(Bollinger(BoDev3a, BoLength3a));
            Add(Bollinger(BoDev1a, BoLength1a));
            Add(Bollinger(BoDev2a, BoLength2a));
            Add(Bollinger(BoDev3a, BoLength3a));
            Add(Bollinger(BoDev1a, BoLength1a));
            Add(Bollinger(BoDev1a, BoLength1a));
            SetProfitTarget(TrgtInDollars);
            SetStopLoss(StpInDollars, false);
            CalculateOnBarClose = true;
        }
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            // Condition set 1
            if (Close[0] > Bollinger(BoDev1a, BoLength1a).Upper[0]
                && Variable0 == 0)
            {
                EnterShort(DefaultQuantity, "Short1");
                Variable0 = 1;
            }
            // Condition set 2
            if (Close[0] > Bollinger(BoDev2a, BoLength2a).Upper[0]
                && Variable0 == 1)
            {
                EnterShort(DefaultQuantity, "Short2");
                Variable0 = 2;
            }
            // Condition set 3
            if (Close[0] > Bollinger(BoDev3a, BoLength3a).Upper[0]
                && Variable0 == 2)
            {
                EnterShort(DefaultQuantity, "Short3");
                Variable0 = 3;
            }
            // Condition set 4
            if (Close[0] < Bollinger(BoDev1a, BoLength1a).Lower[0]
                && Variable0 == 0)
            {
                EnterLong(DefaultQuantity, "Long1");
                Variable0 = 1;
            }
            // Condition set 5
            if (Close[0] < Bollinger(BoDev2a, BoLength2a).Lower[0]
                && Variable0 == 1)
            {
                EnterLong(DefaultQuantity, "Long2");
                Variable0 = 2;
            }
            // Condition set 6
            if (Close[0] < Bollinger(BoDev3a, BoLength3a).Lower[0]
                && Variable0 == 2)
            {
                EnterLong(DefaultQuantity, "Long3");
                Variable0 = 3;
            }
            // Condition set 7
            if (Close[0] < Bollinger(BoDev1a, BoLength1a).Middle[0])
         //       && Position.MarketPosition == MarketPosition.Long)
            {
                ExitLong("", "Long1");
                ExitLong("", "Long2");
                ExitLong("", "Long3");
                Variable0 = 0;
            }
            // Condition set 8
            if (Close[0] > Bollinger(BoDev1a, BoLength1a).Middle[0])
         //       && Position.MarketPosition == MarketPosition.Short)
            {
                ExitShort("", "Short1");
                ExitShort("", "Short2");
                ExitShort("", "short3");
                Variable0 = 0;
            }
        }
        #region Properties
        [Description("")]
        [GridCategory("Parameters")]
        public int BoLength1a
        {
            get { return boLength1a; }
            set { boLength1a = Math.Max(1, value); }
        }
        [Description("")]
        [GridCategory("Parameters")]
        public int BoLength2a
        {
            get { return boLength2a; }
            set { boLength2a = Math.Max(1, value); }
        }
        [Description("")]
        [GridCategory("Parameters")]
        public int BoLength3a
        {
            get { return boLength3a; }
            set { boLength3a = Math.Max(1, value); }
        }
        [Description("")]
        [GridCategory("Parameters")]
        public double BoDev1a
        {
            get { return boDev1a; }
            set { boDev1a = Math.Max(0.00006, value); }
        }
        [Description("")]
        [GridCategory("Parameters")]
        public double BoDev2a
        {
            get { return boDev2a; }
            set { boDev2a = Math.Max(0.00006, value); }
        }
        [Description("")]
        [GridCategory("Parameters")]
        public double BoDev3a
        {
            get { return boDev3a; }
            set { boDev3a = Math.Max(0.00006, value); }
        }
        [Description("")]
        [GridCategory("Parameters")]
        public int TrgtInDollars
        {
            get { return trgtInDollars; }
            set { trgtInDollars = Math.Max(1, value); }
        }
        [Description("")]
        [GridCategory("Parameters")]
        public int StpInDollars
        {
            get { return stpInDollars; }
            set { stpInDollars = Math.Max(1, value); }
        }
        #endregion
    }
}
Last edited by Zigman; 03-30-2012 at 09:35 AM.
Zigman is offline  
Reply With Quote
Old 03-30-2012, 10:18 AM   #2
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
Default

Hello Zigman,
Thanks for your post and I am happy to assist you.

Since you have used the variables as a conditions too, your strategy will depend on the Bollinger order too. Again at the end of the code Variable0 will be reset-ed to zero if the conditions met. You need to filter it further with MarketPosition (which you have commented out).

Can you tell me what result you are getting and what result you expect.

I look forward to assist you further.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 03-30-2012, 12:31 PM   #3
Zigman
Member
 
Join Date: Apr 2009
Location: Toronto ON
Posts: 31
Thanks: 6
Thanked 1 time in 1 post
Red face Still trying

Hi Joydeep,

I tried to modify the code w the martket position commands (see below) but I guess I'm not sure wht you mean exactly. (i.e. still exiting on the next bar every time)
>????

PLEASE SEE ATTACHED SCREEN CAPTURE OF WHAT I'M TRYING TO ACCOMPLISH - HELPS TO CLARIFY A LOT.


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
 
 
 
// posted on forum http://www.ninjatrader.com/support/forum/showthread.php?p=281562#post281562
// posted on forum http://www.ninjatrader.com/support/forum/showthread.php?p=281562#post281562
// posted on forum http://www.ninjatrader.com/support/forum/showthread.php?p=281562#post281562
// posted on forum http://www.ninjatrader.com/support/forum/showthread.php?p=281562#post281562
// posted on forum http://www.ninjatrader.com/support/forum/showthread.php?p=281562#post281562
 
 
 
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Enter the description of your strategy here
/// </summary>
[Description("Enter the description of your strategy here")]
public class MultiFadeAtEachBollingerZiggy3 : Strategy
{
#region Variables
 
private int boLength1a = 100; // Default length for first bolinger band 
private int boLength2a = 100; // Default length for second bollinger band
private int boLength3a = 100; // Default length for third bollinger band
private double boDev1a = 1.500; // Default Std Dev for first bolinger band
private double boDev2a = 2.000; // Default Std Dev for second bolinger band
private double boDev3a = 1.000; // Default Std Dev for third bolinger band
private int trgtInDollars = 2500; // Default setting for profit target in $$
private int stpInDollars = 5000; // Default setting for stop loss in $$
 
#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()
{
Add(Bollinger(BoDev1a, BoLength1a));
Add(Bollinger(BoDev2a, BoLength2a));
Add(Bollinger(BoDev3a, BoLength3a));
Add(Bollinger(BoDev1a, BoLength1a));
Add(Bollinger(BoDev2a, BoLength2a));
Add(Bollinger(BoDev3a, BoLength3a));
Add(Bollinger(BoDev1a, BoLength1a));
Add(Bollinger(BoDev1a, BoLength1a));
SetProfitTarget(TrgtInDollars);
SetStopLoss(StpInDollars, false);
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition set 1
if (Close[0] > Bollinger(BoDev1a, BoLength1a).Upper[0]
&& Variable0 == 0 && Position.MarketPosition == MarketPosition.Flat)
{
EnterShort(DefaultQuantity, "Short1");
Variable0 = 1;
// Position.MarketPosition == MarketPosition.Short;
}
// Condition set 2
if (Close[0] > Bollinger(BoDev2a, BoLength2a).Upper[0]
&& Variable0 == 1 && Position.MarketPosition == MarketPosition.Short)
{
EnterShort(DefaultQuantity, "Short2");
Variable0 = 2;
// Position.MarketPosition == MarketPosition.Short;
}
// Condition set 3
if (Close[0] > Bollinger(BoDev3a, BoLength3a).Upper[0]
&& Variable0 == 2 && Position.MarketPosition == MarketPosition.Short)
{
EnterShort(DefaultQuantity, "Short3");
Variable0 = 3;
// Position.MarketPosition == MarketPosition.Short;
}
// Condition set 4
if (Close[0] < Bollinger(BoDev1a, BoLength1a).Lower[0]
&& Variable0 == 0 && Position.MarketPosition == MarketPosition.Flat)
{
EnterLong(DefaultQuantity, "Long1");
Variable0 = 1;
// Position.MarketPosition == MarketPosition.Long;
}
// Condition set 5
if (Close[0] < Bollinger(BoDev2a, BoLength2a).Lower[0]
&& Variable0 == 1 && Position.MarketPosition == MarketPosition.Long)
{
EnterLong(DefaultQuantity, "Long2");
Variable0 = 2;
// Position.MarketPosition == MarketPosition.Long;
}
// Condition set 6
if (Close[0] < Bollinger(BoDev3a, BoLength3a).Lower[0]
&& Variable0 == 2 && Position.MarketPosition == MarketPosition.Long)
{
EnterLong(DefaultQuantity, "Long3");
Variable0 = 3;
// Position.MarketPosition == MarketPosition.Long;
}
// Condition set 7
if (Close[0] < Bollinger(BoDev1a, BoLength1a).Middle[0]
&& Position.MarketPosition == MarketPosition.Long)
{
ExitLong("", "Long1");
ExitLong("", "Long2");
ExitLong("", "Long3");
Variable0 = 0;
// Position.MarketPosition == MarketPosition.Flat;
}
// Condition set 8
if (Close[0] > Bollinger(BoDev1a, BoLength1a).Middle[0]
&& Position.MarketPosition == MarketPosition.Long)
{
ExitShort("", "Short1");
ExitShort("", "Short2");
ExitShort("", "short3");
Variable0 = 0;
// Position.MarketPosition == MarketPosition.Flat;
}
}
#region Properties
[Description("")]
[GridCategory("Parameters")]
public int BoLength1a
{
get { return boLength1a; }
set { boLength1a = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int BoLength2a
{
get { return boLength2a; }
set { boLength2a = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int BoLength3a
{
get { return boLength3a; }
set { boLength3a = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public double BoDev1a
{
get { return boDev1a; }
set { boDev1a = Math.Max(0.00006, value); }
}
[Description("")]
[GridCategory("Parameters")]
public double BoDev2a
{
get { return boDev2a; }
set { boDev2a = Math.Max(0.00006, value); }
}
[Description("")]
[GridCategory("Parameters")]
public double BoDev3a
{
get { return boDev3a; }
set { boDev3a = Math.Max(0.00006, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int TrgtInDollars
{
get { return trgtInDollars; }
set { trgtInDollars = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int StpInDollars
{
get { return stpInDollars; }
set { stpInDollars = Math.Max(1, value); }
}
#endregion
}
}
Attached Images
File Type: jpg How it should work.JPG (216.6 KB, 14 views)
Last edited by Zigman; 03-30-2012 at 12:33 PM. Reason: more info
Zigman is offline  
Reply With Quote
Old 03-30-2012, 01:10 PM   #4
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
Default

Hello Zigman,
In condition 3, say price crosses above the lowest upper band (BoDev3a = 1). Buy you wont have any order triggered since Variable0 is not equal to 2.

Unless price crosses over the middle and the highest upper band variable0 wont be assigned to 2. You got to rectify it. Unless you set the Bollinger order appropriately you wont get the code to work as per your desire.

Please let me know if I can assist you any further.
NinjaTrader_Joydeep is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Joydeep for this post:
Old 04-02-2012, 11:01 AM   #5
Zigman
Member
 
Join Date: Apr 2009
Location: Toronto ON
Posts: 31
Thanks: 6
Thanked 1 time in 1 post
Red face Thanks!! - I figured it was something simple

Zigman is offline  
Reply With Quote
Reply

Tags
bollinger, not working, problem, strategy analizer, variable0

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Order enters and exits on same bar shadowfax Strategy Development 4 09-13-2011 01:01 PM
Strategy for exits malcolm Automated Trading 1 07-27-2011 09:56 AM
multiple exits in strategy wizard - how? OtterNinja Strategy Development 4 01-17-2011 11:59 AM
RC1 and strategy exits not completing adamus Version 7 Beta General Questions & Bug Reports 9 11-17-2010 10:12 AM
Strategy enters and exits positions 1 bar to late poseidon_sthlm Strategy Development 18 12-23-2009 08:59 AM


All times are GMT -6. The time now is 11:24 PM.