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

// All code writted by ice-e-fresh
// Trading Strategies and Entry Signales care of Pete Renzulli and the STP Community
// @version=5
// @description These functions are used as part of the STP trading strategy and include commonly used candle patterns, trade triggers and frequently monitored stock parameters
library("STPFunctions", overlay=true)

// @function Determines if the last price is abover or below key moving averages. MAs used on the daily are SMA20, SMA50 and SMA200. SMA20 and SMA50 are used intraday.
// @returns 1 if the last price/close was over the moving averages. -1 is returned if the last price/close is below the moving averages. 0 is returned otherwise.
export MAs() =>
    SMA200 = ta.sma(close,200)
    SMA50 = ta.sma(close,50)
    SMA20 = ta.sma(close,20)
    timeframe.isdaily ? (((0.95 * SMA200 < close) and (SMA50 < close) and (SMA20 < close) and (SMA20 > SMA50)) ? 1 : ((1.05 * SMA200 > close) and (SMA50 > close) and (SMA20 > close) and (SMA20 < SMA50)) ? -1 : 0) : (((SMA50 < close) and (SMA20 < close) and (SMA20 > SMA50)) ? 1 : ((SMA50 > close) and (SMA20 > close) and (SMA20 < SMA50)) ? -1 : 0) 

// @function Determine the state of the higher time frame order flow.
// @param HTF1_open float value representing the higher time frame open. 
// @param HTF2_open float value representing the higher time frame open.
// @returns 1 if the last price/close was over the higher time frame open. -1 is returned if the last price/close is below the higher time frame open. 0 is returned otherwise.
export HTFOrderFlow(float HTF1_open = 0, float HTF2_open = 0) =>
    HTFOrderFlow = ((HTF1_open < close) and (HTF2_open < close)) ? 1 :  ((HTF1_open > close) and (HTF2_open > close)) ? -1  : 0
    
// @function Determine the recent order flow... basically are we well bid or well offered
// @returns 1 if the last 2 candles are well bid. -1 is returned if the last 2 candles are well offered. 0 is returned otherwise.
export OrderFlow(float o = 0, float h = 0, float l = 0, float c = 0, float prev_h = 0, float prev_l = 0, float prev_c = 0) =>
    previousBar = 1
    (h > prev_h and l > prev_l and c > o and c > prev_c and c > prev_h) ? 1 : (h < prev_h and l < prev_l and c < o and c < prev_c and c < prev_l) ? -1 : 0

// @function Used to flag inside candles
// @returns 1 if the close >= open. -1 is returned if the close <= open. 0 is returned otherwise.
export isInside() =>
    previousBar = 1
    bodyStatus = (close >= open) ? 1 : -1
    isInsidePattern = high < high[previousBar] and low > low[previousBar]
    isInsidePattern ? bodyStatus : 0

// @function Used to flag outside or engulfing candles
// @returns 1 if the close >= open. -1 is returned if the close <= open. 0 is returned otherwise.
export isOutside() =>
    previousBar = 1
    bodyStatus = (close >= open) ? 1 : -1
    isOutsidePattern = high > high[previousBar] and low > low[previousBar]
    isOutsidePattern ? bodyStatus : 0

// @function Used to flag the U-turn reversal pattern
// @returns 1 for a BUTN. -1 is returned for a BRUTN. 0 is returned otherwise.
export isUTN() =>
    previousBar = 1
    (high < high[previousBar] and close[previousBar] < open[previousBar] and close > close[previousBar] and low < low[previousBar] and close > low[previousBar] and close > open and close < high[previousBar]) ? 1 : (low > low[previousBar] and close[previousBar] > open[previousBar] and close < close[previousBar] and high > high[previousBar] and close < high[previousBar] and close < open and close > low[previousBar]) ? -1 : 0

// @function Flag for Snapback Entries
// @returns 1 for a bullish snapback setup. -1 is returned for a bearish snapback setup. 0 is returned otherwise.
export isSNapBack() =>
    //work in progress
    0
