View Full Version : Is This Possible?
yimbot
11-04-2007, 05:26 PM
I currently use a strategy that has discretionary entries (at least I can't imaging a way to code it...) but has purely mechanical exits based on indicators. As my exits have always been a weak point for me, I have found much greater success in using mechanical exits and stick to my entries which are quite good.
What I would like to build is a strategy that I can enter manually, but when I do, my position size is automatically calculated for me and entered and my exit is automatically taken care of also.
Does this sound like a pipe dream???
yimbot
11-04-2007, 05:28 PM
Does anybody offer a coding service that could handle this type of thing??
NinjaTrader_Ray
11-04-2007, 05:31 PM
No, this is not a dream however, this is not possible at the current time. This is on our list for future enhancement though.
yimbot
11-04-2007, 05:51 PM
Fantastic, can't wait!
gert74
11-05-2007, 07:19 PM
If you know how to code your exits already, it is not that difficult to work around.
Instead of an automated entry, you could simply tell the strategy what position to start out at when you put it on the chart.
Let the strategy enter that position somewhere on historical data and it will simply perform a simulated entry, and from then on you can let it manage your real position.
Just take care to test what happens when you reload strategies on the chart. I always find this to be the time when most things potentially break, as a lot of internal variables get reset.
yimbot
11-05-2007, 08:42 PM
Thanks Gert74.
Sounds great, I could definitely work with that. I am not really sure how to set it up though, could you provide a little more info?
NinjaTrader_Josh
11-06-2007, 12:19 AM
I think what gert74 means is just have the strategy start up with an open strategy position that would match your manually entered position. The strategy would then proceed in all its calculations and exit the position whenever it deems necessary. When the strategy exits it will exit your real position too.
TraderGuy
11-08-2007, 01:00 PM
just have the strategy start up with an open strategy position
I'm interested in this as well, and was going to start a thread with the same question. I often enter positions manually and then would like the position "monitored" and exited when conditions are met.
What is a reasonable way to have the strategy start up with an open position that matches the actual account open position? Can the strategy be "seeded" with this information via user parameters? If not how can this be accomplished within NT?
Thanks,
Guy
NinjaTrader_Ray
11-08-2007, 01:30 PM
This could be done however, there is not a simple approach. We would need to give this some thought on how a strategy could be coded and then deployed. Will add this to my list of reference samples. This will not happen in the short term.
gert74
11-09-2007, 01:55 AM
Fundamentally, all you need are two parameters. One for the market position (using the built in MarketPosition enum data type will give you a nifty little dropdown) and quantity (your basic integer).
If you're a lazy programmer (like me), you could simply use a negative quantity for short positions, and then you would need only one parameter.
Now, your strategy will need to enter that position at some point before it can exit it again. What we want is for the strategy to enter the position somewhere on the historical part of the data series (so that it will be a simulated entry) and exit in realtime (to trigger a live order to the broker).
In theory you could enter at any point like the first bar of the chart. However, if you want you could add another parameter with a time or price point to enter at if you want to make it resemble your real entry for some reason.
That's the basic principle of it.
Just code your strategy to only look for your exit signal on realtime data, and you're good to go.
yimbot
11-10-2007, 08:18 PM
Fundamentally, all you need are two parameters. One for the market position (using the built in MarketPosition enum data type will give you a nifty little dropdown) and quantity (your basic integer).
If you're a lazy programmer (like me), you could simply use a negative quantity for short positions, and then you would need only one parameter.
Now, your strategy will need to enter that position at some point before it can exit it again. What we want is for the strategy to enter the position somewhere on the historical part of the data series (so that it will be a simulated entry) and exit in realtime (to trigger a live order to the broker).
In theory you could enter at any point like the first bar of the chart. However, if you want you could add another parameter with a time or price point to enter at if you want to make it resemble your real entry for some reason.
That's the basic principle of it.
Just code your strategy to only look for your exit signal on realtime data, and you're good to go.
Thanks for all your feedback everyone, I appreciate the help.
I have had an attempt at creating a strategy, but it does not quite work as intended. Would anyone mind commenting on the 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.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Executes a Long trade with donchian Channel Exits
/// </summary>
[Description("Executes a Long trade with donchian Channel Exits")]
[Gui.Design.DisplayName("Long Trade")]
public class LongTrade : Strategy
{
#region Variables
// Wizard generated variables
private int donchianPeriod = 24; // Default setting for DonchianPeriod
private int quantity = 5; // Default setting for Quantity
// User defined variables (add any user defined variables below)
#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()
{
CalculateOnBarClose = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition set 1
if (Position.MarketPosition == MarketPosition.Long)
{
EnterLong(Quantity, "");
}
// Condition set 2
if (GetCurrentBid() < DonchianChannel(DonchianPeriod).Lower[0])
{
ExitLong("", "");
}
}
#region Properties
[Description("Donchian Channel Periods")]
[Category("Parameters")]
public int DonchianPeriod
{
get { return donchianPeriod; }
set { donchianPeriod = Math.Max(1, value); }
}
[Description("Quantity to buy")]
[Category("Parameters")]
public int Quantity
{
get { return quantity; }
set { quantity = Math.Max(1, value); }
}
#endregion
}
}
NinjaTrader_Josh
11-10-2007, 11:07 PM
#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>
/// Executes a Long trade with donchian Channel Exits
/// </summary>
[Description("Executes a Long trade with donchian Channel Exits")]
[Gui.Design.DisplayName("Long Trade")]
public class LongTrade : Strategy
{
#region Variables
// Wizard generated variables
private int donchianPeriod = 24; // Default setting for DonchianPeriod
private int quantity = 5; // Default setting for Quantity
private bool simEntry = false;
// User defined variables (add any user defined variables below)
#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()
{
CalculateOnBarClose = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition set 1
if (Position.MarketPosition == MarketPosition.Long) <--this won't work in entering you your simulated position because Position is your strategy position and not your account position
{
EnterLong(Quantity, "");
}
// try this instead
if (Historical && simEntry == false)
{
EnterLong(Quantity, "");
simEntry = true;
}
// Condition set 2
if (GetCurrentBid() < DonchianChannel(DonchianPeriod).Lower[0])
{
ExitLong("", "");
}
}
#region Properties
[Description("Donchian Channel Periods")]
[Category("Parameters")]
public int DonchianPeriod
{
get { return donchianPeriod; }
set { donchianPeriod = Math.Max(1, value); }
}
[Description("Quantity to buy")]
[Category("Parameters")]
public int Quantity
{
get { return quantity; }
set { quantity = Math.Max(1, value); }
}
#endregion
}
}
yimbot
11-11-2007, 03:05 AM
Perfect!! Thank you so much. I could have wasted days trying to figure that out...
NinjaTrader_Josh
11-11-2007, 03:07 AM
Great. Glad it worked out for you.
yimbot
11-11-2007, 04:24 AM
Copied and modified the code for a short trade, but it does not exit the trade... any ideas?
#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>
/// Exits Trade on Donchian Cross
/// </summary>
[Description("Exits Trade on Donchian Cross")]
[Gui.Design.DisplayName("Short Donchian")]
public class ShortDonchian : Strategy
{
#region Variables
// Wizard generated variables
private int donchianPeriod = 24; // Default setting for DonchianPeriod
private int quantity = 5; // Default setting for Quantity
private bool simEntry = false;
// User defined variables (add any user defined variables below)
#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()
{
CalculateOnBarClose = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition set 1
if (Historical && simEntry == false)
{
EnterShort(Quantity, "");
simEntry = true;
}
// Condition set 2
if (GetCurrentBid() > DonchianChannel(DonchianPeriod).Upper[0])
{
ExitShort("", "");
}
}
NinjaTrader_Ray
11-11-2007, 09:23 AM
If it does not exit that is likely due to exit conditions never being met. You would need to debug the strategy. Here is some helpful information on debugging.
http://www.ninjatrader-support.com/vb/showthread.php?t=3418
yimbot
11-24-2007, 02:55 PM
Hi Ray/Josh
Thanks for your help, but I am still having some issues with this strategy. I am using the one as posted in Josh's reply and initially it seemed to work but that I cannot seem to get it to run correct now.
The strategy does not open a position. It is as if it runs through the code first and the command simEntry=true under condition set 2 is executed first ...??
If I remove that line, the strategy does open a position, but then of course it just loops into buy/sell continuously. If I put that ine back in under condition set 2, it never opens a position...
Any ideas what would cause this behaviour?
NinjaTrader_Josh
11-24-2007, 04:37 PM
Are you talking about real-time or backtesting?
The check against Historical means it will only show up on backtests. Please use Print functions throughout the code to see what value simEntry is set to at various steps of your logic. This will help you determine why it is opening/not opening your position.
yimbot
11-24-2007, 10:58 PM
Are you talking about real-time or backtesting?
I am trying it with the simuated data feed.
NinjaTrader_Josh
11-24-2007, 11:58 PM
I'm guessing you start with a completely blank chart when you start up the Simulated Data Feed then.
The problem is as stated in my previous post. The check against Historical will prevent anything from happening if all the bars on the chart were generated in real-time (sim feed is same as real-time).
Alternatively you could probably try something like
if (CurrentBar == 1)
EnterLong(Quantity, "");