boomscud
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: October 27th, 2022

Add delay to this script before and after an economic annoucement

Hello everyone and thank you for your free help. I was also a helper when I was younger and I know how long it takes. so really thank you :)

Little question. I use alerts and webhooks to automate entry into positions (bot). My bot works fine but I would like to add a nice feature to it.

I discovered this script which displays the important economic events to come (fomc meeting, PPI, CPI, etc.) and therefore it displays the strong volatilities to come which are not often easy to trade.

https://www.tradingview.com/script/HLYD ... -and-more/

I would therefore like to prohibit my bot from taking positions 5 hours before and 5 hours after an event (for example).

But my little pine skills are not enough and I can't create the necessary condition to be added to my trigger with the "and not" argument.

Do you have any advice for me to achieve this?

Thank you in advance for your answers :) and have a nice day

The source code :

Code: Select all

// @version=5
// Β© jdehorty

indicator('Economic Calendar Events', overlay=true, scale=scale.none, max_lines_count=500)

import jdehorty/EconomicCalendar/1 as calendar

// ==================
// ==== Settings ====
// ==================

color1 = color.red
color2 = color.orange
color3 = color.yellow
color4 = color.lime
color5 = color.aqua
color6 = color.fuchsia
color7 = color.silver

show_fomc_meetings = input.bool(defval = true, title = "πŸ“… FOMC", inline = "FOMC", group="βš™οΈ Settings", tooltip="The FOMC meets eight times a year to determine the course of monetary policy. The FOMC's decisions are announced in a press release at 2:15 p.m. ET on the day of the meeting. The press release is followed by a press conference at 2:30 p.m. ET. The FOMC's decisions are based on a review of economic and financial developments and its assessment of the likely effects of these developments on the economic outlook.")
c_fomcMeeting = input.color(color.new(color1, 50), title = "Color", group="βš™οΈ Settings", inline = "FOMC")

show_fomc_minutes = input.bool(defval = true, title = "πŸ“… FOMC Minutes", inline = "FOMCMinutes", group="βš™οΈ Settings", tooltip="The FOMC minutes are released three weeks after each FOMC meeting. The minutes provide a detailed account of the FOMC's discussion of economic and financial developments and its assessment of the likely effects of these developments on the economic outlook.")
c_fomcMinutes = input.color(color.new(color2, 50), title = "Color", group="βš™οΈ Settings", inline = "FOMCMinutes")

show_ppi = input.bool(defval = true, title = "πŸ“… Producer Price Index (PPI)", inline = "PPI", group="βš™οΈ Settings", tooltip="The Producer Price Index (PPI) measures changes in the price level of goods and services sold by domestic producers. The PPI is a weighted average of prices of a basket of goods and services, such as transportation, food, and medical care. The PPI is a leading indicator of CPI.")
c_ppi = input.color(color.new(color3, 50), title = "Color", group="βš™οΈ Settings", inline = "PPI")

show_cpi = input.bool(defval = true, title = "πŸ“… Consumer Price Index (CPI)", inline = "CPI", group="βš™οΈ Settings", tooltip="The Consumer Price Index (CPI) measures changes in the price level of goods and services purchased by households. The CPI is a weighted average of prices of a basket of consumer goods and services, such as transportation, food, and medical care. The CPI-U is the most widely used measure of inflation. The CPI-U is based on a sample of about 87,000 households and measures the change in the cost of a fixed market basket of goods and services purchased by urban consumers.")
c_cpi = input.color(color.new(color4, 50), title = "Color", group="βš™οΈ Settings", inline = "CPI")

show_csi = input.bool(defval = true, title = "πŸ“… Consumer Sentiment Index (CSI)", inline = "CSI", group="βš™οΈ Settings", tooltip="The University of Michigan's Consumer Sentiment Index (CSI) is a measure of consumer attitudes about the economy. The CSI is based on a monthly survey of 500 U.S. households. The index is based on consumers' assessment of present and future economic conditions. The CSI is a leading indicator of consumer spending, which accounts for about two-thirds of U.S. economic activity.")
c_csi = input.color(color.new(color5, 50), title = "Color", group="βš™οΈ Settings", inline = "CSI")

