Skip to content

Commit

Permalink
fix webull TickerId error
Browse files Browse the repository at this point in the history
  • Loading branch information
imvinaypatil committed Jan 17, 2023
1 parent 27c713d commit 8e337cb
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 41 deletions.
6 changes: 5 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
about integration with broker and data streams (currently integrates with zerodha [*]_).
Kinetick is aimed to make systematic trading available for everyone.

Leave the heavy lifting to kinetick so that you can focus on building strategies.
Leave the heavy lifting to kinetick and you focus on building strategies.

WARNING

This project is still in its early stages, please be cautious when dealing with real money.

`Changelog » <./CHANGELOG.rst>`_

Expand Down
2 changes: 1 addition & 1 deletion blotter.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ENV dbport=27017
ENV dbuser=kinetick
ENV dbpassword=kinetick
ENV dbname=kinetick
ENV dbskip=false
ENV dbskip=true
ENV LOGLEVEL=INFO

CMD [ "python", "-m", "kinetick.factory.blotter" ]
35 changes: 35 additions & 0 deletions blotter.arm64.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
FROM python:3.7-slim

WORKDIR /app
ADD ./requirements.txt /app/requirements.txt
ADD ./constraints.txt /app/constraints.txt

RUN apt-get update && apt-get install -y wget && apt-get install -y build-essential && apt-get install -y git
# TA-Lib
RUN wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz && \
tar -xvzf ta-lib-0.4.0-src.tar.gz && \
cd ta-lib/ && \
./configure --prefix=/usr --build=aarch64-unknown-linux-gnu && \
make && \
make install

RUN python -m pip install --upgrade pip && \
pip install -r requirements.txt -c constraints.txt --no-cache-dir

RUN apt-get autoremove --purge --yes build-essential

ADD . /app

ENV PYTHONUNBUFFERED=1
ENV dbport=27017
ENV orderbook=true
ENV threads=2
ENV dbhost='127.0.0.1'
ENV dbport=27017
ENV dbuser=kinetick
ENV dbpassword=kinetick
ENV dbname=kinetick
ENV dbskip=true
ENV LOGLEVEL=INFO

CMD [ "python", "-m", "kinetick.factory.blotter" ]
2 changes: 1 addition & 1 deletion constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ pymongo>=3.11.3
mongoengine>=0.23.0
pip>=21.2.4
wheel~=0.34.2
cryptography~=2.9.2
py~=1.8.1
lxml~=4.5.0
setuptools~=41.2.0
TA-Lib>=0.4.18
python-telegram-bot==12.7
paho-mqtt>=1.5.1
webull-12==2023.0.2
2 changes: 1 addition & 1 deletion kinetick/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

__version__ = '1.0.14'
__version__ = '1.0.15'
__author__ = 'vin8tech'

import os
Expand Down
2 changes: 1 addition & 1 deletion kinetick/blotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ def register(self, instruments):
db.to_csv(self.args['symbols'], header=True, index=False)
chmod(self.args['symbols'])
except Exception as e:
self.log_blotter.error("Skipping symbols file since it couldn't be found in the system", e)
self.log_blotter.warning("Skipping symbols file since it couldn't be found in the system")

# -------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion kinetick/lib/brokers/webull/webull_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(self, paper=False):
self.password = ""
self.paper = paper
if not paper:
self.wb = wb()
self.wb = wb(region_code=12)
else:
self.wb = paper_webull()

Expand Down
21 changes: 15 additions & 6 deletions kinetick/risk_assessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from decimal import Decimal
from datetime import datetime
from math import floor

Expand Down Expand Up @@ -77,14 +78,22 @@ def reset(self, update=None, ctx=None, **kwargs):
def _should_trade(self, entry_price, stop_loss, quantity=None):
spread = abs(entry_price - stop_loss)
spread = 5 * round(spread / 5, 2)
maxsize = floor(max(1, int(self.risk_per_trade / spread)))
maxsize = min(maxsize, int(self.capital / entry_price))
_quantity = quantity or maxsize
margin = _quantity * spread
margin = 0
_quantity = 0
maxsize = 0

if Decimal(spread) > Decimal(0.0):
maxsize = floor(max(1, int(self.risk_per_trade / spread)))
maxsize = min(maxsize, int(self.capital / entry_price))
_quantity = quantity or maxsize
margin = _quantity * spread

should_trade = True
reason = None

