Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using parametric types #5

Open
femtotrader opened this issue Apr 23, 2019 · 13 comments
Open

Using parametric types #5

femtotrader opened this issue Apr 23, 2019 · 13 comments

Comments

@femtotrader
Copy link
Member

function price(ob::AbstractOrderBook, volume::Volume; raise=false)
if volume == Volume(0)

doesn't use parametric type but it should because volume can be integer or float

see #3 (comment)

@roshii
Copy link
Member

roshii commented Apr 24, 2019

Why not using Real instead?

volume::Real
volume == zero(Real)

@roshii roshii mentioned this issue Apr 24, 2019
@roshii
Copy link
Member

roshii commented Apr 24, 2019

On an higher level, I'd proposed to using the following structure, keeping in mind that it would be used as something like Observable{MarketObservable}

MarketObservable
    |_ AbstractOrderBook
        |_ FiniteDepthOrderBook
        ...
    |_ Ticker
    |_ Trades

@femtotrader
Copy link
Member Author

To my understanding, it's generally a bad idea to use abstract types

https://docs.julialang.org/en/v1/manual/performance-tips/index.html

Moreover type for volume can be different depending of the assets

Volume for shares is for example integer
Volume for forex lots is decimal ...

@roshii
Copy link
Member

roshii commented Apr 24, 2019

I indeed suspected a performance implication... Parametric type it is then to cover both Float and Integer cases

@femtotrader
Copy link
Member Author

I think we absolutely need to support volume of several types

see https://en.wikibooks.org/wiki/Introducing_Julia/Types

same for price so we don't have to be explicit about type to use here

It can be Float64, Int64 but also Decimal (to avoid rounding issue)

@femtotrader
Copy link
Member Author

I personally don't use them... but some people may also want to use Unums!

@femtotrader
Copy link
Member Author

I had a types.jl with the following content

const Price = Float64 #Nullable{Float64}
const Volume = Float64 #Nullable{Float64}
const Ticket = Integer #Nullable{Integer}
const Magic = Integer #Nullable{Integer}
const Slippage = Integer #Nullable{Integer}

null_price = NaN #Price()
null_volume = NaN #Volume()
null_datetime = DateTime(0)
null_ticket = -1 #Ticket()
null_magic = -1 #Magic()
null_slippage = -1 #Slippage()

function isnullprice(price::Price)
    isnan(price)
end

function isnullvolume(volume::Volume)
    isnan(volume)
end

function isnulldatetime(dt::DateTime)
    dt == null_datetime
end

function isnullmagic(magic::Magic)
    magic == null_magic
end

function isnullslippage(slippage::Slippage)
    slippage == null_slippage
end

but

const Price = Float64 #Nullable{Float64}
const Volume = Float64 #Nullable{Float64}

is definitely not the correct way of doing...

(that's why I'm not adding this to this repository)

@femtotrader
Copy link
Member Author

femtotrader commented Apr 24, 2019

I also had

abstract type AbstractPriceData{Tprice,Tvol} end

struct TickData{Tprice,Tvol} <: AbstractPriceData{Tprice,Tvol}
    ask::Tprice
    bid::Tprice
    volume::Tvol
end

struct BarData{Tprice,Tvol} <: AbstractPriceData{Tprice,Tvol}
    open::Tprice
    high::Tprice
    low::Tprice
    close::Tprice
    volume::Tvol
    adj_close::Tprice
    
    function BarData(open::Tprice, high::Tprice, low::Tprice, close::Tprice, volume::Tvol) where {Tprice, Tvol}
        new{Tprice,Tvol}(open, high, low, close, volume, close)
    end

    function BarData(open::Tprice, high::Tprice, low::Tprice, close::Tprice, volume::Tvol, adj_close::Tprice) where {Tprice, Tvol}
        new{Tprice,Tvol}(open, high, low, close, volume, adj_close)
    end
end

struct PriceTimedData
    time::DateTime
    data::AbstractPriceData
end

I have just add currently

abstract type AbstractPriceData{Tprice,Tvol} end

in this repository (types.jl) as it's required to compile

femtotrader added a commit that referenced this issue Apr 24, 2019
@roshii
Copy link
Member

roshii commented Apr 24, 2019

It can be Float64, Int64 but also Decimal (to avoid rounding issue)

I like the Decimal approach instead of Float.
Exchanges do have decimal parameters in their API and so it will make setup easier while as you said, avoid rounding issues.

@femtotrader
Copy link
Member Author

I think we should absolutely avoid to decide this now! and we should have our structs / functions able to manage any Number for Volume or Price

@roshii
Copy link
Member

roshii commented Apr 24, 2019

I personally don't use them... but some people may also want to use Unums!

I don't either, let's park it for now, we may still explore in the future

I think we should absolutely avoid to decide this now!

Agreed, nothing is set in stone, just brainstorming :)

@femtotrader
Copy link
Member Author

Working with Decimal brings precision but it is sometimes at the expense of performance... if we can use Decimal or Float (or Integer) given context I think it's better

@femtotrader
Copy link
Member Author

femtotrader commented Apr 24, 2019

Similar code improvement than #10 should probably be done in

Unfortunately I can't spend more time on this this week... I let you @roshii have a look at this

@femtotrader femtotrader mentioned this issue Apr 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants