Building a Strategy Discussion

Hi guys i’m still struggling, my code is as follows and I get the following error message

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

@version=3

strategy(title=“Moving Average Crossing”, overlay = true)

// Date & Time
fromMonth = input (defval=1, title = “From month”, minval = 1)
fromDay = input(defval=1, title = “From day”, minval = 1)
fromYear = input(defval=2019, title = “From year”, minval = 2014)

toMonth = input(defval=1, title = “To month”, minval = 1)
toDay = input(defval=1, title = “To day”, minval = 1)
toYear = input(defval=2025, title = “To year”, minval = 2014)

// Definitions
shortMa=sma(close, 20)
longMa=sma(close, 50)

// Logic
timeInRange = (time>timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time<timestamp(toYear, toMonth, toDay, 23, 59))
longSignal = crossover(shortMa, longMa) and timeInRange
shortSignal = crossover (longMa, shortMa) and timeinRange

// Positions
strategy.entry(id=“longPosition”, long = true, when = longSignal)
strategy.entry(id=“shortPosition”, long = false, when = shortSignal)

Add to Chart operation failed, reason: Script could not be translated from: null

Any guidance would be much appreciated

See @thecil’s comment above. The line

@version=3

needs to be commented out like this:

// @version=3
1 Like

Sorry I didn’t see that comment. Thankyou so much for helping me out @uyan and @thecil

1 Like

I conducted a strategy based on 3 moving averages, so it took a while to put the pieces of the puzzle together.

Here are the resources I used:

3 EMA Crossover Trading Secrets For Any Market
TradingView Resolutions/Timeframes/Periods - TradingView Pine Script Tutorial 7

The strategy is based on the 9/21/55 day moving averages.
It checks the RSI for OB/OS signals before executing trades.

I was messing around with “super” long & short positions, but I’m not actually sure if that methodology makes sense. I’m sure I’ll find out once I’ve done some more trading research.
You can see my positions, and some ideas I was testing, at the bottom of the script.

Triple Many Moving Averages (WARNING: LONG)
//@version=4 

//
// Pine Script v4
// Original Author:
// @author BigBitsIO
// (Original) Script Library: https://www.tradingview.com/u/BigBitsIO/#published-scripts
//
// strategy(title, shorttitle, overlay, format, precision)
// https://www.tradingview.com/pine-script-docs/en/v4/annotations/study_annotation.html 
strategy(shorttitle = "TManyMA Forecasting - edited", title="Triple Many Moving Averages", overlay=true, initial_capital=2000, commission_type=strategy.commission.percent, commission_value=0.2) // Improved with HMA, DEMA and TEMA and source options for MAs

// MA#Period is a variable used to store the indicator lookback period.  In this case, from the input.
// input - https://www.tradingview.com/pine-script-docs/en/v4/annotations/Script_inputs.html
MA1Period = input(20, title="MA1 Period", minval=1, step=1)
MA1Type = input(title="MA1 Type", defval="SMA", options=["RMA", "SMA", "EMA", "WMA", "HMA", "DEMA", "TEMA", "VWMA"])
MA1Source = input(title="MA1 Source", type=input.source, defval=close)
MA1Resolution = input(title="MA1 Resolution", defval="00 Current", options=["00 Current", "01 1m", "02 3m", "03 5m", "04 15m", "05 30m", "06 45m", "07 1h", "08 2h", "09 3h", "10 4h", "11 1D", "12 1W", "13 1M"])
MA1Visible = input(title="MA1 Visible", type=input.bool, defval=true) // Will automatically hide crossBovers containing this MA

MA2Period = input(50, title="MA2 Period", minval=1, step=1)
MA2Type = input(title="MA2 Type", defval="SMA", options=["RMA", "SMA", "EMA", "WMA", "HMA", "DEMA", "TEMA", "VWMA"])
MA2Source = input(title="MA2 Source", type=input.source, defval=close)
MA2Resolution = input(title="MA2 Resolution", defval="00 Current", options=["00 Current", "01 1m", "02 3m", "03 5m", "04 15m", "05 30m", "06 45m", "07 1h", "08 2h", "09 3h", "10 4h", "11 1D", "12 1W", "13 1M"])
MA2Visible = input(title="MA2 Visible", type=input.bool, defval=true) // Will automatically hide crossovers containing this MA

