Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :-dynamic curr_ema/1.
- :-dynamic curr_ema12_list/1.
- :-dynamic curr_ema26_list/1.
- :-dynamic curr_period_ptr/1.
- :-dynamic ema_multiplier/1.
- :-dynamic macd/1.
- main:-
- create_candle_list,
- list_12_ema,
- list_26_ema,
- calculate_macd.
- calculate_macd:-
- retract(curr_ema12_list(X)),
- retract(curr_ema26_list(Y)),
- Z is X-Y,
- asserta(macd(Z)),
- write(Z), nl,
- fail.
- % This rule creates the candlestick list from the text file.
- create_candle_list:-
- open("candlestick-data.txt", read, Str),
- read_file(Str, _),
- close(Str).
- read_file(Stream, []) :-
- at_end_of_stream(Stream), !.
- read_file(Stream,[X|L]) :-
- \+ at_end_of_stream(Stream),
- read(Stream, X),
- assert(X),
- write(X), nl,
- read_file(Stream, L).
- % This is for calculating the 12-period EMA.
- list_12_ema:-
- write('12-period EMA'),
- nl,
- fail.
- list_12_ema:- % reset current sma and period pointer values and ema multiplier
- retract(curr_ema(_)),
- asserta(curr_ema(0)),
- retract(ema_multiplier(_)),
- fail.
- list_12_ema:- % remove every ema listed so far
- retract(curr_ema12_list(_)),
- asserta(curr_ema12_list(0)),
- fail.
- list_12_ema:-
- M is 2 / 13,
- asserta(ema_multiplier(M)),
- fail.
- list_12_ema:- % calculate the ema values
- candle(W, X, Close, Z),
- calculate_ema12_member(Close),
- fail.
- list_12_ema:- % display the ema values
- curr_ema12_list(X),
- write(X), nl,
- fail.
- calculate_ema12_member(Close):- % initialize things
- curr_ema12_list(Prev_Close),
- ema_multiplier(M),
- Prev_M is 1 - M,
- Prev_Weight is Prev_Close * Prev_M,
- Curr_Weight is Close * M,
- Curr_EMA is Prev_Weight + Curr_Weight,
- asserta(curr_ema12_list(Curr_EMA)),
- !.
- % This is for calculating 26-period EMA.
- list_26_ema:-
- write('26-period EMA'),
- nl,
- fail.
- list_26_ema:- % reset current sma and period pointer values and ema multiplier
- retract(curr_ema(_)),
- asserta(curr_ema(0)),
- retract(ema_multiplier(_)),
- fail.
- list_26_ema:- % remove every ema listed so far
- retract(curr_ema26_list(_)),
- asserta(curr_ema26_list(0)),
- fail.
- list_26_ema:-
- M is 2 / 27,
- asserta(ema_multiplier(M)),
- fail.
- list_26_ema:- % calculate the ema values
- candle(W, X, Close, Z),
- calculate_ema26_member(Close),
- fail.
- list_26_ema:- % display the ema values
- curr_ema26_list(X),
- write(X), nl,
- fail.
- calculate_ema26_member(Close):- % initialize things
- curr_ema26_list(Prev_Close),
- ema_multiplier(M),
- Prev_M is 1 - M,
- Prev_Weight is Prev_Close * Prev_M,
- Curr_Weight is Close * M,
- Curr_EMA is Prev_Weight + Curr_Weight,
- asserta(curr_ema26_list(Curr_EMA)),
- !.
- % This rule lists all the candles.
- list_candles:-
- candle(W, X, Y, Z),
- write('candle('),
- write(W), write(', '),
- write(X), write(', '),
- write(Y), write(', '),
- write(Z), write(')'),
- nl, fail.
- curr_candle([]).
- curr_candle_list([]).
- curr_ema(0).
- curr_ema12_list([]).
- curr_ema26_list([]).
- curr_period_ptr(0).
- max_period(0).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement