Guest User

Untitled

a guest
Aug 8th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 1.74 KB | None | 0 0
  1.  
  2.  
  3. % Functor applicator. Functor returns turple passed with some elem
  4.  
  5. %   wrapped into fun-parameter.
  6.  
  7. %
  8.  
  9. on_fst(F) -> fun ({A, B}) -> { F(A), B } end.
  10.  
  11. on_snd(F) -> fun ({A, B}) -> { A, F(B) } end.
  12.  
  13.  
  14.  
  15. % Datetime-structure clearers.
  16.  
  17. %
  18.  
  19. zero_1() -> fun ({A, B, _}) -> { A, B, 0 } end.
  20.  
  21. zero_2() -> fun ({A, _, _}) -> { A, 0, 0 } end.
  22.  
  23. zero_3() -> fun ({_, _, _}) -> { 0, 0, 0 } end.
  24.  
  25.  
  26.  
  27. % Returns the functor, clamping datetime to candle start.
  28.  
  29. %
  30.  
  31. candle_open(Scale) ->
  32.  
  33.     case Scale of
  34.  
  35.         minute -> c([ on_fst(id()),        on_snd(zero_1()) ]);
  36.  
  37.  
  38.  
  39.         hour ->   c([ on_fst(id()),        on_snd(zero_2()) ]);
  40.  
  41.  
  42.  
  43.         day ->    c([ on_fst(id()),        on_snd(zero_3()) ]);
  44.  
  45.  
  46.  
  47.         month ->  c([ on_fst(zero_1()),    on_snd(zero_3()) ]);
  48.  
  49.  
  50.  
  51.         week ->   c([ on_fst(open_week()), on_snd(zero_3()) ])
  52.  
  53.     end.
  54.  
  55.  
  56.  
  57. % Returns the functor clamping date to week start.
  58.  
  59. %
  60.  
  61. open_week() ->
  62.  
  63.     fun (Date) ->
  64.  
  65.         Seconds = t({ Date, { 0, 0, 0} }),
  66.  
  67.         WeekStart = Seconds - (calendar:day_of_the_week(Date) - 1) * 24 * 60 * 60,
  68.  
  69.         { NewDate, _ } = un_t(WeekStart),
  70.  
  71.         NewDate
  72.  
  73.     end.
  74.  
  75.  
  76.  
  77. % Returns property selector from some datetime.
  78.  
  79. %
  80.  
  81. property(Scale) ->
  82.  
  83.     fun (Trade) ->
  84.  
  85.         {{Y, Mth, D}, {H, M, _S}} = Trade#trade.point,
  86.  
  87.         case Scale of
  88.  
  89.             minute -> M;
  90.  
  91.             hour   -> H;
  92.  
  93.             day    -> D;
  94.  
  95.             month  -> Mth;
  96.  
  97.             week   -> calendar:iso_week_number({Y, Mth, D})
  98.  
  99.         end
  100.  
  101.     end.
  102.  
  103.  
  104.  
  105. % Datetime to second (and back) converters.
  106.  
  107. %
  108.  
  109. t   (GregTime) -> calendar:datetime_to_gregorian_seconds(GregTime).
  110.  
  111. un_t(Datetime) -> calendar:gregorian_seconds_to_datetime(Datetime).
Add Comment
Please, Sign In to add comment