import time
from pandas import DataFrame
from zaifbot.modules.api.wrapper import BotPublicApi
import datetime

datetime.date.today() - datetime.timedelta(1)


def get_rsi(currency_pair, period, count, to_epoch_time):
    public_api = BotPublicApi()
    second_api_params = {'period': period, 'count': count, 'to_epoch_time': to_epoch_time}
    infos = DataFrame(public_api.everything('ohlc_data', currency_pair, second_api_params))
    return _calc_rsi(infos)


def _calc_rsi(df):
    close = df['close']
    moved_sum = 0
    increased_sum = 0
    for i in range(1, len(close)):
        moved_sum += abs(close.ix[i] - close.ix[i - 1])
        increased_sum += max(close.ix[i] - close.ix[i - 1], 0)
    return increased_sum / moved_sum * 100


if __name__ == '__main__':
    dt = datetime.date.today() - datetime.timedelta(2)
    t = time.mktime(dt.timetuple())
    info = get_rsi('btc_jpy', '1d', 5, int(t))

@@ -91,71 +91,8 @@
 Api%0A
-import datetime%0A%0Adatetime.date.today() - datetime.timedelta(1)%0A
 %0A%0Ade
@@ -126,15 +126,33 @@
 riod
+='1d'
 , count
+=5, length=14
 , to
@@ -162,16 +162,33 @@
 och_time
+=int(time.time())
 ):%0A    p
@@ -271,16 +271,25 @@
 ': count
+ + length
 , 'to_ep
@@ -418,20 +418,19 @@
 return _
-calc
+get
 _rsi(inf
@@ -435,94 +435,715 @@
 nfos
-)%0A%0A%0Adef _calc_rsi(df):%0A    close = df%5B'close'%5D%0A    moved_sum = 0%0A    increased_sum = 0
+%5B%5B'close', 'time'%5D%5D, count, length)%0A%0A%0Adef _get_rsi(df, count, length):%0A    results = %5B%5D%0A    a_1, b_1 = _calc_first_a_and_b(df.ix%5B:length, 'close'%5D)%0A    t_1 = df.ix%5Blength, 'time'%5D%0A    results.append(_create_dict(t_1, _rsi_formula(a_1, b_1)))%0A%0A    generator = _generate_next_a_and_b(a_1, b_1, length)%0A    next(generator)%0A%0A    for i in range(count - 1):%0A        diff = df.ix%5Blength + i + 1, 'close'%5D - df.ix%5Blength + i, 'close'%5D%0A        t_i = df.ix%5Blength + i + 1, 'time'%5D%0A        a_i, b_i = generator.send(diff)%0A        rsi = _rsi_formula(a_i, b_i)%0A        results.append(_create_dict(t_i, rsi))%0A    return results%0A%0A%0Adef _calc_first_a_and_b(close):%0A    decreased_sum, increased_sum, length = 0, 0, len(close)
 %0A   
@@ -1164,23 +1164,19 @@
 e(1, len
-(close)
+gth
 ):%0A     
@@ -1178,19 +1178,23 @@
         
-mov
+decreas
 ed_sum +
@@ -1199,11 +1199,11 @@
  += 
-abs
+min
 (clo
@@ -1228,16 +1228,26 @@
 x%5Bi - 1%5D
+, 0) * (-1
 )%0A      
@@ -1334,143 +1334,502 @@
 m / 
-moved_sum * 100%0A%0A%0Aif __name__ == '__main__':%0A    dt = datetime.date.today() - datetime.timedelta(2)%0A    t = time.mktime(dt.timetuple())
+length, decreased_sum / length%0A%0A%0Adef _generate_next_a_and_b(a, b, length):%0A    a, b,  = a, b%0A    while True:%0A        diff = yield a, b%0A        a = max(_next_rsi_formula(a, diff, length), a)%0A        b = min(_next_rsi_formula(b, diff, length), b)%0A%0A%0Adef _rsi_formula(a, b):%0A    return a / (a + b) * 100%0A%0A%0Adef _next_rsi_formula(before, diff, length):%0A    return (before * (length - 1) + diff) / length%0A%0A%0Adef _create_dict(t, rsi):%0A    return %7B'timestamp': t, 'rsi': rsi%7D%0A%0A%0Aif __name__ == '__main__':
 %0A   
@@ -1857,23 +1857,22 @@
 jpy'
-, '1d', 5, int(t)
+)%0A    print(info
 )%0A

implement finished