show_cci = input.bool(defval = true, title = "πŸ“… Consumer Confidence Index (CCI)", inline = "CCI", group="βš™οΈ Settings", tooltip="The Conference Board's Consumer Confidence Index (CCI) is a measure of consumer attitudes about the economy. The CCI is based on a monthly survey of 5,000 U.S. households. The index is based on consumers' assessment of present and future economic conditions. The CCI is a leading indicator of consumer spending, which accounts for about two-thirds of U.S. economic activity.")
c_cci = input.color(color.new(color6, 50), title = "Color", group="βš™οΈ Settings", inline = "CCI")

show_nfp = input.bool(defval = true, title = "πŸ“… Non-Farm Payroll (NFP)", inline = "NFP", group="βš™οΈ Settings", tooltip="The Non-Farm Payroll (NFP) is a measure of the change in the number of employed persons, excluding farm workers and government employees. The NFP is a leading indicator of consumer spending, which accounts for about two-thirds of U.S. economic activity.")
c_nfp = input.color(color.new(color7, 50), title = "Color", group="βš™οΈ Settings", inline = "NFP")

show_legend = input.bool(true, "Show Legend", group="βš™οΈ Settings", inline = "Legend", tooltip="Show the color legend for the economic calendar events.")

// =======================
// ==== Dates & Times ====
// =======================

getUnixTime(_eventArr, _index) => 
    switch 
        timeframe.isdaily => array.get(_eventArr, _index) - timeframe.multiplier*86400000 // -n day(s)
        timeframe.isweekly => array.get(_eventArr, _index) - timeframe.multiplier*604800000 // -n week(s)
        timeframe.ismonthly => array.get(_eventArr, _index) - timeframe.multiplier*2592000000 // -n month(s)
        timeframe.isminutes and timeframe.multiplier > 59 => array.get(_eventArr, _index) - timeframe.multiplier*60000 // -n minute(s)
        => array.get(_eventArr, _index)

if barstate.islastconfirmedhistory

    // Note: An offset of -n units is needed to realign events with the timeframe in which they occurred
    if show_fomc_meetings
        fomcMeetingsArr = calendar.fomcMeetings()
        for i = 0 to array.size(fomcMeetingsArr) - 1
            unixTime = getUnixTime(fomcMeetingsArr, i)
            line.new(x1=unixTime, y1=high, x2=unixTime, y2=low, extend=extend.both,color=c_fomcMeeting, width=2, xloc=xloc.bar_time)

    if show_fomc_minutes
        fomcMinutesArr = calendar.fomcMinutes()
        for i = 0 to array.size(fomcMinutesArr) - 1
            unixTime = getUnixTime(fomcMinutesArr, i)
            line.new(x1=unixTime, y1=high, x2=unixTime, y2=low, extend=extend.both,color=c_fomcMinutes, width=2, xloc=xloc.bar_time)

    if show_ppi
        ppiArr = calendar.ppiReleases()
        for i = 0 to array.size(ppiArr) - 1
            unixTime = getUnixTime(ppiArr, i)
            line.new(x1=unixTime, y1=high, x2=unixTime, y2=low, extend=extend.both,color=c_ppi, width=2, xloc=xloc.bar_time)

    if show_cpi
        cpiArr = calendar.cpiReleases()
        for i = 0 to array.size(cpiArr) - 1
            unixTime = getUnixTime(cpiArr, i)
            line.new(x1=unixTime, y1=high, x2=unixTime, y2=low, extend=extend.both,color=c_cpi, width=2, xloc=xloc.bar_time)
    
    if show_csi
        csiArr = calendar.csiReleases()
        for i = 0 to array.size(csiArr) - 1
            unixTime = getUnixTime(csiArr, i)
            line.new(x1=unixTime, y1=high, x2=unixTime, y2=low, extend=extend.both,color=c_csi, width=2, xloc=xloc.bar_time)
    
    if show_cci
        cciArr = calendar.cciReleases()
        for i = 0 to array.size(cciArr) - 1
            unixTime = getUnixTime(cciArr, i)
            line.new(x1=unixTime, y1=high, x2=unixTime, y2=low, extend=extend.both,color=c_cci, width=2, xloc=xloc.bar_time)
    
    if show_nfp
        nfpArr = calendar.nfpReleases()
        for i = 0 to array.size(nfpArr) - 1
            unixTime = getUnixTime(nfpArr, i)
            line.new(x1=unixTime, y1=high, x2=unixTime, y2=low, extend=extend.both,color=c_nfp, width=2, xloc=xloc.bar_time)

