gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)
Can you code me a pinescript tradingview strategy bassed on SMA 20 Every time when price…
- User
Can you code me a pinescript tradingview strategy bassed on SMA 20 Every time when price is above and give es bullish engulfing enter for a buy and ever time price is below and gives bearish engulfing enter for a sell. Stop loss will be 1% of the capital and take profit at 296
- Assistant
Sure, here's the Pine Script code for your trading strategy:
//@version=4 strategy("SMA 20 Engulfing Strategy", overlay=true) // Define the inputs shortSMA = input(20, title = "Short SMA period") takeProfit = input(296, title = "Take Profit") stopLossPct = input(1, title = "Stop Loss %", minval = 0, maxval = 5) / 100 // Calculate the short SMA shortSMAValue = sma(close, shortSMA) // Check for bullish engulfing pattern bullishEngulfing = bullEngulfing(open, high, low, close) // Check for bearish engulfing pattern bearishEngulfing = bearEngulfing(open, high, low, close) // Define long entry and exit signals longCondition = close > shortSMAValue and bullishEngulfing longStopLoss = strategy.position_avg_price * (1 - stopLossPct) longTakeProfit = strategy.position_avg_price + takeProfit // Define short entry and exit signals shortCondition = close < shortSMAValue and bearishEngulfing shortStopLoss = strategy.position_avg_price * (1 + stopLossPct) shortTakeProfit = strategy.position_avg_price - takeProfit // Show the SMA on the chart plot(shortSMAValue, color = color.orange, title = "SMA 20", linewidth = 2) // Place long and short orders if (longCondition) strategy.entry("Buy", true) strategy.exit("Stop Loss", "Buy", stop = longStopLoss) strategy.exit("Take Profit", "Buy", limit = longTakeProfit) if (shortCondition) strategy.entry("Sell", false) strategy.exit("Stop Loss", "Sell", stop = shortStopLoss) strategy.exit("Take Profit", "Sell", limit = shortTakeProfit)Here's how the strategy works:
- The script first defines the input parameters: the period of the short SMA, the take profit target, and the stop loss percentage.
- It then calculates the short SMA using the
sma()function. - Next, it checks for bullish and bearish engulfing patterns using the built-in
bullEngulfing()andbearEngulfing()functions. These functions returntruewhen an engulfing pattern is found. - The
longConditionandshortConditionvariables define the conditions for placing long and short trades, respectively. For example, the long condition is true when the close is above the short SMA and there's a bullish engulfing pattern. - The
longStopLossandshortStopLossvariables calculate the stop loss levels, which are 1% of the capital away from the entry price. - The
longTakeProfitandshortTakeProfitvariables calculate the take profit levels, which are the entry price plus or minus the specified target. - Finally, the
strategy.entry()andstrategy.exit()functions place the long and short orders with the appropriate stop loss and take profit levels.
Note that this is a basic strategy and should be tested thoroughly before using it with real money. It's also important to note that past performance is not indicative of future results.