Adrian Stangell
Adrian Stangell

Reputation: 11

box.new horizontal positions in for loop

My script calculates multiple future date ranges from one anchor date. Example: Anchor date is set to 01.01.2023. The distance between the anchor date and the future date (the wavelength) is set to 120 calendar days. The first calculated future date thus is 01.05.2023. Now my script calculates six time segments based upon the calculated future date.

You can choose how often the described process is repeated. The described steps are being calculated for two different wavelengths - e.g. 120 and 50.

I visualize the future date ranges using boxes. Each segment of a calculated date range is visualized by its own box using yloc=yloc.bar_time for the horizontal placement. To better see the different date segments they are colored differently.

Now my problem: When only 1-4 repetitions are chosen the boxes are being displayed correctly. But when I choose 5 or more repetitions the boxes for the longer wavelength are being displaced to the right. Or you could say: The date ranges from the beginning are being omitted while the later ones are displayed correctly (see pictures).

Correct placement using 4 repetitions

Incorrect placement using 5 repetitions Could this have to do with a lookback period that I have to adjust?

Any ideas are welcome!

Here's the script...

//@version=5
indicator('Calendar Days 2', max_labels_count = 500, overlay = true)


// Inputs
anchor_date_DC = input.time(timestamp("01 Jan 2023 05:00"), "Anchor Date Bottom DC")
anchor_date_SC = input.time(timestamp("01 Jan 2023 05:00"), "Anchor Date Bottom SC")
cycle_period_DC = input.int(120, 'Cycle Period DC(Calendar Days)')
cycle_period_SC = input.int(50, 'Cycle Period SC (Calendar Days)')
number_dates = input.int(5, 'Number of Future Dates' )

// Conversions UNIX time
cycle_period_DC_unix = cycle_period_DC * 86400000
cycle_period_SC_unix = cycle_period_SC * 86400000

bottom_date_DC = anchor_date_DC - cycle_period_DC_unix
bottom_date_SC = anchor_date_SC - cycle_period_SC_unix

for i = 1 to number_dates

    // Draw Dominant Cycle (DC)
    bottom_date_DC := bottom_date_DC + cycle_period_DC_unix

    DC_bottom_up_mid = int(bottom_date_DC + (cycle_period_DC_unix/100*12.5))
    DC_bottom_up_end = int(bottom_date_DC + (cycle_period_DC_unix/100*37.5))
    DC_top_down_start = bottom_date_DC + (cycle_period_DC_unix/100*50)
    DC_top_down_mid = int(bottom_date_DC + (cycle_period_DC_unix/100*62.5))
    DC_top_down_end = int(bottom_date_DC + (cycle_period_DC_unix/100*87.5))
    bottom_date_next_DC = bottom_date_DC + cycle_period_DC_unix

    box.new(bottom_date_DC, 10, DC_bottom_up_mid, 19, xloc=xloc.bar_time, border_color=color.new(color.rgb(0,255,8),70), border_width=0,bgcolor= color.new(color.rgb(0, 255, 8),70))    
    box.new(DC_bottom_up_mid, 10, DC_bottom_up_end, 19, xloc=xloc.bar_time, text=str.tostring(cycle_period_DC), border_color=color.new(#a1fbc1, 0), border_width=0, bgcolor=color.new(#a1fbc1, 70))
    box.new(DC_bottom_up_end, 10, DC_top_down_start, 19, xloc=xloc.bar_time, border_color=color.new(color.rgb(0,255,8),70), border_width=0, bgcolor=color.new(color.rgb(0,255,8),70))
    box.new(DC_top_down_start, 10, DC_top_down_mid, 19, xloc=xloc.bar_time, border_color=color.new(color.rgb(255,0,0),70), border_width=0, bgcolor=color.new(color.rgb(255,0,0),70))
    box.new(DC_top_down_mid, 10, DC_top_down_end, 19, xloc=xloc.bar_time, border_color=color.new(#ff7d7d, 70), border_width=0, bgcolor=color.new(#ff7d7d, 70))
    box.new(DC_top_down_end, 10, bottom_date_next_DC, 19, xloc=xloc.bar_time, border_color=color.new(color.rgb(255,0,0),70), border_width=0, bgcolor=color.new(color.rgb(255,0,0),70))

    // Draw Secondary Cycle (SC)
    bottom_date_SC := bottom_date_SC + cycle_period_SC_unix

    SC_bottom_up_start = bottom_date_SC
    SC_bottom_up_mid = int(SC_bottom_up_start + (cycle_period_SC_unix/100*12.5))
    SC_bottom_up_end = int(SC_bottom_up_start + (cycle_period_SC_unix/100*37.5))
    SC_top_down_start = SC_bottom_up_start + (cycle_period_SC_unix/100*50)
    SC_top_down_mid = int(SC_bottom_up_start + (cycle_period_SC_unix/100*62.5))
    SC_top_down_end = int(SC_bottom_up_start + (cycle_period_SC_unix/100*87.5))
    bottom_date_next_SC = bottom_date_SC + cycle_period_SC_unix

    box.new(SC_bottom_up_start, 0, SC_bottom_up_mid, 9, xloc=xloc.bar_time, border_color=color.new(color.rgb(0,255,8),70), border_width=0,bgcolor= color.new(color.rgb(0, 255, 8),70))    
    box.new(SC_bottom_up_mid, 0, SC_bottom_up_end, 9, xloc=xloc.bar_time, text=str.tostring(cycle_period_SC), border_color=color.new(#a1fbc1, 0), border_width=0, bgcolor=color.new(#a1fbc1, 70))
    box.new(SC_bottom_up_end, 0, SC_top_down_start, 9, xloc=xloc.bar_time, border_color=color.new(color.rgb(0,255,8),70), border_width=0, bgcolor=color.new(color.rgb(0,255,8),70))
    box.new(SC_top_down_start, 0, SC_top_down_mid, 9, xloc=xloc.bar_time, border_color=color.new(color.rgb(255,0,0),70), border_width=0, bgcolor=color.new(color.rgb(255,0,0),70))
    box.new(SC_top_down_mid, 0, SC_top_down_end, 9, xloc=xloc.bar_time, border_color=color.new(#ff7d7d,70), border_width=0, bgcolor=color.new(#ff7d7d, 70))
    box.new(SC_top_down_end, 0, bottom_date_next_SC, 9, xloc=xloc.bar_time, border_color=color.new(color.rgb(255,0,0),70), border_width=0, bgcolor=color.new(color.rgb(255,0,0),70))

Upvotes: 0

Views: 33

Answers (1)

Adrian Stangell
Adrian Stangell

Reputation: 11

I guess I figured it out myself: I added max_boxes_count=500 to the top of the script, now everything works as expected.

Upvotes: 0

Related Questions