I want to define a condition where 3 out of 4 bars are green

C

Camus1612

New member
I want to define a condition that will be true if on the 180D/4H chart 3 out of 4 bars are green.
How do I do that?

Thanks :)
 
T

theelderwand

Member
VIP
Hope the following helps, the time is from the chart itself,
Code:
def greenBar = open < close;
plot greenBarsCount = greenBar + greenBar[1] + greenBar[2] + greenBar[3];
You can use the greenBarsCount to do what you want.
 
C

Camus1612

New member
Awesome!
And if I want to define a condition where 3 out of 4 are green?
 
C

Camus1612

New member
Thank you very much bro,
I have a problem with the last line here which is giving me red squares at 'addorder' and 'win'.
What did I do wrong?
Python:
def SMA180 = Average (close, 180);
def EMA15 = ExpAverage (close, 15);
def greenBar = open < close;
def price = close;

def greenBarsCount = greenBar + greenBar[1] + greenBar[2] + greenBar[3];

def sucess =  greenBarsCount >= 3 and  price > ema15 and price > sma180;

def win = if sucess then 1 else 0;

addorder(ordertype.buy_auto, win = 1, close, 50, color.blue, color.blue, "CONFIRMED");
Thanks.
 
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@Camus1612 Try this:

Code:
addorder(ordertype.buy_auto, condition = win, close, 50, color.blue, color.blue, "CONFIRMED");
 
C

Camus1612

New member
Fantastic, thank you.

Can I code something like :

If 10 bars ago there was a trade with P&L <0 don't set buy order.

(In order to recognize a consolidation period)
Or to use the buy price of the last buy order

I know I can use historical data of the chart, but can I use historical data of code itself?
 
Thread starter Similar threads Forum Replies Date
Z Questions 0
D Questions 3
G Questions 5

Top