EMA Strategy Help

7 replies

Daviden1

Subscriber, bbp_participant, community, 14 replies.

Visit profile

11 years ago #110930

Hi there guys,

I am working on a simple EMA strategy where the trade start as soon as the Fast Ema crosses the Low EMA.

I have already set the S/L and Trailing Stop, but I would like to create an additional S/L that should works in the following case:

Long Trade: If the candle close below the Slow Ma
Short Trade: if the candle close above the Slow Ma

I have input the following code in the wizard to create this S/L but it looks ike I did something wrong with it, because I did not get what I hoped for:

If:

OrderPosition(Magic Number) is Long
and: isBaropen is True
and close[1] <SlowEma

Then:

Close Position (Magic Number)

What exactly did I do wrong and how can I make this S/L work?

Thank you in advance for your help and Merry Christmas

David

0

stearno

Customer, bbp_participant, community, 379 replies.

Visit profile

11 years ago #121067

The only thing I can think could be the problem is you have 2 rules competing against each other to put a stop loss at a price when conditions are met and a trailing stop loss from the main rule. So in your main rule, it says the stop loss should be moved to 1.5028 per the trailing stop loss and then another rule where the Close is < SlowEMA says the stop loss should be at 1.5018.

I would suggest trying to remove the trailing stop loss from the main rule, create an additional rule that puts the trailing stop loss however you want and then this other trailing stop loss if close is < slowEma. So it would be like:

[u][b]Main Entry Rule[/b][/u]
* Remove Trailing Stop. Have it set the profit target and original stop loss

