Recursive variables

RmS59

RmS59

Member
I hope someone can help me with Recursive variables.


I want to capture if the last signal was a buy or if it was a sell.

How do I retain the status? When I try this, nothing plots.

def signalstate = if buysignal then 1 else if sellsignal then -1 else signalstate[1];
# buysignal and sellsignal only have a value when the signal occurs, and is equal to Double.NaN the rest of the time

plot Trend = signalstate;


Can someone educate me on what I am doing wrong?

thanks
 
T

tomsk

Well-known member
VIP
Assuming you already have definitions for buy and sell signals already established, here is how you retain the state whether the last signal was a buy or sell.

Code:
def signalstate;

if buysignal {
    signalstate = 1;
} else if sellsignal {
    signalstate = -1;
} else {
    signalstate = signalstate[1];
}

AddLabel(1, "State = " + if signalstate == 1 then "Buy" else if signalstate == -1 then "Sell" else " ", Color.White);
 
RmS59

RmS59

Member
this didn't work. Do I need to do something to initialize signalstate ? On first bar, there will be no signalstate[1]
 
RmS59

RmS59

Member
I take that back!! Sorry.

I was assuming the wrong data type of my signals.

When I changed it to:

if !IsNaN(buysignal) {
signalstate = 200;
} else if !IsNaN(sellsignal) {
signalstate = -200;
} else {
signalstate = signalstate[1];
}

It worked fine.

Thanks for your help
 

Similar threads

Top