ComiCo - Joel on Crypto - MACD Scalping
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5
strategy(title="ComiCo - Joel on Crypto - MACD Scalping", shorttitle="ComiCo - Joel on Crypto - MACD Scalping", overlay=false, default_qty_type = strategy.percent_of_equity, default_qty_value=100, margin_long=1./50*100, margin_short=1./50*100, pyramiding = 14)

//Get High impact USD related economic news from MyFxBook converted to Pinescript's matching formats
var EventNameTypes = array.from("ISM Manufacturing PMI", "ADP Employment Change", "Markit Services/Composite PMI", "Markit Manufacturing PMI", "Balance of Trade", "Jobless Claims", "ISM Non-Manufacturing PMI", "Non Farm Payrolls", "CPI", "Inflation Rate", "Fed Chair", "Retail Sales", "Michigan Consumer Sentiment", "Treasury Secretary", "Existing Home Sales", "Durable Goods Orders", "Fed Interest Rate Decision", "GDP", "Goods Trade Balance", "New Home Sales", "Personal Income/Spending", "FOMC Minutes", "Fed Press Conference", "FakeTypeForTesting")
var EventTypeAvoidance = array.new_bool(array.size(EventNameTypes), false)
var EventTimestamps = array.from(timestamp(2021, 01, 04, 12, 45, 00), timestamp(2021, 01, 05, 13, 00, 00), timestamp(2021, 01, 06, 11, 15, 00), timestamp(2021, 01, 06, 12, 45, 00), timestamp(2021, 01, 07, 11, 30, 00), timestamp(2021, 01, 07, 11, 30, 00), timestamp(2021, 01, 07, 13, 00, 00), timestamp(2021, 01, 08, 11, 30, 00), timestamp(2021, 01, 13, 11, 30, 00), timestamp(2021, 01, 14, 11, 30, 00), timestamp(2021, 01, 14, 15, 30, 00), timestamp(2021, 01, 15, 11, 30, 00), timestamp(2021, 01, 15, 13, 00, 00), timestamp(2021, 01, 19, 13, 00, 00), timestamp(2021, 01, 21, 11, 30, 00), timestamp(2021, 01, 22, 12, 45, 00), timestamp(2021, 01, 22, 12, 45, 00), timestamp(2021, 01, 22, 13, 00, 00), timestamp(2021, 01, 27, 11, 30, 00), timestamp(2021, 01, 27, 17, 00, 00), timestamp(2021, 01, 27, 17, 30, 00), timestamp(2021, 01, 28, 11, 30, 00), timestamp(2021, 01, 28, 11, 30, 00), timestamp(2021, 01, 28, 11, 30, 00), timestamp(2021, 01, 28, 13, 00, 00), timestamp(2021, 01, 29, 11, 30, 00), timestamp(2021, 01, 29, 13, 00, 00), timestamp(2021, 02, 01, 12, 45, 00), timestamp(2021, 02, 01, 13, 00, 00), timestamp(2021, 02, 03, 11, 15, 00), timestamp(2021, 02, 03, 12, 45, 00), timestamp(2021, 02, 03, 13, 00, 00), timestamp(2021, 02, 04, 11, 30, 00), timestamp(2021, 02, 05, 11, 30, 00), timestamp(2021, 02, 05, 11, 30, 00), timestamp(2021, 02, 10, 11, 30, 00), timestamp(2021, 02, 10, 17, 00, 00), timestamp(2021, 02, 11, 11, 30, 00), timestamp(2021, 02, 12, 13, 00, 00), timestamp(2021, 02, 17, 11, 30, 00), timestamp(2021, 02, 17, 17, 00, 00), timestamp(2021, 02, 18, 11, 30, 00), timestamp(2021, 02, 19, 12, 45, 00), timestamp(2021, 02, 19, 12, 45, 00), timestamp(2021, 02, 19, 13, 00, 00), timestamp(2021, 02, 23, 13, 00, 00), timestamp(2021, 02, 24, 13, 00, 00), timestamp(2021, 02, 24, 13, 00, 00), timestamp(2021, 02, 25, 11, 30, 00), timestamp(2021, 02, 25, 11, 30, 00), timestamp(2021, 02, 26, 11, 30, 00), timestamp(2021, 02, 26, 11, 30, 00), timestamp(2021, 02, 26, 13, 00, 00), timestamp(2021, 03, 01, 12, 45, 00), timestamp(2021, 03, 01, 13, 00, 00), timestamp(2021, 03, 03, 11, 15, 00), timestamp(2021, 03, 03, 12, 45, 00), timestamp(2021, 03, 03, 13, 00, 00), timestamp(2021, 03, 04, 11, 30, 00), timestamp(2021, 03, 04, 15, 05, 00), timestamp(2021, 03, 05, 11, 30, 00), timestamp(2021, 03, 05, 11, 30, 00), timestamp(2021, 03, 10, 11, 30, 00), timestamp(2021, 03, 11, 11, 30, 00), timestamp(2021, 03, 12, 13, 00, 00), timestamp(2021, 03, 16, 10, 30, 00), timestamp(2021, 03, 17, 16, 00, 00), timestamp(2021, 03, 17, 16, 30, 00), timestamp(2021, 03, 18, 10, 30, 00), timestamp(2021, 03, 22, 11, 00, 00), timestamp(2021, 03, 22, 12, 00, 00), timestamp(2021, 03, 23, 12, 00, 00), timestamp(2021, 03, 23, 14, 00, 00), timestamp(2021, 03, 24, 10, 30, 00), timestamp(2021, 03, 24, 11, 45, 00), timestamp(2021, 03, 24, 11, 45, 00), timestamp(2021, 03, 24, 12, 00, 00), timestamp(2021, 03, 25, 10, 30, 00), timestamp(2021, 03, 26, 09, 30, 00), timestamp(2021, 03, 26, 09, 30, 00), timestamp(2021, 03, 26, 11, 00, 00), timestamp(2021, 03, 31, 09, 15, 00), timestamp(2021, 04, 01, 09, 30, 00), timestamp(2021, 04, 01, 10, 45, 00), timestamp(2021, 04, 01, 11, 00, 00), timestamp(2021, 04, 02, 09, 30, 00), timestamp(2021, 04, 05, 10, 45, 00), timestamp(2021, 04, 05, 11, 00, 00), timestamp(2021, 04, 07, 09, 30, 00), timestamp(2021, 04, 07, 15, 00, 00), timestamp(2021, 04, 08, 09, 30, 00), timestamp(2021, 04, 08, 13, 00, 00), timestamp(2021, 04, 13, 09, 30, 00), timestamp(2021, 04, 14, 13, 00, 00), timestamp(2021, 04, 15, 09, 30, 00), timestamp(2021, 04, 15, 09, 30, 00), timestamp(2021, 04, 16, 11, 00, 00), timestamp(2021, 04, 22, 09, 30, 00), timestamp(2021, 04, 22, 11, 00, 00), timestamp(2021, 04, 23, 10, 45, 00), timestamp(2021, 04, 23, 10, 45, 00), timestamp(2021, 04, 23, 11, 00, 00), timestamp(2021, 04, 26, 09, 30, 00), timestamp(2021, 04, 28, 09, 30, 00), timestamp(2021, 04, 28, 15, 00, 00), timestamp(2021, 04, 28, 15, 30, 00), timestamp(2021, 04, 29, 09, 30, 00), timestamp(2021, 04, 29, 09, 30, 00), timestamp(2021, 04, 30, 09, 30, 00), timestamp(2021, 04, 30, 11, 00, 00), timestamp(2021, 05, 03, 10, 45, 00), timestamp(2021, 05, 03, 11, 00, 00), timestamp(2021, 05, 03, 15, 20, 00), timestamp(2021, 05, 04, 09, 30, 00), timestamp(2021, 05, 05, 09, 15, 00), timestamp(2021, 05, 05, 10, 45, 00), timestamp(2021, 05, 05, 11, 00, 00), timestamp(2021, 05, 06, 09, 30, 00), timestamp(2021, 05, 07, 09, 30, 00), timestamp(2021, 05, 12, 09, 30, 00), timestamp(2021, 05, 13, 09, 30, 00), timestamp(2021, 05, 14, 09, 30, 00), timestamp(2021, 05, 14, 11, 00, 00), timestamp(2021, 05, 19, 15, 00, 00), timestamp(2021, 05, 20, 09, 30, 00), timestamp(2021, 05, 21, 10, 45, 00), timestamp(2021, 05, 21, 10, 45, 00), timestamp(2021, 05, 21, 11, 00, 00), timestamp(2021, 05, 25, 11, 00, 00), timestamp(2021, 05, 27, 09, 30, 00), timestamp(2021, 05, 27, 09, 30, 00), timestamp(2021, 05, 28, 09, 30, 00), timestamp(2021, 05, 28, 09, 30, 00), timestamp(2021, 05, 28, 11, 00, 00), timestamp(2021, 06, 01, 10, 45, 00), timestamp(2021, 06, 01, 11, 00, 00), timestamp(2021, 06, 03, 09, 15, 00), timestamp(2021, 06, 03, 09, 30, 00), timestamp(2021, 06, 03, 10, 45, 00), timestamp(2021, 06, 03, 11, 00, 00), timestamp(2021, 06, 04, 08, 00, 00), timestamp(2021, 06, 04, 09, 30, 00), timestamp(2021, 06, 08, 09, 30, 00), timestamp(2021, 06, 10, 09, 30, 00), timestamp(2021, 06, 10, 09, 30, 00), timestamp(2021, 06, 11, 11, 00, 00), timestamp(2021, 06, 15, 09, 30, 00), timestamp(2021, 06, 16, 15, 00, 00), timestamp(2021, 06, 16, 15, 30, 00), timestamp(2021, 06, 17, 09, 30, 00), timestamp(2021, 06, 22, 11, 00, 00), timestamp(2021, 06, 22, 15, 00, 00), timestamp(2021, 06, 23, 10, 45, 00), timestamp(2021, 06, 23, 10, 45, 00), timestamp(2021, 06, 23, 11, 00, 00), timestamp(2021, 06, 24, 09, 30, 00), timestamp(2021, 06, 24, 09, 30, 00), timestamp(2021, 06, 24, 09, 30, 00), timestamp(2021, 06, 25, 09, 30, 00), timestamp(2021, 06, 25, 11, 00, 00), timestamp(2021, 06, 30, 09, 15, 00), timestamp(2021, 07, 01, 09, 30, 00), timestamp(2021, 07, 01, 10, 45, 00), timestamp(2021, 07, 01, 11, 00, 00), timestamp(2021, 07, 02, 09, 30, 00), timestamp(2021, 07, 02, 09, 30, 00), timestamp(2021, 07, 06, 10, 45, 00), timestamp(2021, 07, 06, 11, 00, 00), timestamp(2021, 07, 07, 15, 00, 00), timestamp(2021, 07, 08, 09, 30, 00), timestamp(2021, 07, 13, 09, 30, 00), timestamp(2021, 07, 14, 13, 00, 00), timestamp(2021, 07, 15, 09, 30, 00), timestamp(2021, 07, 15, 10, 30, 00), timestamp(2021, 07, 16, 09, 30, 00), timestamp(2021, 07, 16, 11, 00, 00), timestamp(2021, 07, 22, 09, 30, 00), timestamp(2021, 07, 22, 11, 00, 00), timestamp(2021, 07, 23, 10, 45, 00), timestamp(2021, 07, 23, 10, 45, 00), timestamp(2021, 07, 26, 11, 00, 00), timestamp(2021, 07, 27, 09, 30, 00), timestamp(2021, 07, 28, 09, 30, 00), timestamp(2021, 07, 28, 15, 00, 00), timestamp(2021, 07, 28, 15, 30, 00), timestamp(2021, 07, 29, 09, 30, 00), timestamp(2021, 07, 29, 09, 30, 00), timestamp(2021, 07, 30, 09, 30, 00), timestamp(2021, 07, 30, 11, 00, 00), timestamp(2021, 08, 02, 10, 45, 00), timestamp(2021, 08, 02, 11, 00, 00), timestamp(2021, 08, 04, 09, 15, 00), timestamp(2021, 08, 04, 10, 45, 00), timestamp(2021, 08, 04, 11, 00, 00), timestamp(2021, 08, 05, 09, 30, 00), timestamp(2021, 08, 05, 09, 30, 00), timestamp(2021, 08, 06, 09, 30, 00), timestamp(2021, 08, 11, 09, 30, 00), timestamp(2021, 08, 12, 09, 30, 00), timestamp(2021, 08, 13, 11, 00, 00), timestamp(2021, 08, 17, 12, 30, 00), timestamp(2021, 08, 17, 17, 30, 00), timestamp(2021, 08, 18, 18, 00, 00), timestamp(2021, 08, 19, 12, 30, 00), timestamp(2021, 08, 23, 13, 45, 00), timestamp(2021, 08, 23, 13, 45, 00), timestamp(2021, 08, 23, 14, 00, 00), timestamp(2021, 08, 24, 14, 00, 00), timestamp(2021, 08, 25, 12, 30, 00), timestamp(2021, 08, 26, 12, 30, 00), timestamp(2021, 08, 27, 12, 30, 00), timestamp(2021, 08, 27, 12, 30, 00), timestamp(2021, 08, 27, 14, 00, 00), timestamp(2021, 08, 27, 14, 00, 00), timestamp(2021, 09, 01, 12, 15, 00), timestamp(2021, 09, 01, 13, 45, 00), timestamp(2021, 09, 01, 14, 00, 00), timestamp(2021, 09, 02, 12, 30, 00), timestamp(2021, 09, 02, 12, 30, 00), timestamp(2021, 09, 03, 12, 30, 00), timestamp(2021, 09, 03, 13, 45, 00), timestamp(2021, 09, 03, 14, 00, 00), timestamp(2021, 09, 09, 12, 30, 00), timestamp(2021, 09, 14, 12, 30, 00), timestamp(2021, 09, 16, 12, 30, 00), timestamp(2021, 09, 16, 12, 30, 00), timestamp(2021, 09, 17, 14, 00, 00), timestamp(2021, 09, 22, 14, 00, 00), timestamp(2021, 09, 22, 18, 00, 00), timestamp(2021, 09, 22, 18, 30, 00), timestamp(2021, 09, 23, 12, 30, 00), timestamp(2021, 09, 23, 13, 45, 00), timestamp(2021, 09, 23, 13, 45, 00), timestamp(2021, 09, 24, 14, 00, 00), timestamp(2021, 09, 27, 12, 30, 00), timestamp(2021, 09, 28, 12, 30, 00), timestamp(2021, 09, 28, 14, 00, 00), timestamp(2021, 09, 29, 14, 45, 00), timestamp(2021, 09, 30, 12, 30, 00), timestamp(2021, 10, 01, 12, 30, 00), timestamp(2021, 10, 01, 13, 45, 00), timestamp(2021, 10, 01, 14, 00, 00), timestamp(2021, 10, 01, 14, 00, 00), timestamp(2021, 10, 05, 12, 30, 00), timestamp(2021, 10, 05, 13, 45, 00), timestamp(2021, 10, 05, 14, 00, 00), timestamp(2021, 10, 06, 12, 15, 00), timestamp(2021, 10, 07, 12, 30, 00), timestamp(2021, 10, 08, 12, 30, 00), timestamp(2021, 10, 13, 12, 30, 00), timestamp(2021, 10, 13, 18, 00, 00), timestamp(2021, 10, 14, 12, 30, 00), timestamp(2021, 10, 15, 12, 30, 00), timestamp(2021, 10, 15, 14, 00, 00), timestamp(2021, 10, 21, 12, 30, 00), timestamp(2021, 10, 21, 14, 00, 00), timestamp(2021, 10, 22, 13, 45, 00), timestamp(2021, 10, 22, 13, 45, 00), timestamp(2021, 10, 22, 15, 00, 00), timestamp(2021, 10, 26, 14, 00, 00), timestamp(2021, 10, 27, 12, 30, 00), timestamp(2021, 10, 27, 12, 30, 00), timestamp(2021, 10, 28, 12, 30, 00), timestamp(2021, 10, 28, 12, 30, 00), timestamp(2021, 10, 29, 12, 30, 00), timestamp(2021, 10, 29, 14, 00, 00), timestamp(2021, 11, 01, 13, 45, 00), timestamp(2021, 11, 01, 14, 00, 00), timestamp(2021, 11, 03, 12, 15, 00), timestamp(2021, 11, 03, 13, 45, 00), timestamp(2021, 11, 03, 14, 00, 00), timestamp(2021, 11, 03, 18, 00, 00), timestamp(2021, 11, 03, 18, 30, 00), timestamp(2021, 11, 04, 12, 30, 00), timestamp(2021, 11, 04, 12, 30, 00), timestamp(2021, 11, 05, 12, 30, 00), timestamp(2021, 11, 08, 15, 30, 00), timestamp(2021, 11, 09, 14, 00, 00), timestamp(2021, 11, 10, 13, 30, 00), timestamp(2021, 11, 10, 13, 30, 00), timestamp(2021, 11, 12, 15, 00, 00), timestamp(2021, 11, 16, 13, 30, 00), timestamp(2021, 11, 18, 13, 30, 00), timestamp(2021, 11, 22, 15, 00, 00), timestamp(2021, 11, 23, 14, 45, 00), timestamp(2021, 11, 23, 14, 45, 00), timestamp(2021, 11, 24, 13, 30, 00), timestamp(2021, 11, 24, 13, 30, 00), timestamp(2021, 11, 24, 13, 30, 00), timestamp(2021, 11, 24, 15, 00, 00), timestamp(2021, 11, 24, 15, 00, 00), timestamp(2021, 11, 24, 15, 00, 00), timestamp(2021, 11, 24, 19, 00, 00), timestamp(2021, 11, 29, 20, 05, 00), timestamp(2021, 11, 30, 15, 00, 00), timestamp(2021, 12, 01, 13, 15, 00), timestamp(2021, 12, 01, 14, 45, 00), timestamp(2021, 12, 01, 15, 00, 00), timestamp(2021, 12, 01, 15, 00, 00), timestamp(2021, 12, 02, 13, 30, 00), timestamp(2021, 12, 03, 13, 30, 00), timestamp(2021, 12, 03, 14, 45, 00), timestamp(2021, 12, 03, 15, 00, 00), timestamp(2021, 12, 07, 13, 30, 00), timestamp(2021, 12, 09, 13, 30, 00), timestamp(2021, 12, 10, 13, 30, 00), timestamp(2021, 12, 10, 15, 00, 00), timestamp(2021, 12, 15, 13, 30, 00), timestamp(2021, 12, 15, 19, 00, 00), timestamp(2021, 12, 15, 19, 30, 00), timestamp(2021, 12, 16, 13, 30, 00), timestamp(2021, 12, 16, 14, 45, 00), timestamp(2021, 12, 16, 14, 45, 00), timestamp(2021, 12, 22, 15, 00, 00), timestamp(2021, 12, 23, 13, 30, 00), timestamp(2021, 12, 23, 13, 30, 00), timestamp(2021, 12, 23, 13, 30, 00), timestamp(2021, 12, 23, 15, 00, 00), timestamp(2021, 12, 23, 15, 00, 00), timestamp(2021, 12, 29, 13, 30, 00), timestamp(2021, 12, 30, 13, 30, 00), timestamp(2022, 01, 03, 14, 45, 00), timestamp(2022, 01, 04, 15, 00, 00), timestamp(2022, 01, 05, 13, 15, 00), timestamp(2022, 01, 05, 14, 45, 00), timestamp(2022, 01, 05, 19, 00, 00), timestamp(2022, 01, 06, 13, 30, 00), timestamp(2022, 01, 06, 13, 30, 00), timestamp(2022, 01, 06, 15, 00, 00), timestamp(2022, 01, 07, 13, 30, 00), timestamp(2022, 01, 11, 15, 00, 00), timestamp(2022, 01, 12, 13, 30, 00), timestamp(2022, 01, 13, 13, 30, 00), timestamp(2022, 01, 14, 13, 30, 00), timestamp(2022, 01, 14, 15, 00, 00), timestamp(2022, 01, 20, 13, 30, 00), timestamp(2022, 01, 20, 15, 00, 00), timestamp(2022, 01, 24, 14, 45, 00), timestamp(2022, 01, 24, 14, 45, 00), timestamp(2022, 01, 26, 13, 30, 00), timestamp(2022, 01, 26, 15, 00, 00), timestamp(2022, 01, 26, 19, 00, 00), timestamp(2022, 01, 26, 19, 30, 00), timestamp(2022, 01, 27, 13, 30, 00), timestamp(2022, 01, 27, 13, 30, 00), timestamp(2022, 01, 27, 13, 30, 00), timestamp(2022, 01, 28, 13, 30, 00), timestamp(2022, 01, 28, 15, 00, 00), timestamp(2022, 02, 01, 14, 45, 00), timestamp(2022, 02, 01, 15, 00, 00), timestamp(2022, 02, 02, 13, 15, 00), timestamp(2022, 02, 03, 13, 30, 00), timestamp(2022, 02, 03, 14, 45, 00), timestamp(2022, 02, 03, 15, 00, 00), timestamp(2022, 02, 04, 13, 30, 00), timestamp(2022, 02, 08, 13, 30, 00), timestamp(2022, 02, 10, 13, 30, 00), timestamp(2022, 02, 10, 13, 30, 00), timestamp(2022, 02, 11, 15, 00, 00), timestamp(2022, 02, 16, 13, 30, 00), timestamp(2022, 02, 16, 19, 00, 00), timestamp(2022, 02, 17, 13, 30, 00), timestamp(2022, 02, 18, 15, 00, 00), timestamp(2022, 02, 22, 14, 45, 00), timestamp(2022, 02, 22, 14, 45, 00), timestamp(2022, 02, 24, 13, 30, 00), timestamp(2022, 02, 24, 13, 30, 00), timestamp(2022, 02, 24, 15, 00, 00), timestamp(2022, 02, 25, 13, 30, 00), timestamp(2022, 02, 25, 13, 30, 00), timestamp(2022, 02, 25, 15, 00, 00), timestamp(2022, 02, 28, 13, 30, 00), timestamp(2022, 03, 01, 14, 45, 00), timestamp(2022, 03, 01, 15, 00, 00), timestamp(2022, 03, 02, 13, 15, 00), timestamp(2022, 03, 02, 15, 00, 00), timestamp(2022, 03, 03, 13, 30, 00), timestamp(2022, 03, 03, 14, 45, 00), timestamp(2022, 03, 03, 15, 00, 00), timestamp(2022, 03, 03, 15, 00, 00), timestamp(2022, 03, 04, 13, 30, 00), timestamp(2022, 03, 08, 13, 30, 00), timestamp(2022, 03, 10, 13, 30, 00), timestamp(2022, 03, 10, 13, 30, 00), timestamp(2022, 03, 11, 15, 00, 00), timestamp(2022, 03, 16, 12, 30, 00), timestamp(2022, 03, 16, 18, 00, 00), timestamp(2022, 03, 16, 18, 30, 00), timestamp(2022, 03, 17, 12, 30, 00), timestamp(2022, 03, 18, 14, 00, 00), timestamp(2022, 03, 21, 16, 00, 00), timestamp(2022, 03, 23, 12, 00, 00), timestamp(2022, 03, 23, 14, 00, 00), timestamp(2022, 03, 24, 12, 30, 00), timestamp(2022, 03, 24, 12, 30, 00), timestamp(2022, 03, 24, 13, 45, 00), timestamp(2022, 03, 24, 13, 45, 00), timestamp(2022, 03, 25, 14, 00, 00), timestamp(2022, 03, 28, 12, 30, 00), timestamp(2022, 03, 30, 12, 15, 00), timestamp(2022, 03, 31, 12, 30, 00), timestamp(2022, 03, 31, 12, 30, 00), timestamp(2022, 04, 01, 12, 30, 00), timestamp(2022, 04, 01, 13, 45, 00), timestamp(2022, 04, 01, 14, 00, 00), timestamp(2022, 04, 05, 12, 30, 00), timestamp(2022, 04, 05, 13, 45, 00), timestamp(2022, 04, 05, 14, 00, 00), timestamp(2022, 04, 06, 18, 00, 00), timestamp(2022, 04, 07, 12, 30, 00), timestamp(2022, 04, 12, 12, 30, 00), timestamp(2022, 04, 14, 12, 30, 00), timestamp(2022, 04, 14, 12, 30, 00), timestamp(2022, 04, 14, 14, 00, 00), timestamp(2022, 04, 20, 14, 00, 00), timestamp(2022, 04, 21, 12, 30, 00), timestamp(2022, 04, 21, 15, 00, 00), timestamp(2022, 04, 21, 17, 00, 00), timestamp(2022, 04, 22, 13, 45, 00), timestamp(2022, 04, 22, 13, 45, 00), timestamp(2022, 04, 26, 12, 30, 00), timestamp(2022, 04, 26, 14, 00, 00), timestamp(2022, 04, 27, 12, 30, 00), timestamp(2022, 04, 28, 12, 30, 00), timestamp(2022, 04, 28, 12, 30, 00), timestamp(2022, 04, 29, 12, 30, 00), timestamp(2022, 04, 29, 14, 00, 00), timestamp(2022, 05, 02, 13, 45, 00), timestamp(2022, 05, 02, 14, 00, 00), timestamp(2022, 05, 04, 12, 15, 00), timestamp(2022, 05, 04, 12, 30, 00), timestamp(2022, 05, 04, 13, 45, 00), timestamp(2022, 05, 04, 14, 00, 00), timestamp(2022, 05, 04, 18, 00, 00), timestamp(2022, 05, 04, 18, 30, 00), timestamp(2022, 05, 05, 12, 30, 00), timestamp(2022, 05, 06, 12, 30, 00), timestamp(2022, 05, 11, 12, 30, 00), timestamp(2022, 05, 12, 12, 30, 00), timestamp(2022, 05, 13, 14, 00, 00), timestamp(2022, 05, 17, 12, 30, 00), timestamp(2022, 05, 17, 18, 00, 00), timestamp(2022, 05, 19, 12, 30, 00), timestamp(2022, 05, 19, 14, 00, 00), timestamp(2022, 05, 24, 13, 45, 00), timestamp(2022, 05, 24, 13, 45, 00), timestamp(2022, 05, 24, 14, 00, 00), timestamp(2022, 05, 24, 16, 20, 00), timestamp(2022, 05, 25, 12, 30, 00), timestamp(2022, 05, 25, 18, 00, 00), timestamp(2022, 05, 26, 12, 30, 00), timestamp(2022, 05, 26, 12, 30, 00), timestamp(2022, 05, 27, 12, 30, 00), timestamp(2022, 05, 27, 12, 30, 00), timestamp(2022, 05, 27, 14, 00, 00), timestamp(2022, 06, 01, 13, 45, 00), timestamp(2022, 06, 01, 14, 00, 00), timestamp(2022, 06, 02, 12, 15, 00), timestamp(2022, 06, 02, 12, 30, 00), timestamp(2022, 06, 03, 12, 30, 00), timestamp(2022, 06, 03, 13, 45, 00), timestamp(2022, 06, 03, 14, 00, 00), timestamp(2022, 06, 07, 12, 30, 00), timestamp(2022, 06, 09, 12, 30, 00), timestamp(2022, 06, 10, 12, 30, 00), timestamp(2022, 06, 10, 14, 00, 00), timestamp(2022, 06, 15, 12, 30, 00), timestamp(2022, 06, 15, 18, 00, 00), timestamp(2022, 06, 15, 18, 30, 00), timestamp(2022, 06, 16, 12, 30, 00), timestamp(2022, 06, 17, 12, 45, 00), timestamp(2022, 06, 21, 14, 00, 00), timestamp(2022, 06, 22, 13, 30, 00), timestamp(2022, 06, 23, 12, 30, 00), timestamp(2022, 06, 23, 13, 45, 00), timestamp(2022, 06, 23, 13, 45, 00), timestamp(2022, 06, 23, 14, 00, 00), timestamp(2022, 06, 24, 14, 00, 00), timestamp(2022, 06, 24, 14, 00, 00), timestamp(2022, 06, 27, 12, 30, 00), timestamp(2022, 06, 28, 12, 30, 00), timestamp(2022, 06, 29, 13, 00, 00), timestamp(2022, 06, 30, 12, 30, 00), timestamp(2022, 06, 30, 12, 30, 00), timestamp(2022, 07, 01, 14, 00, 00), timestamp(2022, 07, 06, 14, 00, 00), timestamp(2022, 07, 06, 18, 00, 00), timestamp(2022, 07, 07, 12, 30, 00), timestamp(2022, 07, 07, 12, 30, 00), timestamp(2022, 07, 08, 12, 30, 00), timestamp(2022, 07, 13, 12, 30, 00), timestamp(2022, 07, 13, 12, 30, 00), timestamp(2022, 07, 14, 12, 30, 00), timestamp(2022, 07, 15, 12, 30, 00), timestamp(2022, 07, 15, 14, 00, 00), timestamp(2022, 07, 20, 14, 00, 00), timestamp(2022, 07, 21, 12, 30, 00), timestamp(2022, 07, 26, 14, 00, 00), timestamp(2022, 07, 27, 12, 30, 00), timestamp(2022, 07, 27, 12, 30, 00), timestamp(2022, 07, 27, 18, 00, 00), timestamp(2022, 07, 27, 18, 30, 00), timestamp(2022, 07, 28, 12, 30, 00), timestamp(2022, 07, 28, 12, 30, 00), timestamp(2022, 07, 29, 12, 30, 00), timestamp(2022, 07, 29, 14, 00, 00), timestamp(2022, 08, 01, 14, 00, 00), timestamp(2022, 08, 03, 14, 00, 00), timestamp(2022, 08, 04, 12, 30, 00), timestamp(2022, 08, 04, 12, 30, 00), timestamp(2022, 08, 05, 12, 30, 00), timestamp(2022, 08, 10, 12, 30, 00), timestamp(2022, 08, 10, 12, 30, 00), timestamp(2022, 08, 11, 12, 30, 00), timestamp(2022, 08, 12, 14, 00, 00), timestamp(2022, 08, 17, 12, 30, 00), timestamp(2022, 08, 17, 18, 00, 00), timestamp(2022, 08, 18, 12, 30, 00), timestamp(2022, 08, 18, 14, 00, 00), timestamp(2022, 08, 23, 14, 00, 00), timestamp(2022, 08, 24, 12, 30, 00), timestamp(2022, 08, 25, 12, 30, 00), timestamp(2022, 08, 25, 12, 30, 00), timestamp(2022, 08, 26, 12, 30, 00), timestamp(2022, 08, 26, 12, 30, 00), timestamp(2022, 08, 26, 14, 00, 00), timestamp(2022, 08, 26, 14, 00, 00), timestamp(2022, 08, 31, 12, 15, 00), timestamp(2022, 09, 01, 12, 30, 00), timestamp(2022, 09, 01, 14, 00, 00), timestamp(2022, 09, 02, 12, 30, 00), timestamp(2022, 09, 06, 14, 00, 00), timestamp(2022, 09, 07, 12, 30, 00), timestamp(2022, 09, 08, 12, 30, 00), timestamp(2022, 09, 13, 12, 30, 00), timestamp(2022, 09, 13, 12, 30, 00), timestamp(2022, 09, 15, 12, 30, 00), timestamp(2022, 09, 15, 12, 30, 00), timestamp(2022, 09, 16, 14, 00, 00), timestamp(2022, 09, 21, 14, 00, 00), timestamp(2022, 09, 21, 18, 00, 00), timestamp(2022, 09, 21, 18, 30, 00), timestamp(2022, 09, 22, 12, 30, 00), timestamp(2022, 09, 23, 18, 00, 00), timestamp(2022, 09, 27, 12, 30, 00), timestamp(2022, 09, 27, 14, 00, 00), timestamp(2022, 09, 28, 12, 30, 00), timestamp(2022, 09, 28, 14, 15, 00), timestamp(2022, 09, 29, 12, 30, 00), timestamp(2022, 09, 30, 12, 30, 00), timestamp(2022, 09, 30, 14, 00, 00), timestamp(2022, 10, 03, 14, 00, 00), timestamp(2022, 10, 05, 12, 15, 00), timestamp(2022, 10, 05, 12, 30, 00), timestamp(2022, 10, 05, 14, 00, 00), timestamp(2022, 10, 06, 12, 30, 00), timestamp(2022, 10, 07, 12, 30, 00), timestamp(2022, 10, 12, 18, 00, 00), timestamp(2022, 10, 13, 12, 30, 00), timestamp(2022, 10, 13, 12, 30, 00), timestamp(2022, 10, 13, 12, 30, 00), timestamp(2022, 10, 14, 12, 30, 00), timestamp(2022, 10, 14, 14, 00, 00), timestamp(2022, 10, 20, 12, 30, 00), timestamp(2022, 10, 20, 14, 00, 00), timestamp(2022, 10, 26, 12, 30, 00), timestamp(2022, 10, 26, 14, 00, 00), timestamp(2022, 10, 27, 12, 30, 00), timestamp(2022, 10, 27, 12, 30, 00), timestamp(2022, 10, 27, 12, 30, 00), timestamp(2022, 10, 28, 12, 30, 00), timestamp(2022, 10, 28, 14, 00, 00), timestamp(2022, 11, 01, 14, 00, 00), timestamp(2022, 11, 02, 12, 15, 00), timestamp(2022, 11, 02, 18, 00, 00), timestamp(2022, 11, 02, 18, 30, 00), timestamp(2022, 11, 03, 12, 30, 00), timestamp(2022, 11, 03, 12, 30, 00), timestamp(2022, 11, 03, 14, 00, 00), timestamp(2022, 11, 04, 12, 30, 00), timestamp(2022, 11, 10, 13, 30, 00), timestamp(2022, 11, 10, 13, 30, 00), timestamp(2022, 11, 10, 13, 30, 00), timestamp(2022, 11, 11, 15, 00, 00), timestamp(2022, 11, 16, 13, 30, 00), timestamp(2022, 11, 17, 13, 30, 00), timestamp(2022, 11, 18, 15, 00, 00), timestamp(2022, 11, 23, 13, 30, 00), timestamp(2022, 11, 23, 13, 30, 00), timestamp(2022, 11, 23, 15, 00, 00), timestamp(2022, 11, 23, 15, 00, 00), timestamp(2022, 11, 23, 19, 00, 00), timestamp(2022, 11, 30, 13, 15, 00), timestamp(2022, 11, 30, 13, 30, 00), timestamp(2022, 11, 30, 13, 30, 00), timestamp(2022, 11, 30, 18, 30, 00), timestamp(2022, 12, 01, 13, 30, 00), timestamp(2022, 12, 01, 13, 30, 00), timestamp(2022, 12, 01, 15, 00, 00), timestamp(2022, 12, 02, 13, 30, 00), timestamp(2022, 12, 05, 15, 00, 00), timestamp(2022, 12, 06, 13, 30, 00), timestamp(2022, 12, 08, 13, 30, 00), timestamp(2022, 12, 09, 15, 00, 00), timestamp(2022, 12, 13, 13, 30, 00), timestamp(2022, 12, 13, 13, 30, 00), timestamp(2022, 12, 14, 19, 00, 00), timestamp(2022, 12, 14, 19, 30, 00), timestamp(2022, 12, 15, 13, 30, 00), timestamp(2022, 12, 15, 13, 30, 00), timestamp(2022, 12, 21, 15, 00, 00), timestamp(2022, 12, 22, 13, 30, 00), timestamp(2022, 12, 23, 13, 30, 00), timestamp(2022, 12, 23, 13, 30, 00), timestamp(2022, 12, 23, 15, 00, 00), timestamp(2022, 12, 23, 15, 00, 00), timestamp(2022, 12, 27, 13, 30, 00), timestamp(2022, 12, 29, 13, 30, 00), timestamp(2023, 01, 04, 15, 00, 00), timestamp(2023, 01, 04, 19, 00, 00), timestamp(2023, 01, 05, 13, 15, 00), timestamp(2023, 01, 05, 13, 30, 00), timestamp(2023, 01, 05, 13, 30, 00), timestamp(2023, 01, 06, 13, 30, 00), timestamp(2023, 01, 06, 15, 00, 00), timestamp(2023, 01, 10, 14, 00, 00), timestamp(2023, 01, 12, 13, 30, 00), timestamp(2023, 01, 12, 13, 30, 00), timestamp(2023, 01, 12, 13, 30, 00), timestamp(2023, 01, 13, 15, 00, 00), timestamp(2023, 01, 18, 13, 30, 00), timestamp(2023, 01, 19, 13, 30, 00), timestamp(2023, 01, 20, 15, 00, 00), timestamp(2023, 01, 26, 13, 30, 00), timestamp(2023, 01, 26, 13, 30, 00), timestamp(2023, 01, 26, 13, 30, 00), timestamp(2023, 01, 26, 15, 00, 00), timestamp(2023, 01, 27, 13, 30, 00), timestamp(2023, 01, 27, 15, 00, 00), timestamp(2023, 02, 01, 13, 15, 00), timestamp(2023, 02, 01, 15, 00, 00), timestamp(2023, 02, 01, 19, 00, 00), timestamp(2023, 02, 01, 19, 30, 00), timestamp(2023, 02, 02, 13, 30, 00), timestamp(2023, 02, 03, 13, 30, 00), timestamp(2023, 02, 03, 15, 00, 00), timestamp(2023, 02, 07, 13, 30, 00), timestamp(2023, 02, 07, 17, 40, 00), timestamp(2023, 02, 09, 13, 30, 00), timestamp(2023, 02, 10, 15, 00, 00), timestamp(2023, 02, 14, 13, 30, 00), timestamp(2023, 02, 14, 13, 30, 00), timestamp(2023, 02, 15, 13, 30, 00), timestamp(2023, 02, 16, 13, 30, 00), timestamp(2023, 02, 21, 15, 00, 00), timestamp(2023, 02, 22, 19, 00, 00), timestamp(2023, 02, 23, 13, 30, 00), timestamp(2023, 02, 24, 13, 30, 00), timestamp(2023, 02, 24, 15, 00, 00), timestamp(2023, 02, 24, 15, 00, 00), timestamp(2023, 02, 27, 13, 30, 00), timestamp(2023, 02, 28, 13, 30, 00), timestamp(2023, 03, 01, 15, 00, 00), timestamp(2023, 03, 02, 13, 30, 00), timestamp(2023, 03, 03, 15, 00, 00), timestamp(2023, 03, 07, 15, 00, 00), timestamp(2023, 03, 08, 13, 15, 00), timestamp(2023, 03, 08, 13, 30, 00), timestamp(2023, 03, 09, 13, 30, 00), timestamp(2023, 03, 10, 13, 30, 00), timestamp(2023, 03, 15, 12, 30, 00), timestamp(2023, 03, 16, 12, 30, 00), timestamp(2023, 03, 17, 14, 00, 00), timestamp(2023, 03, 21, 14, 00, 00), timestamp(2023, 03, 22, 18, 00, 00), timestamp(2023, 03, 22, 18, 30, 00), timestamp(2023, 03, 23, 12, 30, 00), timestamp(2023, 03, 23, 14, 00, 00), timestamp(2023, 03, 24, 12, 30, 00), timestamp(2023, 03, 28, 12, 30, 00), timestamp(2023, 03, 30, 12, 30, 00), timestamp(2023, 03, 31, 12, 30, 00), timestamp(2023, 03, 31, 14, 00, 00), timestamp(2023, 04, 03, 14, 00, 00), timestamp(2023, 04, 05, 12, 15, 00), timestamp(2023, 04, 05, 12, 30, 00), timestamp(2023, 04, 05, 14, 00, 00), timestamp(2023, 04, 06, 12, 30, 00), timestamp(2023, 04, 07, 12, 30, 00), timestamp(2023, 04, 12, 12, 30, 00), timestamp(2023, 04, 12, 12, 30, 00), timestamp(2023, 04, 12, 18, 00, 00), timestamp(2023, 04, 13, 12, 30, 00), timestamp(2023, 04, 14, 12, 30, 00), timestamp(2023, 04, 14, 14, 00, 00), timestamp(2023, 04, 20, 12, 30, 00), timestamp(2023, 04, 20, 14, 00, 00), timestamp(2023, 04, 25, 14, 00, 00), timestamp(2023, 04, 26, 12, 30, 00), timestamp(2023, 04, 26, 12, 30, 00), timestamp(2023, 04, 27, 12, 30, 00), timestamp(2023, 04, 27, 12, 30, 00), timestamp(2023, 04, 28, 12, 30, 00), timestamp(2023, 04, 28, 14, 00, 00), timestamp(2023, 05, 01, 14, 00, 00), timestamp(2023, 05, 03, 12, 15, 00), timestamp(2023, 05, 03, 14, 00, 00), timestamp(2023, 05, 03, 18, 00, 00), timestamp(2023, 05, 03, 18, 30, 00), timestamp(2023, 05, 04, 12, 30, 00), timestamp(2023, 05, 04, 12, 30, 00), timestamp(2023, 05, 05, 12, 30, 00), timestamp(2023, 05, 10, 12, 30, 00), timestamp(2023, 05, 10, 12, 30, 00), timestamp(2023, 05, 11, 12, 30, 00), timestamp(2023, 05, 12, 14, 00, 00), timestamp(2023, 05, 16, 12, 30, 00), timestamp(2023, 05, 18, 12, 30, 00), timestamp(2023, 05, 18, 14, 00, 00), timestamp(2023, 05, 19, 15, 00, 00), timestamp(2023, 05, 23, 14, 00, 00), timestamp(2023, 05, 24, 18, 00, 00), timestamp(2023, 05, 25, 12, 30, 00), timestamp(2023, 05, 26, 12, 30, 00), timestamp(2023, 05, 26, 12, 30, 00), timestamp(2023, 05, 26, 12, 30, 00), timestamp(2023, 05, 26, 14, 00, 00), timestamp(2023, 06, 01, 12, 15, 00), timestamp(2023, 06, 01, 12, 30, 00), timestamp(2023, 06, 01, 14, 00, 00), timestamp(2023, 06, 02, 12, 30, 00), timestamp(2023, 06, 07, 12, 30, 00), timestamp(2023, 06, 08, 12, 30, 00), timestamp(2023, 06, 13, 12, 30, 00), timestamp(2023, 06, 13, 12, 30, 00), timestamp(2023, 06, 14, 18, 00, 00), timestamp(2023, 06, 14, 18, 30, 00), timestamp(2023, 06, 15, 12, 30, 00), timestamp(2023, 06, 15, 12, 30, 00), timestamp(2023, 06, 16, 14, 00, 00), timestamp(2023, 06, 22, 12, 30, 00), timestamp(2023, 06, 22, 14, 00, 00), timestamp(2023, 06, 27, 12, 30, 00), timestamp(2023, 06, 27, 14, 00, 00), timestamp(2023, 06, 28, 12, 30, 00), timestamp(2023, 06, 29, 12, 30, 00), timestamp(2023, 06, 30, 12, 30, 00), timestamp(2023, 06, 30, 14, 00, 00), timestamp(2023, 07, 03, 14, 00, 00), timestamp(2023, 07, 05, 18, 00, 00), timestamp(2023, 07, 06, 12, 15, 00), timestamp(2023, 07, 06, 12, 30, 00), timestamp(2023, 07, 06, 12, 30, 00), timestamp(2023, 07, 07, 12, 30, 00), timestamp(2023, 07, 12, 12, 30, 00), timestamp(2023, 07, 12, 12, 30, 00), timestamp(2023, 07, 13, 12, 30, 00), timestamp(2023, 07, 14, 14, 00, 00), timestamp(2023, 07, 18, 12, 30, 00), timestamp(2023, 07, 20, 12, 30, 00), timestamp(2023, 07, 20, 14, 00, 00), timestamp(2023, 07, 26, 14, 00, 00), timestamp(2023, 07, 26, 18, 00, 00), timestamp(2023, 07, 26, 18, 30, 00), timestamp(2023, 07, 27, 12, 30, 00), timestamp(2023, 07, 27, 12, 30, 00), timestamp(2023, 07, 27, 12, 30, 00), timestamp(2023, 07, 27, 12, 30, 00), timestamp(2023, 07, 28, 12, 30, 00), timestamp(2023, 07, 28, 14, 00, 00), timestamp(2023, 08, 01, 14, 00, 00), timestamp(2023, 08, 02, 12, 15, 00), timestamp(2023, 08, 03, 12, 30, 00), timestamp(2023, 08, 04, 12, 30, 00), timestamp(2023, 08, 08, 12, 30, 00), timestamp(2023, 08, 10, 12, 30, 00), timestamp(2023, 08, 10, 12, 30, 00), timestamp(2023, 08, 10, 12, 30, 00), timestamp(2023, 08, 11, 14, 00, 00), timestamp(2023, 08, 15, 12, 30, 00), timestamp(2023, 08, 16, 18, 00, 00), timestamp(2023, 08, 17, 12, 30, 00), timestamp(2023, 08, 22, 14, 00, 00), timestamp(2023, 08, 23, 14, 00, 00), timestamp(2023, 08, 24, 12, 30, 00), timestamp(2023, 08, 24, 12, 30, 00), timestamp(2023, 08, 25, 14, 00, 00), timestamp(2023, 08, 25, 14, 05, 00), timestamp(2023, 08, 30, 12, 15, 00), timestamp(2023, 08, 30, 12, 30, 00), timestamp(2023, 08, 31, 12, 30, 00), timestamp(2023, 08, 31, 12, 30, 00), timestamp(2023, 09, 01, 12, 30, 00), timestamp(2023, 09, 01, 14, 00, 00), timestamp(2023, 09, 07, 12, 30, 00), timestamp(2023, 09, 13, 12, 30, 00), timestamp(2023, 09, 13, 12, 30, 00), timestamp(2023, 09, 14, 12, 30, 00), timestamp(2023, 09, 14, 12, 30, 00), timestamp(2023, 09, 15, 14, 00, 00), timestamp(2023, 09, 20, 18, 00, 00), timestamp(2023, 09, 20, 18, 30, 00), timestamp(2023, 09, 21, 12, 30, 00), timestamp(2023, 09, 21, 14, 00, 00), timestamp(2023, 09, 26, 14, 00, 00), timestamp(2023, 09, 27, 12, 30, 00), timestamp(2023, 09, 28, 12, 30, 00), timestamp(2023, 09, 28, 20, 00, 00), timestamp(2023, 09, 29, 12, 30, 00), timestamp(2023, 09, 29, 12, 30, 00), timestamp(2023, 09, 29, 14, 00, 00), timestamp(2023, 10, 02, 14, 00, 00), timestamp(2023, 10, 02, 15, 00, 00), timestamp(2023, 10, 04, 12, 15, 00), timestamp(2023, 10, 05, 12, 30, 00), timestamp(2023, 10, 06, 12, 30, 00), timestamp(2023, 10, 11, 18, 00, 00), timestamp(2023, 10, 12, 12, 30, 00), timestamp(2023, 10, 12, 12, 30, 00), timestamp(2023, 10, 12, 12, 30, 00), timestamp(2023, 10, 13, 14, 00, 00), timestamp(2023, 10, 17, 12, 30, 00), timestamp(2023, 10, 19, 12, 30, 00), timestamp(2023, 10, 19, 14, 00, 00), timestamp(2023, 10, 25, 14, 00, 00), timestamp(2023, 10, 26, 12, 30, 00), timestamp(2023, 10, 26, 12, 30, 00), timestamp(2023, 10, 26, 12, 30, 00), timestamp(2023, 10, 26, 12, 30, 00), timestamp(2023, 10, 27, 12, 30, 00), timestamp(2023, 10, 27, 14, 00, 00), timestamp(2023, 11, 01, 12, 15, 00), timestamp(2023, 11, 01, 14, 00, 00), timestamp(2023, 11, 01, 18, 00, 00), timestamp(2023, 11, 01, 18, 30, 00), timestamp(2023, 11, 02, 12, 30, 00), timestamp(2023, 11, 03, 12, 30, 00), timestamp(2023, 11, 09, 13, 30, 00), timestamp(2023, 11, 10, 15, 00, 00), timestamp(2023, 11, 14, 13, 30, 00), timestamp(2023, 11, 14, 13, 30, 00), timestamp(2023, 11, 15, 13, 30, 00), timestamp(2023, 11, 16, 13, 30, 00), timestamp(2023, 11, 21, 15, 00, 00), timestamp(2023, 11, 22, 13, 30, 00), timestamp(2023, 11, 22, 15, 00, 00), timestamp(2023, 11, 22, 19, 00, 00), timestamp(2122, 12, 06, 15, 30, 00))
var EventNames = array.from("Markit Manufacturing PMI", "ISM Manufacturing PMI", "ADP Employment Change", "Markit Services/Composite PMI", "Balance of Trade", "Jobless Claims", "ISM Non-Manufacturing PMI", "Non Farm Payrolls", "Inflation Rate", "Jobless Claims", "Fed Chair", "Retail Sales", "Michigan Consumer Sentiment", "Treasury Secretary", "Jobless Claims", "Markit Services/Composite PMI", "Markit Manufacturing PMI", "Existing Home Sales", "Durable Goods Orders", "Fed Interest Rate Decision", "Fed Press Conference", "GDP", "Goods Trade Balance", "Jobless Claims", "New Home Sales", "Personal Income/Spending", "Michigan Consumer Sentiment", "Markit Manufacturing PMI", "ISM Manufacturing PMI", "ADP Employment Change", "Markit Services/Composite PMI", "ISM Non-Manufacturing PMI", "Jobless Claims", "Non Farm Payrolls", "Balance of Trade", "Inflation Rate", "Fed Chair", "Jobless Claims", "Michigan Consumer Sentiment", "Retail Sales", "FOMC Minutes", "Jobless Claims", "Markit Manufacturing PMI", "Markit Services/Composite PMI", "Existing Home Sales", "Fed Chair", "New Home Sales", "Fed Chair", "Durable Goods Orders", "Jobless Claims", "Personal Income/Spending", "Goods Trade Balance", "Michigan Consumer Sentiment", "Markit Manufacturing PMI", "ISM Manufacturing PMI", "ADP Employment Change", "Markit Services/Composite PMI", "ISM Non-Manufacturing PMI", "Jobless Claims", "Fed Chair", "Non Farm Payrolls", "Balance of Trade", "Inflation Rate", "Jobless Claims", "Michigan Consumer Sentiment", "Retail Sales", "Fed Interest Rate Decision", "Fed Press Conference", "Jobless Claims", "Fed Chair", "Existing Home Sales", "New Home Sales", "Fed Chair", "Durable Goods Orders", "Markit Services/Composite PMI", "Markit Manufacturing PMI", "Fed Chair", "Jobless Claims", "Personal Income/Spending", "Goods Trade Balance", "Michigan Consumer Sentiment", "ADP Employment Change", "Jobless Claims", "Markit Manufacturing PMI", "ISM Manufacturing PMI", "Non Farm Payrolls", "Markit Services/Composite PMI", "ISM Non-Manufacturing PMI", "Balance of Trade", "FOMC Minutes", "Jobless Claims", "Fed Chair", "Inflation Rate", "Fed Chair", "Jobless Claims", "Retail Sales", "Michigan Consumer Sentiment", "Jobless Claims", "Existing Home Sales", "Markit Manufacturing PMI", "Markit Services/Composite PMI", "New Home Sales", "Durable Goods Orders", "Goods Trade Balance", "Fed Interest Rate Decision", "Fed Press Conference", "GDP", "Jobless Claims", "Personal Income/Spending", "Michigan Consumer Sentiment", "Markit Manufacturing PMI", "ISM Manufacturing PMI", "Fed Chair", "Balance of Trade", "ADP Employment Change", "Markit Services/Composite PMI", "ISM Non-Manufacturing PMI", "Jobless Claims", "Non Farm Payrolls", "Inflation Rate", "Jobless Claims", "Retail Sales", "Michigan Consumer Sentiment", "FOMC Minutes", "Jobless Claims", "Markit Services/Composite PMI", "Markit Manufacturing PMI", "Existing Home Sales", "New Home Sales", "Durable Goods Orders", "Jobless Claims", "Goods Trade Balance", "Personal Income/Spending", "Michigan Consumer Sentiment", "Markit Manufacturing PMI", "ISM Manufacturing PMI", "ADP Employment Change", "Jobless Claims", "Markit Services/Composite PMI", "ISM Non-Manufacturing PMI", "Fed Chair", "Non Farm Payrolls", "Balance of Trade", "Inflation Rate", "Jobless Claims", "Michigan Consumer Sentiment", "Retail Sales", "Fed Interest Rate Decision", "Fed Press Conference", "Jobless Claims", "Existing Home Sales", "Fed Chair", "Markit Manufacturing PMI", "Markit Services/Composite PMI", "New Home Sales", "Jobless Claims", "Durable Goods Orders", "Goods Trade Balance", "Personal Income/Spending", "Michigan Consumer Sentiment", "ADP Employment Change", "Jobless Claims", "Markit Manufacturing PMI", "ISM Manufacturing PMI", "Balance of Trade", "Non Farm Payrolls", "Markit Services/Composite PMI", "ISM Non-Manufacturing PMI", "FOMC Minutes", "Jobless Claims", "Inflation Rate", "Fed Chair", "Jobless Claims", "Fed Chair", "Retail Sales", "Michigan Consumer Sentiment", "Jobless Claims", "Existing Home Sales", "Markit Manufacturing PMI", "Markit Services/Composite PMI", "New Home Sales", "Durable Goods Orders", "Goods Trade Balance", "Fed Interest Rate Decision", "Fed Press Conference", "GDP", "Jobless Claims", "Personal Income/Spending", "Michigan Consumer Sentiment", "Markit Manufacturing PMI", "ISM Manufacturing PMI", "ADP Employment Change", "Markit Services/Composite PMI", "ISM Non-Manufacturing PMI", "Jobless Claims", "Balance of Trade", "Non Farm Payrolls", "Inflation Rate", "Jobless Claims", "Michigan Consumer Sentiment", "Retail Sales", "Fed Chair", "FOMC Minutes", "Jobless Claims", "Markit Manufacturing PMI", "Markit Services/Composite PMI", "Existing Home Sales", "New Home Sales", "Durable Goods Orders", "Jobless Claims", "Personal Income/Spending", "Goods Trade Balance", "Michigan Consumer Sentiment", "Fed Chair", "ADP Employment Change", "Markit Manufacturing PMI", "ISM Manufacturing PMI", "Balance of Trade", "Jobless Claims", "Non Farm Payrolls", "Markit Services/Composite PMI", "ISM Non-Manufacturing PMI", "Jobless Claims", "Inflation Rate", "Retail Sales", "Jobless Claims", "Michigan Consumer Sentiment", "Existing Home Sales", "Fed Interest Rate Decision", "Fed Press Conference", "Jobless Claims", "Markit Manufacturing PMI", "Markit Services/Composite PMI", "New Home Sales", "Durable Goods Orders", "Goods Trade Balance", "Fed Chair", "Fed Chair", "Jobless Claims", "Personal Income/Spending", "Markit Manufacturing PMI", "Michigan Consumer Sentiment", "ISM Manufacturing PMI", "Balance of Trade", "Markit Services/Composite PMI", "ISM Non-Manufacturing PMI", "ADP Employment Change", "Jobless Claims", "Non Farm Payrolls", "Inflation Rate", "FOMC Minutes", "Jobless Claims", "Retail Sales", "Michigan Consumer Sentiment", "Jobless Claims", "Existing Home Sales", "Markit Services/Composite PMI", "Markit Manufacturing PMI", "Fed Chair", "New Home Sales", "Durable Goods Orders", "Goods Trade Balance", "GDP", "Jobless Claims", "Personal Income/Spending", "Michigan Consumer Sentiment", "Markit Manufacturing PMI", "ISM Manufacturing PMI", "ADP Employment Change", "Markit Services/Composite PMI", "ISM Non-Manufacturing PMI", "Fed Interest Rate Decision", "Fed Press Conference", "Jobless Claims", "Balance of Trade", "Non Farm Payrolls", "Fed Chair", "Fed Chair", "Jobless Claims", "Inflation Rate", "Michigan Consumer Sentiment", "Retail Sales", "Jobless Claims", "Existing Home Sales", "Markit Manufacturing PMI", "Markit Services/Composite PMI", "Jobless Claims", "Durable Goods Orders", "Goods Trade Balance", "Michigan Consumer Sentiment", "New Home Sales", "Personal Income/Spending", "FOMC Minutes", "Fed Chair", "Fed Chair", "ADP Employment Change", "Markit Manufacturing PMI", "Fed Chair", "ISM Manufacturing PMI", "Jobless Claims", "Non Farm Payrolls", "Markit Services/Composite PMI", "ISM Non-Manufacturing PMI", "Balance of Trade", "Jobless Claims", "Inflation Rate", "Michigan Consumer Sentiment", "Retail Sales", "Fed Interest Rate Decision", "Fed Press Conference", "Jobless Claims", "Markit Manufacturing PMI", "Markit Services/Composite PMI", "Existing Home Sales", "Durable Goods Orders", "Personal Income/Spending", "Jobless Claims", "New Home Sales", "Michigan Consumer Sentiment", "Goods Trade Balance", "Jobless Claims", "Markit Manufacturing PMI", "ISM Manufacturing PMI", "ADP Employment Change", "Markit Services/Composite PMI", "FOMC Minutes", "Balance of Trade", "Jobless Claims", "ISM Non-Manufacturing PMI", "Non Farm Payrolls", "Fed Chair", "Inflation Rate", "Jobless Claims", "Retail Sales", "Michigan Consumer Sentiment", "Jobless Claims", "Existing Home Sales", "Markit Services/Composite PMI", "Markit Manufacturing PMI", "Goods Trade Balance", "New Home Sales", "Fed Interest Rate Decision", "Fed Press Conference", "Jobless Claims", "Durable Goods Orders", "GDP", "Personal Income/Spending", "Michigan Consumer Sentiment", "Markit Manufacturing PMI", "ISM Manufacturing PMI", "ADP Employment Change", "Jobless Claims", "Markit Services/Composite PMI", "ISM Non-Manufacturing PMI", "Non Farm Payrolls", "Balance of Trade", "Jobless Claims", "Inflation Rate", "Michigan Consumer Sentiment", "Retail Sales", "FOMC Minutes", "Jobless Claims", "Existing Home Sales", "Markit Manufacturing PMI", "Markit Services/Composite PMI", "GDP", "Jobless Claims", "New Home Sales", "Personal Income/Spending", "Durable Goods Orders", "Michigan Consumer Sentiment", "Goods Trade Balance", "Markit Manufacturing PMI", "ISM Manufacturing PMI", "ADP Employment Change", "Fed Chair", "Jobless Claims", "Markit Services/Composite PMI", "ISM Non-Manufacturing PMI", "Fed Chair", "Non Farm Payrolls", "Balance of Trade", "Inflation Rate", "Jobless Claims", "Michigan Consumer Sentiment", "Retail Sales", "Fed Interest Rate Decision", "Fed Press Conference", "Jobless Claims", "Existing Home Sales", "Fed Chair", "Fed Chair", "New Home Sales", "Durable Goods Orders", "Jobless Claims", "Markit Services/Composite PMI", "Markit Manufacturing PMI", "Michigan Consumer Sentiment", "Goods Trade Balance", "ADP Employment Change", "Jobless Claims", "Personal Income/Spending", "Non Farm Payrolls", "Markit Manufacturing PMI", "ISM Manufacturing PMI", "Balance of Trade", "Markit Services/Composite PMI", "ISM Non-Manufacturing PMI", "FOMC Minutes", "Jobless Claims", "Inflation Rate", "Retail Sales", "Jobless Claims", "Michigan Consumer Sentiment", "Existing Home Sales", "Jobless Claims", "Fed Chair", "Fed Chair", "Markit Manufacturing PMI", "Markit Services/Composite PMI", "Durable Goods Orders", "New Home Sales", "Goods Trade Balance", "GDP", "Jobless Claims", "Personal Income/Spending", "Michigan Consumer Sentiment", "Markit Manufacturing PMI", "ISM Manufacturing PMI", "ADP Employment Change", "Balance of Trade", "Markit Services/Composite PMI", "ISM Non-Manufacturing PMI", "Fed Interest Rate Decision", "Fed Press Conference", "Jobless Claims", "Non Farm Payrolls", "Inflation Rate", "Jobless Claims", "Michigan Consumer Sentiment", "Retail Sales", "Fed Chair", "Jobless Claims", "Existing Home Sales", "Markit Services/Composite PMI", "Markit Manufacturing PMI", "New Home Sales", "Fed Chair", "Durable Goods Orders", "FOMC Minutes", "GDP", "Jobless Claims", "Goods Trade Balance", "Personal Income/Spending", "Michigan Consumer Sentiment", "Markit Manufacturing PMI", "ISM Manufacturing PMI", "ADP Employment Change", "Jobless Claims", "Non Farm Payrolls", "Markit Services/Composite PMI", "ISM Non-Manufacturing PMI", "Balance of Trade", "Jobless Claims", "Inflation Rate", "Michigan Consumer Sentiment", "Retail Sales", "Fed Interest Rate Decision", "Fed Press Conference", "Jobless Claims", "Fed Chair", "Existing Home Sales", "Fed Chair", "Jobless Claims", "Markit Manufacturing PMI", "Markit Services/Composite PMI", "Fed Chair", "Michigan Consumer Sentiment", "New Home Sales", "Durable Goods Orders", "Goods Trade Balance", "Fed Chair", "Personal Income/Spending", "Jobless Claims", "ISM Manufacturing PMI", "ISM Non-Manufacturing PMI", "FOMC Minutes", "Balance of Trade", "Jobless Claims", "Non Farm Payrolls", "Inflation Rate", "CPI", "Jobless Claims", "Retail Sales", "Michigan Consumer Sentiment", "Existing Home Sales", "Jobless Claims", "New Home Sales", "Durable Goods Orders", "Goods Trade Balance", "Fed Interest Rate Decision", "Fed Press Conference", "GDP", "Jobless Claims", "Personal Income/Spending", "Michigan Consumer Sentiment", "ISM Manufacturing PMI", "ISM Non-Manufacturing PMI", "Balance of Trade", "Jobless Claims", "Non Farm Payrolls", "CPI", "Inflation Rate", "Jobless Claims", "Michigan Consumer Sentiment", "Retail Sales", "FOMC Minutes", "Jobless Claims", "Existing Home Sales", "New Home Sales", "Durable Goods Orders", "Jobless Claims", "GDP", "Personal Income/Spending", "Goods Trade Balance", "Michigan Consumer Sentiment", "Fed Chair", "ADP Employment Change", "Jobless Claims", "ISM Manufacturing PMI", "Non Farm Payrolls", "ISM Non-Manufacturing PMI", "Balance of Trade", "Jobless Claims", "Inflation Rate", "CPI", "Retail Sales", "Jobless Claims", "Michigan Consumer Sentiment", "Existing Home Sales", "Fed Interest Rate Decision", "Fed Press Conference", "Jobless Claims", "Fed Chair", "Durable Goods Orders", "New Home Sales", "Goods Trade Balance", "Fed Chair", "Jobless Claims", "Personal Income/Spending", "Michigan Consumer Sentiment", "ISM Manufacturing PMI", "ADP Employment Change", "Balance of Trade", "ISM Non-Manufacturing PMI", "Jobless Claims", "Non Farm Payrolls", "FOMC Minutes", "Inflation Rate", "Jobless Claims", "CPI", "Retail Sales", "Michigan Consumer Sentiment", "Jobless Claims", "Existing Home Sales", "Goods Trade Balance", "New Home Sales", "Durable Goods Orders", "Jobless Claims", "GDP", "Personal Income/Spending", "Michigan Consumer Sentiment", "ISM Manufacturing PMI", "ADP Employment Change", "Fed Interest Rate Decision", "Fed Press Conference", "Balance of Trade", "Jobless Claims", "ISM Non-Manufacturing PMI", "Non Farm Payrolls", "CPI", "Inflation Rate", "Jobless Claims", "Michigan Consumer Sentiment", "Retail Sales", "Jobless Claims", "Existing Home Sales", "Durable Goods Orders", "Jobless Claims", "New Home Sales", "Michigan Consumer Sentiment", "FOMC Minutes", "ADP Employment Change", "GDP", "Goods Trade Balance", "Fed Chair", "Jobless Claims", "Personal Income/Spending", "ISM Manufacturing PMI", "Non Farm Payrolls", "ISM Non-Manufacturing PMI", "Balance of Trade", "Jobless Claims", "Michigan Consumer Sentiment", "CPI", "Inflation Rate", "Fed Interest Rate Decision", "Fed Press Conference", "Jobless Claims", "Retail Sales", "Existing Home Sales", "Jobless Claims", "Personal Income/Spending", "Durable Goods Orders", "Michigan Consumer Sentiment", "New Home Sales", "Goods Trade Balance", "Jobless Claims", "ISM Manufacturing PMI", "FOMC Minutes", "ADP Employment Change", "Jobless Claims", "Balance of Trade", "Non Farm Payrolls", "ISM Non-Manufacturing PMI", "Fed Chair", "Jobless Claims", "Inflation Rate", "CPI", "Michigan Consumer Sentiment", "Retail Sales", "Jobless Claims", "Existing Home Sales", "Jobless Claims", "Goods Trade Balance", "Durable Goods Orders", "New Home Sales", "Personal Income/Spending", "Michigan Consumer Sentiment", "ADP Employment Change", "ISM Manufacturing PMI", "Fed Interest Rate Decision", "Fed Press Conference", "Jobless Claims", "Non Farm Payrolls", "ISM Non-Manufacturing PMI", "Balance of Trade", "Fed Chair", "Jobless Claims", "Michigan Consumer Sentiment", "CPI", "Inflation Rate", "Retail Sales", "Jobless Claims", "Existing Home Sales", "FOMC Minutes", "Jobless Claims", "Personal Income/Spending", "New Home Sales", "Michigan Consumer Sentiment", "Durable Goods Orders", "Goods Trade Balance", "ISM Manufacturing PMI", "Jobless Claims", "ISM Non-Manufacturing PMI", "Fed Chair", "ADP Employment Change", "Balance of Trade", "Jobless Claims", "Non Farm Payrolls", "Retail Sales", "Jobless Claims", "Michigan Consumer Sentiment", "Existing Home Sales", "Fed Interest Rate Decision", "Fed Press Conference", "Jobless Claims", "New Home Sales", "Durable Goods Orders", "Goods Trade Balance", "Jobless Claims", "Personal Income/Spending", "Michigan Consumer Sentiment", "ISM Manufacturing PMI", "ADP Employment Change", "Balance of Trade", "ISM Non-Manufacturing PMI", "Jobless Claims", "Non Farm Payrolls", "Inflation Rate", "CPI", "FOMC Minutes", "Jobless Claims", "Retail Sales", "Michigan Consumer Sentiment", "Jobless Claims", "Existing Home Sales", "New Home Sales", "Goods Trade Balance", "Durable Goods Orders", "Jobless Claims", "GDP", "Personal Income/Spending", "Michigan Consumer Sentiment", "ISM Manufacturing PMI", "ADP Employment Change", "ISM Non-Manufacturing PMI", "Fed Interest Rate Decision", "Fed Press Conference", "Jobless Claims", "Balance of Trade", "Non Farm Payrolls", "Inflation Rate", "CPI", "Jobless Claims", "Michigan Consumer Sentiment", "Retail Sales", "Jobless Claims", "Existing Home Sales", "Fed Chair", "New Home Sales", "FOMC Minutes", "Jobless Claims", "Goods Trade Balance", "Personal Income/Spending", "Durable Goods Orders", "Michigan Consumer Sentiment", "ADP Employment Change", "Jobless Claims", "ISM Manufacturing PMI", "Non Farm Payrolls", "Balance of Trade", "Jobless Claims", "Inflation Rate", "CPI", "Fed Interest Rate Decision", "Fed Press Conference", "Retail Sales", "Jobless Claims", "Michigan Consumer Sentiment", "Jobless Claims", "Existing Home Sales", "Durable Goods Orders", "New Home Sales", "Goods Trade Balance", "Jobless Claims", "Personal Income/Spending", "Michigan Consumer Sentiment", "ISM Manufacturing PMI", "FOMC Minutes", "ADP Employment Change", "Balance of Trade", "Jobless Claims", "Non Farm Payrolls", "Inflation Rate", "CPI", "Jobless Claims", "Michigan Consumer Sentiment", "Retail Sales", "Jobless Claims", "Existing Home Sales", "New Home Sales", "Fed Interest Rate Decision", "Fed Press Conference", "Jobless Claims", "Durable Goods Orders", "Goods Trade Balance", "GDP", "Personal Income/Spending", "Michigan Consumer Sentiment", "ISM Manufacturing PMI", "ADP Employment Change", "Jobless Claims", "Non Farm Payrolls", "Balance of Trade", "Jobless Claims", "Inflation Rate", "CPI", "Michigan Consumer Sentiment", "Retail Sales", "FOMC Minutes", "Jobless Claims", "Existing Home Sales", "New Home Sales", "Durable Goods Orders", "Jobless Claims", "Michigan Consumer Sentiment", "Fed Chair", "ADP Employment Change", "Goods Trade Balance", "Jobless Claims", "Personal Income/Spending", "Non Farm Payrolls", "ISM Manufacturing PMI", "Jobless Claims", "Inflation Rate", "CPI", "Retail Sales", "Jobless Claims", "Michigan Consumer Sentiment", "Fed Interest Rate Decision", "Fed Press Conference", "Jobless Claims", "Existing Home Sales", "New Home Sales", "Durable Goods Orders", "Jobless Claims", "Fed Chair", "Personal Income/Spending", "Goods Trade Balance", "Michigan Consumer Sentiment", "ISM Manufacturing PMI", "Fed Chair", "ADP Employment Change", "Jobless Claims", "Non Farm Payrolls", "FOMC Minutes", "Jobless Claims", "Inflation Rate", "CPI", "Michigan Consumer Sentiment", "Retail Sales", "Jobless Claims", "Existing Home Sales", "New Home Sales", "Goods Trade Balance", "Durable Goods Orders", "GDP", "Jobless Claims", "Personal Income/Spending", "Michigan Consumer Sentiment", "ADP Employment Change", "ISM Manufacturing PMI", "Fed Interest Rate Decision", "Fed Press Conference", "Jobless Claims", "Non Farm Payrolls", "Jobless Claims", "Michigan Consumer Sentiment", "CPI", "Inflation Rate", "Retail Sales", "Jobless Claims", "Existing Home Sales", "Durable Goods Orders", "Michigan Consumer Sentiment", "FOMC Minutes", "FakeTypeForTesting")


// Getting inputs
fastMA1_switch = input.string(title="Fast moving average (MA)", defval="EMA" ,options=["SMA","EMA","DEMA","TEMA" ])
slowMA1_switch = input.string(title="Slow moving average (MA)", defval="EMA" ,options=["SMA","EMA","DEMA","TEMA" ])
fast_length1 = input(title="Fast MA length", defval=50)
slow_length1 = input(title="Slow MA length", defval=200)
counterTrendMAangleMin = input(title="counterTrendMAangleMin -- exp.", defval=3)
MAangleDiffMin = input(title="MAangleDiffMin -- exp.", defval=3)
showMA = input.bool(title="Show MA -- exp.", defval=false)
displayMA = showMA ? display.all : display.none
fast_length = input(title="MACD Fast Length", defval=12)
slow_length = input(title="MACD Slow Length", defval=26)
signal_length = input.int(title="MACD Signal Smoothing",  minval = 1, maxval = 50, defval = 9)
src = input(title="MACD Source", defval=close)

i_switch = input.string(title="Tick Highlight", defval="Moving average" ,options=["Moving average","Fixed value" ])
i_switch2 = input.string(title="Tick Source", defval="Highest bar" ,options=["Highest bar","Average","Last bar"])

signal_lengthup = input.int(title="Upticks Avg. Length",  minval = 1, maxval = 5000, defval = 72)
signal_lengthdown = input.int(title="Downticks Avg. Length",  minval = 1, maxval = 5000, defval = 72)

Orig_signal_lengthMA = input.float(title="Ticks Avg. Multiplier",  minval = 0, maxval = 5000, defval = 1.39, step = 0.01)

sma_source = "EMA"
sma_signal = "EMA"
// Plot colors

col_grow_above = #26A69A
col_fall_above =#B2DFDB
col_grow_below = #FFCDD2
col_fall_below = #FF5252
// Calculating

fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)

time_macd=timeframe.period=="1"?"1": timeframe.period=="3"?"1": timeframe.period=="5"?"1": timeframe.period=="15"?"3":timeframe.period=="30"?"5":timeframe.period=="60"?"15":timeframe.period=="120"?"30":timeframe.period=="240"?"60":timeframe.period=="D"?"240":timeframe.period=="W"?"D":timeframe.period=="M"?"W":timeframe.period=="12M"?"M":timeframe.period



macd = fast_ma - slow_ma
macd1=request.security(syminfo.tickerid, time_macd, macd)
signal = sma_signal == "SMA" ? ta.sma(macd1, signal_length) : ta.ema(macd1, signal_length)

fastMAsignal = (0.0)
fastMA1 = switch fastMA1_switch
    "SMA" => fastMAsignal:= ta.sma(close,fast_length1)
    "EMA" => fastMAsignal:= ta.ema(close,fast_length1)
    "DEMA" =>
        ema1 = ta.ema(close,fast_length1)
        ema2 = ta.ema(ema1,fast_length1)
        fastMAsignal:= 2 * ema1 - ema2
    "TEMA" =>
        ema1 = ta.ema(close, fast_length1)
        ema2 = ta.ema(ema1, fast_length1)
        ema3 = ta.ema(ema2, fast_length1)
        fastMAsignal:= 3 * (ema1 - ema2) + ema3
    =>
        runtime.error("No matching MA type found.")
        float(na)

plot(fastMAsignal, "fast MA", color=color.orange, linewidth=2, display=displayMA)

slowMAsignal = (0.0)
slowMA1 = switch slowMA1_switch
    "SMA" => slowMAsignal:= ta.sma(close,slow_length1)
    "EMA" => slowMAsignal:= ta.ema(close,slow_length1)
    "DEMA" =>
        ema1 = ta.ema(close,slow_length1)
        ema2 = ta.ema(ema1,slow_length1)
        slowMAsignal:= 2 * ema1 - ema2
    "TEMA" =>
        ema1 = ta.ema(close, slow_length1)
        ema2 = ta.ema(ema1, slow_length1)
        ema3 = ta.ema(ema2, slow_length1)
        slowMAsignal:= 3 * (ema1 - ema2) + ema3
    =>
        runtime.error("No matching MA type found.")
        float(na)

plot(slowMAsignal, "slow MA", color=color.aqua, linewidth=2, display=displayMA)

var TradeCounter = 0
FirstCount = input.int(title = "First trade after EMA cross", minval = 1, maxval = 1000, defval = 1)-1
LastCount = input.int(title = "Last trade after EMA cross", minval = 1, maxval = 1000, defval = 19)-1
var TradeGroupCounter = 0
MaxTradeGroups = input.int(title = "Max No. of trade groups after EMA cross", minval = 1, maxval = 100, defval = 100)
bullOrig = fastMA1>slowMA1
bearOrig = fastMA1<slowMA1
bull = bullOrig
bear = bearOrig
// get the MA ratio
MA_ratio = math.abs(((fastMA1/slowMA1) > 0 ? (fastMA1/slowMA1) : (slowMA1/fastMA1)) - 1.0)
plot(MA_ratio  * 100 , "MA ratio (%)", display = display.data_window)

ReverseDetectedTrend = input.bool(title="experimental (leave unchecked) -- Reverse Detected Trend", defval = false, group = "order parameters")
if ReverseDetectedTrend
    bull := bearOrig
    bear := bullOrig

ReverseOn_FastMAdirChange = input.bool(title="experimental (leave unchecked) -- Reverse Detected Trend on fast MA direction change", defval = false, group = "order parameters")
ReverseOn_AndOnMAdiff = input.bool(title="experimental (leave unchecked) -- And on MA difference", defval = false, group = "order parameters")
ReverseOn_MAdiffBiggerThan = input.float(title="MA difference bigger than (%)",  minval = 0, maxval = 100, defval = 0.0, step = 0.05, group = "order parameters") / 100
SqueezeIf_MAdiff = input.bool(title="experimental (leave unchecked) -- Squeeze TP and SL if...", defval = false, group = "order parameters")
SqueezeIf_SmallerThan = input.float(title="MA difference smaller than",  minval = 0, maxval = 100, defval = 0.00, step = 0.05, group = "order parameters") / 100

var tradeSessionReversed = false
var MAdiffSqueezed = false
var MinMAdiffExceeded = false

if bullOrig != bullOrig[1] // or bearOrig != bearOrig[1]
    TradeCounter := 0
    TradeGroupCounter := 0
    tradeSessionReversed := false
    MAdiffSqueezed := false
    MinMAdiffExceeded := false

CountCriteriaToTrade = FirstCount <= TradeCounter and TradeCounter <= LastCount

if strategy.opentrades == 0 and strategy.opentrades[1] > 0
    TradeGroupCounter := TradeGroupCounter + 1

MaxTradeGroupsCriteria = TradeGroupCounter < MaxTradeGroups
plot(TradeGroupCounter, "TradeGroupCounter", display = display.data_window)
hist = request.security(syminfo.tickerid, time_macd, macd1 - signal)

fastMAinc = fastMA1 - fastMA1[1]
fastMAincPos = fastMAinc > 0
plot(fastMAinc, "fastMAinc", display = display.data_window)
plot(fastMAincPos ? 1 : 0, "fastMAincPos", color=color.new(#f84f4f, 100), display = display.data_window)
if not tradeSessionReversed
    if bullOrig == bullOrig[1]
        if ReverseOn_FastMAdirChange
            if (fastMAincPos != fastMAincPos[1])
                bull := bearOrig
                bear := bullOrig
                tradeSessionReversed := true
                if ReverseOn_AndOnMAdiff
                    if ReverseOn_MAdiffBiggerThan < MA_ratio
                        bull := bearOrig
                        bear := bullOrig
                        tradeSessionReversed := true
                    else
                        bull := bullOrig
                        bear := bearOrig
                        tradeSessionReversed := false
else
    bull := bearOrig
    bear := bullOrig
    tradeSessionReversed := true

plot(tradeSessionReversed ? 1 : 0, "tradeSessionReversed", color=color.new(#f84f4f, 100), display = display.data_window)

ReverseOn_signal_lengthMA = input.float(title="If reversed: Ticks Avg. Multiplier",  minval = 0, maxval = 5000, defval = 1.39, step = 0.01, group = "order parameters")
signal_lengthMA = tradeSessionReversed ? ReverseOn_signal_lengthMA : Orig_signal_lengthMA


f() => [hist[4],hist[3],hist[2],hist[1], hist]
ss=request.security(syminfo.tickerid, time_macd, hist, barmerge.gaps_on,barmerge.lookahead_off)



[ss5,ss4,ss3,ss2,ss1]=request.security(syminfo.tickerid, time_macd, f(), barmerge.gaps_on,barmerge.lookahead_off)



a = array.from(ss5,ss4,ss3,ss2,ss1)

s3=i_switch2=="Highest bar"?(ss>0? array.max(a, 0) : array.min(a, 0)):i_switch2=="Average"?array.avg(a):i_switch2=="Last bar"?ss1:0

saa=timeframe.period == '1'? ss:s3

saa2=timeframe.period == '1'? ss:s3*signal_lengthMA


colorss=(s3>=0 ? (s3[1] < s3 ? col_grow_above : col_fall_above) : (s3[1] < s3 ? col_grow_below : col_fall_below))


saadown = saa2
saaup = saa2

saadown:=saa>=0? saa2:saadown[1]

saaup:=saa<0? saa2:saaup[1]



verr=ta.ema(saadown,signal_lengthup)
dowww=ta.ema(saaup,signal_lengthdown)

ss22=plot(verr, title="Avg. Cloud Upper 1", color=color.new(color.white, 100))
ss33=plot(dowww, title="Avg. Cloud Lower 1", color=color.new(color.white, 100))

fill(ss22, ss33, color.new(color.white, 93), title="Avg. Cloud Background")

fixeduptick = input(title="Fixed Uptick Value", defval=30)
fixeddowntick = input(title="Fixed Downtick Value", defval=-30)
minl = i_switch=="Fixed value"? fixeduptick  :  verr
maxl = i_switch=="Fixed value"? fixeddowntick : dowww 

plot(minl, title="Avg. Cloud Upper 2", color=color.new(color.white, 81))
plot(maxl, title="Avg. Cloud Lower 2", color=color.new(color.white, 81))


colors2= s3<=minl and s3>=maxl ? #2a2e39 : colorss

coro2=s3>0? bull ? #2a2e39 :  colors2 : bear ? #2a2e39: colors2
plot(saa, title="Histogram", style=plot.style_columns, color=coro2)

actOnFirstN_min = input.int(title = "Act on the first N signal (min)" , defval = 1, minval = 1)
actOnFirstN_max = input.int(title = "Act on the first N signal (max)" , defval = 6, minval = 1)

minMAdiff = input.float(title = "Min. MA difference (%)", defval = 0.82, step = 0.01) / 100
maxMAdiff = input.float(title = "Max. MA difference (%)", defval = 3.1, step = 0.1) / 100

if not MinMAdiffExceeded
    if minMAdiff < MA_ratio
        MinMAdiffExceeded := true

if not MAdiffSqueezed and SqueezeIf_MAdiff
    if MinMAdiffExceeded
        if MA_ratio < SqueezeIf_SmallerThan
            MAdiffSqueezed := true

plot(MAdiffSqueezed ? 1 : 0, "MAdiffSqueezed", color=color.new(#f84f4f, 100), display = display.data_window)


LimitDiff = input.float(title="Limit Price Difference (%) (0-> market order)",  minval = 0, maxval = 10, defval = 0.0, step = 0.01, group = "order parameters") / 100
Show_TPSL = input.bool(title = "Show TP and SL? (only works if the strategy is in the main graph)", defval = false, group = "order parameters")
TPSL_linewidth = input.int(title="linewidth of the TP and SL lines",  minval = 1, maxval = 10, defval = 1, group = "order parameters")
Orig_TP = input.float(title="Take Profit - from market price (%)",  minval = 0, maxval = 10, defval = 0.9, step = 0.05, group = "order parameters") / 100
ReverseOn_TP = input.float(title="If reversed: TP (%)",  minval = 0, maxval = 10, defval = 0.9, step = 0.05, group = "order parameters") / 100
SqueezeIf_TP = input.float(title="If squeezed: TP (%)",  minval = 0, maxval = 10, defval = 0.9, step = 0.05, group = "order parameters") / 100
TP = MAdiffSqueezed ? SqueezeIf_TP : tradeSessionReversed ? ReverseOn_TP : Orig_TP
col_TP = input.color(color.green, "TP color", group = "order parameters")
Orig_SL = input.float(title="Stop Loss - from market price (%)",  minval = 0, maxval = 10, defval = 1.3, step = 0.05, group = "order parameters") / 100
ReverseOn_SL = input.float(title="If reversed: SL (%)",  minval = 0, maxval = 10, defval = 1.3, step = 0.05, group = "order parameters") / 100
SqueezeIf_SL = input.float(title="If squeezed: SL (%)",  minval = 0, maxval = 10, defval = 1.3, step = 0.05, group = "order parameters") / 100
SL = MAdiffSqueezed ? SqueezeIf_SL : tradeSessionReversed ? ReverseOn_SL : Orig_SL
col_SL = input.color(color.red, "SL color", group = "order parameters")
TrStop = input.bool(title="Use Trailing Stop? -- experimental", defval = false, group = "order parameters")
TrTrigger = input.float(title="Trailing Stop - trigger price (%)",  minval = 0, maxval = 10, defval = 0.65, step = 0.05, group = "order parameters") / 100
TrOffset = input.float(title="Trailing Stop - stop distance (%)",  minval = 0, maxval = 10, defval = 0.1, step = 0.01, group = "order parameters") / 100
maxCountertrendDiff = input.float(title="Max. counter trend diff. (%) -- experimental",  minval = 0, maxval = 100, defval = 100, step = 0.05, group = "order parameters") / 100

// Big thanks to © FloppyDisque for sharing the Time Filter part, that got reused in the following section:
// Time Filter {
// Beginning / End date filter {
g_time          = "Time Filter"
timeZone        = input.string("GMT-4", "Time Zone", group = g_time, 
     tooltip = "GMT and UTC is the same thing \nMatch this setting to bottom right time",
     options = ["GMT-10", "GMT-9", "GMT-8", "GMT-7", "GMT-6", "GMT-5", "GMT-4", "GMT-3", "GMT+0", "GMT+1", "GMT+2", "GMT+3", "GMT+4", "GMT+5", "GMT+6", "GMT+7", "GMT+8", "GMT+9", "GMT+10", "GMT+10:30", "GMT+11", "GMT+12", "GMT+13", "GMT+13:45"])
startTimeIn     = input.time(timestamp("01 Jan 2000"), "Start Date Filter", group = g_time,
     tooltip = "Changing timezone at bottom right of chart will change start time\nSet chart timezone to your prefered time first, then change indicator setting")
endTimeIn       = input.time(timestamp("01 Jan 2099"), "End Date Filter", group = g_time)

startTimeYear   = year      (startTimeIn, timeZone)
startTimeMonth  = month     (startTimeIn, timeZone)
startTimeDay    = dayofmonth(startTimeIn, timeZone)
endTimeYear     = year      (endTimeIn, timeZone)
endTimeMonth    = month     (endTimeIn, timeZone)
endTimeDay      = dayofmonth(endTimeIn, timeZone)

startTime   = timestamp(timeZone, startTimeYear, startTimeMonth, startTimeDay)
endTime     = timestamp(timeZone, endTimeYear, endTimeMonth, endTimeDay)
inDate      = time >= startTime and time <= endTime
//}
// Weekdays Filter {
useWeek = input.bool(true, "Use Weekdays Filter?", group = g_time,
     tooltip = "Disable to allow all weekdays, Enable to choose certain days")
useMon  = input.bool(true, "Mon  ", inline = "Days", group = g_time)
useTue  = input.bool(true, "Tue  ", inline = "Days", group = g_time)
useWed  = input.bool(true, "Wed  ", inline = "Days", group = g_time)
useThu  = input.bool(true, "Thu  ", inline = "Days", group = g_time)
useFri  = input.bool(true, "Fri  ", inline = "Days", group = g_time)
useSat  = input.bool(false, "Sat  ", inline = "Days", group = g_time)
useSun  = input.bool(true, "Sun", inline = "Days", group = g_time)
inWeek  = if useWeek and useMon and dayofweek(time, timeZone) == dayofweek.monday
    true
else if useWeek and useTue and dayofweek(time, timeZone) == dayofweek.tuesday
    true
else if useWeek and useWed and dayofweek(time, timeZone) == dayofweek.wednesday
    true
else if useWeek and useThu and dayofweek(time, timeZone) == dayofweek.thursday
    true
else if useWeek and useFri and dayofweek(time, timeZone) == dayofweek.friday
    true
else if useWeek and useSat and dayofweek(time, timeZone) == dayofweek.saturday
    true
else if useWeek and useSun and dayofweek(time, timeZone) == dayofweek.sunday
    true
else if not(useWeek)
    true

//}
// Session Filter {
isInSess(_sess) => time(timeframe.period, _sess, timeZone)

useSess     = input.bool(true, "Use Session Filter?", group = g_time)
timeSess1    = input.session("0130-1145", "Time Session", group = g_time)
inSess1 = isInSess(timeSess1)

useSess2    = input.bool(true, "Use 2ND Session Filter?", group = g_time)
timeSess2   = input.session("1300-0000", "Time Session 2", group = g_time)
inSess2 = isInSess(timeSess2)

useSessFri    = input.bool(true, "Use 'Friday' Filter?", group = g_time)
timeSessFri   = input.session("0000-2030", "Friday Session", group = g_time)
inSessFri = isInSess(timeSessFri)

useSessFriSatClose    = input.bool(false, "Close all at the end of the Friday/Saturday Session?", group = g_time)
timeSessFriClose   = input.session("0000-0000", "Friday Session Close (0000-0000 : inactive)", group = g_time)
timeSessSatClose   = input.session("0000-0000", "Saturday Session Close (0000-0000 : inactive)", group = g_time)
inSessFriClose = isInSess(timeSessFriClose)
inSessSatClose = isInSess(timeSessSatClose)

useSessSun    = input.bool(true, "Use 'Sunday' Filter?", group = g_time)
timeSessSun   = input.session("2145-0000", "Sunday Session", group = g_time)
inSessSun = isInSess(timeSessSun)


inSess = if useSess and inSess1
    true
else if useSess2 and inSess2
    true
else if not (useSess)
    true

if dayofweek(time, timeZone) == dayofweek.friday and useSessFri
    inSess := inSess and inSessFri
if dayofweek(time, timeZone) == dayofweek.sunday and useSessSun
    inSess := inSess and inSessSun
//}
// Prepare Time Filter --- USE VARIABLE -->"inTime"<-- AS TIME FILTER IN ANY CODE {
inTime = inDate and inWeek and inSess
//bgcolor(inTime ? color.new(color.blue, 90) : na, title = "Time Filter")
//}
//}

// News effect avoidance
EventTypeAvoidance_00 = input.bool(defval=false, title="ISM Manufacturing PMI", group="News effect avoidance")
EventTypeAvoidance_01 = input.bool(defval=true, title="ADP Employment Change", group="News effect avoidance")
EventTypeAvoidance_02 = input.bool(defval=false, title="Markit Services/Composite PMI", group="News effect avoidance")
EventTypeAvoidance_03 = input.bool(defval=false, title="Markit Manufacturing PMI", group="News effect avoidance")
EventTypeAvoidance_04 = input.bool(defval=false, title="Balance of Trade", group="News effect avoidance")
EventTypeAvoidance_05 = input.bool(defval=true, title="Jobless Claims", group="News effect avoidance")
EventTypeAvoidance_06 = input.bool(defval=false, title="ISM Non-Manufacturing PMI", group="News effect avoidance")
EventTypeAvoidance_07 = input.bool(defval=true, title="Non Farm Payrolls", group="News effect avoidance")
EventTypeAvoidance_08 = input.bool(defval=true, title="CPI", group="News effect avoidance")
EventTypeAvoidance_09 = input.bool(defval=true, title="Inflation Rate", group="News effect avoidance")
EventTypeAvoidance_10 = input.bool(defval=true, title="Fed Chair", group="News effect avoidance")
EventTypeAvoidance_11 = input.bool(defval=false, title="Retail Sales", group="News effect avoidance")
EventTypeAvoidance_12 = input.bool(defval=false, title="Michigan Consumer Sentiment", group="News effect avoidance")
EventTypeAvoidance_13 = input.bool(defval=true, title="Treasury Secretary", group="News effect avoidance")
EventTypeAvoidance_14 = input.bool(defval=false, title="Existing Home Sales", group="News effect avoidance")
EventTypeAvoidance_15 = input.bool(defval=false, title="Durable Goods Orders", group="News effect avoidance")
EventTypeAvoidance_16 = input.bool(defval=true, title="Fed Interest Rate Decision", group="News effect avoidance")
EventTypeAvoidance_17 = input.bool(defval=true, title="GDP", group="News effect avoidance")
EventTypeAvoidance_18 = input.bool(defval=false, title="Goods Trade Balance", group="News effect avoidance")
EventTypeAvoidance_19 = input.bool(defval=false, title="New Home Sales", group="News effect avoidance")
EventTypeAvoidance_20 = input.bool(defval=false, title="Personal Income/Spending", group="News effect avoidance")
EventTypeAvoidance_21 = input.bool(defval=true, title="FOMC Minutes", group="News effect avoidance")
EventTypeAvoidance_22 = input.bool(defval=true, title="Fed Press Conference", group="News effect avoidance")
array.set(EventTypeAvoidance, 0, EventTypeAvoidance_00)
array.set(EventTypeAvoidance, 1, EventTypeAvoidance_01)
array.set(EventTypeAvoidance, 2, EventTypeAvoidance_02)
array.set(EventTypeAvoidance, 3, EventTypeAvoidance_03)
array.set(EventTypeAvoidance, 4, EventTypeAvoidance_04)
array.set(EventTypeAvoidance, 5, EventTypeAvoidance_05)
array.set(EventTypeAvoidance, 6, EventTypeAvoidance_06)
array.set(EventTypeAvoidance, 7, EventTypeAvoidance_07)
array.set(EventTypeAvoidance, 8, EventTypeAvoidance_08)
array.set(EventTypeAvoidance, 9, EventTypeAvoidance_09)
array.set(EventTypeAvoidance, 10, EventTypeAvoidance_10)
array.set(EventTypeAvoidance, 11, EventTypeAvoidance_11)
array.set(EventTypeAvoidance, 12, EventTypeAvoidance_12)
array.set(EventTypeAvoidance, 13, EventTypeAvoidance_13)
array.set(EventTypeAvoidance, 14, EventTypeAvoidance_14)
array.set(EventTypeAvoidance, 15, EventTypeAvoidance_15)
array.set(EventTypeAvoidance, 16, EventTypeAvoidance_16)
array.set(EventTypeAvoidance, 17, EventTypeAvoidance_17)
array.set(EventTypeAvoidance, 18, EventTypeAvoidance_18)
array.set(EventTypeAvoidance, 19, EventTypeAvoidance_19)
array.set(EventTypeAvoidance, 20, EventTypeAvoidance_20)
array.set(EventTypeAvoidance, 21, EventTypeAvoidance_21)
array.set(EventTypeAvoidance, 22, EventTypeAvoidance_22)

avoidance_TimeBeforeNews = 1000 * 60 * input.float(title="suspend trading X minutes before the news event", defval=80, step = 10, group="News effect avoidance")
avoidance_TimeAfterNews = 1000 * 60 * input.float(title="continue trading X minutes after the news event", defval=120, step=10, group="News effect avoidance")

currentTime = time(timeframe.period)

lastEvent = array.binary_search_leftmost(EventTimestamps, currentTime)
nextEvent = lastEvent + 1

lastEventName = array.get(EventNames, lastEvent)
lastEventTypeIndex = array.indexof(EventNameTypes, lastEventName)

nextEventName = array.get(EventNames, nextEvent)
nextEventTypeIndex = array.indexof(EventNameTypes, nextEventName)

avoidTrading = ((array.get(EventTimestamps, lastEvent) + avoidance_TimeAfterNews) > currentTime and array.get(EventTypeAvoidance, lastEventTypeIndex)) or ((array.get(EventTimestamps, nextEvent) - avoidance_TimeBeforeNews) < currentTime and array.get(EventTypeAvoidance, nextEventTypeIndex))
// test out what if trades are closed before news.
closeIfAvoidTrading = input.bool(title="experimental (leave unchecked) -- Close all trades when trading is suspended before news", defval=false, group="News effect avoidance")
if avoidTrading and closeIfAvoidTrading
    strategy.close_all()

inTime := inTime and (not avoidTrading)

bgcolor(inTime ? color.new(color.blue, 90) : na, title = "Time Filter")


// automated trading specific part
autoTradeMode = input.string(title="Automated trading service", defval="None" ,options=["None","aleeert","TTA" ], group = "Automated Trading")


// aleeert specific part
aleeert_key = input.string(title="aleeert userkey", defval="", group = "aleeert")
aleeert_slot_id = input.string(title="aleeert slot_id", defval="", group = "aleeert")
aleeert_tradingPair_override = ""
aleeert_tradingPair_override := input.string(title="aleeert trading pair override (original if empty)", defval="", group = "aleeert")
aleeert_tradingPair = aleeert_tradingPair_override == "" ? syminfo.ticker : aleeert_tradingPair_override
aleeert_leverage = input.int(title="aleeert leverage", defval=50, minval = 1, maxval = 1000, group = "aleeert")
aleeert_tradeSize_percent = input.float(title = "aleeert trade size (%)", defval = 2.0, step = 0.1, minval = 0.0, maxval = 100.0, group = "aleeert")


aleeert_buy_order_precondition = "if short: " + aleeert_tradingPair + ", close, 100%, - "
  + ", " + aleeert_slot_id + ", " + aleeert_key

aleeert_buy_order_entry = aleeert_tradingPair + "(x" + str.tostring((aleeert_leverage), '#') + ")" + ", cancel-buy, " + str.tostring((aleeert_tradeSize_percent), '#.##') + "%, market"
  + ", " + aleeert_slot_id + ", " + aleeert_key
    
aleeert_buy_order_exit = aleeert_tradingPair + "(x" + str.tostring((aleeert_leverage), '#') + ")" + ", tpsl, -, " 
  + str.tostring((close), '#.##') + "+" + str.tostring((TP*100), '#.##') + "%(100%)" + " | " +  str.tostring((close), '#.##') + "-" + str.tostring((SL*100), '#.##') + "%(100%)"
  + ", " + aleeert_slot_id + ", " + aleeert_key

// trail_price = close * (1 + TrTrigger), trail_offset = (close * TrOffset) / syminfo.mintick,

aleeert_buy_order_trstop = aleeert_tradingPair + ", sell, 100%, " 
  + str.tostring((close * (1 + TrTrigger)), '#.##') + ":" + str.tostring((TrOffset*100), '#.###') + "%(TS)"
  + ", " + aleeert_slot_id + ", " + aleeert_key
        
aleeert_buy_order_command = aleeert_buy_order_precondition + "; " + aleeert_buy_order_entry + "; " + aleeert_buy_order_exit
aleeert_buy_order_w_trstop_command = aleeert_buy_order_entry + "; " + aleeert_buy_order_exit + "; " + aleeert_buy_order_trstop


aleeert_sell_order_precondition = "if long: " + aleeert_tradingPair + ", close, 100%, - "
  + ", " + aleeert_slot_id + ", " + aleeert_key

aleeert_sell_order_entry = aleeert_tradingPair + "(x" + str.tostring((aleeert_leverage), '#') + ")" + ", cancel-sell, " + str.tostring((aleeert_tradeSize_percent), '#.##') + "%, market"
  + ", " + aleeert_slot_id + ", " + aleeert_key
    
aleeert_sell_order_exit = aleeert_tradingPair + "(x" + str.tostring((aleeert_leverage), '#') + ")" + ", tpsl, -, " 
  + str.tostring((close), '#.##') + "-" + str.tostring((TP*100), '#.##') + "%(100%)" + " | " +  str.tostring((close), '#.##') + "+" + str.tostring((SL*100), '#.##') + "%(100%)"
  + ", " + aleeert_slot_id + ", " + aleeert_key
        
aleeert_sell_order_trstop = aleeert_tradingPair + ", buy, 100%, " 
  + str.tostring((close * (1 - TrTrigger)), '#.##') + ":" + str.tostring((TrOffset*100), '#.###') + "%(TS)"
  + ", " + aleeert_slot_id + ", " + aleeert_key

aleeert_sell_order_command = aleeert_sell_order_precondition + "; " + aleeert_sell_order_entry + "; " + aleeert_sell_order_exit
aleeert_sell_order_w_trstop_command = aleeert_sell_order_entry + "; " + aleeert_sell_order_exit + "; " + aleeert_sell_order_trstop


aleeert_positon_close_command = aleeert_tradingPair + ", close, 100%, -, " + aleeert_slot_id + ", " + aleeert_key
aleeert_positon_close_w_trstop_command = aleeert_tradingPair + ", cancel-close, 100%, -, " + aleeert_slot_id + ", " + aleeert_key


// TTA specfic part
TTA_tradingPair_override = ""
TTA_tradingPair_override := input.string(title="TTA trading pair override (original if empty)", defval="", group = "TradingView to Anywhere (TTA)")
TTA_tradingPair = TTA_tradingPair_override == "" ? syminfo.ticker : TTA_tradingPair_override
TTA_tradeSize_percent = input.float(title = "TTA trade size (%)", defval = 2.0, step = 0.1, minval = 0.0, maxval = 100.0, group = "TradingView to Anywhere (TTA)")
// trade command examples
// buy BTCUSD q=1% sl=19000.00 tp=20000.00
// buy BTCUSD sl=19040.00 tp=20040.00 modify

TTA_buy_order_entry = "buy " + TTA_tradingPair + " q=" + str.tostring((TTA_tradeSize_percent), '#.##') + "%" 
  + " sl=" + str.tostring((close * (1 - SL)), '#.##') + " tp=" + str.tostring((close * (1 + TP)), '#.##')
TTA_buy_order_exit = "buy " + TTA_tradingPair
  + " sl=" + str.tostring((close * (1 - SL)), '#.##') + " tp=" + str.tostring((close * (1 + TP)), '#.##') + " modify"
  
TTA_sell_order_entry = "sell " + TTA_tradingPair + " q=" + str.tostring((TTA_tradeSize_percent), '#.##') + "%" 
  + " sl=" + str.tostring((close * (1 + SL)), '#.##') + " tp=" + str.tostring((close * (1 - TP)), '#.##')
TTA_sell_order_exit = "sell " + TTA_tradingPair
  + " sl=" + str.tostring((close * (1 + SL)), '#.##') + " tp=" + str.tostring((close * (1 - TP)), '#.##') + " modify"

TTA_buy_order_command = TTA_buy_order_entry + "\n" + TTA_buy_order_exit
TTA_sell_order_command = TTA_sell_order_entry + "\n" + TTA_sell_order_exit

// general part

buy_order_command = ""
sell_order_command = ""
positon_close_command = ""

switch autoTradeMode
    "aleeert" =>
        if TrStop
            buy_order_command := aleeert_buy_order_w_trstop_command
            sell_order_command := aleeert_sell_order_w_trstop_command
            positon_close_command := aleeert_positon_close_w_trstop_command
        else
            buy_order_command := aleeert_buy_order_command
            sell_order_command := aleeert_sell_order_command
            positon_close_command := aleeert_positon_close_command
    "TTA" =>
        buy_order_command := TTA_buy_order_command
        sell_order_command := TTA_sell_order_command
    "None" =>
        buy_order_command := ""
        sell_order_command := ""
    =>
        buy_order_command := ""
        sell_order_command := ""
        runtime.error("No trade mode")
        float(na)

var NthSignal = 0

actOnCoro2 = actOnFirstN_min <= NthSignal + 1 and NthSignal + 1 <= actOnFirstN_max
uniquePostfix = ""// + "_" + str.tostring(time)
enterShort = "enter short" + uniquePostfix
exitShort = "exit short" + uniquePostfix
enterLong = "enter long" + uniquePostfix
exitLong = "exit long" + uniquePostfix

var minBuyPrice = -1.0
var maxSellPrice = -1.0

var maxPyramiding = strategy.opentrades

if strategy.position_size == 0
    minBuyPrice := -1.0
    maxSellPrice := -1.0
    
nextBuyAllowed = true
if minBuyPrice != -1
    nextBuyAllowed := minBuyPrice <= close
   
nextSellAllowed = true
if maxSellPrice != -1
    nextSellAllowed := maxSellPrice >= close

var actualTP = 0.0
var actualSL = 0.0

if #2a2e39 != coro2 and MaxTradeGroupsCriteria
    NthSignal := NthSignal + 1
    //TradeCounter := TradeCounter + 1
    if bull and CountCriteriaToTrade and minMAdiff < MA_ratio and MA_ratio < maxMAdiff and actOnCoro2
        if nextBuyAllowed and inTime and CountCriteriaToTrade
            LimitPrice = close * (1 - LimitDiff)

            if LimitDiff != 0
                strategy.entry(enterLong, strategy.long, limit = LimitPrice, alert_message = buy_order_command)
            if LimitDiff == 0
                strategy.entry(enterLong, strategy.long, alert_message = buy_order_command)

            if TrStop
                actualTP := close * (1 + TP)
                actualSL := close * (1 - SL)
                strategy.exit(exitLong, enterLong, limit = actualTP, stop = actualSL,
                  trail_price = close * (1 + TrTrigger), trail_offset = (close * TrOffset) / syminfo.mintick,
                  alert_message = "", alert_profit = positon_close_command, alert_loss = positon_close_command)
            else
                actualTP := close * (1 + TP)
                actualSL := close * (1 - SL)
                strategy.exit(exitLong, enterLong, limit = actualTP, stop = actualSL, alert_message = "", alert_profit = 
                  positon_close_command, alert_loss = positon_close_command)

            maxSellPrice := -1.0
            if minBuyPrice == -1.0 or (minBuyPrice / (1 - maxCountertrendDiff)) <= close
                minBuyPrice := close * (1 - maxCountertrendDiff)
        TradeCounter := TradeCounter + 1


    if bear and minMAdiff < MA_ratio and MA_ratio < maxMAdiff and actOnCoro2
        if nextSellAllowed and inTime and CountCriteriaToTrade
            LimitPrice = close * (1 + LimitDiff)

            if LimitDiff != 0
                strategy.entry(enterShort, strategy.short, limit = LimitPrice, alert_message = sell_order_command)
            if LimitDiff == 0
                strategy.entry(enterShort, strategy.short, alert_message = sell_order_command)

            if TrStop
                actualTP := close * (1 - TP)
                actualSL := close * (1 + SL)
                strategy.exit(exitShort, enterShort, limit = actualTP, stop = actualSL,
                  trail_price = close * (1 - TrTrigger), trail_offset = (close * TrOffset) / syminfo.mintick,
                  alert_message = "", alert_profit = positon_close_command, alert_loss = positon_close_command)
            else
                actualTP := close * (1 - TP)
                actualSL := close * (1 + SL)
                strategy.exit(exitShort, enterShort, limit = actualTP, stop = actualSL, alert_message = "", alert_profit = 
                  positon_close_command, alert_loss = positon_close_command)

            minBuyPrice := -1.0
            if maxSellPrice == -1.0 or (maxSellPrice / (1 + maxCountertrendDiff)) <= close
                maxSellPrice := close * (1 + maxCountertrendDiff)
        TradeCounter := TradeCounter + 1
else
    NthSignal := 0

if tradeSessionReversed != tradeSessionReversed[1] and tradeSessionReversed
    strategy.close_all(alert_message = positon_close_command)

if MAdiffSqueezed and not MAdiffSqueezed[1]
    if bull
        actualTP := close * (1 + TP)
        actualSL := close * (1 - SL)
        strategy.exit(exitLong, enterLong, limit = actualTP, stop = actualSL, alert_message = "", alert_profit = positon_close_command, alert_loss = positon_close_command)
    if bear
        actualTP := close * (1 - TP)
        actualSL := close * (1 + SL)
        strategy.exit(exitShort, enterShort, limit = actualTP, stop = actualSL, alert_message = "", alert_profit = positon_close_command, alert_loss = positon_close_command)

TPSL_visible = Show_TPSL and (strategy.opentrades != 0 or (strategy.opentrades == 0 and strategy.opentrades[1] != 0))
plot((TPSL_visible ? actualTP : na), "TP", col_TP, TPSL_linewidth, style = plot.style_linebr)
plot((TPSL_visible ? actualSL : na), "SL", col_SL, TPSL_linewidth, style = plot.style_linebr)
plot(strategy.opentrades, "OpenTrades", display = display.data_window)


if useSessFriSatClose
  and 
  (
  ((dayofweek(time, timeZone) == dayofweek.friday) and (inSessFriClose[1] and not inSessFriClose))
  or ((dayofweek(time, timeZone) == dayofweek.saturday) and (inSessSatClose[1] and not inSessSatClose))
  )
  
  // run once, at the end of the session, on Fridays
    strategy.cancel_all()
    strategy.close_all(alert_message = positon_close_command)



//alertcondition(#2a2e39 != coro2 , title='MACD Tick Alert', message='Joel on Crypto - MACD Tick Alert')

