Custom Column to calculate Open value (1 min) at 0930


Category:
0
0

Dear Sir, I have a very quick question regarding my code below. I tried to calculate the first available Open (1 minute) value from 0930. However my code will sometimes generate error (NaN or Infinity) when the Open value is not available at 0930.

 

Specifically:

  • NaN
    • Value @ 0930 not available
    • Immediate previous value is within IsToday (i.e. Today 0928)
  • Infinity
    • Value @ 0930 not available
    • Immediate previous value is not within IsToday, but yesterday (i.e. Yesterday 1559)

 

It will be really great if can advice me on how can I correct my code. Thank you so much sir!!

 


 

# ————————————————-

# Define Time System

 

def isToday = if ( getDay() == getLastDay() ) then 1 else 0;

 

# ———————————————

# Definition: Candlestick

 

def C_1m = CLOSE(period = AggregationPeriod.MIN);

def O_1m = OPEN(period = AggregationPeriod.MIN);

def H_1m = HIGH(period = AggregationPeriod.MIN);

def L_1m = LOW(period = AggregationPeriod.MIN);

def V_1m = VOLUME(period = AggregationPeriod.MIN);

 

# ————————————————-

 

def Time_Start = 0930;

def Time___End = 0959;

 

# ————————————————-

# Calculate : RT Open

 

def RT_Open_Price =

# Not yet reaching RT_StartTime (0930), therefore value not available.

if

(

SecondsFromTime(Time_Start) < 0

and

isToday

)

then

Double.NaN

else

# Time equals RT_StartTime (0930). Set initial value.

if

(

SecondsFromTime(Time_Start) == 0 and

isToday

)

then

O_1m

else

# Time equals RT_StartTime (0930), however no value is available because no trade

# happens at RT_StartTime. Go to next time interval and use that value as initial value.

if

(

SecondsFromTime(Time_Start) >= 0 and

SecondsTillTime(Time___End) >= 0 and

isToday

and

isNaN(RT_Open_Price[1]) == 1

and

isNaN(O_1m) != 1

)

then

O_1m

else

RT_Open_Price[1];

Marked as spam
Posted by (Questions: 1, Answers: 2)
Asked on June 4, 2019 10:46 pm
207 views
0
Private answer

I can't really do anything with your code. It has a degree of complexity that far exceeds the intended task. This makes the code extremely difficult to read. So I would start over from scratch, if I could understand exactly what it is you are trying to accomplish.

One thing about your code I will mention. The following statements are completely unnecessary:

def C_1m = CLOSE(period = AggregationPeriod.MIN);
def O_1m = OPEN(period = AggregationPeriod.MIN);
def H_1m = HIGH(period = AggregationPeriod.MIN);
def L_1m = LOW(period = AggregationPeriod.MIN);
def V_1m = VOLUME(period = AggregationPeriod.MIN);

Why?

Because the only place you can use these statements is on a chart that is set to a 1 min time frame. Understand that? When your code says "period = AggregationPeriod.MIN", this code can only run on the 1 min time frame. Any other time frame applied to the chart will generate errors. In case you are asking why. It is because you cannot read data from a lower time frame than the chart is set to. You can only read data from a higher time frame, or the time frame the chart is set to.

Which means all of those lines I listed above are best written as:

def c_1m = close;
def o_1m = open;
def h_1m = high;
def l_1m = low;
def v_1m = volume;

But that still does nothing to answer your question.

It seems that you are trying to read the opening price of the first candle of the regular trading hours. There is a very simple solution to that. So the complexity of your code leads me to believe you are trying to achieve something entirely different. But I don't know what that is. Perhaps you can clarify things, and provide a screenshot showing what values you are trying to plot on a chart.

If you really are trying to read the value of the first opening bar on the chart. That is very simply done like this:

def regularSessionOpen = open(period = AggregationPeriod.DAY);

This reads the daily data and reports the opening bar of the current day. If the markets have not yet opened for regular session hours, you will get N/A. But you can work around this, depending on what you are doing with this value. Until I know that, I cannot provide any further solution.

Marked as spam
Posted by (Questions: 37, Answers: 4086)
Answered on June 5, 2019 12:20 pm