// ================
// ==== Legend ====
// ================
if show_legend
    var tbl = table.new(position.top_right, columns=1, rows=8, frame_color=#151715, frame_width=1, border_width=2, border_color=color.new(color.black, 100))
    units = timeframe.isminutes ? "m" : ""
    table.cell(tbl, 0, 0, syminfo.ticker + ' | ' + str.tostring(timeframe.period) + units, text_halign=text.align_center, text_color=color.gray, text_size=size.normal)
    table.cell(tbl, 0, 1, 'FOMC Meeting', text_halign=text.align_center, bgcolor=color.black, text_color=color1, text_size=size.small)
    table.cell(tbl, 0, 2, 'FOMC Minutes', text_halign=text.align_center, bgcolor=color.black, text_color=color2, text_size=size.small)
    table.cell(tbl, 0, 3, 'Producer Price Index (PPI)', text_halign=text.align_center, bgcolor=color.black, text_color=color3, text_size=size.small)
    table.cell(tbl, 0, 4, 'Consumer Price Index (CPI)', text_halign=text.align_center, bgcolor=color.black, text_color=color4, text_size=size.small)
    table.cell(tbl, 0, 5, 'Consumer Sentiment Index (CSI)', text_halign=text.align_center, bgcolor=color.black, text_color=color5, text_size=size.small)
    table.cell(tbl, 0, 6, 'Consumer Confidence Index (CCI)', text_halign=text.align_center, bgcolor=color.black, text_color=color6, text_size=size.small)
    table.cell(tbl, 0, 7, 'Non-Farm Payrolls (NFP)', text_halign=text.align_center, bgcolor=color.black, text_color=color7, text_size=size.small)

boomscud
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: October 27th, 2022

Re: Add delay to this script before and after an economic annoucement

Helo :) Little UP. Nobody can help me ?? :wink:

purplemint22
Pine Script Rookie
Pine Script Rookie
Posts: 17
Joined: October 14th, 2022

Re: Add delay to this script before and after an economic annoucement

Yeah I think your indicator is awesome, but it has gone over my head a little bit. I wish I could help because I would love to have something like this myself as well. I hope you are able to get it worked out in time.

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: Add delay to this script before and after an economic annoucement

Hey,

I went through the script, the calendar gives general dates and I really do not see strong volatilities after these events.
Either way, your question of if we can use these times as non trade times, yes it is very much possible.
You need to use if logic , to check if time is between the plus minus the times , than do not call the order entry line otherwise continue with normal checks and trades.

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: Add delay to this script before and after an economic annoucement

Boomscud,

I hope you were able to work it out, but in case you weren't able to find the answer, I hope this helps.

Assuming unixTime is the time variable for each economic event, you can simply subtract 5hrs of unix time (1000 * 60 * 60 * 5) =18000000 from the unixTime variable and add 18000000 to the unixTime variable to get the range.

The challenge you will have is this Economic Calendar Events indicator cannot send data to your strategy and visa versa. Therefore you would need to incorporate the indicator into your strategy then compare the current time (in unix time) to the event time and if it is up to 5hrs (18000000) before or after the event time then don't trade.

So you could do something like this:
fromTime = unixTime - 18000000 // 5hrs before the calendar event
toTime = unixTime = 18000000 // 5hrs after the calendar event
If time <= fromTime and time >= toTime // Only enter trades when not between the 10hr wilndow of the event
strategy.enter() // your strategy entry statement

Return to β€œPine Script Q&A”