NexusFi: Find Your Edge


Home Menu

 





Certain times excluded from trading


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Big Mike with 4 posts (0 thanks)
    2. looks_two vast with 4 posts (0 thanks)
    3. looks_3 piersh with 1 posts (0 thanks)
    4. looks_4 MXASJ with 1 posts (0 thanks)
    1. trending_up 7,781 views
    2. thumb_up 3 thanks given
    3. group 3 followers
    1. forum 10 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
 
vast's Avatar
 vast 
Australia
 
Experience: Intermediate
Platform: Ninja
Posts: 167 since Jun 2009
Thanks Given: 154
Thanks Received: 62

I am in need again of help. Sorry
I am trying to blank out certain times that I want the strategy to ignore.
The code seems to be ok, but it isn't enforcing it. I have debugged it and the sessions are correct, but for some reason it is not enforcing it on the condition set.
Is there a way of enforcing an open position to exit at 30sec before the session time? I found something on the NInja chat, but couldn't get it to work either.
Any ideas please?

{
// Condition set 1



if (ToTime(Time[0]) >= 083000 && ToTime(Time[0]) < 085700 || (ToTime(Time[0]) >= 090200 && ToTime(Time[0]) <= 092800 ||
(ToTime(Time[0]) >= 093200 && ToTime(Time[0]) < 095800 || (ToTime(Time[0]) >= 100200 && ToTime(Time[0]) <= 102800 ||
(ToTime(Time[0]) >= 103200 && ToTime(Time[0]) < 105800 || (ToTime(Time[0]) >= 110200 && ToTime(Time[0]) <= 115800 ||
(ToTime(Time[0]) >= 120200 && ToTime(Time[0]) <= 125800)))))))
DrawVerticalLine("tag1"+CurrentBar,0,Color.Blue,DashStyle.Dot,3);

{
if (CrossAbove(ECO2NEW(7, 5, 21).Main, EMA(7, 5, 21).Signal, 1)
&& Close[0] > EMA(144)[0]
&& HMA2[0] > HMA2[1]);
{
EnterLong(DefaultQuantity, "LongCross");
}
}


Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Netherlands & Germany Surge as World Cup Field Narro …
Prediction Markets & Event Contracts
Iran Lebanon Problem Kills Switzerland Talks, Brent at $ …
Prediction Markets & Event Contracts
Ninjatrader users - good CONNECTIONs post and article fr …
NinjaTrader
Would a node-based workflow for automated trading be useful?
Traders Hideout
Fed Hike Odds at 57% After Warsh: England Surges 12.9%, …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Want your NinjaTrader indicator created, free?
5 thanks
Afternoon-close session
1 thanks
Franks Trading Journey
1 thanks
Denied 20K payouts: Alpha Futures
1 thanks
FootPrintV2 Chart for NT8
1 thanks
  #2 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 R.I.P. 1977-2026 
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,669 since Jun 2009
Thanks Given: 33,669
Thanks Received: 102,586

I like to write my code so it's easy to follow. I suggest doing that and taking it one step at a time to find the logic error.

 
Code
                            
// do not trade before 4:30am or after 2:30pm
if (ToTime(Time[0]) <= 043000 || ToTime(Time[0]) >= 143000)
return;

// do not trade between 11:30am and 12:30pm
if (ToTime(Time[0]) >= 113000 && ToTime(Time[0]) <= 123000)
return;

// do not trade on Wednesday between 9:25am and 10:15am
if (Time[0].DayOfWeek == DayOfWeek.Friday)
  if (
ToTime(Time[0]) >= 092500 && ToTime(Time[0]) <= 101500)
     return; 
Mike




We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on X Visit my NexusFi Trade Journal Reply With Quote
  #3 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 801


ToTime works on your PC time, right?


Reply With Quote
  #4 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 R.I.P. 1977-2026 
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,669 since Jun 2009
Thanks Given: 33,669
Thanks Received: 102,586


MXASJ View Post
ToTime works on your PC time, right?

ToTime is just a converter to make it easy to test against using a numbers like 080000 for 08:00:00am (hours:minutes:seconds).

Time[0] is the time of bar 0. So it works for historical data (backtesting) as it's tested against the bar time, not against the current PC clock time. In other words, in real live trading, Time[0] will be based on the time of the last close of the bar, and it's the same for historical.

Mike




We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on X Visit my NexusFi Trade Journal Reply With Quote
  #5 (permalink)
 
vast's Avatar
 vast 
Australia
 
Experience: Intermediate
Platform: Ninja
Posts: 167 since Jun 2009
Thanks Given: 154
Thanks Received: 62

Hi Mike
Thanks for the guide. I should do this as it tells me what my code is talking about.
My times seem to be valid. I run the script and the "draw vert lines after the time code draws the lines in all the correct places. What doesn't seem to be happening is the enforcing of it, to the place a trade signal. ie, trades are still being taken in times that I have deemed to be out of bounds.
Any ideas?


Started this thread Reply With Quote
  #6 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 R.I.P. 1977-2026 
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,669 since Jun 2009
Thanks Given: 33,669
Thanks Received: 102,586


vast View Post
Hi Mike
Thanks for the guide. I should do this as it tells me what my code is talking about.
My times seem to be valid. I run the script and the "draw vert lines after the time code draws the lines in all the correct places. What doesn't seem to be happening is the enforcing of it, to the place a trade signal. ie, trades are still being taken in times that I have deemed to be out of bounds.
Any ideas?

You have no code telling it not to trade in these times... your code simply draws a plot...

Mike




We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on X Visit my NexusFi Trade Journal Reply With Quote
  #7 (permalink)
 
vast's Avatar
 vast 
Australia
 
Experience: Intermediate
Platform: Ninja
Posts: 167 since Jun 2009
Thanks Given: 154
Thanks Received: 62

Ok. Well I am giving up.
I thought that using the totime sets the parameters and that the trade entry requirements after it will only execute if the totime parameters are met.
Just goes to show how much I understand C# and the Ninja guides.
Oh well. Another idea wasted. I am wasting so much time trying to get a strategy to work.
I am better to go back to my old ways of manually backtesting. I realise that this is a backwards move, but the wizard and Ninja guides are frustrating me.
Thanks Mike for trying to help.


Started this thread Reply With Quote
  #8 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 R.I.P. 1977-2026 
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,669 since Jun 2009
Thanks Given: 33,669
Thanks Received: 102,586


vast View Post
Ok. Well I am giving up.
I thought that using the totime sets the parameters and that the trade entry requirements after it will only execute if the totime parameters are met.
Just goes to show how much I understand C# and the Ninja guides.
Oh well. Another idea wasted. I am wasting so much time trying to get a strategy to work.
I am better to go back to my old ways of manually backtesting. I realise that this is a backwards move, but the wizard and Ninja guides are frustrating me.
Thanks Mike for trying to help.

Backtesting can be the wrong path. I've blogged about that.

But, if you are giving up because of ToTime() then you are 99.9% there, in this post I already provided an example specifically showing how not to trade. It's called "return;". You can { Drawyourlines(); return; } and that will prevent the script from continuing further and thereby not executing your Enter() command.

Mike




We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on X Visit my NexusFi Trade Journal Reply With Quote
  #9 (permalink)
 
vast's Avatar
 vast 
Australia
 
Experience: Intermediate
Platform: Ninja
Posts: 167 since Jun 2009
Thanks Given: 154
Thanks Received: 62

Thank you Mike. I can't believe that all my problem was a "return;" missing. Aghhh

I promise that I will no longer look at C# programming
I promise that I will no longer look at C# programming
I promise that I will no longer look at C# programming
I promise that I will no longer look at C# programming


Started this thread Reply With Quote
  #10 (permalink)
piersh
California
 
Posts: 87 since Jun 2009
Thanks Given: 5
Thanks Received: 120



Big Mike View Post
ToTime is just a converter to make it easy to test against using a numbers like 080000 for 08:00:00am (hours:minutes:seconds).

Time[0] is the time of bar 0. So it works for historical data (backtesting) as it's tested against the bar time, not against the current PC clock time. In other words, in real live trading, Time[0] will be based on the time of the last close of the bar, and it's the same for historical.

Mike

You can also use DateTime.TimeOfDay:

DateTime timeNow = Time[0].TimeOfDay;
TimeSpan timeStart = new TimeSpan (4,30,0);
TimeSpan timeEnd = new TimeSpan (14,30,0);
if (timeNow <= timeStart || timeNow >= timeEnd)
....


Reply With Quote




Last Updated on August 7, 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