Dark_Trader-x01
Pine Script Rookie
Pine Script Rookie
Posts: 7
Joined: September 26th, 2021
Contact: TradingView Profile

minimum value of a set of candles around a crossover of moving averages and plot the candle with this minimum value

Please, I need some advice about the strategy to follow, commands to use, etc. . . to get the minimum value of a set (for example 3) of japanese candles around a crossover of moving averages in order to set there my stop-loss and indicate somehow the candle in which this minimum value is given:
https://imgur.com/a/qjltm6T


This is the code I am developing at the moment. This is a small part of a bigger one:
https://imgur.com/a/qVDm9bE
https://pastebin.com/bZgBLrxi



Code: Select all

This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Dark_Trader-x01
 
//@version=4
strategy("SL_around_cross", overlay=true)
 
 
// DECLARACION DE VARIABLES
var float sl = na
 
 
// EMAs
EMA_9 = ema(close, 9)
EMA_21 = ema(close, 21)
EMA_100 = ema(close, 100)
 
 
// CONDICION CRUCE EMAs
cruce_BUY = crossover(EMA_9, EMA_21)
cruce_SELL = crossunder(EMA_9, EMA_21)
 
 
// STOP-lOSS
if cruce_BUY
    sl := lowest(close, 3) 
 
 
// TAKE PROFIT
 
 
// ESTRATEGIA
/// ALCISTA
//strategy.entry("BUY", true, 2000, when = candle_BUY_activada)
//strategy.close("BUY", when = cruce_SELL)
/// BAJISTA
//strategy.entry("SELL", false, 2000, when = candle_SELL_activada)
//strategy.close("SELL", when = cruce_BUY)
 
 
// PLOTS
plotshape(cruce_BUY, style=shape.arrowup, text="cruce_BUY", location=location.belowbar, color=color.green)
plotshape(cruce_SELL, style=shape.arrowdown, text="cruce_SELL", location=location.abovebar, color=color.red)



Any advice will be welcome!

Thank you in advance!

User avatar
Matthew
Site Admin
Site Admin
Posts: 92
Joined: July 1st, 2020
Location: Australia
Contact: Website Facebook Twitter TradingView Profile

Re: minimum value of a set of candles around a crossover of moving averages and plot the candle with this minimum value

Hi Dark_Trader!

Unfortunately I don't have time to help with this as it would take me at least half an hour or maybe more to figure out what you're trying to do with your script and how to add this functionality to it, but hopefully someone here who has taken my Mastery Course will be able to help you. Good luck with your trading my friend and sorry I can't be of more assistance with this one

Deep-Wave
Pine Script Master
Pine Script Master
Posts: 44
Joined: September 4th, 2020
Contact: TradingView Profile

Re: minimum value of a set of candles around a crossover of moving averages and plot the candle with this minimum value

Hi Dark_Trader,

i tried to figure out what you want to do with your script and adjusted your code and added some comments, I hope this gets you headed in the right direction!
Best of luck! :cool:

Code: Select all

//This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Dark_Trader-x01
 
//@version=4
strategy("SL_around_cross", overlay=true)
lookback = input(title="Lookback Candles", defval=3) // amount of candles to look back for SL placement
rr = input(title="Risk:Reward", type=input.integer, defval=1) // risk reward ratio, 1 means 1 to 1 ratio, 2 means 1 to 2 ratio (risk 1 in order to win 2) and so on

// DECLARACION DE VARIABLES
var float sl_long  = na
var float sl_short = na
var float tp_long  = na
var float tp_short = na
var float ep_long  = na
var float ep_short = na

longstop  = lowest(low,lookback) //lowest low over lookback candles input  - for long stoploss
shortstop = highest(high,lookback) //highest high over lookback candles input  - for short stoploss
longstop_distance  = abs(close - longstop) //distance bewteen entry price and stop loss for long trade
shortstop_distance = abs(close - shortstop) //distance bewteen entry price and stop loss for short trade
TP_long  = close + rr * longstop_distance // tp calculation as a function of risk to reward
TP_short = close - rr * shortstop_distance // tp calculation as a function of risk to reward

// EMAs
EMA_9 = ema(close, 9)
EMA_21 = ema(close, 21)
EMA_100 = ema(close, 100)
 
 
// CONDICION CRUCE EMAs
cruce_BUY = crossover(EMA_9, EMA_21)
cruce_SELL = crossunder(EMA_9, EMA_21)

 
// STOP-lOSS
if cruce_BUY and strategy.position_size == 0 // if buy signal occurs and you have no position open
    ep_long := close
    sl_long := longstop
    tp_long := TP_long

if cruce_SELL and strategy.position_size == 0 // if sell signal occurs and you have no position open
    ep_short := close
    sl_short := shortstop
    tp_short := TP_short


// ESTRATEGIA
/// ALCISTA
strategy.entry("BUY", strategy.long, 2000, when = cruce_BUY)
strategy.exit("CLOSE BUY", from_entry = "BUY", stop=sl_long, limit=tp_long)
strategy.close("BUY", when = cruce_SELL)

/// BAJISTA
strategy.entry("SELL", strategy.short, 2000, when = cruce_SELL)
strategy.exit("CLOSE SELL", from_entry = "SELL", stop=sl_short, limit=tp_short)
strategy.close("SELL", when = cruce_BUY)
 
 
// PLOTS
plot(EMA_9,   color=color.blue,  linewidth=1)
plot(EMA_21,  color=color.gray,  linewidth=1)
plot(EMA_100, color=color.black, linewidth=2)

plotshape(cruce_BUY, style=shape.arrowup, text="cruce_BUY", location=location.belowbar, color=color.green)
plotshape(cruce_SELL, style=shape.arrowdown, text="cruce_SELL", location=location.abovebar, color=color.red)




Return to “Request Scripts”