Multiple entry criteria with ‘OR’


Category:
0
0

I’ve had some good luck poking around in here but am having trouble adapting my strategy code.

Some background of a similar dummy strategy:

Entry Conditions (if ANY of the following is valid):

Daily 50SMA crossed above 200SMA OR Daily 50SMA > Daily 200MA.

Exit Conditions (if ANY of the following is valid):

Exit After 16 Daily Candles passed.

Basically, I want to get long after the cross and pretty much stay long as long as the shorter MA > longer MA.

Existing Script:

input price = close;

input length1 = 50;

input length2 = 200;

input averageType = AverageType.SIMPLE;

def Avg50 = MovingAverage(averageType, price, length1);

def Avg200 = MovingAverage(averageType, price, length2);

def crossabove = Avg50 crosses above Avg200;

def greaterthan = Avg50 is greater than Avg200;

def LongEntry = crossabove or greaterthan;

input barexit = 16;

def LongExt= LongEntry from barexit bars ago;

AddOrder(OrderType.BUY_TO_OPEN, LongEntry,close[0], tickcolor = GetColor(2), arrowcolor = GetColor(2), name = “ENTRY”);

AddOrder(OrderType.SELL_TO_CLOSE, LongExt,close[0], tickcolor = GetColor(1), arrowcolor = GetColor(1), name = “EXIT”);

When I take out the ‘greaterthan’ variable in the logical ‘OR’ argument where ‘LongEntry’ is defined, this strategy seems to paint entries/exits correctly on the chart.  So what is happening to this script here when that gets introduced?

I have more I want to add to this strategy (stop loss, take profit, etc.), but can’t seem to get the basic function of it working first.  

 

Marked as spam
Posted by (Questions: 1, Answers: 0)
Asked on January 28, 2020 12:24 pm
92 views
0
Private answer

Let's focus on your entry conditions. Which I believe is faulty.

def crossabove = Avg50 crosses above Avg200;
def greaterthan = Avg50 is greater than Avg200;
def LongEntry = crossabove or greaterthan;

Notice that whenever the "crossAbove" statement is true, this means that the "greaterThan" statement is also true. The only time the "greaterThan" statement comes into play is when you have an exit while the fast moving average is above the slow moving average.

Let's do a bit of a thought experiment. Say you have a section of the chart in which the fast moving average crosses above the slow moving average and stays above the slow moving average for a period of 20 bars. On the first bar where the fast crosses above the slow, both of your entry rules are satisfied. The strategy posts a theoretical buy order and holds that until 16 bars have passed since either entry rule has been true. So you exit on bar 16, and since your "greaterThan" rule is still true the strategy immediately posts a new theoretical buy order on the following bar. But then your exit rule is still true because the "greaterThan" rule has been true for more than 16 bars, so the strategy immediately posts a sell order. This behavior continues until the fast moving average crosses back below the slow moving average.

So get rid of the "greaterThan" statement. But you already knew this. So I'm not sure what you are really asking for here.

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on January 28, 2020 2:50 pm