Page 1 of 1

Setting multiple indicators to give alerts

Posted: Tue Feb 22, 2022 1:33 pm
by Champy4
Hi!

I’m pretty new to pine coding and have recently been looking at creating a strategy based around a few indicators giving signals before the buy or sell alert is actioned. I have managed to create a crossover which looks like below and is working and plotted on my chart.

DINcross = ta.crossover(minus, adx ) ? minus : na

However I’m looking to combine this with the alert only being given if there is also an engulfing candle at the time of the cross and only alerts when with trend based on a 28 moving average, I’m struggling to know how or where to include the ‘and’functions and best code for engulfing candles.

Any help would be really appreciated


Sorry if this doesn’t make much sense!

Thanks
Adam

Re: Setting multiple indicators to give alerts

Posted: Mon Feb 28, 2022 2:30 am
by chooka
Hey there,

If you can find some examples of code for the Engulfing Candle (note: there's quite a few already on TV).
There is also an inbuilt one located here:
Indicators -> Technicals -> Candlestick Patterns -> Engulfing - Bullish

Then im sure you can get help to put it together with what you have already.

If you get your code and post it within

Code: Select all

 CODE GOES HERE 
It makes it a lot easier to read on this forum also

-Chooka

Re: Setting multiple indicators to give alerts

Posted: Tue Mar 01, 2022 11:55 am
by processingclouds
You can combine multiple conditions using logic operators , like "and" "or" "not"

So if you have engulfing candle variable eCandle set to true when it detects it, you can combine them as :

alertNow = ta.crossover(minus, adx ) and eCandle

you can add more conditions to it like ema is above 200

alertNow = ta.crossover(minus, adx ) and eCandle and ta.ema(close, 200)

Re: Setting multiple indicators to give alerts

Posted: Sun Mar 06, 2022 7:41 pm
by Champy4
Great thank you! I have managed to get them working :up: , I am new to pine coding and coding in general so I am trying to convert or create different strategy's to get used to the whole process and try to gain a little knowledge.. I do seem to keep coming up against an issue with the strategy tester not entering trades on the conditions i have set? ideally i'd lile to close after a certain amount of bars, I also am not ever able to see any stats on profit or drawn down - it seems to stay on $0 or a few pence difference. I have copied the code from one such example, could you see if there's anything obvious causing this? thanks again, this forum and pinemasters have been a god send!


Code: Select all

 strategy("My strategy", overlay=true, currency = currency.USD, initial_capital = 100000, default_qty_type = strategy.percent_of_equity, default_qty_value = 1000, margin_long = 1000, margin_short = 1000)




RSI_PER = 14
pivot_right = 5
pivot_left = 5
max_range=50
min_range=5


// Creating rsi indicator
rsi_value = ta.rsi(close, RSI_PER)

//plot(rsi_value, title="RSI", linewidth=2, color=color.white)
//hline(50, linestyle=hline.style_dotted)
//rsi_ob = hline(70, linestyle=hline.style_dotted)
//rsi_os = hline(30, linestyle=hline.style_dotted)
//fill(rsi_ob, rsi_os, color=color.new(color.red, 80))

//check if we have pivot low in rsi
pivot_low_true = na(ta.pivotlow(rsi_value, pivot_left, pivot_right)) ? false : true //returns price of the pivot low point. It returns 'NaN', if there was no pivot low point.
pivot_high_true = na(ta.pivothigh(rsi_value, pivot_left, pivot_right)) ? false : true
//Create a function that returns true/false
confirm_range(x) => 
    bars = ta.barssince(x == true) //Counts the number of bars since the last time the condition was true
    min_range <= bars and bars <= max_range // makes ure bars is less than 60 and less than 5 and returns true

//------------------------------------------------------------------------------
// RSI higher low check

RSI_HL_check = rsi_value[pivot_right] > ta.valuewhen(pivot_low_true, rsi_value[pivot_right], 1) and confirm_range(pivot_low_true[1])
RSI_LH_check = rsi_value[pivot_right] > ta.valuewhen(pivot_high_true, rsi_value[pivot_right], 1) and confirm_range(pivot_low_true[1])
// Price Lower Low check

price_ll_check = low[pivot_right] < ta.valuewhen(pivot_low_true, low[pivot_right], 1)
price_12_check = high[pivot_right] < ta.valuewhen(pivot_high_true, high[pivot_right], 1)

bullCond = price_ll_check and RSI_HL_check and pivot_low_true
bearCond = price_12_check and RSI_LH_check and pivot_high_true

//STRATEGY TEST
if (bullCond)
    strategy.entry("Go Long", strategy.long, 100, when = bullCond)
    strategy.close("Go Long")

if (bearCond)
    strategy.entry("Go Short", strategy.long, 100, when = bearCond)
    strategy.close("Go Short")

// SIMPLE ENTRY LOGIC (insert long/short signal here)









//Plot the areas, terneary conditional operator
//plot(pivot_low_true ? rsi_value[pivot_right] : na,offset=-pivot_right,linewidth=3,color=(bullCond ? color.green : color.new(color.white, 100)))//colornew applies the specified transparency to the given color
plotshape(bullCond ? rsi_value[pivot_right] : na,offset=-pivot_right,text=" BUY ",style=shape.arrowup,location=location.belowbar, size = size.normal,color=color.green,textcolor=color.white)
//plot(pivot_high_true ? rsi_value[pivot_right] : na,offset=-pivot_right,linewidth=3,color=(bearCond ? color.red : color.new(color.white, 100)))//colornew applies the specified transparency to the given color
plotshape(bearCond ? rsi_value[pivot_right] : na,offset=-pivot_right,text=" SELL ",style=shape.arrowdown,location=location.abovebar, size = size.normal, color=color.red,textcolor=color.white)

Re: Setting multiple indicators to give alerts

Posted: Wed Mar 09, 2022 9:40 am
by processingclouds
You need to put version number and put with your code, so its easier to paste and check the code.
//@version=5

Your strategy entry lines should be as :
strategy.entry("Go Long", strategy.long, 100, when = bullCond)
strategy.entry("Go Short", strategy.short, 100, when = bearCond)

NOTE:
1. You do not need if (bearCond) or if (bullCond) as you are already using when = bullCond and when = bearCond within the strategry.entry -- They are your if statements within the entry function
2. You need strategy.short to specify short direction (You had strategy.long for short entries)
3. You were entering and closing the trade at the same time so i removed the strategy.close lines.
4. If you are placing short and long as swings, than strategy tester will handle that. i.e. if you are in long position and you send short entry, it will close long position and short it by itself.

Re: Setting multiple indicators to give alerts

Posted: Wed Mar 09, 2022 4:53 pm
by Champy4
processingclouds wrote:
Wed Mar 09, 2022 9:40 am
You need to put version number and put with your code, so its easier to paste and check the code.
//@version=5

Your strategy entry lines should be as :
strategy.entry("Go Long", strategy.long, 100, when = bullCond)
strategy.entry("Go Short", strategy.short, 100, when = bearCond)

NOTE:
1. You do not need if (bearCond) or if (bullCond) as you are already using when = bullCond and when = bearCond within the strategry.entry -- They are your if statements within the entry function
2. You need strategy.short to specify short direction (You had strategy.long for short entries)
3. You were entering and closing the trade at the same time so i removed the strategy.close lines.
4. If you are placing short and long as swings, than strategy tester will handle that. i.e. if you are in long position and you send short entry, it will close long position and short it by itself.
Thank you! I've played around with the changes and it's working perfectly, thanks again :zen: