Execute only once while condition is present  [SOLVED]

Questions about MultiCharts and user contributed studies.
quod_erat
Posts: 33
Joined: 14 Mar 2020
Has thanked: 4 times
Been thanked: 1 time

Execute only once while condition is present

Postby quod_erat » 08 May 2020

I think similar questions have been asked, but nothing specifically about what I'm looking for:

Let's say my strategy is to buy when RSI > 70, with some kind of profit condition and stop loss. Dummy code would look something like

Code: Select all

if RSI > 70 then Buy next bar market; setstopposition; setstoploss(100)
If I'm on the wrong side of the trade, I want this to only execute once. In other words, if the stoploss is hit, I don't want to execute another Buy even if RSI remains > 70. However, if RSI goes below 70, and then goes above 70 again, it's a new condition, and the then-statement should get executed.

is this possible? I tried using various loop logics, but the compiler doesn't let me place an order command inside a for loop. I thought that `once` might work --

Code: Select all

if RSI > 70 then once buy next bar market
... but it doesn't.

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: Execute only once while condition is present

Postby TJ » 08 May 2020

What you should be looking for is the first instance of RSI crossing into 70+.

quod_erat
Posts: 33
Joined: 14 Mar 2020
Has thanked: 4 times
Been thanked: 1 time

Re: Execute only once while condition is present  [SOLVED]

Postby quod_erat » 09 May 2020

What you should be looking for is the first instance of RSI crossing into 70+.
What a great example of how you just need a second pair of eyes on a problem sometimes. I was deep in the for/while/repeat-loops weeds and complex counters. I can't believe I was so blind to

Code: Select all

crosses over
. Thanks!

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: Execute only once while condition is present

Postby TJ » 09 May 2020

Another way to code this logic:

Code: Select all

If RSI > 70 and RSI [1] <= 70 then . . .

quod_erat
Posts: 33
Joined: 14 Mar 2020
Has thanked: 4 times
Been thanked: 1 time

Re: Execute only once while condition is present

Postby quod_erat » 09 May 2020

Another way to code this logic:

Code: Select all

If RSI > 70 and RSI [1] <= 70 then . . .
Is there an advantage of doing it this way (computationally or otherwise)?

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: Execute only once while condition is present

Postby TJ » 09 May 2020

They should produce the same result. No advantage to either.


Return to “MultiCharts”