NexusFi: Find Your Edge


Home Menu

 





NinjaTrader scripting language: last month's friday


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Astrogirl with 2 posts (0 thanks)
    2. looks_two shodson with 1 posts (4 thanks)
    3. looks_3 Elite Trader with 1 posts (0 thanks)
    4. looks_4 sam028 with 1 posts (0 thanks)
    1. trending_up 2,647 views
    2. thumb_up 4 thanks given
    3. group 4 followers
    1. forum 4 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
Astrogirl
Milan/Italy
 
Posts: 51 since Oct 2013
Thanks Given: 25
Thanks Received: 12

An hard question about ninjascript. How can I translate, in NinjaTrader code, the following condition:

If it is the last Friday of each month, enter long position.

I really appreciate any help.


Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Thursday May 28: GDP + Core PCE + Jobless Claims All at …
Traders Hideout
Iran Airspace Collapses 18 Points to 15.5% While Hormuz …
Prediction Markets & Event Contracts
Hormuz Surges From 14% to 26.5% Intraday as Irans Answer …
Prediction Markets & Event Contracts
Warsh Confirmed 54-45 on PPI Day -- 97% Say He Holds in …
Prediction Markets & Event Contracts
Khamenei Vetoes Uranium Transfer as Peace Odds Surge to …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
NexusFi site changelog and issues/problem reporting
13 thanks
Darmok and Jalad at Tanagra
3 thanks
Big Mike in Ecuador
1 thanks
30 Sessions
1 thanks
  #2 (permalink)
 
Elite Trader's Avatar
 Elite Trader 
Pune India
 
Experience: Beginner
Platform: NinjaTrader
Trading: Stocks
Posts: 28 since Apr 2015
Thanks Given: 56
Thanks Received: 15


Astrogirl View Post
An hard question about ninjascript; how can I translate in code the following condition:

If it is the last friday of any month, enter long position.

I really appreciate any help.

Hi Astrogirl,

For this condition you can write a method to check every last friday's date of the month.

Example : 08/28/2015 this date is the last friday of this month and 09/25/2015 is the last friday of the next month, In this way you can add the date of the last fridays of the month for the one year two years and so on....

public bool CheckLastFriday(DateTime barTime)
{
if ( ToDay(barTime) == 20150828 || ToDay(barTime) == 20150925 || ......)
{

return true;

}
else
{
return false;
}
}


if ( CheckLastFriday(Time[0]) == true )
{

EnterLong();

}



Elite Trader.


Reply With Quote
  #3 (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


Assumes you are running on daily bars. Enters at the open of the last Friday of the month, exits the next Monday morning.

 
Code
protected override void OnBarUpdate()
{
	DateTime tomorrow = Time[0].Date.AddDays(1);
	
	// if tomorrow is a Friday...
	if (tomorrow.DayOfWeek == DayOfWeek.Friday)	
	{
		// if next week is not the same month as tomorrow, then tomorrow is the last friday of the month
		if (tomorrow.Month != tomorrow.AddDays(7).Month)
			EnterLong();
	}
	// if long, exit
	else if (Position.MarketPosition == MarketPosition.Long)
		ExitLong();
}


Follow me on X Visit my NexusFi Trade Journal Reply With Quote
  #4 (permalink)
Astrogirl
Milan/Italy
 
Posts: 51 since Oct 2013
Thanks Given: 25
Thanks Received: 12


Elite Trader View Post
Hi Astrogirl,

For this condition you can write a method to check every last friday's date of the month.

Example : 08/28/2015 this date is the last friday of this month and 09/25/2015 is the last friday of the next month, In this way you can add the date of the last fridays of the month for the one year two years and so on....


Elite Trader.

Thanks Elite Trader, your help is very useful. However I would need to find a logic that allow me to avoid entering each single Friday manually in the code as my script works on several years!


Reply With Quote
  #5 (permalink)
 
sam028's Avatar
 sam028 
Site Moderator
 
Posts: 3,759 since Jun 2009
Thanks Given: 3,828
Thanks Received: 4,647


Astrogirl View Post
Thanks Elite Trader, your help is very useful. However I would need to find a logic that allow me to avoid entering each single Friday manually in the code as my script works on several years!

Check @shodson reply, he gives the right piece of code for the next billion years.


Success requires no deodorant! (Sun Tzu)
Reply With Quote




Last Updated on August 3, 2015


© 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