NexusFi: Find Your Edge


Home Menu

 





Python / Sierra Chart modules


Discussion in Sierra Chart

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




 
Search this Thread

Python / Sierra Chart modules

  #1 (permalink)
 kiwi 
Gold Coast
 
Experience: Advanced
Platform: Sierra Chart
Trading: Futures and Forex
Posts: 49 since Aug 2010
Thanks Given: 23
Thanks Received: 96

Here's a little module that will convert all text files in a directory to scid files in the same or another directory. If the constant KEEP_UPDATING is True it will then check the text files every REFRESH seconds and if they've changed it will update the scid files.

Set FILES_IN and FILES_OUT. In windows the format is "C:\\mydir\\mysubdir\\"

Also set Timezone if you wish to change the times in Sierra's scid files (Sierra saves them in the UTC time zone).

Save the code in a file named something like Text2Scid.py and run it using python Text2SCid.py.
Tested with Python 3.7 on Linux & Windows. Have fun:

 
Code
"""
Convert's Text files to SCID files for SierraChart
It will create a new scid file for every text file it finds
  format is assumed to be D T O H L C V T with comma separated variables
It will continue to update the scid file every REFRESH seconds
  generally refresh would be set to 1/2 the time period for the text file updates
"""

import os
import struct
from time import sleep
from datetime import datetime as dt, timedelta as tdelta
import subprocess

# these are linux format, windows might need to use double backslashes \\
FILES_IN = "/home/john/Python/Trading/textfiles/"
FILES_OUT = "/home/john/zRamdisk/SierraData/"
TIMEZONE = 0  # scid saved as UTC, use this if the text file is another time zone
KEEP_UPDATING = True  # set to False to convert and stop; True for live updating
REFRESH = 0.25  # refresh every 1/4 second for 1/2 second updating text files
TEXT_FILE_SUFFIX = ".txt"  # usually .txt or .csv

sizeHeader = 0x38
sizeRecord = 0x28
file_header = (b'SCID8\x00\x00\x00\x28\x00\x00\x00\x01\x00\x00\x00\x00\x00' +
               b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' +
               b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' +
               b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')


def excel_date(datetime, offset=TIMEZONE):
    """convert date&time with offset in hours to excel format for scid"""
    delta = datetime - dt(1899, 12, 30) + tdelta(hours=offset)
    return delta.days + delta.seconds / 86400


def python_date(exceldt, offset=TIMEZONE):
    """convert date&time with offset in hours to python format"""
    excel_as_delta = tdelta(days=exceldt//1) + tdelta(seconds=exceldt%1*86400)
    return dt(1899, 12, 30) + excel_as_delta + tdelta(hours=offset)

def write_struct(record, f):
    if len(record) == 8 and record[0][0].isnumeric():
        out = [excel_date(dt.strptime(f'{record[0]} {record[1]}', '%Y-%m-%d %H:%M:%S.%f'))]
        out.extend([float(s) for s in record[2:]])
        # note that SC packs dt, O, H, L, C, T, V not V, T so swap them
        wt = struct.pack('d4f4I', out[0], out[1], out[2], out[3], out[4], int(out[6]), int(out[5]), 0, 0)
        f.write(wt)


def create_base(f):
    record = None
    with open(FILES_OUT + f[: -3] +'scid', "wb") as scidfile:
        scidfile.write(file_header)
        with open(FILES_IN + f, "r") as txtfile:
            for line in txtfile:
                record = line.split(',')
                write_struct(record, scidfile)
    return f'{record[0]} {record[1]}'  # will update when date+time changes


def update_if_changed(files):
    for f in files:
        with open(FILES_IN + f, "r") as txtfile:
            record = txtfile.readlines()[-1].split(',')
            last_dt = f'{record[0]} {record[1]}'
            if files[f] == last_dt:
                continue
            files[f] = last_dt
            with open(FILES_OUT + f[: -3] +'scid', "ab") as scidfile:
                write_struct(record, scidfile)


if __name__ == '__main__':
    files = sorted(k for k in os.listdir(path=FILES_IN) if k[-4:] == TEXT_FILE_SUFFIX)
    files = {k: '' for k in files}
    for f in files:
        files[f] = create_base(f)
    print('finished base')
    if KEEP_UPDATING:
        # now test each input file each REFRESH interval & update if datetime changed
        while True:
            sleep(REFRESH)
            update_if_changed(files)

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Trade idea based off three indicators.
Traders Hideout
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
REcommedations for programming help
Sierra Chart
Cheap historycal L1 data for stocks
Stocks and ETFs
Quantum physics & Trading dynamics
The Elite Circle
 
  #2 (permalink)
asterix
PORTLAND
 
Posts: 2 since Mar 2012
Thanks Given: 1
Thanks Received: 0

hello Kiwi
I saw your code on sierracharts forums (and then here) (the one which reads scid file into pd df) but it keeps crashing for me, and as I am relatively new to programming I could not yet figure out why. May be you have a newer version , you could share?
I am also running sierra charts on linux.

Reply With Quote
  #3 (permalink)
Dayvid23
Salt lake city utah
 
Posts: 15 since Dec 2014
Thanks Given: 4
Thanks Received: 13


same here. keeps crashing

Reply With Quote
  #4 (permalink)
asterix
PORTLAND
 
Posts: 2 since Mar 2012
Thanks Given: 1
Thanks Received: 0

I figured out a "workaround", probably not as fast as the SCID file. I used a study which is writing the bar+study data to txt file. I can read that into a dataframe and work with that. It takes ~0.01 sec to update and return a dataframe at each update. That is sufficient for now.

Reply With Quote




Last Updated on February 25, 2020


© 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