MA3Period = input(100, title="MA3 Period", minval=1, step=1)
MA3Type = input(title="MA3 Type", defval="SMA", options=["RMA", "SMA", "EMA", "WMA", "HMA", "DEMA", "TEMA", "VWMA"])
MA3Source = input(title="MA3 Source", type=input.source, defval=close)
MA3Resolution = input(title="MA3 Resolution", defval="00 Current", options=["00 Current", "01 1m", "02 3m", "03 5m", "04 15m", "05 30m", "06 45m", "07 1h", "08 2h", "09 3h", "10 4h", "11 1D", "12 1W", "13 1M"])
MA3Visible = input(title="MA3 Visible", type=input.bool, defval=true) // Will automatically hide crossovers containing this MA

ShowCrosses = input(title="Show Crosses", type=input.bool, defval=true)
ForecastBias = input(title="Forecast Bias", defval="Neutral", options=["Neutral", "Bullish", "Bearish"])
ForecastBiasPeriod = input(14, title="Forecast Bias Period")
ForecastBiasMagnitude = input(1, title="Forecast Bias Magnitude", minval=0.25, maxval=20, step=0.25)
ShowForecasts = input(title="Show Forecasts", type=input.bool, defval=true)


// MA# is a variable used to store the actual moving average value.
// if statements - https://www.tradingview.com/pine-script-reference/#op_if
// MA functions - https://www.tradingview.com/pine-script-reference/ (must search for appropriate MA)
// custom functions in  pine - https://www.tradingview.com/wiki/Declaring_Functions
ma(MAType, MASource, MAPeriod) =>
    if MAType == "SMA"
        sma(MASource, MAPeriod)
    else
        if MAType == "EMA"
            ema(MASource, MAPeriod)
        else
            if MAType == "WMA"
                wma(MASource, MAPeriod)
            else
                if MAType == "RMA"
                    rma(MASource, MAPeriod)
                else
                    if MAType == "HMA"
                        wma(2*wma(MASource, MAPeriod/2)-wma(MASource, MAPeriod), round(sqrt(MAPeriod)))
                    else
                        if MAType == "DEMA"
                            e = ema(MASource, MAPeriod)
                            2 * e - ema(e, MAPeriod)
                        else
                            if MAType == "TEMA"
                                e = ema(MASource, MAPeriod)
                                3 * (e - ema(e, MAPeriod)) + ema(ema(e, MAPeriod), MAPeriod)
                            else
                                if MAType == "VWMA"
                                    vwma(MASource, MAPeriod)
                                
res(MAResolution) =>
    if MAResolution == "00 Current"
        timeframe.period
    else
        if MAResolution == "01 1m"
            "1"
        else
            if MAResolution == "02 3m"
                "3"
            else
                if MAResolution == "03 5m"
                    "5"
                else
                    if MAResolution == "04 15m"
                        "15"
                    else
                        if MAResolution == "05 30m"
                            "30"
                        else
                            if MAResolution == "06 45m"
                                "45"
                            else
                                if MAResolution == "07 1h"
                                    "60"
                                else
                                    if MAResolution == "08 2h"
                                        "120"
                                    else
                                        if MAResolution == "09 3h"
                                            "180"
                                        else
                                            if MAResolution == "10 4h"
                                                "240"
                                            else
                                                if MAResolution == "11 1D"
                                                    "1D"
                                                else
                                                    if MAResolution == "12 1W"
                                                        "1W"
                                                    else
                                                        if MAResolution == "13 1M"
                                                            "1M"

MA1 = security(syminfo.tickerid, res(MA1Resolution), ma(MA1Type, MA1Source, MA1Period))     
MA2 = security(syminfo.tickerid, res(MA2Resolution), ma(MA2Type, MA2Source, MA2Period))
MA3 = security(syminfo.tickerid, res(MA3Resolution), ma(MA3Type, MA3Source, MA3Period))   

//MA4 = sma(security(syminfo.tickerid, "1D", close), 200)
//MA4 = security(syminfo.tickerid, '1D', ma(MA3Type, MA3Source, MA3Period))
//plot(MA4, color=color.orange, linewidth=4)

                    
// Plotting crossover/unders for all combinations of crosses
// https://www.tradingview.com/pine-script-reference/v4/#fun_label%7Bdot%7Dnew
// X means "cross(over/under)"
// Green X = crossover (e.g. A crossover B)
// Red X = crossunder (e.g. A crossunder B)
if ShowCrosses and MA1Visible and MA2Visible and crossunder(MA1, MA2)
    lun1 = label.new(bar_index, na, tostring(MA1Period)+' '+MA1Type+' X '+tostring(MA2Period)+' '+MA2Type, 
      color=color.red, 
      textcolor=color.red,
      style=label.style_xcross, size=size.small)
    label.set_y(lun1, MA1)
if ShowCrosses and MA1Visible and MA2Visible and crossover(MA1, MA2)
    lup1 = label.new(bar_index, na, tostring(MA1Period)+' '+MA1Type+' X '+tostring(MA2Period)+' '+MA2Type, 
      color=color.green, 
      textcolor=color.green,
      style=label.style_xcross, size=size.tiny)
    label.set_y(lup1, MA1)
if ShowCrosses and MA1Visible and MA3Visible and crossunder(MA1, MA3)
    lun2 = label.new(bar_index, na, tostring(MA1Period)+' '+MA1Type+' X '+tostring(MA3Period)+' '+MA3Type, 
      color=color.red, 
      textcolor=color.red,
      style=label.style_xcross, size=size.tiny)
    label.set_y(lun2, MA1)
if ShowCrosses and MA1Visible and MA3Visible and crossover(MA1, MA3)
    lup2 = label.new(bar_index, na, tostring(MA1Period)+' '+MA1Type+' X '+tostring(MA3Period)+' '+MA3Type, 
      color=color.green, 
      textcolor=color.green,
      style=label.style_xcross, size=size.tiny)
    label.set_y(lup2, MA1)
if ShowCrosses and MA2Visible and MA3Visible and crossunder(MA2, MA3)
    lun3 = label.new(bar_index, na, tostring(MA2Period)+' '+MA2Type+' X '+tostring(MA3Period)+' '+MA3Type, 
      color=color.red, 
      textcolor=color.red,
      style=label.style_xcross, size=size.tiny)
    label.set_y(lun3, MA2)
if ShowCrosses and MA2Visible and MA3Visible and crossover(MA2, MA3)
    lup3 = label.new(bar_index, na, tostring(MA2Period)+' '+MA2Type+' X '+tostring(MA3Period)+' '+MA3Type, 
      color=color.green, 
      textcolor=color.green,
      style=label.style_xcross, size=size.tiny)
    label.set_y(lup3, MA2) 

// plot - This will draw the information on the chart
// plot - https://www.tradingview.com/pine-script-docs/en/v4/annotations/plot_annotation.html
plot(MA1Visible ? MA1 : na, color=color.green, linewidth=1, title="MA1")
plot(MA2Visible ? MA2 : na, color=color.yellow, linewidth=1, title="MA2")
plot(MA3Visible ? MA3 : na, color=color.red, linewidth=1, title="MA3")


// Forecasting - forcasted prices are calculated using our MAType and MASource for the MAPeriod - the last X candles.
//              it essentially replaces the oldest X candles, with the selected source * X candles
// Bias - We'll add an "adjustment" for each additional candle being forecasted based on ATR of the previous X candles
// custom functions in  pine - https://www.tradingview.com/wiki/Declaring_Functions
bias(Bias, BiasPeriod) =>
    if Bias == "Neutral"
        0
    else
        if Bias == "Bullish"
            (atr(BiasPeriod) * ForecastBiasMagnitude)
        else
            if Bias == "Bearish"
                ((atr(BiasPeriod)  * ForecastBiasMagnitude) * -1) // multiplying by -1 to make it a negative, bearish bias


// Note - Can not show forecasts on different resolutions at the moment, x-axis is an issue
Bias = bias(ForecastBias, ForecastBiasPeriod) // 14 is default atr period
MA1Forecast1 = (security(syminfo.tickerid, res(MA1Resolution), ma(MA1Type, MA1Source, MA1Period - 1)) * (MA1Period - 1) + ((MA1Source * 1) + (Bias * 1))) / MA1Period
MA1Forecast2 = (security(syminfo.tickerid, res(MA1Resolution), ma(MA1Type, MA1Source, MA1Period - 2)) * (MA1Period - 2) + ((MA1Source * 2) + (Bias * 2))) / MA1Period
MA1Forecast3 = (security(syminfo.tickerid, res(MA1Resolution), ma(MA1Type, MA1Source, MA1Period - 3)) * (MA1Period - 3) + ((MA1Source * 3) + (Bias * 3))) / MA1Period
MA1Forecast4 = (security(syminfo.tickerid, res(MA1Resolution), ma(MA1Type, MA1Source, MA1Period - 4)) * (MA1Period - 4) + ((MA1Source * 4) + (Bias * 4))) / MA1Period
MA1Forecast5 = (security(syminfo.tickerid, res(MA1Resolution), ma(MA1Type, MA1Source, MA1Period - 5)) * (MA1Period - 5) + ((MA1Source * 5) + (Bias * 5))) / MA1Period

plot(MA1Resolution == "00 Current" and ShowForecasts and MA1Visible ? MA1Forecast1 : na, color=color.green, linewidth=1, style=plot.style_circles, title="Long 200 EMA Forecast 1", offset=1, show_last=1)
plot(MA1Resolution == "00 Current" and ShowForecasts and MA1Visible ? MA1Forecast2 : na, color=color.green, linewidth=1, style=plot.style_circles, title="Long 200 EMA Forecast 1", offset=2, show_last=1)
plot(MA1Resolution == "00 Current" and ShowForecasts and MA1Visible ? MA1Forecast3 : na, color=color.green, linewidth=1, style=plot.style_circles, title="Long 200 EMA Forecast 1", offset=3, show_last=1)
plot(MA1Resolution == "00 Current" and ShowForecasts and MA1Visible ? MA1Forecast4 : na, color=color.green, linewidth=1, style=plot.style_circles, title="Long 200 EMA Forecast 1", offset=4, show_last=1)
plot(MA1Resolution == "00 Current" and ShowForecasts and MA1Visible ? MA1Forecast5 : na, color=color.green, linewidth=1, style=plot.style_circles, title="Long 200 EMA Forecast 1", offset=5, show_last=1)


MA2Forecast1 = (security(syminfo.tickerid, res(MA2Resolution), ma(MA2Type, MA2Source, MA2Period - 1)) * (MA2Period - 1) + ((MA1Source * 1) + (Bias * 1))) / MA2Period
MA2Forecast2 = (security(syminfo.tickerid, res(MA2Resolution), ma(MA2Type, MA2Source, MA2Period - 2)) * (MA2Period - 2) + ((MA1Source * 2) + (Bias * 2))) / MA2Period
MA2Forecast3 = (security(syminfo.tickerid, res(MA2Resolution), ma(MA2Type, MA2Source, MA2Period - 3)) * (MA2Period - 3) + ((MA1Source * 3) + (Bias * 3))) / MA2Period
MA2Forecast4 = (security(syminfo.tickerid, res(MA2Resolution), ma(MA2Type, MA2Source, MA2Period - 4)) * (MA2Period - 4) + ((MA1Source * 4) + (Bias * 4))) / MA2Period
MA2Forecast5 = (security(syminfo.tickerid, res(MA2Resolution), ma(MA2Type, MA2Source, MA2Period - 5)) * (MA2Period - 5) + ((MA1Source * 5) + (Bias * 5))) / MA2Period

