NexusFi: Find Your Edge


Home Menu

 





Timed exit in auto strategy


Discussion in NinjaTrader

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




 
Search this Thread
  #1 (permalink)
 
nanobiotech's Avatar
 nanobiotech 
Brisbane, Australia
 
Experience: Intermediate
Platform: NinjaTrader
Broker: Mirus/Zen-Fire
Trading: CL
Posts: 670 since Jun 2009
Thanks Given: 134
Thanks Received: 430

I know you can set the Session begin and end time in Chart properties - is this a reliable way to limit the time an auto strategy runs?

Also, is there a practical way to code this in the strategy? Say to limit the time the strategy runs for 2 hours or a time variable only?

Nano


Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
[WTS] ATAS Lifetime ULTRA license – EU buyers only – …
Platforms and Indicators
AI for Traders — Agents, Memory, Tools & Real-Worl …
Elite Quantitative GenAI/LLM
What we know about Binances $6.3bn hack - BBC News
Cryptocurrency
No book, only a stair: journaling fills where price move …
Cryptocurrency
requesting .nrd replay files from 2026 spy and qqq
The Elite Circle
 
  #2 (permalink)
 
shodson's Avatar
 shodson 
OC, California, USA
 
Experience: Advanced
Platform: IB/TWS, NinjaTrader, ToS
Broker: IB, ToS, Kinetick
Trading: stocks, options, futures, VIX
Posts: 1,972 since Jun 2009
Thanks Given: 534
Thanks Received: 3,720


nanobiotech View Post
I know you can set the Session begin and end time in Chart properties - is this a reliable way to limit the time an auto strategy runs?

Also, is there a practical way to code this in the strategy? Say to limit the time the strategy runs for 2 hours or a time variable only?

Nano

Sure. Here's how the OnBarUpdate() of all of my strats look like

 
Code
                            
protected override void OnBarUpdate()
{
    if (
TimeToTrade)
    {
        if (
InPosition)
            
ManageTrade();
    
        if (!
InPosition)
            
LookForTrade();
    }
    else
    {
        if (
InPosition)
            
FlattenPosition();
    }
}

.........


private 
bool TimeToTrade
{
    
get
    
{
        if (
_tradeTimeBegin == 0 || _tradeTimeEnd == 0)
            return 
true;
        else
        {
            if (
_tradeTimeBegin > _tradeTimeEnd)    // sessions that begin previous day and end next day
              
return ToTime(Time[0]) >= _tradeTimeBegin || ToTime(Time[0]) <= _tradeTimeEnd;
            else
              return 
ToTime(Time[0]) >= _tradeTimeBegin && ToTime(Time[0]) <= _tradeTimeEnd;
        }
    }
}    

...............

private 
int _tradeTimeBegin = 0; // Default setting for TTB
[Description("Beginning trading time")]
[
Category("Parameters")]
public 
int TTB
{
    
get { return _tradeTimeBegin; }
    
set { _tradeTimeBegin = Math.Max(0, value); }
}

private 
int _tradeTimeEnd = 0; // Default setting for TT
[Description("Ending trading time")]
[
Category("Parameters")]
public 
int TTE
{
    
get { return _tradeTimeEnd; }
    
set { _tradeTimeEnd = Math.Max(0, value); }
} 
So when I run/backtest the strat I set the TTB and TTE to the times I want trading to begin/end, regardless of the session time as defined by Ninja because often you have indicators that rely on previous bars of data that are outside of your trading range.


Follow me on X Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #3 (permalink)
 
nanobiotech's Avatar
 nanobiotech 
Brisbane, Australia
 
Experience: Intermediate
Platform: NinjaTrader
Broker: Mirus/Zen-Fire
Trading: CL
Posts: 670 since Jun 2009
Thanks Given: 134
Thanks Received: 430


Thanks Shodson, I appreciate your help. I'll give this a go.
Nano


Started this thread Reply With Quote




Last Updated on December 28, 2009


© 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