# Setting up a Condition correctly

Questions about MultiCharts and user contributed studies.
Jonny473
Posts: 60
Joined: 04 Apr 2016
Has thanked: 5 times
Been thanked: 1 time

### Setting up a Condition correctly

Ok here is what I want to accomplish with this:

I built a strategy that buys x-days before month end and sells y-days after beginning of month. Therefore I used the "DaysFromBOM" and "DaysBeforeEOM" function, which calculates the number of trading days left ("DaysBeforeEOM") and finished ("DaysFromBOM"). Lets take a look at an example applied to an Daily chart: In this case ss stands for the number of days before month end and rr descibes the count starting from a new month.

Code: Select all

```Inputs:ss(2),rr(4); Variablespatterndetected1(false); patterndetected1=false; if (DaysFromBOM(Date)<rr or DaysBeforeEOM(Date)<ss) then patterndetected=true; if patterndetected1=false then sell 1 contract this bar close; if patterndetected1=true then buy 1 contract next bar open;```

Looks as supposed so far.

Lets add a sendond time span for days within a month to also go long:

Code: Select all

```Inputs:ss(0),rr(0); Variables:patterndetected(false),patterndetected1(false); patterndetected1=false; patterndetected=false; if (DaysFromBOM(Date)<rr or DaysBeforeEOM(Date)<ss) then patterndetected1=true; if patterndetected1=false then sell 1 contract from entry ("1") this bar close; if patterndetected1=true then buy ("1") 1 contract next bar open; if (DaysFromBOM(Date)=7 or DaysFromBOM(Date)=8 or DaysFromBOM(Date)=9 or DaysFromBOM(Date)=10) then patterndetected=true; if patterndetected=false then sell 1 contract from entry ("2") this bar close; if patterndetected=true then buy ("2") 1 contract next bar open; ```
So I want to be in the market on trading day 7-9 as well. Works also for a simple buy strategy by giving the orders names ("1") and ("2").

As you can see I remodeled the initial strategy using the MRO function, which takes into account the condition (DaysBeforeEOM(Date)) entering 2 days before month end and condition staying true for the next 6 bars (again to exit after 4 trading days in new month). This also works fine for the moment.

Code: Select all

```if value1=-1 then condition1=false; If condition1=false then sell 1 contract this bar close; value1=MRO(DaysBeforeEOM(Date)=3,6,1); if value1=1 then condition1=true; If condition1=true then buy 1 contract next bar open; ```
Now the second time span (same as above) comes into play:

Code: Select all

```if value1=-1 then condition1=false; if value2=-1 then condition2=false; If condition1=false or condition2=false then sell 1 contract this bar close; value1=MRO(DaysBeforeEOM(Date)=3,6,1); value2=MRO(DaysFromBOM(Date)=6,3,1); if value1=1 then condition1=true; if value2=1 then condition2=true; If condition1=true or condition2=true then buy 1 contract next bar open;```
Whats the problem:

The dates are still correct but instead of keeping the condition true and buying and selling only once for every time span an order is entered and exited every day. Why is that: I guess the conditon 1 and 2 "cross" each other at same point.
In one of the first buying examples I gave you I was able to avoid that by naming the entry and exit conditions. How could I do something similar for the conditions?

Anna MultiCharts
Posts: 462
Joined: 14 Jul 2017
Has thanked: 31 times
Been thanked: 113 times

### Re: Setting up a Condition correctly

Hello, Jonny473!

You can use BarsSinceEntry and BarsSinceExit keywords and add a condition that won’t allow exiting a position until N number of days is passed.