[u][b]Trailing Stop 1[/b][/u]
* Put conditions of how you want the trailing stop to be managed. What it appears is it might look like:
IF
[indent=1][color=#282828][font=helvetica, arial, sans-serif]OrderPosition(Magic Number) is Long[/font][/color]
[color=#282828][font=helvetica, arial, sans-serif]and: isBaropen is True[/font][/color]
[color=#282828][font=helvetica, arial, sans-serif]and close[1] > SlowEma[/font][/color][/indent]
Then
[indent=1]Enter your trailing stop loss[/indent]

[u][b]Trailing Stop 2[/b][/u]
IF
[indent=1][color=#282828][font=helvetica, arial, sans-serif]OrderPosition(Magic Number) is Long[/font][/color]
[color=#282828][font=helvetica, arial, sans-serif]and: isBaropen is True[/font][/color]
[color=#282828][font=helvetica, arial, sans-serif]and close[1] < SlowEma[/font][/color][/indent]
Then
[indent=1]Enter your trailing stop loss[/indent]

Now I would think you will not have competing trailing stop loss and one rule will control the trailing stop at a time depending upon iwhich conditions are met. You could try this and see if it fixes the problem.

0

Daviden1

Subscriber, bbp_participant, community, 14 replies.

Visit profile

11 years ago #121069

Thanks Stearno,

The fact is that I don’t have the trailing stop in my main entry rules, in fact, in my entry rules I have only the stop loss.
Then, I have added an additional rule, as you suggested, with new rules for Trailing stop and then, now, I am trying to create this new Stop Loss that should works when the close crosses or it is below the slowMA.

If the code I am using is not wrong, probably, as you said, some other rule compete with this new rule for the S/L.

Is there any code that I can add to those above to avoid the conflict of rules.

Thanks again

0

stearno

Customer, bbp_participant, community, 379 replies.

Visit profile

11 years ago #121070

Okay, good. So you now have 2 rules that are doing trailing stops. Let me name them to make this easier.

What I have gotten from you so far is that TS Rule 2 is:
[color=#282828][font=helvetica, arial, sans-serif]IF[/font][/color]

[indent=1]OrderPosition(Magic Number) is Long
and: isBaropen is True
and close[1] > SlowEma[/indent]
[color=#282828][font=helvetica, arial, sans-serif]Then[/font][/color]

[indent=1][color=#282828][font=helvetica, arial, sans-serif]Enter your trailing stop loss[/font][/color][/indent]

[color=#282828][font=helvetica, arial, sans-serif][color=#282828][font=helvetica, arial, sans-serif]TS Rule 1[/font][/color]
[color=#282828][font=helvetica, arial, sans-serif]? – I don’t know the statements used in this one.[/font][/color]

[b][color=#282828][font=helvetica, arial, sans-serif]2 thoughts:[/font][/color][/b]
[color=#282828][font=helvetica, arial, sans-serif]1. Need to make the THEN statement for these rules actually “Move SL to” instead of doing a Trailing stop as seen in the attached. [/font][/color][/font][/color]You are actually just changing the stop loss each bar when the listed conditions are met instead of using the field called Trailing Stop

[font=”helvetica, arial, sans-serif”][color=”#282828″]2. TS Rule 1 – needs to have a condition that separates it from TS Rule 2. So if TS Rule 2 says “close[1] > SlowEma”, then I would suggest TS Rule 1 to say [/color][/font][color=#282828][font=helvetica, arial, sans-serif]”close[1] < SlowEma". That way only one rule will apply at any one time. [/font][/color]

[color=#282828][font=helvetica, arial, sans-serif]Does either of these fix the problem? If not, post the SQW file and we can look at the actual code.[/font][/color]

[color=#282828][font=helvetica, arial, sans-serif]-Stearno[/font][/color]

0

Daviden1

Subscriber, bbp_participant, community, 14 replies.

Visit profile

11 years ago #121074

Thanks Stearno, but I think I have not been too clear in explain you my program.

I have the following rules:

1) Main entry rules with just the S/L in the “THEN section”.

2) Dynamic TS rule that it is based on a custom indicator with “move S/L to” in the “THEN section”, which is:

If:
Market positon is Long
and: Order S/L (MagicLong) < icustom…………..

Then:

Move S/L to:

Magic number: magicLong
Price: icustom……………..

Obviously I got a TS with opposite rules for the Short trades.

3) Now I want to create a S/L (both for Long and Short trades) which will immediately close the trade if the close of the bar crosses the SlowMA down for Long trades and Up for Short trades

I thought something like that, but it does not work:

For Long Trades:

If:

MarketPosition is Long
and: isBarOpen is True
and: OrderS/L(MagicLong) <-20
and: Close[1] < SlowMA

Then:

ClosePosition

MagicNumber: MagicLong

The same with opposite rule, for the Short Trades.

Please, let me know what you think, if you don’t find anything wrong I will attach the .mq4

Thank you again

0

Daviden1

Subscriber, bbp_participant, community, 14 replies.

Visit profile

11 years ago #121075

Sorry Stearno,

In the new rules to close the trade, I made a mistake, the following is the right rule:

For Long Trades:

If:

MarketPosition is Long
and: isBarOpen is True
and: OpenS/L(MagicLong) <-20 (not OrderS/L like above)
and: Close[1] < SlowMA

Anyway, I am attaching the .sqw on it, so you can see for yourself.

Thanks

0

stearno

Customer, bbp_participant, community, 379 replies.

Visit profile

11 years ago #121077

One thing I noticed is that the EA references S/R (Barry) v1 and v2 both. Is this a mistake?

File: v1.jpgv1.jpg

Second thing is, good news, your rules are not conflicting with each other as I originally discussed. You have one rule moving the stop loss and then an emergency stop loss of closing the order if -20 (Mark will have to verify if this is right format and use of this as I have never used -20; if it tests okay, then it is okay)

On another note, one thing I noticed is that Barry S/R moves. So if using that as the Trailing Stop Loss, then the stop loss will continually move, even down. If this is what you want, then it is okay.

For example,

right now S/R Barry says to put the level at 1.60778. So you EA puts the stop x pips below that level. Then the price went down (but not far enough to hit your stop loss becaue you put it x pips below the support), Now S/R Barry says to put a support line 1.60749. So now the EA will move the stop loss x pips below that level. This continues until price either moves big enough to hit your stop loss or it hits your emergency of -20 loss.

An example of this is here:

As you can see, as long as there is not a sharp price swing, the stop loss will just continue to move down until a sharp swing to hit your SL or until the P/L of the order is < -20. Okay, so to your question, why is the SL not working as seen here:

I believe Mark said in another post that you have to use “Is Bar Open is True” on every rule. It is a trigger that starts the rule each time a bar is open. So if this is correct, then that would explain why the SL is not being moved per your rule. I can see what you are trying to do, which is have the EA calculate on each Tick instead of Bar Open. But I do not know how to do this in EA Wizard, and I don’t see it as an option in the software like it was an option in EA Generator. That would lead me to assume it is not available right now and have to use Bar Open statement in each rule. I don’t have the SR Barry v1 or v2 indicators, so I cannot test that this would fix the EA to work. So try that and see what results you get.

-Stearno

0

Daviden1

Subscriber, bbp_participant, community, 14 replies.

Visit profile

11 years ago #121078

Hi there Stearno,

first of all, thank you very much to take the time to test my EA… I have really appreciated that.

I will answer your questions in order:

1) The V1 output in the “IF” codition and the V2 output in the “THEN” condition, I don’t know if it is a mistake, because Mark helped me with this code… but the strategy works as I wanted, therefore, I don’t really think it is a mistake; I will ask Mark for that.

2) As for the Open P/L < – 20, this is a formula which Mark wrote in the tutorial in the Using Strategy Controls section.

3) Yes, that is what I want. The S/R (Barry) follows the market and draws support and resistant levels as soon as the condition are met, and it is great as TS…. at list for my strategy.
I don’t have Take Profit for my strategy, I just leave the S/L following the market till the TS is hit.

4) The meaning of the < -20 actually was not to close the trade if my S/L hit 20 pips or more. What I tried to do with this code is to make TRUE the other conditions in the Emergency S/L only if my Open Loss is under the 20 pips.

This, that follows, is exactly what I am trying to say to the EA with the Emergency Stop:

When a new bar opens, check if the close of the previous bar is below the Slow MA ( in a Long trade) or above the Slow MA (in a Short Trade).
If that is TRUE and my current Loss is below 20 pips (this is the reason why I put the Open P/L < – 20) THEN close the trade.

Correct me if I am wrong… Basically you are saying that this emergency S/L is not working because I should input the isBarOpen code even in my MAIN ENTRY rules?

Thanks again

0

Viewing 7 replies - 1 through 7 (of 7 total)