 iMoku (Ichimoku Complete Tool) - The Quant Science 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © thequantscience

/////////////////////////////// Design by The Quant Science ™  ////////////////////////////////////

start = 1000

//@version=5
strategy(" iMoku - The Quant Science ™ ", 
             overlay = true, 
             default_qty_type = strategy.percent_of_equity, 
             default_qty_value = 100,
             currency = currency.EUR, 
             initial_capital = start, 
             commission_type = strategy.commission.percent, 
             commission_value = 0.10,
             slippage = 1) 

conversionPeriods = input.int(
         9, 
         minval=1, 
         title="Tenkan Sen Length", 
         group = "Indicator configuration"
         )
basePeriods = input.int(
         26, minval=1, 
         title="Kjun Sen Length", 
         group = "Indicator configuration"
         )
laggingSpan2Periods = input.int(
         52, 
         minval=1, 
         title="Leading Span B Length", 
         group = "Indicator configuration"
         )
displacement = input.int(
         26, minval=1, 
         title="Lagging Span", 
         group = "Indicator configuration"
         )

strategy_1 = input.bool(
         false, 
         title = "Tenkan Sen - Kijun Sen Strategy", 
         group = "Tenkan Sen - Kijun Sen Strategy Configuration"
         )
strategy_2 = input.bool(
         false, 
         title = "Price - Kijun Sen Strategy", 
         group = "Price - Kijun Sen Strategy Configuration"
         )
strategy_3 = input.bool(
         false, 
         title = "Kumo Breakout Strategy", 
         group = "Kumo Breakout Strategy Configuration"
         )
strategy_4 = input.bool(
         false, 
         title = "Kumo Twist Strategy", 
         group = "Kumo Twist Strategy Configuration"
         )
stop_loss = input.float(
         defval = 1, 
         minval = 0.01, 
         step = 0.05, 
         maxval = 500, 
         title = "Stop Loss", 
         group = "Kumo Breakout Strategy Configuration" 
         )
take_profit = input.float(
         defval = 1, 
         minval = 0.01, 
         step = 0.05, 
         maxval = 500, 
         title = "Take Profit", 
         group = "Kumo Breakout Strategy Configuration"
         )

// ########################################################################################################################
// ########################################################################################################################
// ########################################################################################################################
// ########################################################################################################################
// ########################################################################################################################
// ########################################################################################################################
// ########################################################################################################################

donchian(len) => math.avg(
         ta.lowest(
             len
             ), 
             ta.highest(
                 len
                 ))
conversionLine = donchian(
     conversionPeriods
     )
baseLine = donchian(
     basePeriods
     )
leadLine1 = math.avg(
     conversionLine, 
     baseLine
     )
leadLine2 = donchian(
     laggingSpan2Periods
     )

adj_leadLine1 = leadLine1[displacement -1]
adj_leadLine2 = leadLine2[displacement -1]

// 1. Cross Tenkan Sen - Kijun Sen 

price_over_kumo = close > adj_leadLine1
price_under_kumo = close < adj_leadLine2
price_in_kumo = close > adj_leadLine1 
     and close < adj_leadLine2

strong_xover_tenkan_kijun = ta.crossover(
     conversionLine, 
     baseLine
     ) 
     and price_over_kumo
neutro_xover_tenkan_kijun = ta.crossover(
     conversionLine, 
     baseLine
     ) 
     and price_in_kumo
light_xover_tenkan_kijun = ta.crossover(
     conversionLine, 
     baseLine
     ) 
     and price_under_kumo

close_tenkan_kijun = ta.crossunder(
     conversionLine, 
     baseLine
     )

SelectTenkanKijun(type) =>

	switch type 

		"Strong Crossover Tenkan Sen - Kijun Sen" => strong_xover_tenkan_kijun
		"Neutro Crossover Tenkan Sen - Kijun Sen" => neutro_xover_tenkan_kijun
		"Light Crossover Tenkan Sen - Kijun Sen" => light_xover_tenkan_kijun		

