gj_lo_4H_GBPJPY
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © fujimoto_ka

//@version=5
strategy("gj_lo_4H_GBPJPY", overlay=true, margin_long=100, margin_short=100,commission_type = strategy.commission.cash_per_contract,commission_value = 0.009,default_qty_value = 1000)


length = input.int(20, minval=1)
maType = input.string("EMA", "Basis MA Type", options = ["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
src = input(close, title="Source")
mult = input.float(1.9, minval=0.001, maxval=50, title="StdDev")

ma(source, length, _type) =>
    switch _type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

basis = ma(src, length, maType)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
offset = input.int(0, "Offset", minval = -500, maxval = 500)
plot(basis, "Basis", color=#FF6D00, offset = offset)
p1 = plot(upper, "Upper", color=#2962FF, offset = offset)
p2 = plot(lower, "Lower", color=#2962FF, offset = offset)
fill(p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))


emano = ta.ema(close ,800)
emano4 = ta.ema(close ,3200)
plot( emano,color = color.red  )
plot( emano4,color = color.rgb(12, 8, 8)  )


ema_macd1 =ta.ema(close ,12)
ema_macd2 =ta.ema(close ,26)
ema_macd3 =ta.ema(close ,12*6)
ema_macd4 =ta.ema(close ,26*6)
ema_macd =ta.ema(close ,3)
ema_macd0 =ta.ema(close ,6)
plot( ema_macd,color = color.rgb(143, 164, 4)  )
plot( ema_macd0,color = color.rgb(131, 136, 3)  )

plot( ema_macd1,color = color.rgb(91, 255, 82)  )
plot( ema_macd2,color = color.rgb(63, 235, 6)  )
plot( ema_macd3,color = color.rgb(82, 255, 243)  )
plot( ema_macd4,color = color.rgb(18, 224, 243)  )

macd_short_0 =  ema_macd < ema_macd0
macd_long_0 =  ema_macd > ema_macd0
macd_short =  ema_macd1 < ema_macd2 - 0.03
macd_long =  ema_macd1 > ema_macd2
macd_short_2 =  ema_macd3 < ema_macd4
macd_long_2 =  ema_macd3 > ema_macd4

macd_trend_0  =ta.ema(close[1] ,3) - ema_macd < 0 ? true:false
macd_trend  =ta.ema(close[1] ,12) - ema_macd1 < 0 ? true:false
macd_trend2  =ta.ema(close[1] ,12*16) - ema_macd3 < 0 ? true:false

MA_short_trend =macd_short and macd_short_2
MA_long_trend = macd_long and macd_long_2

//perfect_sort
isPerfect_sort = open[0] - close[0]  >= 0.1 and high[1] < high[0] and low[1] > low[0] and close[0] < open[0] and open[1] >= close[0] and close[0]<low[1]   ?  true:na
//陰線ツツミ　ショート
isTUTUMI_1 =   open[0] - close[0]  >= 0.1 and high[1] < high[0] and low[1] > low[0] and close[0] < open[0] and open[1] >= close[0]    ?  true:na
//上髭残しショート　一つ前陰線
isTUTUMI_7 =   open[1] - close[1]  >= 0 and open[0] - close[0]  >= 0 and high[1] > close[0] and open[1] > low[0] and open[1] >= close[0] and close[1] >= close[0]   and  open[1] < high[0]   ?  true:na
//上髭残しショート　一つ前陽線
isTUTUMI_8 =   open[1] - close[1]  <= 0 and open[0] - close[0]  >= 0 and high[1] > close[0] and open[1] > low[0] and open[1] >= close[0] and close[1] >= close[0]      ?  true:na


//陽線
isTUTUMI_2 =   open[0] - close[0]  <= 0 and high[1] > high[0] and low[1] > low[0] and close[0] > open[0] and close[0] > open[1]  ?  true:na
//陽線
isTUTUMI_3 =   open[0] - close[0]  <= 0 and high[1] < close[0] and open[1] > low[0] and close[1] > open[0]  ?  true:na
//陽線ツツミ　一つ前陰線
isTUTUMI_4 =   open[1] - close[1]  >= 0 and open[0] - close[0]  <= 0 and high[1] < close[0] and open[1] > low[0]   ?  true:na
//上髭残しロング 一つ前が陰線
isTUTUMI_5 =   open[1] - close[1]  >= 0 and open[0] - close[0]  <= 0 and high[1] > close[0] and open[1] > low[0] and open[1] <= close[0] and close[1] <= close[0]   ?  true:na
//した髭残し陽線ロング　一つ前陽線
isTUTUMI_6 =   open[1] - close[1]  <= 0 and open[0] - close[0]  <= 0 and high[1] < close[0] and open[1] >= low[0] and open[1] <= close[0] and close[1] <= close[0]   ?  true:na
//した髭残し陽線ロング　一つ前陽線 一つ前のhighがcloseより高い
isTUTUMI_9 =   open[1] - close[1]  <= 0 and open[0] - close[0]  <= 0 and high[1] > close[0] and open[1] >= low[0] and open[1] <= close[0] and close[1] <= close[0]   ?  true:na


isTUTUMI = isTUTUMI_1 or isTUTUMI_2 or isTUTUMI_3 or isTUTUMI_4 or isTUTUMI_5 or isTUTUMI_6 or isTUTUMI_7 or isTUTUMI_8 or isTUTUMI_9 ? true:false
isTUTUMI_short = (close[0] > emano) and (isTUTUMI_1 or isTUTUMI_7 or isTUTUMI_8)? true:false
isTUTUMI_long = (close[0] < emano) and (isTUTUMI_2 or isTUTUMI_3 or isTUTUMI_4 or isTUTUMI_5 or isTUTUMI_6 or isTUTUMI_9)? true:false
isTUTUMI_long_without_ema =  isTUTUMI_2 or isTUTUMI_3 or isTUTUMI_4 or isTUTUMI_5 or isTUTUMI_6 or isTUTUMI_9? true:false
isTUTUMI_short_without_ema = isTUTUMI_1 or isTUTUMI_7 or isTUTUMI_8? true:false



//=====================================================================
val_min_1H	=	ta.lowest(low,6)
val_max_1H	=	ta.highest(high ,6 )
val_max_1H_within	=	ta.highest(high ,4 )
val_min_4H	=	ta.lowest(low ,18 )

val_min_5m	=	math.min(low[	0	],low[	1	],low[	2	])																			
val_max_5m	=	math.max(high[	0	],high[	1	],high[	2	])	

val_min_1bar	=	low[	0	]
val_max_1bar	=	high[	0	]

val_min_0day	=	math.min(low[	0	],low[	1	],low[	2	],low[	3	],low[	4	],low[	5	])
val_max_0day	=	math.max(high[	0	],high[	1	],high[	2	],high[	3	],high[	4	],high[	5	])

val_min_1day	=	math.min(low[	1	],low[	2	],low[	3	],low[	4	],low[	5	],low[	6	])
val_max_1day	=	math.max(high[	1	],high[	2	],high[	3	],high[	4	],high[	5	],high[	6	])
val_min_2day	=	math.min(low[	7	],low[	8	],low[	9	],low[	10	],low[	11	],low[	12	])
val_max_2day	=	math.max(high[	7	],high[	8	],high[	9	],high[	10	],high[	11	],high[	12	])
val_min_3day	=	math.min(low[	13	],low[	14	],low[	15	],low[	16	],low[	17	],low[	18	])
val_max_3day	=	math.max(high[	13	],high[	14	],high[	15	],high[	16	],high[	17	],high[	18	])
val_min_4day	=	math.min(low[	19	],low[	20	],low[	21	],low[	22	],low[	23	],low[	24	])
val_max_4day	=	math.max(high[	19	],high[	20	],high[	21	],high[	22	],high[	23	],high[	24	])
val_min_5day	=	math.min(low[	25	],low[	26	],low[	27	],low[	28	],low[	29	],low[	30	])
val_max_5day	=	math.max(high[	25	],high[	26	],high[	27	],high[	28	],high[	29	],high[	30	])
val_min_6day	=	math.min(low[	31	],low[	32	],low[	33	],low[	34	],low[	35	],low[	36	])
val_max_6day	=	math.max(high[	31	],high[	32	],high[	33	],high[	34	],high[	35	],high[	36	])
val_min_7day	=	math.min(low[	37	],low[	38	],low[	39	],low[	40	],low[	41	],low[	42	])
val_max_7day	=	math.max(high[	37	],high[	38	],high[	39	],high[	40	],high[	41	],high[	42	])
val_min_8day	=	math.min(low[	43	],low[	44	],low[	45	],low[	46	],low[	47	],low[	48	])
val_max_8day	=	math.max(high[	43	],high[	44	],high[	45	],high[	46	],high[	47	],high[	48	])
val_min_9day	=	math.min(low[	49	],low[	50	],low[	51	],low[	52	],low[	53	],low[	54	])
val_max_9day	=	math.max(high[	49	],high[	50	],high[	51	],high[	52	],high[	53	],high[	54	])
val_min_10day	=	math.min(low[	55	],low[	56	],low[	57	],low[	58	],low[	59	],low[	60	])
val_max_10day	=	math.max(high[	55	],high[	56	],high[	57	],high[	58	],high[	59	],high[	60	])

dif_min_max_day0	=math.abs(	val_max_0day	-	val_min_0day	)
dif_min_max_day1	=math.abs(	val_max_1day	-	val_min_1day	)
dif_min_max_day2	=math.abs(	val_max_2day	-	val_min_2day	)
dif_min_max_day3	=math.abs(	val_max_3day	-	val_min_3day	)
dif_min_max_day4	=math.abs(	val_max_4day	-	val_min_4day	)
dif_min_max_day5	=math.abs(	val_max_5day	-	val_min_5day	)
dif_min_max_day6   =math.abs(val_max_6day-val_min_6day)
dif_min_max_day7   =math.abs(val_max_7day-val_min_7day)
dif_min_max_day8   =math.abs(val_max_8day-val_min_8day)
dif_min_max_day9   =math.abs(val_max_9day-val_min_9day)
dif_min_max_day10  =math.abs(val_max_10day-val_min_10day)

BigVol = 2
too_near_BigVol = (dif_min_max_day0>BigVol) 
too_near_BigVol2 = (dif_min_max_day0>BigVol) or (dif_min_max_day1>BigVol) 


limithaba = 1.5
DayVol_Under =  (dif_min_max_day4 > limithaba)or (dif_min_max_day5 > limithaba)
// DayVol_Under = (dif_min_max_day1 > limithaba) or (dif_min_max_day2 > limithaba)or (dif_min_max_day3 > limithaba)or (dif_min_max_day4 > limithaba)or (dif_min_max_day5 > limithaba)
DayVol_Under2 = (dif_min_max_day6 > limithaba) or (dif_min_max_day7 > limithaba)or (dif_min_max_day8 > limithaba)
// DayVol_Under2 = (dif_min_max_day6 > limithaba) or (dif_min_max_day7 > limithaba)or (dif_min_max_day8 > limithaba)or (dif_min_max_day9 > limithaba)or (dif_min_max_day10 > limithaba)

Underhaba = 1.1

DayVol_within = (dif_min_max_day1 < Underhaba) or (dif_min_max_day2 < Underhaba)or (dif_min_max_day3 < Underhaba)or (dif_min_max_day4 < Underhaba)or (dif_min_max_day5 < Underhaba) or (dif_min_max_day6 < Underhaba)
DayVol_within2 = (dif_min_max_day6 < Underhaba) 
// DayVol_within2 = (dif_min_max_day6 < Underhaba) or (dif_min_max_day7 < Underhaba)or (dif_min_max_day8 < Underhaba)or (dif_min_max_day9 < Underhaba)or (dif_min_max_day10 < Underhaba)


val_min_0	=	math.min(low[	1	],low[	2	],low[	3	],low[	4	],low[	5	],low[	6	],low[	7	],low[	8	],low[	9	],low[	10	],low[	11	],low[	12	],low[	13	],low[	14	],low[	15	],low[	16	],low[	17	],low[	18	],low[	19	],low[	20	],low[	21	],low[	22	],low[	23	],low[	24	],low[	25	],low[	26	],low[	27	],low[	28	],low[	29	],low[	30	])
val_max_0	=	math.max(high[	1	],high[	2	],high[	3	],high[	4	],high[	5	],high[	6	],high[	7	],high[	8	],high[	9	],high[	10	],high[	11	],high[	12	],high[	13	],high[	14	],high[	15	],high[	16	],high[	17	],high[	18	],high[	19	],high[	20	],high[	21	],high[	22	],high[	23	],high[	24	],high[	25	],high[	26	],high[	27	],high[	28	],high[	29	],high[	30	])
val_min_1	=	math.min(low[	31	],low[	32	],low[	33	],low[	34	],low[	35	],low[	36	],low[	37	],low[	38	],low[	39	],low[	40	],low[	41	],low[	42	],low[	43	],low[	44	],low[	45	],low[	46	],low[	47	],low[	48	],low[	49	],low[	50	],low[	51	],low[	52	],low[	53	],low[	54	],low[	55	],low[	56	],low[	57	],low[	58	],low[	59	],low[	60	])
val_max_1	=	math.max(high[	31	],high[	32	],high[	33	],high[	34	],high[	35	],high[	36	],high[	37	],high[	38	],high[	39	],high[	40	],high[	41	],high[	42	],high[	43	],high[	44	],high[	45	],high[	46	],high[	47	],high[	48	],high[	49	],high[	50	],high[	51	],high[	52	],high[	53	],high[	54	],high[	55	],high[	56	],high[	57	],high[	58	],high[	59	],high[	60	])
val_min_2	=	math.min(low[	61	],low[	62	],low[	63	],low[	64	],low[	65	],low[	66	],low[	67	],low[	68	],low[	69	],low[	70	],low[	71	],low[	72	],low[	73	],low[	74	],low[	75	],low[	76	],low[	77	],low[	78	],low[	79	],low[	80	],low[	81	],low[	82	],low[	83	],low[	84	],low[	85	],low[	86	],low[	87	],low[	88	],low[	89	],low[	90	])
val_max_2	=	math.max(high[	61	],high[	62	],high[	63	],high[	64	],high[	65	],high[	66	],high[	67	],high[	68	],high[	69	],high[	70	],high[	71	],high[	72	],high[	73	],high[	74	],high[	75	],high[	76	],high[	77	],high[	78	],high[	79	],high[	80	],high[	81	],high[	82	],high[	83	],high[	84	],high[	85	],high[	86	],high[	87	],high[	88	],high[	89	],high[	90	])
val_min_3	=	math.min(low[	91	],low[	92	],low[	93	],low[	94	],low[	95	],low[	96	],low[	97	],low[	98	],low[	99	],low[	100	],low[	101	],low[	102	],low[	103	],low[	104	],low[	105	],low[	106	],low[	107	],low[	108	],low[	109	],low[	110	],low[	111	],low[	112	],low[	113	],low[	114	],low[	115	],low[	116	],low[	117	],low[	118	],low[	119	],low[	120	])
val_max_3	=	math.max(high[	91	],high[	92	],high[	93	],high[	94	],high[	95	],high[	96	],high[	97	],high[	98	],high[	99	],high[	100	],high[	101	],high[	102	],high[	103	],high[	104	],high[	105	],high[	106	],high[	107	],high[	108	],high[	109	],high[	110	],high[	111	],high[	112	],high[	113	],high[	114	],high[	115	],high[	116	],high[	117	],high[	118	],high[	119	],high[	120	])
val_min_4	=	math.min(low[	121	],low[	122	],low[	123	],low[	124	],low[	125	],low[	126	],low[	127	],low[	128	],low[	129	],low[	130	],low[	131	],low[	132	],low[	133	],low[	134	],low[	135	],low[	136	],low[	137	],low[	138	],low[	139	],low[	140	],low[	141	],low[	142	],low[	143	],low[	144	],low[	145	],low[	146	],low[	147	],low[	148	],low[	149	],low[	150	])
val_max_4	=	math.max(high[	121	],high[	122	],high[	123	],high[	124	],high[	125	],high[	126	],high[	127	],high[	128	],high[	129	],high[	130	],high[	131	],high[	132	],high[	133	],high[	134	],high[	135	],high[	136	],high[	137	],high[	138	],high[	139	],high[	140	],high[	141	],high[	142	],high[	143	],high[	144	],high[	145	],high[	146	],high[	147	],high[	148	],high[	149	],high[	150	])
dif_min_max_3	=math.abs(	val_max_3	-	val_min_3	)																																																																																																																																																																																																																
dif_min_max_1	=math.abs(	val_max_1	-	val_min_1	)																																																																																																																																																																																																																
dif_min_max_0	=math.abs(	val_max_0	-	val_min_0	)																																																																																																																																																																																																																
dif_min_max_4	=math.abs(	val_max_4	-	val_min_4	)																																																																																																																																																																																																																
dif_min_max_2	=math.abs(	val_max_2	-	val_min_2	)

dif_max_min = math.abs(	dif_min_max_4 + dif_min_max_0+dif_min_max_1+dif_min_max_2+dif_min_max_3)/5*1.2

week_nehaba_ave_up =(dif_min_max_0 > dif_max_min)

plotchar(week_nehaba_ave_up,location = location.top,char = '周',size = size.tiny,color =color.rgb(9, 19, 19)  )

haba = 0.4
now_max_condition_1H = (val_max_1H - close[0]) < 0.2
no_near_big_down = ((val_max_1H - val_min_1H) < haba)
near_min_50 = ( close[0] - val_min_4H) > dif_min_max_0 /2.9
no_near_max_within_30 =(val_max_1H_within - close[0] ) > dif_min_max_0 /5

plotchar(near_min_50,location = location.top,char = '二',size = size.tiny,color =color.rgb(105, 248, 246)  )

plotchar(no_near_max_within_30,location = location.bottom,char = '一',size = size.tiny,color =color.rgb(248, 31, 78)  )

kiriage = low[1]<low[0]
kirisage = high[1]>high[0] and open[0] > close[0]
// plotchar(kirisage,location = location.top,char = '♪',size = size.tiny,color =color.rgb(8, 10, 10)  )
// plotchar(kiriage,location = location.bottom,char = '♪',size = size.tiny,color =color.rgb(8, 10, 10)  )


close_Without_lower = (lower > val_min_5m)
close_Without_uper = (upper < high[0])
plotchar(close_Without_lower,location = location.bottom,char = 'w',size = size.tiny,color =color.rgb(105, 248, 246))

loss_haba = close[0] - val_min_1H
tooBig_loss_haba = loss_haba > 1.2

plotchar(tooBig_loss_haba,location = location.bottom,char = 'w',size = size.normal,color =color.rgb(105, 248, 246))

barcolor(isTUTUMI and week_nehaba_ave_up ? color.rgb(255, 235, 59, 61) : na)


hr_25 = hour == 18  ?  true:false

hr_0 = hour == 0  ?  true:false
hr_1 = hour == 1  ?  true:false
hr_2 = hour == 2  ?  true:false
hr_3 = hour == 3  ?  true:false
hr_14 = hour == 14  ?  true:false
hr_15 = hour == 15  ?  true:false
hr_16 = hour == 3  ?  true:false
hr_17 = hour == 4  ?  true:false
hr_18 = hour == 5  ?  true:false
hr_19 = hour == 6  ?  true:false

hr_8 = hour == 8  ?  true:false
hr_9 = hour == 9  ?  true:false
hr_10 = hour == 10  ?  true:false
hr_11 = hour == 11  ?  true:false
hr_12 = hour == 12  ?  true:false
hr_13 = hour == 13  ?  true:false
hr_20 = hour == 20  ?  true:false
hr_21 = hour == 21  ?  true:false
hr_24 = hour == 24  ?  true:false
hr_22 = hour == 16  ?  true:false
hr_23 = hour == 23  ?  true:false
min_0 = minute == 0
min_15 = minute == 15
min_30 = minute == 30
min_45  = minute == 45

loss_val = val_max_1H - close[0]
profit_val = 50

bgcolor(hr_1? color.rgb(248, 163, 229, 85)  : na)

longCondition = (close_Without_uper) 
if (longCondition ) 
    strategy.entry("My Entry Id_9", strategy.long,comment = "En")
