forexfactory calendar downloader - Matlab, R project and Python | futures io social day trading
futures io futures trading


forexfactory calendar downloader
Updated: Views / Replies:14,309 / 13
Created: by enjoyaol Attachments:3

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 100,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 3  
 
Thread Tools Search this Thread
 

forexfactory calendar downloader

  #11 (permalink)
Stuttgart, Germany
 
Trading Experience: Master
Platform: Tradestation, Jigsaw
Broker/Data: TS Brokerage / InteractiveBrokers / Rithmic
Favorite Futures: ES, TF, NQ, Stocks
 
Posts: 20 since May 2010
Thanks: 23 given, 18 received

i had some issues with the "actual" and found another (working) script by accident...

https://gist.github.com/pohzipohzi/ad7942fc5545675022c1f31123e64c0c

 
Code
from bs4 import BeautifulSoup
import requests
import datetime
import logging
import csv

def setLogger():
    logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(levelname)s - %(message)s',
                    filename='logs_file',
                    filemode='w')
    console = logging.StreamHandler()
    formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
    console.setFormatter(formatter)
    logging.getLogger('').addHandler(console)

def getEconomicCalendar(startlink,endlink):

    # write to console current status
    logging.info("Scraping data for link: {}".format(startlink))

    # get the page and make the soup
    baseURL = "https://www.forexfactory.com/"
    r = requests.get(baseURL + startlink)
    data = r.text
    soup = BeautifulSoup(data, "lxml")

    # get and parse table data, ignoring details and graph
    table = soup.find("table", class_="calendar__table")

    # do not use the ".calendar__row--grey" css selector (reserved for historical data)
    trs = table.select("tr.calendar__row.calendar_row")
    fields = ["date","time","currency","impact","event","actual","forecast","previous"]

    # some rows do not have a date (cells merged)
    curr_year = startlink[-4:]
    curr_date = ""
    curr_time = ""
    for tr in trs:

        # fields may mess up sometimes, see Tue Sep 25 2:45AM French Consumer Spending
        # in that case we append to errors.csv the date time where the error is
        try:
            for field in fields:
                data = tr.select("td.calendar__cell.calendar__{}.{}".format(field,field))[0]
                # print(data)
                if field=="date" and data.text.strip()!="":
                    curr_date = data.text.strip()
                elif field=="time" and data.text.strip()!="":
                    # time is sometimes "All Day" or "Day X" (eg. WEF Annual Meetings)
                    if data.text.strip().find("Day")!=-1:
                        curr_time = "12:00am"
                    else:
                        curr_time = data.text.strip()
                elif field=="currency":
                    currency = data.text.strip()
                elif field=="impact":
                    # when impact says "Non-Economic" on mouseover, the relevant
                    # class name is "Holiday", thus we do not use the classname
                    impact = data.find("span")["title"]
                elif field=="event":
                    event = data.text.strip()
                elif field=="actual":
                    actual = data.text.strip()
                elif field=="forecast":
                    forecast = data.text.strip()
                elif field=="previous":
                    previous = data.text.strip()

            dt = datetime.datetime.strptime(",".join([curr_year,curr_date,curr_time]),
                                            "%Y,%a%b %d,%I:%M%p")
            print(",".join([str(dt),currency,impact,event,actual,forecast,previous]))
        except:
            with open("errors.csv","a") as f:
                csv.writer(f).writerow([curr_year,curr_date,curr_time])

    # exit recursion when last available link has reached
    if startlink==endlink:
        logging.info("Successfully retrieved data")
        return

    # get the link for the next week and follow
    follow = soup.select("a.calendar__pagination.calendar__pagination--next.next")
    follow = follow[0]["href"]
    getEconomicCalendar(follow,endlink)

if __name__ == "__main__":
    """
    Run this using the command "python `script_name`.py >> `output_name`.csv"
    """
    setLogger()
    getEconomicCalendar("calendar.php?week=jan7.2007","calendar.php?week=dec24.2017")

Reply With Quote
 
  #12 (permalink)
Salt Lake City, Utah
 
Trading Experience: Beginner
Platform: Jigsaw, NinjaTrader8
Favorite Futures: ZB
 
Posts: 477 since Apr 2016
Thanks: 15 given, 441 received

That's funny. I searched and searched for something like this and couldn't find it. Now after I've already written my own code I see this thread.

https://github.com/twdsje/FinanceBot/blob/master/financebot.go

The code did the parsing part. The next bit was to get the bot to poll forexfactory at the right time and report the results. However, I put that part on the shelf after I found some twitter bots. Seeing how that goes before I go to finishing it.