type_input = input.string(
     "Strong Crossover Tenkan Sen - Kijun Sen",
     title = "Select strategy: ",
     options = ["Strong Crossover Tenkan Sen - Kijun Sen", "Neutro Crossover Tenkan Sen - Kijun Sen", "Light Crossover Tenkan Sen - Kijun Sen" ],
     tooltip = "",
     group = "Tenkan Sen - Kijun Sen Strategy Configuration"
     )

select_tenkan_kijun = SelectTenkanKijun(
     type_input
     ) 
     and barstate.isconfirmed
price_entry_tenkan_kijun = ta.valuewhen(
     select_tenkan_kijun, 
     close, 
     0
     )

// 2. Cross Price - Kijun Sen 

strong_xover_price_kijun = ta.crossover(
         close, 
         baseLine
         ) 
         and price_over_kumo
neutro_xover_price_kijun  = ta.crossover(
         close, 
         baseLine
         ) 
         and price_in_kumo
light_xover_price_kijun = ta.crossover(
         close, 
         baseLine
         ) 
         and price_under_kumo

close_price_kijun = ta.crossunder(
     close,
     baseLine
     )

SelectPriceKijun(type) =>

	switch type 

		"Strong Crossover Price - Kijun Sen" => strong_xover_price_kijun
		"Neutro Crossover Price - Kijun Sen" => neutro_xover_price_kijun
		"Light Crossover Price - Kijun Sen" => light_xover_price_kijun		

type_input_2 = input.string(
     "Strong Crossover Price - Kijun Sen",
     title = "Select strategy: ",
     options = ["Strong Crossover Price - Kijun Sen", "Neutro Crossover Price - Kijun Sen", "Light Crossover Price - Kijun Sen" ],
     tooltip = "",
     group = "Price - Kijun Sen Strategy Configuration"
     )

select_price_kijun = SelectPriceKijun(
     type_input_2
     ) 
     and barstate.isconfirmed
price_entry_price_kijun = ta.valuewhen(
     select_price_kijun, 
     close, 
     0
     )

// 3. Kumo breakout 

kumo_breakout = ta.crossover(
     close, 
     adj_leadLine1
     ) 

// 4. Kumo Twist 

strong_kumo_twist = ta.crossover(
     adj_leadLine1, 
     adj_leadLine2
     ) 
     and price_over_kumo

light_kumo_twist = ta.crossover(
     adj_leadLine1, 
     adj_leadLine2
     ) 
     and price_under_kumo
close_kumo_twist = ta.crossunder(
     adj_leadLine1, 
     adj_leadLine2)

SelectKumoTwist(type) =>

	switch type 

		"Strong Crossover Kumo Twist" => strong_kumo_twist
		"Light Crossover Kumo Twist" => light_kumo_twist		

type_input_3 = input.string(
     "Strong Crossover Kumo Twist",
     title = "Select strategy: ",
     options = ["Strong Crossover Kumo Twist", "Light Crossover Kumo Twist" ],
     tooltip = "",
     group = "Kumo Twist Strategy Configuration"
     )

select_kumo_twist = SelectKumoTwist(
     type_input_3
     ) 
     and barstate.isconfirmed
price_entry_kumo_twist = ta.valuewhen(
     select_kumo_twist, 
     close, 
     0
     )

// ########################################################################################################################

var tb = table.new(
     position.bottom_left, 
     1, 1, 
     bgcolor = color.new(
         color.blue, 
         90
         ))

if barstate.isfirst
    table.cell(
      tb, 
      0, 0, 
      'Developed by The Quant Science™'
      ,text_size = size.normal
      ,text_color = color.new(
           color.blue, 
           20
           ))

// ########################################################################################################################
// ########################################################################################################################
// ########################################################################################################################
// ########################################################################################################################
// ########################################################################################################################
// ########################################################################################################################

entry_string_1 = input.string(
     title = "Entry string:", 
     defval = "#######", 
     confirm = false, 
     group = "Tenkan Sen - Kijun Sen Strategy Configuration",
     tooltip = "Enter 3Commas or Cryptohopper string link. Enter only the open entry link in this field."
     )

exit_string_1 = input.string(
     title = "Exit string:", 
     defval = "#######", 
     confirm = false, 
     group = "Tenkan Sen - Kijun Sen Strategy Configuration",
     tooltip = "Enter 3Commas or Cryptohopper string link. Enter only the close exit link in this field."
     )

isLong  = false
isLong := nz(
     isLong[1], 
     false
     )

isSellLong  = false
isSellLong := nz(
     isSellLong[1], 
     false
     )

LONG = not isLong and select_tenkan_kijun

price_1 = ta.valuewhen(
     LONG, 
     close, 
     0
     )

lot_1 = start / price_1

CLOSE_LONG = not isSellLong and close_tenkan_kijun

if (LONG) and strategy_1 == true 

	isLong := true
	isSellLong := false
    strategy.entry(
         id = "Tenkan Kijun Open", 
         direction = strategy.long, 
         qty = lot_1, 
         limit = price_entry_tenkan_kijun
         )
    alert(
         entry_string_1, 
         alert.freq_once_per_bar
         )
		
if (CLOSE_LONG) and strategy_1 == true 

	isLong := false
	isSellLong := true
    strategy.close_all(
         comment = "Tenkan Kijun Closing"
         )
    alert(
         exit_string_1, 
         alert.freq_once_per_bar
         )

// ########################################################################################################################

entry_string_2 = input.string(
     title = "Entry string:", 
     defval = "#######", 
     confirm = false, 
     group = "Price - Kijun Sen Strategy Configuration",
     tooltip = "Enter 3Commas or Cryptohopper string link. Enter only the open entry link in this field."
     )

exit_string_2 = input.string(
     title = "Exit string:", 
     defval = "#######", 
     confirm = false, 
     group = "Price - Kijun Sen Strategy Configuration",
     tooltip = "Enter 3Commas or Cryptohopper string link. Enter only the close exit link in this field."
     )

isLong_2  = false
isLong_2 := nz(
     isLong_2[1], 
     false
     )

isSellLong_2 = false
isSellLong_2 := nz(
     isSellLong_2[1], 
     false
     )

LONG_2 = not isLong_2 and select_price_kijun

price_2 = ta.valuewhen(
     LONG_2, 
     close, 
     0
     )

lot_2 = start / price_2

CLOSE_LONG_2 = not isSellLong_2 and close_price_kijun

if (LONG_2) and strategy_2 == true 

	isLong_2 := true
	isSellLong_2 := false
    strategy.entry(
         id = "Price Kijun Open", 
         direction = strategy.long, 
         qty = lot_2, 
         limit = price_entry_price_kijun
         )
    alert(
         entry_string_2, 
         alert.freq_once_per_bar
         )

if (CLOSE_LONG_2) and strategy_2 == true 

	isLong_2 := false
	isSellLong_2 := true
    strategy.close_all(comment = "Price Kijun Closing")
    alert(
         exit_string_2, 
         alert.freq_once_per_bar
         )

// ########################################################################################################################

entry_string_3 = input.string(
     title = "Entry string:", 
     defval = "#######", 
     confirm = false, 
     group = "Kumo Breakout Strategy Configuration",
     tooltip = "Enter 3Commas or Cryptohopper string link. Enter only the open entry link in this field."
     )

exit_string_3 = input.string(
     title = "Exit string:", 
     defval = "#######", 
     confirm = false, 
     group = "Kumo Breakout Strategy Configuration",
     tooltip = "Enter 3Commas or Cryptohopper string link. Enter only the close exit link in this field."
     )

isLong_3  = false
isLong_3 := nz(
     isLong_3[1], 
     false
     )

isSellLong_3  = false
isSellLong_3 := nz(
     isSellLong_3[1], 
     false 
     )

LONG_3 = not isLong_3 and kumo_breakout 
price_kumobrk = ta.valuewhen(
     LONG_3, 
     close, 
     0
     )
lot_3 = start / price_kumobrk
take_profit_kumobrk_price = (
     price_kumobrk * (
          100 + 
          take_profit
          ) / 100
          )
