Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
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 for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Assistance with exiting a trade at a specific time
In the below code my strategy enters trades within the given window of time while my exit code is not. I'd like my trades to exit at 2pm yet when I Optimize/Backtest a strategy the code will exit at 9am / 9:05am / 10am / 12:01pm / 5pm / 5:31pm....pretty much everything BUT 2pm. Can someone please point out what i'm doing incorrect?
namespace NinjaTrader.NinjaScript.Strategies
{
public class Test : Strategy
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "Test ";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = false;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
OrderFillResolution = OrderFillResolution.Standard; Slippage = 2;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 1;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
}
}
protected override void OnBarUpdate()
{
if (
(Time[0].TimeOfDay >= Start_Time.TimeOfDay)
&& (Time[0].TimeOfDay <= End_Time.TimeOfDay)
)
{
// Look for Long Entries
if ( TradeLong )
{
EnterLong(Contract_Qty,@"L");
}
// Look for Short Entries
if ( TradeShort )
{
EnterShort(Contract_Qty,@"S");
}
}
//Exit Trades at specific time
if (
Time[0].TimeOfDay >= Exit_Trades.TimeOfDay
)
{
if (
Position.MarketPosition == MarketPosition.Long
)
{
ExitLong(1, "eLong", "L");
}
I don't really know about ninjatrader programming, just throwing out ideas here before competent help arrives.
If entering during the correct time, then maybe the problem is with the market position part. Do you have to specify the market position? Thinking maybe can test by doing something like