plot(MA2Resolution == "00 Current" and ShowForecasts and MA2Visible ? MA2Forecast1 : na, color=color.yellow, linewidth=1, style=plot.style_circles, title="Long 200 EMA Forecast 1", offset=1, show_last=1)
plot(MA2Resolution == "00 Current" and ShowForecasts and MA2Visible ? MA2Forecast2 : na, color=color.yellow, linewidth=1, style=plot.style_circles, title="Long 200 EMA Forecast 1", offset=2, show_last=1)
plot(MA2Resolution == "00 Current" and ShowForecasts and MA2Visible ? MA2Forecast3 : na, color=color.yellow, linewidth=1, style=plot.style_circles, title="Long 200 EMA Forecast 1", offset=3, show_last=1)
plot(MA2Resolution == "00 Current" and ShowForecasts and MA2Visible ? MA2Forecast4 : na, color=color.yellow, linewidth=1, style=plot.style_circles, title="Long 200 EMA Forecast 1", offset=4, show_last=1)
plot(MA2Resolution == "00 Current" and ShowForecasts and MA2Visible ? MA2Forecast5 : na, color=color.yellow, linewidth=1, style=plot.style_circles, title="Long 200 EMA Forecast 1", offset=5, show_last=1)


MA3Forecast1 = (security(syminfo.tickerid, res(MA3Resolution), ma(MA3Type, MA3Source, MA3Period - 1)) * (MA3Period - 1) + ((MA1Source * 1) + (Bias * 1))) / MA3Period
MA3Forecast2 = (security(syminfo.tickerid, res(MA3Resolution), ma(MA3Type, MA3Source, MA3Period - 2)) * (MA3Period - 2) + ((MA1Source * 2) + (Bias * 2))) / MA3Period
MA3Forecast3 = (security(syminfo.tickerid, res(MA3Resolution), ma(MA3Type, MA3Source, MA3Period - 3)) * (MA3Period - 3) + ((MA1Source * 3) + (Bias * 3))) / MA3Period
MA3Forecast4 = (security(syminfo.tickerid, res(MA3Resolution), ma(MA3Type, MA3Source, MA3Period - 4)) * (MA3Period - 4) + ((MA1Source * 4) + (Bias * 4))) / MA3Period
MA3Forecast5 = (security(syminfo.tickerid, res(MA3Resolution), ma(MA3Type, MA3Source, MA3Period - 5)) * (MA3Period - 5) + ((MA1Source * 5) + (Bias * 5))) / MA3Period

plot(MA3Resolution == "00 Current" and ShowForecasts and MA3Visible ? MA3Forecast1 : na, color=color.red, linewidth=1, style=plot.style_circles, title="Long 200 EMA Forecast 1", offset=1, show_last=1)
plot(MA3Resolution == "00 Current" and ShowForecasts and MA3Visible ? MA3Forecast2 : na, color=color.red, linewidth=1, style=plot.style_circles, title="Long 200 EMA Forecast 1", offset=2, show_last=1)
plot(MA3Resolution == "00 Current" and ShowForecasts and MA3Visible ? MA3Forecast3 : na, color=color.red, linewidth=1, style=plot.style_circles, title="Long 200 EMA Forecast 1", offset=3, show_last=1)
plot(MA3Resolution == "00 Current" and ShowForecasts and MA3Visible ? MA3Forecast4 : na, color=color.red, linewidth=1, style=plot.style_circles, title="Long 200 EMA Forecast 1", offset=4, show_last=1)
plot(MA3Resolution == "00 Current" and ShowForecasts and MA3Visible ? MA3Forecast5 : na, color=color.red, linewidth=1, style=plot.style_circles, title="Long 200 EMA Forecast 1", offset=5, show_last=1)

//--------------------------------------------------------------------------------------

//independent variables; change these to change the strategy
// MA1Period (default: 20)
// MA2Period (default: 50)
// MA3Period (default: 100)

//date and time
fromMonth = input(defval=1, title="from month", minval=1)
fromDay = input(defval=15, title="from day", minval=1)
fromYear = input(defval=2015, title="from year", minval=2014)

toMonth = input(defval=1, title="to month", minval=1)
toDay = input(defval=15, title="to day", minval=1)
toYear = input(defval=2021, title="to year", minval=2014)

//definitions
shortMa = sma(close, MA1Period)
mediumMa = sma(close, MA2Period)
longMa = sma(close, MA3Period)

period = input(14)
r = rsi(close, period)

//logic
timeInRange = (time > timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time < timestamp(toYear, toMonth, toDay, 23, 59))

// l = long, xl = super long, xxl = super super long
//xxlSignal = crossover(shortMa, longMa) and timeInRange and r < 50
//xlSignal = crossover(mediumMa, longMa) and timeInRange and r < 50
lSignal = timeInRange and crossover(shortMa, mediumMa) and shortMa > longMa and r < 70

//s = short, xs = super short, xxs = super super short
sSignal = timeInRange and crossover(mediumMa, shortMa) and longMa > shortMa and r > 30
//xsSignal = crossover(mediumMa, shortMa) and timeInRange and r > 50
//xxsSignal = crossover(longMa, shortMa) and timeInRange and r > 50

//positions
//strategy.entry(id="xxlPosition", long=true, qty=0.1, when=xxlSignal)
//strategy.entry(id="xlPosition", long=true, qty=0.1, when=xlSignal)
strategy.entry(id="lPosition", long=true, qty=0.1, when=lSignal)
//strategy.entry(id="testPosition", long=true, qty=0.1, when=shortMa > longMa and timeInRange)

strategy.entry(id="sPosition", long=false, qty=0.1, when=sSignal)

I would post it on github but I don’t know if it’s appropriate for github and it’s mostly not my code, even though the code is open source.

Strategy Demo

strategy_demonstration

The following datasets were tested:

1/15/2015 to 1/14/2016 (loss)

2015_2016_trade_BTC_fail

1/15/2016 to 1/14/2017 (no data/trades)

1/15/2017 to 1/14/2018 (no data/trades)

1/15/2018 to 1/14/2019 (profit)

2018_2019_trade_BTC


1/15/2019 to 1/14/2020 (profit)

2019_2020_trade_BTC


1/15/2020 to 1/14/2021 (profit)

2020_2021_trade_BTC

1 Like

Hi there!
Just for practice purpose I tried to follow the steps in [Building a Strategy/Programming] section, but can’t run this code in TradingView, here is the error:
image

Could someone please advise what’s wrong?

1 Like

Please provide the code in order that we can test it to help you solve the issue :face_with_monocle:

Carlos Z

1 Like
//@version=1
strategy(title="Moving Average Crossing", overlay=true);

//DATE AND TIME
fromMonth = input(defval=2, title =  "From month", minval=1)
fromDay = input(defval=1, title =  "From day", minval=1)
fromYear= input(defval=2021, title =  "Fromyear", minval=2017)

toMonth = input(defval=1, title = "To month", minval=1)
toDay = input(defval=1, title =  "To day", minval=1)
toYear = input(defval=2022, title = "To month", minval=2017)

//DEFINITIONS
shortMa = sma(close, 20)
longMa = sma(close, 50)

//LOGIC
timeInRange = (time > timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time < timestamp(toYear, toMonth, toDay, 23,59))
longSignal = crossover(shortMa, longMa)
shortSignal = crossover(longMa, shortMa)

//POSITIONS
strategy.entry(id="longPosition", long=true, when=longSignal)
strategy.entry(id="shortPosition", long=false, when=shortSignal)

You have a ; at the end of the declaration, delete it and it should work :nerd_face:

Carlos Z

1 Like

Great, now it works! Thank you Carlos :clap:
Seems like I was learning to much of Javascript before writing this pinescript, hahaha:)))

1 Like

Can’t figure out why I am getting this error message.
Screenshot from 2021-04-15 03-38-51

1 Like

Please provide the code so i can test it properly.

https://forum.ivanontech.com/t/faq-how-to-post-code-in-the-forum/35357/2

Carlos Z

Sorry for the delay.

//@version=3

strategy(title=“Moving Average Crossing”, overlay=true, initial_capital=2000, commission_type=strategy.commission.percent, commission_value=0.2)

//DATE AND TIME
fromMonth = input(defval = 5, title = “From month”, minval=1)
fromDay = input(defval = 15, title = “From day”, minval=1)
fromYear = input(defval = 2018, title = “From year”, minval=2014)

toMonth = input(defval = 8, title = “To month”, minval=1)
toDay = input(defval = 15, title = “To day”, minval=1)
toYear = input(defval = 2018, title = “To year”, minval=2014)

//DEFINITIONS
shortMa = sma(close, 67)
longMa = sma(close, 87)
r = rsi(close, 14)
//LOGIC
timeInRange = (time > timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time < timestamp(toYear, toMonth, toDay, 23, 59))
longSignal = crossover(shortMa, longMa) and timeInRange r < 50
shortSignal = crossover(longMa, shortMa) and timeInRange r > 50

