Untitled

 avatar
unknown
plain_text
3 years ago
7.4 kB
16
Indexable
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ScryptoMB

//@version=5
strategy("My script", overlay=true)

// INPUT ============================================================================================================
// Time Filter {
g_time          = "Time Filter"
timeZone        = input.string("GMT+2", "Time Zone", group = g_time, 
     tooltip = "GMT and UTC are the same \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(true, "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(false, "Use Session Filter?", group = g_time)
timeSess1    = input.session("0900-1730", "Time Session", group = g_time)
inSess1 = isInSess(timeSess1)

useSess2    = input.bool(false, "Use 2ND Session Filter?", group = g_time)
timeSess2   = input.session("1930-2100", "Time Session 2", group = g_time)
inSess2 = isInSess(timeSess2)

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


// Display Time Filter --- USE VARIABLE -->"inTime"<-- AS TIME FILTER IN ANY CODE {
inTime = inDate and inWeek and inSess 

useMovingAverage = input.bool(false, title="MovingAverage", group="🟣 Entry Conditions", inline="Entry C1")

// MA
maType = input.string('Price Above/Below MA 1', title="MA Entry Type", options=['Price Above/Below MA 1', 'Price Crossing Up/Down MA 1', 'MA 1 Above/Below MA 2', 'MA 1 Crossing Up/Down MA 2'], group="• Moving Average")

typeMA1 = input.string("SMA", title = "Moving Average 1",  options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA"], inline='ema1', group="• Moving Average")
lenEMA1 = input.int(100, minval=1, title="Length", inline='ema1', group="• Moving Average")

typeMA2 = input.string("SMA", title = "Moving Average 2",  options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA"], inline='ema2', group="• Moving Average")
lenEMA2 = input.int(200, minval=1, title="Length", inline='ema2', group="• Moving Average")

maTimeFrame = input.timeframe("", title='Resolution          ', inline="MATFS", group="• Moving Average")
maSymbol = input.symbol("", title="Symbol", inline="MATFS", group="• Moving Average")

srcEMA = close

// var bool timeFrameChart = na

// if maTimeFrame == ""
//     timeFrameChart := true
// else  
//     timeFrameChart := false

// f_secureSecurity(_symbol, _res, _src) => request.security(_symbol, _res, _src[0], lookahead = barmerge.lookahead_on)

indexHighTF = barstate.isrealtime ? 1 : 0
indexCurrTF = barstate.isrealtime ? 0 : 1

f_secureSecurity(_symbol, _res, _src) => request.security(_symbol, _res, _src[indexHighTF])[indexCurrTF]

// 4. Moving Average Calculations
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)
        "HMA" => ta.wma(2*ta.wma(source, length/2)-ta.wma(source, length), math.floor(math.sqrt(length)))

out1 = f_secureSecurity(maSymbol, maTimeFrame, ma(srcEMA, lenEMA1, typeMA1))
out2 = f_secureSecurity(maSymbol, maTimeFrame, ma(srcEMA, lenEMA2, typeMA2))

// out1 = ma(srcEMA, lenEMA1, typeMA1)
// out2 = ma(srcEMA, lenEMA2, typeMA2)

// MovingAVerage 
var bool longCond4 = na
var bool shortCond4 = na    

if maType == 'Price Above/Below MA 1'
    longCond4 := close > out1
    shortCond4 := close < out1
if maType == 'Price Crossing Up/Down MA 1'
    longCond4 := ta.crossover(close, out1)
    shortCond4 := ta.crossunder(close, out1)
if maType == 'MA 1 Above/Below MA 2'
    longCond4 := out1 > out2
    shortCond4 := out1 < out2
if maType == 'MA 1 Crossing Up/Down MA 2'
    longCond4 := ta.crossover(out1, out2)
    shortCond4 := ta.crossunder(out1, out2)


var bool longEntry = na
var bool shortEntry = na

if useMovingAverage == true
    longEntry := longCond4 and inTime

if useMovingAverage == true
    shortEntry := shortCond4 and inTime
 // Plots
plotshape(longEntry, title='Long Entry', location=location.belowbar, style=shape.labelup, color=color.new(#2C9670, 0), text='Long', textcolor=color.new(color.white, 0))
plotshape(shortEntry, title='Short Entry', location=location.abovebar, style=shape.labeldown, color=color.new(#B84343, 0), text='Short', textcolor=color.new(color.white, 0))


// EMA
emaColor1 = close > out1 ? color.teal : color.red
emaColor2 = close > out2 ? color.teal : color.red
bgCol = out1 > out2 ? color.rgb(0, 137, 123, 80) : color.rgb(255, 82, 82, 80)

emaplot1 = plot(useMovingAverage ? out1 : na, title="EMA1", color=emaColor1, linewidth=2)
emaplot2 = plot(useMovingAverage and maType == 'MA 1 Above/Below MA 2' or maType == 'MA 1 Crossing Up/Down MA 2' ? out2 : na, title="EMA2", color=emaColor2, linewidth=2)

fill(emaplot1, emaplot2, color=bgCol)

if longEntry    
    strategy.entry("Long", strategy.long)

if shortEntry
    strategy.entry("Short", strategy.short)
Editor is loading...