/ Data / src / prices.jl
prices.jl
 1  module Prices
 2  using Data.DataFrames
 3  
 4  const pranges_futures = (0.98, 0.985, 1.015, 1.02, 1.025)
 5  const pranges_bal = (0.94, 0.95, 0.955, 1.045, 1.05, 1.06)
 6  const pranges_tight = (0.94, 0.98, 0.985, 1.015, 1.02, 1.06)
 7  const pranges_expa = (0.7, 0.8, 0.9, 0.98, 0.985, 1.015, 1.02, 1.1, 1.2, 1.3)
 8  
 9  @doc """Given a price, output price at given ratios.
10  (Predefined ratios - :bal,:futures,:tight,:expa)
11  """
12  function price_ranges(price::Number, ranges=:expa)
13      if ranges === :bal
14          p = pranges_bal
15      elseif ranges === :futures
16          p = pranges_futures
17      elseif ranges === :tight
18          p = pranges_tight
19      elseif ranges === :expa
20          p = pranges_expa
21      else
22          p = ranges
23      end
24      DataFrame(Dict(:price => price, [Symbol(r) => price * r for r in p]...))
25      # (price, -price * ranges[1], -price * ranges[2], -price * ranges[3], price * ranges[4])
26  end
27  
28  @doc "Get the price range of a map of pairs, using the last available close price."
29  function price_ranges(mrkts::AbstractDict, args...; kwargs...)
30      r = []
31      for p in values(mrkts)
32          push!(r, (p.name, price_ranges(p.data.close[end], args...; kwargs...)))
33      end
34      r
35  end
36  
37  function price_ranges(pass::AbstractVector, mrkts::AbstractDict; kwargs...)
38      r = []
39      for (name, _) in pass
40          push!(r, (name, price_ranges(mrkts[name].data.close[end]; kwargs...)))
41      end
42      r
43  end
44  
45  @doc "Total profit of a ladder of trades"
46  function gprofit(peak=0.2, grids=10)
47      sum(collect((peak / grids):(peak / grids):peak) .* inv(grids))
48  end
49  
50  end