//POSITIONS
strategy.entry(id=“longPostion”, long=true, qty=0.1, when=longSignal)
strategy.entry(id=“shortPosition”, long=false, qty=0.1, when=shortSignal)

1 Like

Problem is on the declaration for timeInRange variable whitin those 2 variables (long and short signals), that one should only return true or false based on the values you declare it.

timeInRange = (time > timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time < timestamp(toYear, toMonth, toDay, 23, 59))

When [time is greater than the specified time] and [time is less than specified time] it will return true or false.

To fix it, you just need to add an and between the timeInRange and r >50
it will look like this:

longSignal = crossover(shortMa, longMa) and timeInRange and r < 50
shortSignal = crossover(longMa, shortMa) and timeInRange and r > 50

Carlos Z

Ok, thanks Carlos.

Troy

image

Hi filip,

For some reason I get the message “no data” when I run test the strategy on 1 hour time interval.
Do you have any idea how this is possible?

I wrote my own code, but I also copied code from this forum with the same result.
So guess the problem is not in the code itself but maybe in the time interval or Symbol?

Hope you can help,

Thanks

PS if the image is not clear, here is the script:

//@version=3
strategy(title =“moving average crossing”, overlay =true)

//Date and Time
fromMonth = input(defval =5, title =“from month”,minval=1)
fromDay = input(defval =15, title =“from day”,minval=1)
fromYear = input(defval =2018, title =“from year”,minval=2014)

toMonth = input(defval =8, title =“to month”,minval=1)
toDay = input(defval =15, title =“to day”,minval=1)
toYear = input(defval =2018, title =“to year”,minval=2014)

//Definitions
shortMa = sma(close,20)
longMa = sma(close, 50)

//Logic
timeInRange =(time > timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time < timestamp(toYear, toMonth, toDay, 23,59))
longSignal = crossover(shortMa, longMa)and timeInRange
shortSignal = crossover(longMa,shortMa) and timeInRange

//Positions
strategy.entry(id=“longPosition”, long=true, when=longSignal)
strategy.entry(id=“shortPosition”, long=false, when=shortSignal)

1 Like

Hey @NiandraLaDes, hope you are ok.

Can you please provide your code in the following way please?

Then I can help you review your code to check the errors on it :face_with_monocle:

Carlos Z

He Carlos,

Sorry didn’t know about this. Works the same as on github I see :grinning:

strategy(title="MACStrategy", overlay=true)

//DATE AND TIME
fromMonth = input(defval = 1, title = "From month", minval=1)
fromDay = input(defval = 1, title = "From day", minval=1)
fromYear = input(defval = 2019, title = "From year", minval=2014)

toMonth = input(defval = 1, title = "To month", minval=1)
toDay = input(defval = 1, title = "To day", minval=1)
toYear = input(defval = 2025, title = "To year", minval=2014)

// MA DEFINITIONS
shortMa = sma(close, 20)
longMa = sma(close, 50)

// LOGIC
timeInRange = (time > timestamp(fromYear, fromMonth, fromDay, 00, 00, 00)) and (time < timestamp(toYear, toMonth, toDay, 00, 00, 00))
longSignal = crossover(shortMa, longMa) and timeInRange
shortSignal = crossover(longMa, shortMa) and timeInRange

// POSITIONS
strategy.entry("longPosition", true, when=longSignal)
strategy.entry("shortPosition", false, when=shortSignal)
1 Like

I dont see any issue with your code, it runs properly for me by just copy/paste.

image

Maybe you can describe how to replicate the issue :face_with_monocle:

Carlos Z

He Carlos I made a video so you can see my the problem, the code, settings and time interval + Symbol.

I uploaded it to dropbox since it is too big for uploading it to the forum here.
See Link

Thanks

1 Like

Its also working on different time frames, 1 hour, 4 hour, 1 day


I think you have to zoom out or in the charts, I do it by the scroll wheel of my mouse, also you can zoom in/out the price by clicking it and holding the click while zooming.

Carlos Z