Skip to content

Commit

Permalink
#51 NANO, NanoToFloat() and FloatToNano() methods were moved to the n…
Browse files Browse the repository at this point in the history
…ew `TradeRoutines` library
  • Loading branch information
Tim55667757 committed Nov 18, 2022
1 parent fc69bd3 commit f08d3bd
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 46 deletions.
47 changes: 1 addition & 46 deletions tksbrokerapi/TKSBrokerAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import pandas as pd

from TKSEnums import * # A lot of constants from enums sections: https://tinkoff.github.io/investAPI/swagger-ui/
from TradeRoutines import * # This library contains some methods used by trade scenarios implemented with TKSBrokerAPI module

from pricegenerator.PriceGenerator import PriceGenerator, uLogger # This module has a lot of instruments to work with candles data. See docs here: https://github.com/Tim55667757/PriceGenerator
from pricegenerator.UniLogger import DisableLogger as PGDisLog # Method for disable log from PriceGenerator
Expand All @@ -71,52 +72,6 @@
CPU_COUNT = cpu_count() # host's real CPU count
CPU_USAGES = CPU_COUNT - 1 if CPU_COUNT > 1 else 1 # how many CPUs will be used for parallel calculations

# --- Main constants:

NANO = 0.000000001 # SI-constant nano = 10^-9


def NanoToFloat(units: str, nano: int) -> float:
"""
Convert number in nano-view mode with string parameter `units` and integer parameter `nano` to float view. Examples:
`NanoToFloat(units="2", nano=500000000) -> 2.5`
`NanoToFloat(units="0", nano=50000000) -> 0.05`
:param units: integer string or integer parameter that represents the integer part of number
:param nano: integer string or integer parameter that represents the fractional part of number
:return: float view of number
"""
return int(units) + int(nano) * NANO


def FloatToNano(number: float) -> dict:
"""
Convert float number to nano-type view: dictionary with string `units` and integer `nano` parameters `{"units": "string", "nano": integer}`. Examples:
`FloatToNano(number=2.5) -> {"units": "2", "nano": 500000000}`
`FloatToNano(number=0.05) -> {"units": "0", "nano": 50000000}`
:param number: float number
:return: nano-type view of number: `{"units": "string", "nano": integer}`
"""
splitByPoint = str(number).split(".")
frac = 0

if len(splitByPoint) > 1:
if len(splitByPoint[1]) <= 9:
frac = int("{}{}".format(
int(splitByPoint[1]),
"0" * (9 - len(splitByPoint[1])),
))

if (number < 0) and (frac > 0):
frac = -frac

return {"units": str(int(number)), "nano": frac}


def GetDatesAsString(start: str = None, end: str = None) -> tuple:
"""
Expand Down
72 changes: 72 additions & 0 deletions tksbrokerapi/TradeRoutines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-
# Author: Timur Gilmullin

"""
This library contains some methods used by trade scenarios implemented with TKSBrokerAPI module.
- **TKSBrokerAPI module documentation:** https://tim55667757.github.io/TKSBrokerAPI/docs/tksbrokerapi/TKSBrokerAPI.html
- **About Tinkoff Invest API:** https://tinkoff.github.io/investAPI/
- **Tinkoff Invest API documentation:** https://tinkoff.github.io/investAPI/swagger-ui/
- **Open account for trading:** http://tinkoff.ru/sl/AaX1Et1omnH
"""

# Copyright (c) 2022 Gilmillin Timur Mansurovich
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.


# --- Main constants:

NANO = 0.000000001 # SI-constant nano = 10^-9


def NanoToFloat(units: str, nano: int) -> float:
"""
Convert number in nano-view mode with string parameter `units` and integer parameter `nano` to float view. Examples:
`NanoToFloat(units="2", nano=500000000) -> 2.5`
`NanoToFloat(units="0", nano=50000000) -> 0.05`
:param units: integer string or integer parameter that represents the integer part of number
:param nano: integer string or integer parameter that represents the fractional part of number
:return: float view of number
"""
return int(units) + int(nano) * NANO


def FloatToNano(number: float) -> dict:
"""
Convert float number to nano-type view: dictionary with string `units` and integer `nano` parameters `{"units": "string", "nano": integer}`. Examples:
`FloatToNano(number=2.5) -> {"units": "2", "nano": 500000000}`
`FloatToNano(number=0.05) -> {"units": "0", "nano": 50000000}`
:param number: float number
:return: nano-type view of number: `{"units": "string", "nano": integer}`
"""
splitByPoint = str(number).split(".")
frac = 0

if len(splitByPoint) > 1:
if len(splitByPoint[1]) <= 9:
frac = int("{}{}".format(
int(splitByPoint[1]),
"0" * (9 - len(splitByPoint[1])),
))

if (number < 0) and (frac > 0):
frac = -frac

return {"units": str(int(number)), "nano": frac}

0 comments on commit f08d3bd

Please sign in to comment.