stop_loss_kumobrk_price = (
     price_kumobrk * (
         100 - 
         stop_loss
         ) / 100
         )

if (LONG_3) and strategy_3 == true 

	isLong_3 := true
	isSellLong_3 := false
    strategy.entry(
         id = "Kumo Breakout Open", 
         direction = strategy.long, 
         qty = lot_3, 
         limit = price_kumobrk
         )
    alert(
         entry_string_3, 
         alert.freq_once_per_bar
         )

if (take_profit_kumobrk_price) 

	isLong_3 := false
	isSellLong_3 := true
    strategy.exit(
         id = "Kumo Breakout Closing", 
         from_entry = "Kumo Breakout Open", 
         limit = take_profit_kumobrk_price, 
         stop = stop_loss_kumobrk_price
         )
    alert(
         exit_string_3, 
         alert.freq_once_per_bar
         )

if (stop_loss_kumobrk_price) 

	isLong_3 := false
	isSellLong_3 := true
    strategy.exit(
         id = "Kumo Breakout Closing", 
         from_entry = "Kumo Breakout Open", 
         limit = take_profit_kumobrk_price, 
         stop = stop_loss_kumobrk_price
         )
    alert(
         exit_string_3, 
         alert.freq_once_per_bar
         )

// ########################################################################################################################

entry_string_4 = input.string(
     title = "Entry string:", 
     defval = "#######", 
     confirm = false, 
     group = "Kumo Twist Strategy Configuration",
     tooltip = "Enter 3Commas or Cryptohopper string link. Enter only the open entry link in this field."
     )

exit_string_4 = input.string(
     title = "Exit string:", 
     defval = "#######", 
     confirm = false, 
     group = "Kumo Twist Strategy Configuration",
     tooltip = "Enter 3Commas or Cryptohopper string link. Enter only the close exit link in this field."
     )

isLong_4  = false
isLong_4 := nz(
     isLong_4[1], 
     false
     )

isSellLong_4  = false
isSellLong_4 := nz(
     isSellLong_4[1], 
     false
     )

LONG_4 = not isLong_4 and select_kumo_twist
price_4 = ta.valuewhen(
     LONG_4, 
     close, 
     0
     )
lot_4 = start / price_4
CLOSE_LONG_4 = not isSellLong_4 and close_kumo_twist

if (LONG_4) and strategy_4 == true 

	isLong_4 := true
	isSellLong_4 := false
    strategy.entry(
         id = "Kumo Twist Open", 
         direction = strategy.long, 
         qty = lot_4, 
         limit = price_entry_kumo_twist
         )
    alert(
         entry_string_4, 
         alert.freq_once_per_bar
         )

if (CLOSE_LONG_4) and strategy_4 == true 

	isLong_4 := false
	isSellLong_4 := true
    strategy.close_all(comment = "Kumo Twist Closing")
    alert(
         exit_string_4, 
         alert.freq_once_per_bar
         )

// ########################################################################################################################
// ########################################################################################################################
// ########################################################################################################################
// ########################################################################################################################
// ########################################################################################################################

plot(
     conversionLine, 
     color =color.new(
          color.aqua, 
          65), 
          title = "Conversion Line or Tenkan Sen"
          )
plot(
     baseLine, 
     color=color.new(
         color.purple, 
         65), 
         title="Base Line or Kijun Sen"
         )
plot(
     close, 
     offset = -displacement + 1, 
     color= color.new(
         color.gray, 
         85
         ), 
         title="Lagging Span Chokou"
         )
p1 = plot(
     leadLine1, 
     offset = displacement - 1, 
     color= color.aqua,
	 title="Leading Span A"
     )
p2 = plot(
     leadLine2, 
     offset = displacement - 1, 
     color= color.aqua,
	 title="Leading Span B"
     )
fill(
     p1, 
     p2, 
     color = leadLine1 > leadLine2 ? color.rgb(
         21, 149, 253, 90
         ) : color.rgb(
             21, 149, 253, 90
             ))

// ########################################################################################################################
// ########################################################################################################################
// ########################################################################################################################
// ########################################################################################################################
// ########################################################################################################################
