Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-08-06· OpenAIAi2 WildChat-4.8M (AI2)

make this strategy for tradingview. // This source code is subject to the terms of the…

  1. User

    make this strategy for tradingview. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © zkdev

    //@version=5 indicator('Bull Market Support Band', overlay=true, timeframe='W', timeframe_gaps=true)

    source = close smaLength = 20 emaLength = 21

    sma = ta.sma(source, smaLength) ema = ta.ema(source, emaLength)

    outSma = request.security(syminfo.tickerid, timeframe.period, sma) outEma = request.security(syminfo.tickerid, timeframe.period, ema)

    smaPlot = plot(outSma, color=color.new(color.red, 0), title='20w SMA') emaPlot = plot(outEma, color=color.new(color.green, 0), title='21w EMA')

    fill(smaPlot, emaPlot, color=color.new(color.orange, 75), fillgaps=true)

  2. Assistant

    To convert your indicator into a strategy on TradingView, you need to define entry and exit conditions for trades. In your code, you have a simple Bull Market Support Band indicator using a 20-week SMA and a 21-week EMA. Let's define a simple strategy that will buy when the current price crosses above both the SMA and EMA and sell when the price crosses below both. Remember, this is just a basic example; you can adjust the logic as needed to better fit your trading style or objectives.

    Here's how you might convert your indicator into a strategy:

    //@version=5
    strategy('Bull Market Support Strategy', overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.1)
    
    // Inputs
    smaLength = input(20, 'SMA Length')
    emaLength = input(21, 'EMA Length')
    useClose = input(true, 'Use close prices for crossover', inline='crossover')
    source = useClose ? close : hl2
    
    // Calculate SMA and EMA
    sma = ta.sma(source, smaLength)
    ema = ta.ema(source, emaLength)
    
    // Weekly timeframe calculation
    [outSma, outEma] = request.security(syminfo.tickerid, 'W', [sma, ema])
    
    // Plotting
    smaPlot = plot(outSma, color=color.new(color.red, 0), title='20w SMA')
    emaPlot = plot(outEma, color=color.new(color.green, 0), title='21w EMA')
    fill(smaPlot, emaPlot, color=color.new(color.orange, 75), fillgaps=true)
    
    // Strategy logic
    bullishCondition = crossover(close, outSma) and crossover(close, outEma)
    bearishCondition = crossunder(close, outSma) and crossunder(close, outEma)
    
    // Execute strategy
    if (bullishCondition)
        strategy.entry('Long', strategy.long)
    
    if (bearishCondition)
        strategy.close('Long')
    
    // Visualization
    bgcolor(bullishCondition ? color.new(color.green, 85) : na, title='Bullish Background')
    bgcolor(bearishCondition ? color.new(color.red, 85) : na, title='Bearish Background')
    

    Explanation:

    • Inputs: The length of SMA and EMA can be configured with input() functions. I've also added an option to switch between using closing prices or the midpoint hl2 for crossover calculations which you can activate or deactivate using the useClose boolean.

    • Security Function: You are using request.security() to compute weekly SMA and EMA on the current time frame which remains unchanged because it's already suitable for your needs.

    • Conditions: The strategy buys when the close price crosses over both the weekly SMA and EMA, and it closes the position when it crosses below both.

    • Entry and Exit: Defined using strategy.entry() and strategy.close(), which handle the execution of trades based on the defined conditions.

    • Visualization: Adds background color to indicate bullish and bearish conditions for visual clarity.

    Adjust and expand upon this basic framework to tailor it to your specific trading requirements or integrate additional conditions and indicators as needed.

  3. User

    fixed line 23 Could not find function or function reference 'crossover' Could not find function or function reference 'crossover'

  4. Assistant

    The error message you're encountering indicates that the crossover function is not being recognized. In TradingView's Pine Script, functionality like crossover should be part of the built-in technical analysis functions. If you're encountering this error, it might be due to a typo or similar issue.

    However, since the crossover function is a standard part of Pine Script (it checks if one series crosses over another), it's important to ensure there's no misuse of variables or scope issues that might lead to such an error. Here’s a corrected version to verify the usage of these functions:

    Corrected Pine Script Strategy

    //@version=5
    strategy('Bull Market Support Strategy', overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.1)
    
    // Inputs
    smaLength = input(20, 'SMA Length')
    emaLength = input(21, 'EMA Length')
    useClose = input(true, 'Use close prices for crossover', inline='crossover')
    source = useClose ? close : hl2
    
    // Calculate SMA and EMA
    sma = ta.sma(source, smaLength)
    ema = ta.ema(source, emaLength)
    
    // Weekly timeframe calculation
    [outSma, outEma] = request.security(syminfo.tickerid, 'W', [sma, ema])
    
    // Plotting
    smaPlot = plot(outSma, color=color.new(color.red, 0), title='20w SMA')
    emaPlot = plot(outEma, color=color.new(color.green, 0), title='21w EMA')
    fill(smaPlot, emaPlot, color=color.new(color.orange, 75), fillgaps=true)
    
    // Strategy logic
    bullishCondition = ta.crossover(close, outSma) and ta.crossover(close, outEma)
    bearishCondition = ta.crossunder(close, outSma) and ta.crossunder(close, outEma)
    
    // Execute strategy
    if (bullishCondition)
        strategy.entry('Long', strategy.long)
    
    if (bearishCondition)
        strategy.close('Long')
    
    // Visualization
    bgcolor(bullishCondition ? color.new(color.green, 85) : na, title='Bullish Background')
    bgcolor(bearishCondition ? color.new(color.red, 85) : na, title='Bearish Background')
    

    Key Points:

    • Prefix ta.: Ensure you prefix crossover and crossunder with ta. to explicitly reference the built-in technical analysis functions in Pine Script version 5. Using ta.crossover and ta.crossunder should properly define these functions and resolve the error.

    • Use of strategy.entry and strategy.close: These functions are correct as-is if your goal is to enter long positions and close them based on the crossover conditions.

    If you continue to have issues, double-check if there might be any unseen or unintended code alterations leading to this error, such as stray characters or syntax errors elsewhere in your script.