2021-07-28 09:43:58 -07:00
# # ⚠ Warning
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
# LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
# NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# [🥭 Mango Markets](https://mango.markets/) support is available at:
# [Docs](https://docs.mango.markets/)
# [Discord](https://discord.gg/67jySBhxrg)
# [Twitter](https://twitter.com/mangomarkets)
# [Github](https://github.com/blockworks-foundation)
# [Email](mailto:hello@blockworks.foundation)
from decimal import Decimal
2021-11-08 03:39:09 -08:00
from . token import Instrument
2021-07-28 09:43:58 -07:00
2021-08-22 11:48:20 -07:00
2021-07-28 09:43:58 -07:00
# # 🥭 LotSizeConverter class
#
class LotSizeConverter ( ) :
2021-11-09 05:23:36 -08:00
def __init__ ( self , base : Instrument , base_lot_size : Decimal , quote : Instrument , quote_lot_size : Decimal ) - > None :
2021-11-08 03:39:09 -08:00
self . base : Instrument = base
2021-07-28 09:43:58 -07:00
self . base_lot_size : Decimal = base_lot_size
2021-11-08 03:39:09 -08:00
self . quote : Instrument = quote
2021-07-28 09:43:58 -07:00
self . quote_lot_size : Decimal = quote_lot_size
2021-09-30 10:53:33 -07:00
@property
2021-11-15 12:39:29 -08:00
def lot_size ( self ) - > Decimal :
2021-09-30 10:53:33 -07:00
return self . base_size_lots_to_number ( Decimal ( 1 ) )
2021-08-22 11:48:20 -07:00
@property
def tick_size ( self ) - > Decimal :
2021-09-09 09:17:23 -07:00
return self . price_lots_to_number ( Decimal ( 1 ) )
2021-08-22 11:48:20 -07:00
2021-09-09 09:17:23 -07:00
def price_lots_to_number ( self , price_lots : Decimal ) - > Decimal :
2021-09-30 10:53:33 -07:00
adjusted = 10 * * ( self . base . decimals - self . quote . decimals )
lots_to_native = self . quote_lot_size / self . base_lot_size
return ( price_lots * lots_to_native ) * adjusted
2021-09-09 09:17:23 -07:00
def price_number_to_lots ( self , price : Decimal ) - > int :
base_factor : Decimal = 10 * * self . base . decimals
quote_factor : Decimal = 10 * * self . quote . decimals
return round ( ( price * quote_factor * self . base_lot_size ) / ( base_factor * self . quote_lot_size ) )
def base_size_lots_to_number ( self , size_lots : Decimal ) - > Decimal :
size : int = round ( size_lots )
base_factor : Decimal = 10 * * self . base . decimals
return Decimal ( size * self . base_lot_size ) / base_factor
2021-07-28 09:43:58 -07:00
2021-09-09 09:17:23 -07:00
def base_size_number_to_lots ( self , size : Decimal ) - > int :
base_factor : Decimal = 10 * * self . base . decimals
return int ( round ( size * base_factor ) / self . base_lot_size )
2021-07-28 09:43:58 -07:00
2021-09-09 09:17:23 -07:00
def quote_size_lots_to_number ( self , size_lots : Decimal ) - > Decimal :
size : int = round ( size_lots )
quote_factor : Decimal = 10 * * self . quote . decimals
return Decimal ( size * self . quote_lot_size ) / quote_factor
2021-07-28 09:43:58 -07:00
2021-10-04 10:27:07 -07:00
def quote_lots_to_number ( self , size_lots : Decimal ) - > Decimal :
quote_factor : Decimal = 10 * * self . quote . decimals
return Decimal ( size_lots * self . quote_lot_size ) / quote_factor
2021-09-09 09:17:23 -07:00
def quote_size_number_to_lots ( self , size : Decimal ) - > int :
quote_factor : Decimal = 10 * * self . quote . decimals
return int ( round ( size * quote_factor ) / self . quote_lot_size )
2021-07-28 09:43:58 -07:00
2021-08-31 04:36:23 -07:00
def round_base ( self , quantity : Decimal ) - > Decimal :
2021-09-09 09:17:23 -07:00
lots : int = self . base_size_number_to_lots ( quantity )
return self . base_size_lots_to_number ( Decimal ( lots ) )
2021-08-31 04:36:23 -07:00
def round_quote ( self , price : Decimal ) - > Decimal :
2021-09-09 09:17:23 -07:00
lots : int = self . quote_size_number_to_lots ( price )
return self . quote_size_lots_to_number ( Decimal ( lots ) )
2021-08-31 04:36:23 -07:00
2021-08-01 10:03:46 -07:00
def __str__ ( self ) - > str :
2021-09-09 09:17:23 -07:00
return f " « 𝙻 𝚘 𝚝 𝚂 𝚒 𝚣 𝚎 𝙲 𝚘 𝚗 𝚟 𝚎 𝚛 𝚝 𝚎 𝚛 { self . base . symbol } / { self . quote . symbol } [base lot size: { self . base_lot_size } ( { self . base . decimals } decimals), quote lot size: { self . quote_lot_size } ( { self . quote . decimals } decimals)] » "
2021-07-28 09:43:58 -07:00
def __repr__ ( self ) - > str :
return f " { self } "
# # 🥭 NullLotSizeConverter class
#
class NullLotSizeConverter ( LotSizeConverter ) :
2021-11-09 05:23:36 -08:00
def __init__ ( self ) - > None :
super ( ) . __init__ ( Instrument ( " NULLBASE " , " Null Base " , Decimal ( 0 ) ) , Decimal (
1 ) , Instrument ( " NULLQUOTE " , " Null Quote " , Decimal ( 0 ) ) , Decimal ( 1 ) )
2021-07-28 09:43:58 -07:00
2021-09-09 09:17:23 -07:00
def price_lots_to_number ( self , price_lots : Decimal ) - > Decimal :
2021-07-28 09:43:58 -07:00
return price_lots
2021-09-09 09:17:23 -07:00
def price_number_to_lots ( self , price : Decimal ) - > int :
return round ( price )
2021-07-28 09:43:58 -07:00
2021-09-09 09:17:23 -07:00
def base_size_lots_to_number ( self , size_lots : Decimal ) - > Decimal :
return size_lots
def base_size_number_to_lots ( self , size : Decimal ) - > int :
return round ( size )
2021-07-28 09:43:58 -07:00
2021-09-09 09:17:23 -07:00
def quote_size_lots_to_number ( self , size_lots : Decimal ) - > Decimal :
return size_lots
def quote_size_number_to_lots ( self , size : Decimal ) - > int :
return round ( size )
2021-07-28 09:43:58 -07:00
2021-08-01 10:03:46 -07:00
def __str__ ( self ) - > str :
2021-07-28 09:43:58 -07:00
return " « 𝙽 𝚞 𝚕 𝚕 𝙻 𝚘 𝚝 𝚂 𝚒 𝚣 𝚎 𝙲 𝚘 𝚗 𝚟 𝚎 𝚛 𝚝 𝚎 𝚛 » "
2021-08-22 11:48:20 -07:00
# # 🥭 RaisingLotSizeConverter class
#
class RaisingLotSizeConverter ( LotSizeConverter ) :
2021-11-09 05:23:36 -08:00
def __init__ ( self ) - > None :
super ( ) . __init__ ( Instrument ( " RAISINGBASE " , " Raising Base " , Decimal ( 0 ) ) , Decimal ( - 1 ) ,
Instrument ( " RAISINGQUOTE " , " Raising Quote " , Decimal ( 0 ) ) , Decimal ( - 1 ) )
2021-08-22 11:48:20 -07:00
2021-09-09 09:17:23 -07:00
def price_lots_to_number ( self , price_lots : Decimal ) - > Decimal :
raise NotImplementedError (
" RaisingLotSizeConverter.price_lots_to_number() is not implemented. RaisingLotSizeConverter is a stub used where no LotSizeConverter members should be called. " )
def price_number_to_lots ( self , price : Decimal ) - > int :
raise NotImplementedError (
" RaisingLotSizeConverter.price_number_to_lots() is not implemented. RaisingLotSizeConverter is a stub used where no LotSizeConverter members should be called. " )
def base_size_lots_to_number ( self , size_lots : Decimal ) - > Decimal :
2021-08-22 11:48:20 -07:00
raise NotImplementedError (
2021-09-09 09:17:23 -07:00
" RaisingLotSizeConverter.base_size_lots_to_number() is not implemented. RaisingLotSizeConverter is a stub used where no LotSizeConverter members should be called. " )
2021-08-22 11:48:20 -07:00
2021-09-09 09:17:23 -07:00
def base_size_number_to_lots ( self , size : Decimal ) - > int :
2021-08-22 11:48:20 -07:00
raise NotImplementedError (
2021-09-09 09:17:23 -07:00
" RaisingLotSizeConverter.base_size_number_to_lots() is not implemented. RaisingLotSizeConverter is a stub used where no LotSizeConverter members should be called. " )
2021-08-22 11:48:20 -07:00
2021-09-09 09:17:23 -07:00
def quote_size_lots_to_number ( self , size_lots : Decimal ) - > Decimal :
2021-08-22 11:48:20 -07:00
raise NotImplementedError (
2021-09-09 09:17:23 -07:00
" RaisingLotSizeConverter.quote_size_lots_to_number() is not implemented. RaisingLotSizeConverter is a stub used where no LotSizeConverter members should be called. " )
2021-08-22 11:48:20 -07:00
2021-09-09 09:17:23 -07:00
def quote_size_number_to_lots ( self , size : Decimal ) - > int :
2021-08-22 11:48:20 -07:00
raise NotImplementedError (
2021-09-09 09:17:23 -07:00
" RaisingLotSizeConverter.quote_size_number_to_lots() is not implemented. RaisingLotSizeConverter is a stub used where no LotSizeConverter members should be called. " )
2021-08-22 11:48:20 -07:00
def __str__ ( self ) - > str :
2021-09-09 09:17:23 -07:00
return " « 𝚁 𝚊 𝚒 𝚜 𝚒 𝚗 𝚐 𝙻 𝚘 𝚝 𝚂 𝚒 𝚣 𝚎 𝙲 𝚘 𝚗 𝚟 𝚎 𝚛 𝚝 𝚎 𝚛 » "