Page 1 of 1

How can I have my pinescript strategy enter after two conditions are met? (But not necessarily at the same time)

Posted: Thu Sep 01, 2022 6:39 pm
by mikendo
for example:

When the MFI gives me a signal for an overbought stock, i wait for a bearish cross of the MACD lines. If this happens, we go short.

Similarly, it acts the same way in the opposite direction. An oversold MFI reading and a bullish cross of the MACD lines generates a long signal but we wait for the bullish cross AFTER the MFI reading, they don't have to happen at the same time.

I've coded out the whole strategy in pinescript but I'm stuck trying to figure out how to allow for the initial MFI signal to uphold long enough to find the cross of the MACD lines- so that I can enter when either the conditions are met at the same time or the MACD cross comes after the MFI signal.

I hope I've making sense :zen:

I can share my current code if needed :happy2:

Re: How can I have my pinescript strategy enter after two conditions are met? (But not necessarily at the same time)

Posted: Sun Oct 16, 2022 11:17 pm
by purplemint22
I think you would have to play around with getting an MFI reading from a certain number of bars back, but it would help to see the code if you are still able to share it.

Re: How can I have my pinescript strategy enter after two conditions are met? (But not necessarily at the same time)

Posted: Fri Dec 23, 2022 3:11 am
by josh
so do you mean that condition 1 is true and then one hour later is false but then condition 2 becomes true that it would still execute?

not sure about that one.

In this code, the position will be entered only if both condition1 and condition2 are true. If either condition is false, the position will not be entered. If condition1 is true and condition2 becomes true later, the position will still be entered. Similarly, if condition2 is true and condition1 becomes true later, the position will still be entered.

Code: Select all

if (condition1)
{
    // Do something
}

if (condition2)
{
    // Do something
}

if (condition1 and condition2)
{
    // Enter position
}