Reply With Quote
 
  #13 (permalink)
Manchester, UK
 
 
Posts: 1 since Jul 2017
Thanks: 0 given, 0 received


I've been using the script corvin wrote for the past year, works perfect on a linux system and then using grep to filter what you want out.

It's written in Python2

I can't post URL's so search Corvin Codirla, he has a link to the script on his website.

Reply With Quote
 
  #14 (permalink)
Suffield, CT
 
Trading Experience: Advanced
Platform: MC, NT, Python, R
Broker/Data: IB, Oanda, IQFeed
Favorite Futures: ES, NQ, YM, CL, RB, 6E
 
Fu510n's Avatar
 
Posts: 104 since Oct 2009
Thanks: 774 given, 97 received

Tweaked for Python3


Jens89 View Post
hi,

do you have an alternative for python 3? I see that mechanize is only available for python2. it'd be awesome if you could change it for python3 as I'm not that good of a python programmer yet!

thanks


I just saw your request as I went back to grab the code I tweaked previously - as I've mostly been using Python 3 of late, I updated the code to work with Python 3 now (embedded below). Primarily just had to swap out the cookielib module with http.cookiejar and update print() statements to the Python 3 syntax variant.

-Guy

 
Code
#!/usr/bin/env python

from __future__ import unicode_literals
import sys
import datetime
import getopt
# import codecs
import pprint
import lxml.html
import mechanize
#import cookielib
import http.cookiejar

# some utils
pp = pprint.PrettyPrinter()
debug = 0


#########################
# variables
#########################
START_YEAR = datetime.datetime.now().year
END_YEAR = START_YEAR
WEEKURL = r"http://www.forexfactory.com/calendar.php?week="
MONTHURL = r"http://www.forexfactory.com/calendar.php?month="
#OUTFILE = r"events.csv"
USAGE = "ffcal.py <-h> <-f {filename}> <-w {this|next|mmmdd.yyyy}> <-m {this|next|mmm.yyyy}>\n"
#########################


# our month list for the URL
monthslist = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]

# sets up the browser
br = mechanize.Browser()
#cj = cookielib.LWPCookieJar()
cj = http.cookiejar.LWPCookieJar()
br.set_cookiejar(cj)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

# set correct timezone
br.open("http://www.forexfactory.com/timezone.php")
formindex = 0
for form in br.forms():
    if "timezone.php" in form.action:
        form["timezoneoffset"] = ["0"]
        break
    formindex += 1

br.select_form(nr=formindex)
# br.submit()


def getData(html, outfile):
    """
    Gets data from one page of events
    """
    root = lxml.html.fromstring(html)
    #lines = root.find_class("calendar__row calendar_row calendar__row--grey")
    #if not lines:
    lines = root.find_class("calendar__row calendar_row")

    # curWeekDay = None
    curMonthDay = None
    time = curTime = ""
    # pp.pprint(lines)
    for event in lines:
        # pp.pprint(event)
        if len(event.xpath("td[@class='calendar__cell calendar__date date']")) > 0:
            date = event.xpath("td[@class='calendar__cell calendar__date date']")[0]
        else:
            sys.exit("BOOM")

        # get the day of the month
        weekDay = date.xpath("span")
        monthDay = date.xpath("span/span")
        if len(weekDay) > 0:
            # curWeekDay = weekDay[0].text
            # print "curWeekDay=[" + curWeekDay + "]"
            curMonthDay = monthDay[0].text
            if debug:
                print( "curMonthDay=[", curMonthDay, "]" )

        # get the time
        curTime = time
        time = event.xpath("td[contains(@class, 'calendar__time')]")[0].text if len(event.xpath("td[contains(@class, 'calendar__time')]")) else ""
        if time == '' or time == None:
            time = curTime
        if debug:
            print( "time=[", str(time), "]" )

        # get currency
        currency = event.xpath("td[contains(@class, 'calendar__currency')]")[0].text if len(event.xpath("td[contains(@class, 'calendar__currency')]")) else ""
        currency = currency.strip( "\n" )
        if currency == None:
            continue
        if debug:
            print( "currency=[", currency, "]" )

        # get impact
        impact = event.xpath("td[contains(@class, 'calendar__impact')]/div/span/@title")[0] if len(event.xpath("td[contains(@class, 'calendar__impact')]/div/span/@title")) else ""
        if debug:
            print( "impact=[", impact, "]" )

        # get name of event
        nevent = event.xpath("td[contains(@class, 'calendar__event')]/div/span")[0].text if len(event.xpath("td[contains(@class, 'calendar__event')]/div/span")) else ""
        if debug:
            print( "nevent=[", nevent, "]" )

        # get actual
        actual = event.xpath("td[contains(@class, 'calendar__actual')]/span")[0].text if len(event.xpath("td[contains(@class, 'calendar__actual')]/span")) else ""

        # retry if actual is in a span (can happen if they colorize it)
        # if actual is None or len(actual.strip()) == 0:
        #     actual = event.xpath("td[@class='actual']/span")[0].text if len(event.xpath("td[@class='actual']/span")) else ""
        actual = actual.strip().replace("\n", " ") if actual is not None else ""
        if debug:
            print( "actual=[", actual, "]" )

        # get forecast
        forecast = event.xpath("td[contains(@class, 'calendar__forecast')]")[0].text if len(event.xpath("td[contains(@class, 'calendar__forecast')]")) else ""
        # retry if forecast is in a span (can happen if they colorize it)
        # if forecast is None or len(forecast.strip()) == 0:
        #    forecast = event.xpath("td[@class='forecast']/span")[0].text if len(event.xpath("td[@class='forecast']/span")) else ""
        forecast = forecast.strip().replace("\n", " ") if forecast is not None else ""
        if debug:
            print( "forecast=[", forecast, "]" )

        # get previous
        previous = event.xpath("td[contains(@class, 'calendar__previous')]")[0].text if len(event.xpath("td[contains(@class, 'calendar__previous')]")) else ""
        # retry if previous is in a span (can happen if they colorize it)
        if previous is None or len(previous.strip()) == 0:
            previous = event.xpath("td[contains(@class, 'calendar__previous')]/span")[0].text if len(event.xpath("td[contains(@class, 'calendar__previous')]/span")) else ""
        previous = previous.strip().replace("\n", " ") if previous is not None else ""
        if debug:
            print( "previous=[", previous, "]\n" )

        outfile.write("{};{};{};{};{};{};{};{}\n".format(curMonthDay, time, currency, impact, nevent, actual, forecast, previous))


