Skip to content

Commit aea314d

Browse files
Merge pull request #2527 from SyedImtiyaz-1/tradingView
Added Trading view Python Script
2 parents c01c03f + e58664e commit aea314d

File tree

13 files changed

+2925
-0
lines changed

13 files changed

+2925
-0
lines changed

TradingView/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 David Arnold
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

TradingView/NOTES.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Publishing a Release
2+
====================
3+
4+
Check the setup.py version.
5+
Commit and push everything.
6+
Activate the 3.6 venv (to get twine, wheel, etc)
7+
Run: rm -rf dist
8+
Run: python setup.py sdist
9+
Run: python setup.py bdist_wheel --universal
10+
Run: twine upload dist/*
11+
Run: git tag v$VERSION
12+
Run: git push --tags
13+
Go to GitHub and make a release
14+
Bump the setup.py version for next time, and commit.
15+
16+
Control Protocol
17+
================
18+
19+
The control protocol is used between the language APIs (including the
20+
CLI) and the agent. It enables the API clients to control the agent,
21+
creating and using client (initiator) and server (responder) FIX peers.
22+
23+
The agent is controled via one or more TCP sessions. The message
24+
format used over this TCP session is as follows:
25+
26+
Each message consists of a framing header and a payload.
27+
28+
The framing header is a 4 byte big-endian integer number of bytes in
29+
the payload. That is, the number of bytes following the header in this
30+
message. The total TCP payload length will be this value, plus four.
31+
32+
The payload is JSON. Each message must contain:
33+
* the "type" field, a string, being the type of this message
34+
* the "name" field, a string, identifying the entity to which the message
35+
applies
36+
37+
Response messages, returned from the agent to the APIs will also contain:
38+
* the "result" field, a boolean, indicating the success (or failure) of
39+
their matching request.
40+
* the "message" field, a string, describing the error if there was one.
41+
42+
FIX messages are transported as a JSON string. JSON requires strings to
43+
be valid UTF8, and a FIX message is not that, so they're encoded using
44+
BASE64 before being sent.
45+
46+
47+
So, applications should pass FIX messages to the language APIs as a
48+
formatted byte array.

TradingView/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
=======
3+
Trading View
4+
=======
5+
6+
| |PyPI| |Python|
7+
8+
Introduction
9+
============
10+
11+
This utility provides a means of establishing a connection, using the
12+
FIX protocol, with an existing FIX application. The connection is made
13+
from a background agent process, but controlled by either a command-line
14+
tool, or a programming language API.
15+
16+
It is intended for use in either ad-hoc FIX testing (using the command-line)
17+
or for integration testing of FIX applications, where it can be configured
18+
to simulate the intended FIX peer. To automate testing, an existing
19+
language unit testing framework is helpful: test cases can be written to
20+
exercise your code, and interleaved with that you can drive the FIX peer
21+
to confirm receipt of appropriate messages from your application, and to
22+
craft responses (both correct and incorrect) to implement your testing
23+
scenarios.
24+
25+
The agent process communicates with the command-line tool and programming
26+
language APIs using a TCP session. The protocol is simple, and uses
27+
JSON-formatted messages.
28+
29+
Language SDKs are planned for Python, Java, DotNET, and possibly Go and
30+
C/C++. The command-line client enables use from shell scripts or ad-hoc
31+
use from a shell session.
32+
33+
Caveats
34+
=======
35+
36+
This project is very young. It's nowhere near finished. It probably
37+
doesn't do what you need yet.
38+
39+
Contributing
40+
============
41+
42+
Comments, suggestions, bug reports, bug fixes -- all contributions to
43+
this project are welcomed. See the project's `GitHub
44+
<https://github.com/da4089/fixtool>`_ page for access to the latest
45+
source code, and please open an `issue
46+
<https://github.com/da4089/fixtool/issues>`_ for comments,
47+
suggestions, and bugs.
48+
49+
50+
51+
.. |Build Status| image:: https://travis-ci.org/da4089/fixtool.svg?branch=master
52+
:target: https://travis-ci.org/da4089/fixtool
53+
:alt: Build status
54+
.. |Docs| image:: https://readthedocs.org/projects/fixtool/badge/?version=latest
55+
:target: http://fixtool.readthedocs.io/en/latest/
56+
:alt: Docs
57+
.. |Code Health| image:: https://api.codacy.com/project/badge/Grade/abd5c37cfe834d5ca5edb74853223986
58+
:target: https://app.codacy.com/app/da4089/fixtool/dashboard
59+
:alt: Code Health
60+
.. |Coverage| image:: https://api.codacy.com/project/badge/Coverage/abd5c37cfe834d5ca5edb74853223986
61+
:target: https://app.codacy.com/app/da4089/fixtool/dashboard
62+
:alt: Coverage
63+
.. |PyPI| image:: https://img.shields.io/pypi/v/fixtool.svg
64+
:target: https://pypi.python.org/pypi/fixtool
65+
:alt: PyPI
66+
.. |Python| image:: https://img.shields.io/pypi/pyversions/fixtool.svg
67+
:target: https://pypi.python.org/pypi/fixtool
68+
:alt: Python
69+
.. |Landscape| image:: https://landscape.io/github/da4089/fixtool/master/landscape.svg?style=flat
70+
:target: https://landscape.io/github/da4089/fixtool/master
71+
:alt: Code Health
72+
.. |Coveralls| image:: https://coveralls.io/repos/github/da4089/fixtool/badge.svg?branch=master
73+
:target: https://coveralls.io/github/da4089/fixtool?branch=master
74+
:alt: Coverage

TradingView/cli/fixtool

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#! /usr/bin/env python3
2+
##################################################################
3+
# fixtool
4+
# Copyright (C) 2017-2018, David Arnold.
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a
7+
# copy of this software and associated documentation files (the "Software"),
8+
# to deal in the Software without restriction, including without limitation
9+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
# and/or sell copies of the Software, and to permit persons to whom the
11+
# Software is furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in
14+
# all copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22+
# DEALINGS IN THE SOFTWARE.
23+
#
24+
##################################################################
25+
26+
"""CLI for controlling fixtool FIX protocol simulator."""
27+
28+
import fixtool
29+
30+
31+
def main():
32+
"""Main function for CLI."""
33+
34+
# FIXME: use similar requests as rnps FIX module?
35+
# FIXME: use asyncio? cjson over TCP?
36+
# FIXME: use type annotations?
37+
38+
return
39+
40+
41+
if __name__ == "__main__":
42+
main()
43+
44+
45+
##################################################################

TradingView/fixtool-agent

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#! /bin/bash
2+
##################################################################
3+
# fixtool
4+
# Copyright (C) 2018, David Arnold.
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a
7+
# copy of this software and associated documentation files (the "Software"),
8+
# to deal in the Software without restriction, including without limitation
9+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
# and/or sell copies of the Software, and to permit persons to whom the
11+
# Software is furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in
14+
# all copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22+
# DEALINGS IN THE SOFTWARE.
23+
#
24+
##################################################################
25+
26+
# Find source directory of this script
27+
SOURCE="${BASH_SOURCE[0]}"
28+
# Resolve $SOURCE until the file is no longer a symlink
29+
while [ -h "$SOURCE" ]; do
30+
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
31+
SOURCE="$(readlink "$SOURCE")"
32+
# If $SOURCE was a relative symlink, we need to resolve it relative
33+
# to the path where the symlink file was located
34+
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
35+
done
36+
ROOT="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
37+
38+
export PYTHONPATH=$ROOT/python
39+
exec python3 $ROOT/python/fixtool/agent.py $*
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#! /usr/bin/env python3
2+
##################################################################
3+
# fixtool
4+
# Copyright (C) 2017-2018, David Arnold.
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a
7+
# copy of this software and associated documentation files (the "Software"),
8+
# to deal in the Software without restriction, including without limitation
9+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
# and/or sell copies of the Software, and to permit persons to whom the
11+
# Software is furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in
14+
# all copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22+
# DEALINGS IN THE SOFTWARE.
23+
#
24+
##################################################################
25+
26+
"""FIX protocol simulator and control APIs."""
27+
28+
import logging
29+
import os
30+
import stat
31+
from .proxy import FixToolProxy
32+
from .version import VERSION
33+
34+
35+
def spawn_agent():
36+
"""Create a new agent, and associated proxy.
37+
38+
:returns: Reference to proxy, or None on error."""
39+
40+
# Spawned agents are spawned from the calling process, and usually
41+
# dedicated to it. It's possible to contact a spawned agent from
42+
# an independent process, but that's not the intended use case.
43+
#
44+
# When the agent starts, it is allocated an ephemeral port number.
45+
# That port number is reported on stdout from the process, and
46+
# can be read by the proxy which subsequently connects to it on
47+
# that port.
48+
#
49+
# The agent can be explicitly shutdown at any time by the proxy,
50+
# but will also be implicitly killed if the proxy instance is
51+
# deleted.
52+
53+
# Look for fixtool-agent in PATH, then parent dirs of $CWD
54+
path = os.getenv("PATH", "")
55+
path_dirs = path.split(os.pathsep)
56+
cwd = os.path.realpath(os.curdir)
57+
while cwd != "/":
58+
path_dirs.append(cwd)
59+
cwd = os.path.dirname(cwd)
60+
61+
fixtool_agent = None
62+
for dir_name in path_dirs:
63+
file_name = os.path.join(dir_name, "fixtool-agent")
64+
if os.path.exists(file_name):
65+
mode = os.stat(file_name).st_mode
66+
if mode & stat.S_IXUSR or \
67+
mode & stat.S_IXGRP or \
68+
mode & stat.S_IXOTH:
69+
fixtool_agent = file_name
70+
break
71+
72+
if not fixtool_agent:
73+
logging.error("Unable to find agent executable")
74+
return None
75+
76+
logging.info("Using agent: %s", fixtool_agent)
77+
78+
agent = os.popen(fixtool_agent + ' start')
79+
status = agent.readline()
80+
agent.close() # Just the parent; the forked agent is still running
81+
82+
logging.info("Agent output: %s", status)
83+
84+
if status[:2] != "OK":
85+
logging.error("Failed to start agent: %s", status)
86+
return None
87+
88+
try:
89+
port = int(status[3:])
90+
except ValueError:
91+
logging.error("Unable to read port number from agent output")
92+
return None
93+
94+
agent_proxy = FixToolProxy("localhost", port)
95+
return agent_proxy
96+
97+
98+
def connect_agent(host: str, port: int):
99+
"""Create a proxy, and connect it to an existing agent.
100+
101+
:param host: String host name or IP address for the agent.
102+
:param port: Integer TCP port number for the agent."""
103+
104+
agent_proxy = FixToolProxy(host, port)
105+
return agent_proxy

0 commit comments

Comments
 (0)