Advertisement
KDLPro

MACD on Swipl

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