NexusFi: Find Your Edge


Home Menu

 





Function to get Close price for given date and time


Discussion in EasyLanguage Programming

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




 
Search this Thread

Function to get Close price for given date and time

  #1 (permalink)
 
vmodus's Avatar
 vmodus 
Somewhere, Delaware, USA
 
Experience: Intermediate
Platform: MultiCharts
Broker: Barchart.com
Trading: Everything, it all tastes like chicken
Posts: 1,271 since Feb 2017
Thanks Given: 2,958
Thanks Received: 2,853

I am trying to get the close price from a chart for a given time and date. For example:
  • My current chart is NQH19
  • 60 minute chart
  • Get Close from February 20th, 2019 at 1700 (or in EasyLanguage, date = 1190220 and time = 1700)

In one case, I wanted to know the close of a day based on a 60 minute chart (the daily close does not match the 60 minute daily close). I have tried using the function CloseSession(1,1), which would return the close price for one regular session back. However, that sometimes returns a value that is on the weekend, i.e. not a regular session day. I'm not sure if that is a bug or feature, but I dread taking this to TradeStation for support.

So this lead me to writing my own function. I have been trying for the past several days to code a function that will return the closing price for a given date and time. Try as I might, I cannot get it to work. I've tried a number of different ideas, all of which involved using a for loop to work backwards through each bar until I found a date/time match.

Here is my latest iterration, which doesn't work, it just runs through the entire loop, the conditions are never met, and the function returns 0. (I use print statement in an indicator to test it).


 
Code
{ Z_Close_in_Time }

inputs:
   in_date(numeric),
   in_time(numeric)
   ;
   
variables:
  v_time(0),
  v_date(0),
  v_counter(0)  // for bars back
 ;

Condition1 = Date[v_counter] = in_date  ;
Condition2 = Time[v_counter] = in_time  ;
 
For Value1 = 1 to 5000 
Begin 
	v_counter = v_counter + 1 ;
	If condition1 and condition2 
	then
		Begin 
			oClose = Close[v_counter] ;
			Break ;
		End  ;
End ;

Z_Close_in_Time = 1 ;
Can anyone assist? Any help is greatly appreciated.

Follow me on Twitter Visit my NexusFi Trade Journal Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Are there any eval firms that allow you to sink to your …
Traders Hideout
Exit Strategy
NinjaTrader
Futures True Range Report
The Elite Circle
Better Renko Gaps
The Elite Circle
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
39 thanks
NexusFi site changelog and issues/problem reporting
26 thanks
The Program
18 thanks
GFIs1 1 DAX trade per day journal
18 thanks
  #3 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,431 since Apr 2013
Thanks Given: 481
Thanks Received: 1,623


vmodus,

the best approach depends a bit on how often you need to find a value and if you know the next time and date in advance. The latter would allow you to simply store the value at that date and time and you wouldn't have to loop through the previous bars.

You could also store the values in a dictionary and look them up using BarDateTime.ToString() as the key.

Regards,

ABCTG

Follow me on Twitter Reply With Quote
  #4 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,431 since Apr 2013
Thanks Given: 481
Thanks Received: 1,623

Hi vmodus,

for your specific function code the conditions might not be able to ever become true as you check them outside the loop. Put them within the loop and reset v_counter to 0 in front of the loop.
Using a loop here might create problems with the Maximum Number of Bars a study references i.e. the number of bars your code requires here might become unnecessarily high (as TS could theoretically require 5000 bars on the chart before it can return a value):
Maximum Number of Bars Study Will Reference Setting

That's why one of the approaches mentioned in my other post might be better suited here (although the loop will work).

Regards,

ABCTG

Follow me on Twitter Reply With Quote
  #5 (permalink)
 
vmodus's Avatar
 vmodus 
Somewhere, Delaware, USA
 
Experience: Intermediate
Platform: MultiCharts
Broker: Barchart.com
Trading: Everything, it all tastes like chicken
Posts: 1,271 since Feb 2017
Thanks Given: 2,958
Thanks Received: 2,853


ABCTG View Post
Using a loop here might create problems with the Maximum Number of Bars a study references i.e. the number of bars your code requires here might become unnecessarily high (as TS could theoretically require 5000 bars on the chart before it can return a value):

Thanks for the tips and the condition outside of the loop makes sense (*duh*). I generally only need to go back 50-60 bars, give or take. I put the 5000 in to see how far it would run.

I'll let you know how this works and I'll publish the function if I actually get it to work.

Follow me on Twitter Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #6 (permalink)
 
vmodus's Avatar
 vmodus 
Somewhere, Delaware, USA
 
Experience: Intermediate
Platform: MultiCharts
Broker: Barchart.com
Trading: Everything, it all tastes like chicken
Posts: 1,271 since Feb 2017
Thanks Given: 2,958
Thanks Received: 2,853

I think I figured this out. Since this is just development code, I named the function Z_Close_in_Time. If you use it, call it whatever you want.

Considerations:
  • If a value does not exist on a given date/time, then the function will return zero (0);
  • You may get a maxbarsback error if your in_bars_back value is too large; just adjust on your indicator, etc.;
  • Use this code at your own risk (it works for what I need);

 
Code
{
   Z_Close_in_Time
   The purpose of this function is to find a the close price for a given 
   time and date.  This is designed for intraday charts, but could probably be 
   used on daily or weekly charts.
   
   Note: You may need to increase maxbarsback 
}

inputs:
   in_date(numeric), 
   in_time(numeric)   ,
   in_bars_back(numeric),  // How many bars back should we go before stopping?
   oClose(numericref)  // This is the value that gets assigned to oClose output variable
   ;
   
variables:
  v_time(0),
  v_date(0),
  v_counter(0)  // for bars back
 ;

v_counter = 0 ;

 For Value1 = 1 to in_bars_back 
Begin 
	v_counter = v_counter + 1 ;
	Condition1 = Date[v_counter] = in_date  ;
	Condition2 = Time[v_counter] = in_time  ;

	If condition1 and condition2 
	then
		Begin 
			oClose = Close[v_counter] ;
			Break ;
		End  ;
End ;

Z_Close_in_Time = 1 ;

Here is how you would might use it in an indicator, strategy, etc. :

 
Code
variables:
   in_date (0), 
   in_time(0),
   in_max_bars_back(1),
   oClose(0),   // this variable stores the output of the function, i.e. the close
   my_close(0) 
; 


in_date = 1190101 ; 
in_time = 1200 ; 
in_max_bars_back = 50 ;

// This calls the function and returns the value to oClose
Value1 = Z_Close_in_Time (in_date , in_time, in_max_bars_back, oClose) ;
Thanks to ABCTG for helping me puzzle this out!

Follow me on Twitter Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #7 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,431 since Apr 2013
Thanks Given: 481
Thanks Received: 1,623

Hi vmodus,

you are welcome. One idea about a change might be to either set v_counter to -1 in front of the loop or to place the line v_counter = v_counter + 1 ; after condition2 within the loop. This way your function would also be able to detect the Close when in_date and in_time fall on the current bar i.e. are at Date[0] and Time[0]. At the moment the function would start with checking the values for the bar one ago.

Regards,

ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #8 (permalink)
 
vmodus's Avatar
 vmodus 
Somewhere, Delaware, USA
 
Experience: Intermediate
Platform: MultiCharts
Broker: Barchart.com
Trading: Everything, it all tastes like chicken
Posts: 1,271 since Feb 2017
Thanks Given: 2,958
Thanks Received: 2,853

Good idea. I have noticed that on a live chart, the values returned for today are not correct. My indicator calls a function that determines the highest and lowest of three days. That function calls the function above to get that specific date in time.

I'm currently learning how to use and manipulate ELDate. I'm also going to dig into the Global Dictionary, as you have recommended in other posts.

Thanks!

Follow me on Twitter Visit my NexusFi Trade Journal Started this thread Reply With Quote




Last Updated on March 6, 2019


© 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