mikendo
Pine Script Rookie
Pine Script Rookie
Posts: 3
Joined: September 1st, 2022

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

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:

purplemint22
Pine Script Rookie
Pine Script Rookie
Posts: 17
Joined: October 14th, 2022

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

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.

josh
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: December 23rd, 2022

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

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
}

Return to “Pine Script Q&A”