Advertisement
KDLPro

MACD Calculation

Jun 9th, 2023 (edited)
2,225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 2.72 KB | None | 0 0
  1. main:-
  2.     create_candle_list,
  3.     list_ema(12).
  4.  
  5. create_candle_list:-
  6.     open("candlestick-data.txt", read, Str),
  7.     read_file(Str,Lines),
  8.     close(Str).
  9.  
  10. read_file(Stream,[]) :-
  11.     at_end_of_stream(Stream).
  12.  
  13. read_file(Stream,[X|L]) :-
  14.     \+ at_end_of_stream(Stream),
  15.     read(Stream,X),
  16.     assert(X),
  17.     read_file(Stream,L).
  18.    
  19.    
  20. % This rule lists all the candles.
  21. list_candles:-
  22.     candle(W, X, Y, Z),
  23.     write('candle('),
  24.     write(W), write(', '),
  25.     write(X), write(', '),
  26.     write(Y), write(', '),
  27.     write(Z), write(')'),
  28.     nl, fail.
  29.    
  30.  
  31. % This rule lists the exponential moving averages.
  32.    
  33. list_ema(P):-
  34.     retract(period(X)),
  35.     write(2),
  36.     fail.
  37. list_ema(P):-                                           % set the initial period to 0
  38.     asserta(period(0)),
  39.     write(2),
  40.     fail.
  41. list_ema(P):-                                           % reset current sma and period pointer values and ema multiplier
  42.     write(2),
  43.     retract(curr_ema(Z)),
  44.     asserta(curr_ema(0)),
  45.     retract(curr_period_ptr(Y)),
  46.     asserta(curr_period_ptr(0)),
  47.     retract(ema_multiplier(X)),
  48.     fail.
  49. list_ema(P):-                                           % remove every ema listed so far
  50.     write(2),
  51.     retract(curr_ema_list(Y)),
  52.     asserta(curr_ema_list(0)),
  53.     fail.
  54. list_ema(P):-                              
  55.     write(2),
  56.     Denom is P + 1,
  57.     M is 2 / Denom,
  58.     asserta(ema_multiplier(M)),
  59.     fail.
  60. list_ema(P):-                                           % calculate the ema values
  61.     write(2),
  62.     candle(W, X, Close, Z),
  63.     calculate_sub_ema(Close),
  64.     fail.
  65. list_ema(P):-                                           % display the ema values
  66.     curr_ema_list(X), write(X), nl,
  67.     fail.
  68.  
  69. ema(P):-
  70.     retract(period(X)),
  71.     fail.
  72. ema(P):-                                            % set the initial period to 0
  73.     asserta(period(0)),
  74.     fail.
  75. ema(P):-                                            % reset current sma and period pointer values and ema multiplier
  76.     retract(curr_ema(Z)),
  77.     asserta(curr_ema(0)),
  78.     retract(curr_period_ptr(Y)),
  79.     asserta(curr_period_ptr(0)),
  80.     retract(ema_multiplier(X)),
  81.     fail.
  82. ema(P):-                                            % remove every ema listed so far
  83.     retract(curr_ema_list(Y)),
  84.     asserta(curr_ema_list(0)),
  85.     fail.
  86. ema(P):-                               
  87.     Denom is P + 1,
  88.     M is 2 / Denom,
  89.     asserta(ema_multiplier(M)),
  90.     fail.
  91. ema(P):-                                            % calculate the ema values
  92.     candle(W, X, Close, Z),
  93.     calculate_sub_ema(Close),
  94.     fail.
  95. ema(P):-                                            % display the ema values
  96.     curr_ema_list(X),
  97.     X = 0,
  98.     retract(curr_ema_list(0)),
  99.     fail.
  100. ema(P):-                                            % display the ema values
  101.     curr_ema_list(X),
  102.     write(X), nl,
  103.     fail.
  104.    
  105. calculate_sub_ema(Close):-      % initialize things
  106.     curr_ema_list(Prev_Close),
  107.     ema_multiplier(M),
  108.     Prev_M is 1 - M,
  109.     Prev_Weight is Prev_Close * Prev_M,
  110.     Curr_Weight is Close * M,
  111.     Curr_EMA is Prev_Weight + Curr_Weight,
  112.     asserta(curr_ema_list(Curr_EMA)),
  113.     !.
  114.    
  115. curr_candle([]).
  116. curr_candle_list([]).
  117. curr_sma(0).
  118. curr_sma_list([]).
  119. curr_ema(0).
  120. curr_ema_list([]).
  121. curr_period_ptr(0).
  122. max_period(0).
Tags: MACD Prolog
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement