มา Refactor code กัน

Flow การ Code
-> Start with Flowchart
-> Start code with small unit
-> Test drive driven แต่ละ small unit {จะง่ายต่อชีวิตมาก} Refactor code แต่ละ unit
-> Refactor code โดยรวม
-> Test โดยรวม
(แนวคิดการ refactor เบื้องต้น = หมาก็อ่านง่าย, ลดการทำงานของขั้นตอน, ลดสิ่งที่ไม่ได้ใช้ หรือไม่จำเป็นทิ้งไป และ เป็นไปเพื่อทดสอบได้ง่าย)

Basic knowledge
1. หาก Code มีเป้าหมายเพื่อทำอะไรบางแล้วนำผลลัพธ์ไปใช้ต่างกัน ก็ควรแยก Class, ถึงแม้จะ code คล้ายกันก็ตาม (แนวคิดเหมือนกับ method แต่มีเป้าหมายใหญ่กว่า)
2. Method นึง ทำงานอย่างเดียว มีหน้าที่เฉพาะตน เรียกว่า Business Logic
3. Parameter ของ Method ควรมีเท่ากับ Business Logic เท่านั้น -> Test ง่าย+อ่านรู้เรื่อง
4. แต่ละ Class/Method ควรมีผลรวม Code complexity ไม่เกิน 10
_การนับ CC
_4.1 Class/Method ที่ไม่มี condition นับเป็น 1
_4.2 Class/Method ที่มี Condition นับเป็น 2
5. Code ที่เหมือนกันเยอะๆ ไปทำ method หรือ for loop
6. การ Map โดยใช้ Class(ใหญ่) หรือ Hashmap(Key, Value) จะลด variable ได้มหาศาล
7. การตั้งชื่อ varialbe หากอยู่ใน class/ method ให้เป็นตัวพิมพ์เล็ก อยู่นอกเป็นตัวพิมพ์ใหญ่
8. การเรียง code ตามลำดับความสำคัญดังนี้
_8.1 Class ทำอะไร = Main มาก่อน
_8.2 Method อยู่ด้านล่าง Main ที่เรียกใช้
_8.3 หากจะสร้าง local variable เพื่อใช้ ก็ควรอยู่ด้านบนติดกับกลุ่ม Code ที่เรียกใช้มัน
9. การตั้งชื่อ variable หรือ parameter ต้องให้คนอื่นเข้าใจ
10. การ comment code ที่ไม่ได้ช้งานแล้ว ต้อง ‘ลบ’ ออกเท่านั้น ไม่มี comment ใดๆ การ comment จะเป็นการอธิบายวิธีการทำงาน
11. เขียน flowchart การทำงาน
12. Method ที่ใช้ Class เดียว ก็ควรอยู่ใน Class นั้น
13. ค่าที่ถูกใช้ 2 ตัวขึ้นไป ให้สร้าง variable (หากมีการเปลี่ยนแปลงค่าที่เปลี่ยนค่าได้)
14. ค่าที่ถูกใช้ 2 ตัวขึ้นไป หรือ เป็นเลขที่คนอื่นไม่เข้าใจว่ามาได้อย่างไร ต้องบอกว่ามันเป็นค่าอะไร โดย ให้สร้าง CONSTANT (ไม่มีการเปลี่ยนแปลงค่า)

Other Technque:
1. Key ใน Hash map จะไม่ซ้ำ ถ้าอยากเรียงอะไรไม่ซ้ำ หรือเป็น Prime_key ให้มันเป็น Key ซะ
2. การคิดเพื่อ Test: แต่ละ parameter

Example
Before

from collections import defaultdict

import datetime
import luigi
import re
import operator

from sqlalchemy import create_engine, text
from collections import defaultdict
from cons import const

"""
 PROCESS INDICATOR
"""


def higher_high(data_list, day, shift_day):
 if len(data_list) < day + shift_day - 1:
 return 0
 data_spec_days = data_list[shift_day:day + shift_day]
 lower_low_data = min(data_spec_days, key=lambda item: item['low'])
 lower_low_value = lower_low_data['low']
 if lower_low_value == 0.0:
 return 0
 higher_high_data = max(data_spec_days, key=lambda item: item['high'])
 higher_high_value = higher_high_data['high']
 return higher_high_value


def lower_low(data_list, day, shift_day):
 if len(data_list) < day + shift_day - 1:
 return 0
 data_spec_day = data_list[shift_day:day + shift_day]
 lower_low_data = min(data_spec_day, key=lambda item: item['low'])
 lower_low_value = lower_low_data['low']
 return lower_low_value


def process_price_day_ago(data_list, day):
 price_26day_ago = 0.0
 if day <= len(data_list) + 1:
 data = data_list[day]
 price_26day_ago = data['cclose']
 return price_26day_ago


def process_tenkan(hhv9, llv9):
 tenkan_value = 0.0
 tenkan_value = (float(hhv9) + float(llv9)) / 2
 return tenkan_value


def process_kijun(hhv26, llv26):
 kijun_value = 0.0
 kijun_value = (float(hhv26) + float(llv26)) / 2
 return kijun_value


def process_senkou_a(tenkan, kijun):
 senkou_a = 0.0
 senkou_a = (float(tenkan) + float(kijun)) / 2
 return senkou_a


def process_senkou_b(hhv52, llv52):
 senkou_b = (float(hhv52) + float(llv52)) / 2
 return senkou_b


class Ichimoku(luigi.Task):
 date = luigi.DateParameter(default=datetime.date.today())

 def run(self):
 engine = create_engine(const.data_db_host())
 radar_price_above_kijun = 352
 radar_chikou_above_26day_ago = 353
 radar_tenkan_above_kijun = 354
 radar_price_above_cloud = 355
 radar_green_cloud = 356
 radar_chikou_above_cloud = 357
 radar_price_below_kijun = 358
 radar_chikou_below_26day_ago = 359
 radar_tenkan_below_kijun = 360
 radar_price_below_cloud = 361
 radar_red_cloud = 362
 radar_chikou_below_cloud = 363

 engine_web = create_engine(const.web_db())
 symbol_list = engine_web.execute("""
 SELECT
 I_symbol,
 N_symbol
 FROM stockradars.MAP_ID
 JOIN stockgrid.data_all_stock D ON symbol = N_symbol
 WHERE D.[Security Type] = 'S'
 """).fetchall()
 symbol_data = {row['N_symbol']: row['I_symbol'] for row in symbol_list}
 print symbol_data

 fetch_string = """
 SELECT TOP 157
 symbol,
 datadate,
 CAST(high AS float) AS high,
 CAST(cclose AS float) AS cclose,
 CAST(low AS float) AS low
 FROM history
 WHERE symbol = '{symbol}'
 AND datadate <= '{datadate}'
 AND datadate > DATEADD(DAY, -365, '{datadate}')
 AND CAST(cclose AS float) > 0.0
 AND CAST(volume AS float) > 0.0
 ORDER BY datadate DESC;
 """
 radars_list = []
 radars_dictionary = defaultdict(list)

 for symbol, symbol_id in symbol_data.iteritems():
 print symbol_id, symbol
 data_all_list = engine.execute(
 fetch_string.format(symbol=symbol, datadate=self.date.strftime("%Y-%m-%d"))).fetchall()
 if not data_all_list:
 continue

 data = data_all_list[0]
 price = data['cclose']

 hhv9 = higher_high(data_all_list, 9, 0)
 llv9 = lower_low(data_all_list, 9, 0)
 tenkan = process_tenkan(hhv9, llv9)

 hhv26 = higher_high(data_all_list, 26, 0)
 llv26 = lower_low(data_all_list, 26, 0)
 kijun = process_kijun(hhv26, llv26)

 hhv9_shift26 = higher_high(data_all_list, 9, 26)
 llv9_shift26 = lower_low(data_all_list, 9, 26)
 tenkan_shift26 = process_tenkan(hhv9_shift26, llv9_shift26)
 hhv26_shift26 = higher_high(data_all_list, 26, 26)
 llv26_shift26 = lower_low(data_all_list, 26, 26)
 kijun_shift26 = process_tenkan(hhv26_shift26, llv26_shift26)
 senkou_a = process_senkou_a(tenkan_shift26, kijun_shift26)

 hhv52_shift26 = higher_high(data_all_list, 52, 26)
 llv52_shift26 = lower_low(data_all_list, 52, 26)
 senkou_b = process_senkou_b(hhv52_shift26, llv52_shift26)

 price_26day_ago = process_price_day_ago(data_all_list, 26)
 chikou = data['cclose']

 hhv9_shift52 = higher_high(data_all_list, 9, 52)
 llv9_shift52 = lower_low(data_all_list, 9, 52)
 tenkan_shift52 = process_tenkan(hhv9_shift52, llv9_shift52)
 hhv26_shift52 = higher_high(data_all_list, 26, 52)
 llv26_shift52 = lower_low(data_all_list, 26, 52)
 kijun_shift52 = process_tenkan(hhv26_shift52, llv26_shift52)
 senkou_a_52 = process_senkou_a(tenkan_shift52, kijun_shift52)

 hhv52_shift52 = higher_high(data_all_list, 52, 52)
 llv52_shift52 = lower_low(data_all_list, 52, 52)
 senkou_b_52 = process_senkou_b(hhv52_shift52, llv52_shift52)

 try:
 percent_price_kijun = (price - kijun) / kijun * 100
 percent_chikou_price_26day = (chikou - price_26day_ago) / price_26day_ago * 100
 percent_tenkan_kijun = (tenkan - kijun) / kijun * 100
 percent_price_cloud = (price - max(senkou_a, senkou_b)) / max(senkou_a, senkou_b) * 100
 percent_senkou_a_senkou_b = (senkou_a - senkou_b) / senkou_b * 100
 percent_chikou_cloud = (chikou - max(senkou_a_52, senkou_b_52)) / max(senkou_a_52, senkou_b_52) * 100
 except Exception:
 continue

 if price > kijun:
 radars_list += [
 (radar_price_above_kijun, symbol_id, symbol, price, tenkan, kijun, senkou_a, senkou_b, chikou,
 price_26day_ago, percent_price_kijun, percent_chikou_price_26day, percent_tenkan_kijun,
 percent_price_cloud, percent_senkou_a_senkou_b, percent_chikou_cloud)]

 if chikou > price_26day_ago and process_price_day_ago > 0:
 radars_list += [
 (radar_chikou_above_26day_ago, symbol_id, symbol, price, tenkan, kijun, senkou_a, senkou_b,
 chikou, price_26day_ago, percent_price_kijun, percent_chikou_price_26day,
 percent_tenkan_kijun,
 percent_price_cloud, percent_senkou_a_senkou_b, percent_chikou_cloud)]

 if tenkan > kijun:
 radars_list += [
 (radar_tenkan_above_kijun, symbol_id, symbol, price, tenkan, kijun, senkou_a, senkou_b, chikou,
 price_26day_ago, percent_price_kijun, percent_chikou_price_26day, percent_tenkan_kijun,
 percent_price_cloud, percent_senkou_a_senkou_b, percent_chikou_cloud)]

 if (price > senkou_a) or (price > senkou_b):
 radars_list += [
 (radar_price_above_cloud, symbol_id, symbol, price, tenkan, kijun, senkou_a, senkou_b, chikou,
 price_26day_ago, percent_price_kijun, percent_chikou_price_26day, percent_tenkan_kijun,
 percent_price_cloud, percent_senkou_a_senkou_b, percent_chikou_cloud)]

 if senkou_a > senkou_b:
 radars_list += [(radar_green_cloud, symbol_id, symbol, price, tenkan, kijun, senkou_a, senkou_b, chikou,
 price_26day_ago, percent_price_kijun, percent_chikou_price_26day, percent_tenkan_kijun,
 percent_price_cloud, percent_senkou_a_senkou_b, percent_chikou_cloud)]

 if chikou > max(senkou_a_52, senkou_b_52):
 radars_list += [
 (radar_chikou_above_cloud, symbol_id, symbol, price, tenkan, kijun, senkou_a, senkou_b, chikou,
 price_26day_ago, percent_price_kijun, percent_chikou_price_26day, percent_tenkan_kijun,
 percent_price_cloud, percent_senkou_a_senkou_b, percent_chikou_cloud)]

 str_date = self.date.strftime("%Y-%m-%d")
 # print radars_list
 for radars, symbol_id, symbol, price, tenkan, kijun, senkou_a, senkou_b, chikou, price_26, percent_price_kijun, percent_chikou_price_26day, percent_tenkan_kijun, percent_price_senkou_a, percent_senkou_a_senkou_b, percent_chikou_cloud in radars_list:
 if radars == 352:
 radars_dictionary[radars].append((symbol_id, symbol, 352, percent_price_kijun))
 elif radars == 353:
 radars_dictionary[radars].append((symbol_id, symbol, 353, percent_chikou_price_26day))
 elif radars == 354:
 radars_dictionary[radars].append((symbol_id, symbol, 354, percent_tenkan_kijun))
 elif radars == 355:
 radars_dictionary[radars].append((symbol_id, symbol, 355, percent_price_senkou_a))
 elif radars == 356:
 radars_dictionary[radars].append((symbol_id, symbol, 356, percent_senkou_a_senkou_b))
 elif radars == 357:
 radars_dictionary[radars].append((symbol_id, symbol, 357, percent_chikou_cloud))
 sorted(radars_dictionary.items())

 radar_352_symbol = radars_dictionary[radar_price_above_kijun]
 radar_353_symbol = radars_dictionary[radar_chikou_above_26day_ago]
 radar_354_symbol = radars_dictionary[radar_tenkan_above_kijun]
 radar_355_symbol = radars_dictionary[radar_price_above_cloud]
 radar_356_symbol = radars_dictionary[radar_green_cloud]
 radar_357_symbol = radars_dictionary[radar_chikou_above_cloud]

 radar_352_symbol.sort(key=operator.itemgetter(3), reverse=True)
 radar_353_symbol.sort(key=operator.itemgetter(3), reverse=True)
 radar_354_symbol.sort(key=operator.itemgetter(3), reverse=True)
 radar_355_symbol.sort(key=operator.itemgetter(3), reverse=True)
 radar_356_symbol.sort(key=operator.itemgetter(3), reverse=True)
 radar_357_symbol.sort(key=operator.itemgetter(3), reverse=True)

 limit_top = 24
 insert_to_db_str = """
 INSERT INTO stockradars.DAILY_RADARS
 (I_security, D_trade, I_radars, I_order)
 VALUES ({symbol_id}, '{datadate}', {radar_id}, {radar_order})
 """
 # engine.execute(
 #
 # ).fetchall()

 for i in range(len(radar_352_symbol)):
 if i >= limit_top:
 break
 engine_web.execute(insert_to_db_str.format(symbol_id=radar_352_symbol[i][0], datadate=str_date,
 radar_id=radar_352_symbol[i][2],
 radar_order=i + 1))

 for i in range(len(radar_353_symbol)):
 if i >= limit_top:
 break
 engine_web.execute(insert_to_db_str.format(symbol_id=radar_353_symbol[i][0], datadate=str_date,
 radar_id=radar_353_symbol[i][2],
 radar_order=i + 1))

 for i in range(len(radar_354_symbol)):
 if i >= limit_top:
 break
 engine_web.execute(insert_to_db_str.format(symbol_id=radar_354_symbol[i][0], datadate=str_date,
 radar_id=radar_354_symbol[i][2],
 radar_order=i + 1))

 for i in range(len(radar_355_symbol)):
 if i >= limit_top:
 break
 engine_web.execute(insert_to_db_str.format(symbol_id=radar_355_symbol[i][0], datadate=str_date,
 radar_id=radar_355_symbol[i][2],
 radar_order=i + 1))

 for i in range(len(radar_356_symbol)):
 if i >= limit_top:
 break
 engine_web.execute(insert_to_db_str.format(symbol_id=radar_356_symbol[i][0], datadate=str_date,
 radar_id=radar_356_symbol[i][2],
 radar_order=i + 1))

 for i in range(len(radar_357_symbol)):
 if i >= limit_top:
 break
 engine_web.execute(insert_to_db_str.format(symbol_id=radar_357_symbol[i][0], datadate=str_date,
 radar_id=radar_357_symbol[i][2],
 radar_order=i + 1))

 # print " "
 # print "PriceAboveKijun\t\t\t", len(radar_352_symbol), "\t", radar_352_symbol
 # print "ChikouAbove26DaysAgo\t", len(radar_353_symbol), "\t", radar_353_symbol
 # print "TenkanAboveKijun\t\t", len(radar_354_symbol), "\t", radar_354_symbol
 # print "PriceAboveCloud\t\t\t", len(radar_355_symbol), "\t", radar_355_symbol
 # print "GreenCloud\t\t\t\t", len(radar_356_symbol), "\t", radar_356_symbol
 # print "ChikouAboveCloud\t\t", len(radar_357_symbol), "\t", radar_357_symbol


if __name__ == '__main__':
 luigi.run()

After
ปล. if ที่ main code ยังไม่สวยเท่าไหร่นัก

import datetime
import time
import luigi
import operator

from collections import defaultdict
from sqlalchemy import create_engine
from cons import const

"""
 PROCESS ICHIMOKU KINKO HYO RADARS EOD
"""


def higher_high(data_list, day, shift_day):
 if day + 1 + shift_day > len(data_list):
 return 0
 data_spec_days = data_list[shift_day:day + shift_day]
 higher_high_data = max(data_spec_days, key=lambda item: item['high'])
 higher_high_value = higher_high_data['high']
 lower_low_data = min(data_spec_days, key=lambda item: item['low'])
 lower_low_value = lower_low_data['low']
 if lower_low_value == 0:
 return 0
 return higher_high_value


def lower_low(data_list, day, shift_day):
 if day + 1 + shift_day > len(data_list):
 return 0
 data_spec_days = data_list[shift_day:day + shift_day]
 lower_low_data = min(data_spec_days, key=lambda item: item['low'])
 lower_low_value = lower_low_data['low']
 return lower_low_value


def process_day_ago(data_list, day):
 data = data_list[day]
 price_26day_ago = data['datadate']
 return price_26day_ago


def process_price_day_ago(data_list, day):
 if day + 1 <= len(data_list) - 1:
 data = data_list[day]
 price_26day_ago = data['cclose']
 else:
 price_26day_ago = 0.0
 return price_26day_ago


def process_tenkan(data_all_list, shift_previous_day=0):
 hhv9 = higher_high(data_all_list, 9, shift_previous_day)
 llv9 = lower_low(data_all_list, 9, shift_previous_day)
 if hhv9 > 0 and llv9 > 0:
 tenkan_value = (float(hhv9) + float(llv9)) / 2
 else:
 tenkan_value = 0.0
 return tenkan_value


def process_kijun(data_all_list, shift_previous_day=0):
 hhv26 = higher_high(data_all_list, 26, shift_previous_day)
 llv26 = lower_low(data_all_list, 26, shift_previous_day)
 if hhv26 > 0 and llv26 > 0:
 kijun_value = (float(hhv26) + float(llv26)) / 2
 else:
 kijun_value = 0.0
 return kijun_value


def process_senkou_a(data_all_list, shift_previous_day):
 tenkan = process_tenkan(data_all_list, shift_previous_day)
 kijun = process_kijun(data_all_list, shift_previous_day)
 if tenkan > 0 and kijun > 0:
 senkou_a = (float(tenkan) + float(kijun)) / 2
 else:
 senkou_a = 0.0
 return senkou_a


def process_senkou_b(data_all_list, shift_previous_day):
 senkou_b = 0.0
 hhv52 = higher_high(data_all_list, 52, shift_previous_day)
 llv52 = lower_low(data_all_list, 52, shift_previous_day)
 if hhv52 > 0 and llv52 > 0:
 senkou_b = (float(hhv52) + float(llv52)) / 2
 return senkou_b


def percent_price_kijun(price, kijun):
 return abs((price - kijun) / kijun)


def percent_chikou_price_26day(chikou, price_26day_ago):
 return abs((chikou - price_26day_ago) / price_26day_ago)


def percent_tenkan_kijun(tenkan, kijun):
 return abs((tenkan - kijun) / kijun)


def percent_price_cloud(price, senkou_a, senkou_b):
 return abs((price - max(senkou_a, senkou_b)) / max(senkou_a, senkou_b))


def percent_senkoua_senkoub(senkou_a, senkou_b):
 return abs((senkou_a - senkou_b) / senkou_b)


def percent_chikou_cloud(chikou, senkou_a_52, senkou_b_52):
 return abs((chikou - max(senkou_a_52, senkou_b_52)) / max(senkou_a_52, senkou_b_52))


def insert_to_db(engine_web, radar_list, radar_id, datadate):
 insert_str = """
 INSERT INTO stockradars.DAILY_RADARS
 (I_security, D_trade, I_radars, I_order)
 VALUES ({symbol_id}, '{datadate}', {radar_id}, {radar_order})
 """
 i = 0
 for row in radar_list[:24]:
 i += 1
 engine_web.execute(insert_str.format(symbol_id=row['symbol_id'], datadate=datadate,
 radar_id=radar_id,
 radar_order=i))


def radar_is_price_above_kijun(price, kijun):
 return price > kijun


def radar_is_price_below_kijun(price, kijun):
 return price < kijun


def radar_is_chikou_above_price26days(chikou, price_26day_ago):
 return chikou > price_26day_ago > 0


def radar_is_chikou_below_price26days(chikou, price_26day_ago):
 return chikou < price_26day_ago > 0


def radar_is_tenkan_above_kijun(tenkan, kijun):
 return tenkan > kijun


def radar_is_tenkan_below_kijun(tenkan, kijun):
 return tenkan < kijun


def radar_is_price_above_cloud(price, senkou_a, senkou_b):
 return price > max(senkou_a, senkou_b)


def radar_is_price_below_cloud(price, senkou_a, senkou_b):
 return price < min(senkou_a, senkou_b)


def radar_is_green_cloud(senkou_a, senkou_b):
 return senkou_a > senkou_b


def radar_is_red_cloud(senkou_a, senkou_b):
 return senkou_a < senkou_b


def radar_is_chikou_above_cloud(chikou, senkou_a_52, senkou_b_52):
 return chikou > max(senkou_a_52, senkou_b_52)


def radar_is_chikou_below_cloud(chikou, senkou_a_52, senkou_b_52):
 return chikou < min(senkou_a_52, senkou_b_52)


class Ichimoku(luigi.Task):
 date = luigi.DateParameter(default=datetime.date.today())

 def run(self):
 engine = create_engine(const.data_db_host())
 engine_web = create_engine(const.web_db())

 symbol_list = engine_web.execute("""
 SELECT
 I_symbol,
 N_symbol
 FROM stockradars.MAP_ID
 JOIN stockgrid.data_all_stock D ON symbol = N_symbol
 WHERE D.[Security Type] = 'S'
 """).fetchall()
 symbol_data = {row['N_symbol']: row['I_symbol'] for row in symbol_list}

 radar_price_above_kijun = 352
 radar_chikou_above_26day_ago = 353
 radar_tenkan_above_kijun = 354
 radar_price_above_cloud = 355
 radar_green_cloud = 356
 radar_chikou_above_cloud = 357
 radar_price_below_kijun = 358
 radar_chikou_below_26day_ago = 359
 radar_tenkan_below_kijun = 360
 radar_price_below_cloud = 361
 radar_red_cloud = 362
 radar_chikou_below_cloud = 363

 fetch_string = """
 SELECT TOP 105
 symbol,
 datadate,
 CAST(high AS float) AS high,
 CAST(cclose AS float) AS cclose,
 CAST(low AS float) AS low
 FROM history
 WHERE symbol = '{symbol}'
 AND datadate <= '{datadate}'
 AND datadate > DATEADD(DAY, -365, '{datadate}')
 AND CAST(volume AS float) > 0.0
 AND CAST(high AS float) > 0.0
 AND CAST(low AS float) > 0.0
 AND CAST(cclose AS float) > 0.0
 ORDER BY datadate DESC;
 """
 result = defaultdict(list)
 for symbol, symbol_id in symbol_data.iteritems():
 data_all_list = engine.execute(
 fetch_string.format(symbol=symbol, datadate=self.date.strftime("%Y-%m-%d"))).fetchall()
 if not data_all_list:
 continue

 shift_day_26 = 26
 shift_day_52 = 52
 max_day_cal_26 = 27
 max_day_cal_78 = 79
 max_day_cal_104 = 105

 latest = data_all_list[0]
 price = latest['cclose']
 stock = dict()
 stock['symbol_id'] = symbol_id
 stock['symbol'] = symbol
 stock['price'] = price
 stock['chikou'] = price
 stock['tenkan'] = process_tenkan(data_all_list)
 stock['kijun'] = process_kijun(data_all_list)
 stock['chikou'] = price
 stock['price_26day_ago'] = process_price_day_ago(data_all_list, shift_day_26)
 stock['senkou_a'] = process_senkou_a(data_all_list, shift_day_26)
 stock['senkou_b'] = process_senkou_b(data_all_list, shift_day_26)
 stock['senkou_a_52'] = process_senkou_a(data_all_list, shift_day_52)
 stock['senkou_b_52'] = process_senkou_b(data_all_list, shift_day_52)

 if len(data_all_list) >= max_day_cal_26:
 if radar_is_price_above_kijun(stock['price'], stock['kijun']):
 stock[radar_price_above_kijun] = percent_price_kijun(stock['price'], stock['kijun'])
 result[radar_price_above_kijun].append(stock)

 if radar_is_chikou_above_price26days(stock['chikou'], stock['price_26day_ago']):
 stock[radar_chikou_above_26day_ago] = percent_chikou_price_26day(stock['chikou'],
 stock['price_26day_ago'])
 result[radar_chikou_above_26day_ago].append(stock)

 if radar_is_tenkan_above_kijun(stock['tenkan'], ['kijun']):
 stock[radar_chikou_above_26day_ago] = percent_tenkan_kijun(stock['tenkan'], ['kijun'])
 result[radar_tenkan_above_kijun].append(stock)

 if radar_is_price_below_kijun(stock['price'], stock['kijun']):
 stock[radar_price_below_kijun] = percent_price_kijun(stock['price'], stock['kijun'])
 result[radar_price_below_kijun].append(stock)

 if radar_is_chikou_below_price26days(stock['chikou'], stock['price_26day_ago']):
 stock[radar_chikou_below_26day_ago] = percent_chikou_price_26day(stock['chikou'],
 stock['price_26day_ago'])
 result[radar_chikou_below_26day_ago].append(stock)

 if radar_is_tenkan_below_kijun(stock['tenkan'], stock['kijun']):
 stock[radar_tenkan_below_kijun] = percent_tenkan_kijun(stock['tenkan'], stock['kijun'])
 result[radar_tenkan_below_kijun].append(stock)

 if len(data_all_list) >= max_day_cal_78:
 if radar_is_price_above_cloud(stock['price'], stock['senkou_a'], stock['senkou_b']):
 stock[radar_price_above_cloud] = percent_price_cloud(stock['price'], stock['senkou_a'],
 stock['senkou_b'])
 result[radar_price_above_cloud].append(stock)

 if radar_is_green_cloud(stock['senkou_a'], (stock['senkou_b'])):
 stock[radar_green_cloud] = percent_senkoua_senkoub(stock['senkou_a'], stock['senkou_b'])
 result[radar_green_cloud].append(stock)

 if radar_is_price_below_cloud(stock['price'], stock['senkou_a'], stock['senkou_b']):
 stock[radar_price_below_cloud] = percent_price_cloud(stock['price'], stock['senkou_a'],
 stock['senkou_b'])
 result[radar_price_below_cloud].append(stock)

 if radar_is_red_cloud(stock['senkou_a'], stock['senkou_b']):
 stock[radar_red_cloud] = percent_senkoua_senkoub(stock['senkou_a'], stock['senkou_b'])
 result[radar_red_cloud].append(stock)

 if len(data_all_list) >= max_day_cal_104:
 if radar_is_chikou_above_cloud(stock['chikou'], stock['senkou_a_52'], stock['senkou_b_52']):
 stock[radar_chikou_above_cloud] = percent_chikou_cloud(stock['chikou'], stock['senkou_a_52'],
 stock['senkou_b_52'])
 result[radar_chikou_above_cloud].append(stock)

 if radar_is_chikou_below_cloud(stock['chikou'], stock['senkou_a_52'], stock['senkou_b_52']):
 stock[radar_chikou_below_cloud] = percent_chikou_cloud(stock['chikou'], stock['senkou_a_52'],
 stock['senkou_b_52'])
 result[radar_chikou_below_cloud].append(stock)

 str_date = self.date.strftime("%Y-%m-%d")

 for radar_id, value in result.iteritems():
 sorted_list = sorted(value, key=lambda k: k[radar_id], reverse=True)
 print(radar_id, len(sorted_list), sorted_list, " ")
 # insert_to_db(engine_web, sorted_list, radar_id, str_date)

if __name__ == '__main__':
 luigi.run()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s