if self.available_margin <= 0:
if Decimal(spread) <= Decimal(0.0):
should_trade = False
reason = "Stoploss spread results in negative"
elif self.available_margin <= 0:
should_trade = False
reason = "Insufficient margin"
elif margin >= self.available_margin:
Expand Down
57 changes: 34 additions & 23 deletions kinetick/strategies/buy_low_sell_high.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,16 @@ class BuyLowSellHigh(Algo):
buy low sell high algorithm
"""

def init_instruments(self, instruments=None):
if instruments is None:
instruments = self.instruments
for instrument in instruments:
if not self.backtest:
self.sync_bars(instrument)

def on_start(self):
""" initialize your strategy. Perform any operation that are required to be run before starting algo."""
pass
self.init_instruments()

# ---------------------------------------
def on_quote(self, instrument):
Expand All @@ -62,28 +69,32 @@ def on_tick(self, instrument):

# ---------------------------------------
def on_bar(self, instrument):
bars = instrument.get_bars(lookback=3)
if instrument.position is None:
""" check if there any open positions on the instrument."""
if len(bars) >= 2: # at least 2 bars are needed
if bars['close'][-1] < bars['close'][0]:
position = instrument.create_position(entry_price=bars['close'][-1],
stop_loss=bars['low'][-1],
quantity=1, pos_type=PositionType.MIS)
""" Create position instance with inputs.
If quantity is not provided then it will be automatically calculated based on risk parameters.
"""
instrument.open_position(position)
""" instrument.open_position() will send order request to bot and
attach the position instance to instrument to indicate about open position.
when order request is accepted from bot the position becomes active.
"""
elif instrument.position.active:
""" check IF the position was executed in bot """
if len(bars) >= 2:
if bars['close'][-1] > bars['close'][0]:
instrument.close_position(instrument.position)
""" close the position. """
try:
bars = instrument.get_bars(lookback=3)
self.log.info(bars)
if instrument.position is None:
""" check if there any open positions on the instrument."""
if len(bars) >= 2: # at least 2 bars are needed
if bars['close'][-1] < bars['close'][0]:
position = instrument.create_position(entry_price=bars['close'][-1],
stop_loss=bars['low'][-1],
quantity=1, pos_type=PositionType.MIS)
""" Create position instance with inputs.
If quantity is not provided then it will be automatically calculated based on risk parameters.
"""
instrument.open_position(position)
""" instrument.open_position() will send order request to bot and
attach the position instance to instrument to indicate about open position.
when order request is accepted from bot the position becomes active.
"""
elif instrument.position.active:
""" check IF the position was executed in bot """
if len(bars) >= 2:
if bars['close'][-1] > bars['close'][0]:
instrument.close_position(instrument.position)
""" close the position. """
except Exception as e:
self.log.warn(e)

# --------------------------------------------

Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ pymongo
mongoengine
pip
wheel
cryptography
py
lxml
setuptools
TA-Lib
python-telegram-bot
paho-mqtt
git+https://github.com/imvinaypatil/webull.git
webull-12
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

setup(
name='kinetick',
version='1.0.14',
version='1.0.15',
description='Simplifying Trading',
long_description=long_description,
url='https://github.com/imvinaypatil/kinetick',
Expand Down Expand Up @@ -48,11 +48,11 @@
packages=find_packages(exclude=['contrib', 'docs', 'tests', 'demo', 'demos', 'examples']),
install_requires=[
'python-dateutil>=2.5.3',
'numpy>=1.11.1', 'pandas>=0.22.0',
'numpy==1.21.2', 'pandas==1.3.2',
'pytz>=2016.6.1', 'requests>=2.10.0', 'pyzmq>=15.2.1',
'mongoengine>=0.20.0',
'python-telegram-bot==12.7', 'paho-mqtt>=1.5.0',
'TA-Lib>=0.4.18', 'webull-12>=2023.0.1#egg=webull'
'TA-Lib>=0.4.18', 'webull-12>=2023.0.2#egg=webull'
],
entry_points={
'console_scripts': [
Expand Down
2 changes: 1 addition & 1 deletion strategy.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ENV dbport='27017'
ENV dbuser=kinetick
ENV dbpassword=kinetick
ENV dbname=kinetick
ENV dbskip=false
ENV dbskip=true
ENV zerodha_user=kinetick
ENV zerodha_password=kinetick
ENV zerodha_pin=kinetick
Expand Down
39 changes: 39 additions & 0 deletions strategy.arm64.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
FROM python:3.7-slim

WORKDIR /app
ADD ./requirements.txt /app/requirements.txt
ADD ./constraints.txt /app/constraints.txt

RUN apt-get update && apt-get install -y wget && apt-get install -y build-essential && apt-get install -y git

RUN wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz && \
tar -xvzf ta-lib-0.4.0-src.tar.gz && \
cd ta-lib/ && \
./configure --prefix=/usr --build=aarch64-unknown-linux-gnu && \
make && \
make install

RUN python -m pip install --upgrade pip && \
pip install -r requirements.txt -c constraints.txt --no-cache-dir

RUN apt-get autoremove --purge --yes build-essential

ADD . /app

ENV dbport=27017
ENV orderbook=true
ENV threads=4
ENV dbhost='127.0.0.1'
ENV dbport='27017'
ENV dbuser=kinetick
ENV dbpassword=kinetick
ENV dbname=kinetick
ENV dbskip=true
ENV zerodha_user=kinetick
ENV zerodha_password=kinetick
ENV zerodha_pin=kinetick
ENV resolution=1m
ENV LOGLEVEL=INFO
ENV PYTHONUNBUFFERED=1

CMD [ "python", "-m", "kinetick.factory.strategy" ]

0 comments on commit 8e337cb

Please sign in to comment.