OUTFILE = ""

try:
    opts, args = getopt.getopt(sys.argv[1:], "f:hm:w:")
except getopt.GetoptError:
    sys.stderr.write(USAGE)
    sys.exit(2)

for opt, arg in opts:

    if opt == "-h":
        sys.stderr.write(USAGE)
        sys.exit()

    if opt == "-f":
        OUTFILE = arg
    elif opt == "-w" or opt == "-m":
        outfile = open(OUTFILE, "w") if OUTFILE != "" else sys.stdout
        if opt == "-w":
            url = "{}{}".format(WEEKURL, arg)
        else:
            url = "{}{}".format(MONTHURL, arg)
        sys.stderr.write("Getting {} from {}\n".format(arg, url))
        br.open(url)
        html = br.response().read()
        getData(html, outfile)
        if outfile is not sys.stdout:
            outfile.close()
        sys.exit()

year = START_YEAR
outfile = open(OUTFILE, "w") if OUTFILE != "" else sys.stdout
while year <= END_YEAR:
    for month in monthslist:
        url = "{}{}.{}".format(MONTHURL, month, year)
        sys.stderr.write("Getting {} {} from {}\n".format(month.title(), year, url))
        br.open(url)
        html = br.response().read()
        getData(html, outfile)
    year += 1
if outfile is not sys.stdout:
    outfile.close()

Follow me on Twitter Visit my Facebook Reply With Quote

Reply



futures io > > > > forexfactory calendar downloader

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)
 

futures io is celebrating 10-years w/ over $18,000 in prizes!

Right now
 

$1,000 in Amazon Gift Cards being given away right now from GFF Brokers

Right now
 

$250 Amazon Gift Cards with our "Thanks Contest" challenge!

Right now
 

Show us your trading desks and win over $5,000 in prizes w/Jigsaw Trading

August
 

Webinar: Chart Patterns and Practical Trading Stats w/Suri Duddella

Elite only
 

Webinar: Richard Bailey (TBA)

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Economic Events Downloader (news) Nicolas11 Platforms and Indicators 27 June 2nd, 2018 10:16 AM
chard downloader NT7 snusnufreak NinjaTrader 3 December 10th, 2013 04:02 PM
Market Analyzer as a Historical Downloader? LostTrader NinjaTrader 2 February 17th, 2013 09:49 PM
NT7 excel downloader tinkerz NinjaTrader 0 February 16th, 2013 04:11 PM
Data Downloader StockJock AmiBroker 22 April 8th, 2012 01:44 PM


All times are GMT -4. The time now is 11:27 AM. (this page content is cached, log in for real-time version)

Copyright © 2019 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts