NexusFi: Find Your Edge


Home Menu

 





Assistance with exiting a trade at a specific time


Discussion in NinjaTrader

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




 
Search this Thread

Assistance with exiting a trade at a specific time

  #1 (permalink)
DirtyGoats
Lakeville
 
Posts: 11 since Dec 2017
Thanks Given: 3
Thanks Received: 1

Evening.

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;

Start_Time = DateTime.Parse("07:00", System.Globalization.CultureInfo.InvariantCulture) ;
End_Time = DateTime.Parse("12:00", System.Globalization.CultureInfo.InvariantCulture) ;
Exit_Trades = DateTime.Parse("14:00", System.Globalization.CultureInfo.InvariantCulture) ;

TradeLong = false;
TradeShort = false;

}

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");
}

if (
Position.MarketPosition == MarketPosition.Short
)
{
ExitShort(1, "eShort", "S");
}
}



}​

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
What broker to use for trading palladium futures
Commodities
Better Renko Gaps
The Elite Circle
Trade idea based off three indicators.
Traders Hideout
How to apply profiles
Traders Hideout
 
  #2 (permalink)
RandomDude
Las Vegas, NV
 
Posts: 66 since Aug 2022
Thanks Given: 2
Thanks Received: 18

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

if (
Time[0].TimeOfDay >= Exit_Trades.TimeOfDay
)
{
ExitLong(1, "eLong", "L");
}

See if it exits your long positions. If not, then probably the time part.

Or maybe because you are specifying 1 in the exit, and not "Contract_Qty" and that's not matching up somehow? Can you just do ExitLong()?

Or maybe

Time[0].TimeOfDay >= Exit_Trades.TimeOfDay should be

Time[0].TimeOfDay >= Exit_Trades.TimeOfDay && Time[1].TimeOfDay < Exit_Trades.TimeOfDay

Probably not the last one.

Reply With Quote
Thanked by:
  #3 (permalink)
DirtyGoats
Lakeville
 
Posts: 11 since Dec 2017
Thanks Given: 3
Thanks Received: 1


Thank you RandomDude. Kinda exactly what I needed to do. Take a step back and isolate where the issue was stemming from.

In my case, not sure why, I discovered (or it appears) that I need to code my Exits as followed: Add a bool for the TradeLong & TradeShort


//Exit Trades at specific time

if ( TradeLong )
{

if (
Time[0].TimeOfDay >= Exit_Trades.TimeOfDay
)
{
if (
Position.MarketPosition == MarketPosition.Long
)
{
ExitLong(Contract_Qty, "eLong", "L");
}

}

}


if ( TradeShort )
{

if (
Time[0].TimeOfDay >= Exit_Trades.TimeOfDay
)
{
if (
Position.MarketPosition == MarketPosition.Short
)
{
ExitShort(Contract_Qty, "eShort", "S");
}
}

}

Reply With Quote




Last Updated on October 23, 2022


© 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