Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main:-
- create_candle_list,
- list_ema(12).
- create_candle_list:-
- open("candlestick-data.txt", read, Str),
- read_file(Str,Lines),
- 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),
- read_file(Stream,L).
- % 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.
- % This rule lists the exponential moving averages.
- list_ema(P):-
- retract(period(X)),
- write(2),
- fail.
- list_ema(P):- % set the initial period to 0
- asserta(period(0)),
- write(2),
- fail.
- list_ema(P):- % reset current sma and period pointer values and ema multiplier
- write(2),
- retract(curr_ema(Z)),
- asserta(curr_ema(0)),
- retract(curr_period_ptr(Y)),
- asserta(curr_period_ptr(0)),
- retract(ema_multiplier(X)),
- fail.
- list_ema(P):- % remove every ema listed so far
- write(2),
- retract(curr_ema_list(Y)),
- asserta(curr_ema_list(0)),
- fail.
- list_ema(P):-
- write(2),
- Denom is P + 1,
- M is 2 / Denom,
- asserta(ema_multiplier(M)),
- fail.
- list_ema(P):- % calculate the ema values
- write(2),
- candle(W, X, Close, Z),
- calculate_sub_ema(Close),
- fail.
- list_ema(P):- % display the ema values
- curr_ema_list(X), write(X), nl,
- fail.
- ema(P):-
- retract(period(X)),
- fail.
- ema(P):- % set the initial period to 0
- asserta(period(0)),
- fail.
- ema(P):- % reset current sma and period pointer values and ema multiplier
- retract(curr_ema(Z)),
- asserta(curr_ema(0)),
- retract(curr_period_ptr(Y)),
- asserta(curr_period_ptr(0)),
- retract(ema_multiplier(X)),
- fail.
- ema(P):- % remove every ema listed so far
- retract(curr_ema_list(Y)),
- asserta(curr_ema_list(0)),
- fail.
- ema(P):-
- Denom is P + 1,
- M is 2 / Denom,
- asserta(ema_multiplier(M)),
- fail.
- ema(P):- % calculate the ema values
- candle(W, X, Close, Z),
- calculate_sub_ema(Close),
- fail.
- ema(P):- % display the ema values
- curr_ema_list(X),
- X = 0,
- retract(curr_ema_list(0)),
- fail.
- ema(P):- % display the ema values
- curr_ema_list(X),
- write(X), nl,
- fail.
- calculate_sub_ema(Close):- % initialize things
- curr_ema_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_ema_list(Curr_EMA)),
- !.
- curr_candle([]).
- curr_candle_list([]).
- curr_sma(0).
- curr_sma_list([]).
- curr_ema(0).
- curr_ema_list([]).
- curr_period_ptr(0).
- max_period(0).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement