Opening, closing in Expert Advisor generated by MQL5 Wizard - page 2

 
PeretsCHILI:

I have decided on the wording of the problem:

open long: fast MA crosses slow MA from bottom to top, plus time filter

close long: fast МА intersects slow MA from above downwards

Open short: fast МА intersects slow MA from above and plus time filter

close shorts: fast МА intersects slow MA from bottom to top.

In the previously mentioned example CheckOpenLong, CheckCloseLong, CheckOpenShort, CheckCloseShort were used for this respectively.

If you make two modules, one with signals for opening and one for closing, I understand that the first would use LongCondition and ShortCondition, as in the standard module library. What to use in the second module to close?


And most importantly where? What does "... crosses ..." mean? That's the beauty of it :)

 

Man, I'm just more confused)) The article"Create a trading robot in 6 steps!" just presented the first module with signals to open by crossing two MAs.

https://www.mql5.com/ru/articles/367

What do you mean by "crossover"? When the difference between the FastMA-SlowMA on the first bar is greater than zero and that on the second bar is less than zero, the fast МА crosses the slow MA from the bottom to the top. Top-down is when the difference of the FastMA-SlowMA on the first bar is less than zero and more than zero on the second bar.

You said that the issue can be solved by creating two modules with open and close signals. Can I use the module from the article as the base for creating a module with closing signals?

Создай торгового робота за 6 шагов!
Создай торгового робота за 6 шагов!
  • 2012.06.01
  • MetaQuotes Software Corp.
  • www.mql5.com
Вы не знаете, как устроены торговые классы, и пугаетесь слов "Объектно-ориентированное программирование"? На самом деле вовсе не обязательно всё это знать, чтобы написать свой собственный модуль торговых сигналов - достаточно следовать простым правилам. Всё остальное сделает Мастер MQL5, и вы получите готовый торговый робот!
 
PeretsCHILI:

...

What does 'crosses' mean? A fast MA crosses a slow MA from bottom to top is when the difference between the FastMA-SlowMA on the first bar is greater than zero and less than zero on the second bar. Top-down is when the difference of the FastMA-SlowMA values is less than zero on the first bar and greater than zero on the second bar.

...


That's good. When the task is specified it is very good.


PeretsCHILI:

...

You have said that the issue can be solved by creating two modules with open and close signals. Can the module with signals to close be used as the basis for the module from the above article?

I did not mean it correctly. The trading signals module produces signals "It's time to OPEN BUY" and "It's time to OPEN SELL". That is they give a signal for OPEN. And the rest is up to CExpert to decide: to close and reverse an existing position or open a position.



Take the module from the articleCreate a trading robot in 6 steps!- What do you have a problem with it?

 

So, I want to write a module with opening signals by crossing two MAs with a time filter, and closing occurred in the following cases: arrival of the opposite signal; coming of SL; coming of TP. You can write a module (let's call it Cross2MA) as per the article and generate an Expert Advisor with Cross2MA and SignalITF modules, but this will not work, because the SignalITF filter will also be checked when closing.

For example: We have 2MA and SignalITF has a trade on Mondays only. A Buy signal is received on Monday; we open a Buy position and when the time expires on Tuesday, when neither Sl nor TP are reached, a reverse cross of 2MA occurs and the Buy position should be closed but it is not closed because it is already another day of the week.

How it should be: same conditions, 2MA, trade on Mondays: Monday, there is a Buy signal, we open a Buy position, on Tuesday we get a Sell signal, but because it is Tuesday we do not open a Sell position, we only close a Buy position.

It appears that there are two commands in LongCondition: opening Buy and closing Sell. We need to separate them somehow)

 

"Flies separate, cutlets separate".


PeretsCHILI:

So, I want to write a module with signals to open by crossing two MAs with a time filter, and to close in the following cases: arrival of the opposite signal; arrival of SL; arrival of TP. ...

Trading signals module may only display two types of signals: "It is time to open BUY" and "It is time to open SELL".

We will not control stop loss and take profit - since if a stop loss or take profit position is closed, this only means one thing: the position is closed, and it is time to see what the trading signals module generates (in other words, when the number of positions is zero, we start a circle: waiting for a signal).

Further: this is a misinterpretation of the modules' operation, since there is no "opposite signal" - it can be as follows:

  1. we have an open BUY position and we get a"PAY TO open SELL" signal
  2. or have an open SELL position and"PAY to BUY" signal appears
In both cases the current position will be closed and a new one opened.


 
Andy:
LongCondition can just open a buy and hold it regardless of any conditions, and have it close at stop profit or manually?

LongCondition class CExpertSignal cannot in principle "hold" anything. CExpertSignal only gives two signals:"Time to OPEN BUY" and "Time to OPEN SELL".


Do you want your Expert Advisor built by the wizard to never close? Then change the input parameter of the Expert Advisor "// Signal threshold value to close [0...100]" - set the bar of scales to close at "100".

 
Andy:
What closes them then?

CExpert class object - it polls all signal modules, evaluates their weights and sums up the resulting score: signal weight




 

I understand about the interpretation of the modules. All this dancing is because I will optimize my Expert Advisor as follows: 1. Optimization of two MA (period, shift, smoothing), stops and takeaways by zero; 2. 3. Trailing optimization.

The problem is this: if I generate an Expert Advisor with Cross2MA and SignalITF filter, it will work as follows (same conditions, zero stops and takeaways)

Monday, FastMA crosses SlowMA from bottom to top, opens Buy position, position is held until the next Monday, on the next Monday there could be 3 variants:

1. FastMA crosses SlowMA from top to bottom - current position is closed, Sell position is opened.

2. No crossovers are observed - position is kept.

3. FastMA intersects SlowMA from the bottom upwards - position is kept.

And so on, until the next Monday. Many things may happen between Mondays, 2 MAs may be crossed several times and the price will go down.

Can we implement the following (same conditions): On Tuesday, the FastMA crosses the SlowMA from the top downwards, and the Sell position is not opened because it is Tuesday and not Monday, but the current position is closed.

 

I think I've figured out how to do this by applying threshold.

Let Cross2MA return 80 and SignalITF return 40.

Threshold to open we put 50, threshold to close we put 30.

If both modules provide a signal, arithmetic average will return 60 that is higher than the threshold of 50, then the position is opened.

If only Cross2MA triggers, the arithmetic mean is 40, greater than 30, the current position is closed; if it is less than 50, a new position is not opened.

Right?

Except SignalITF returns empty_value.

 
PeretsCHILI:

I think I've figured out how to do this by applying threshold.

Let Cross2MA return 80 and SignalITF return 40.

Threshold to open we put 50, threshold to close we put 30.

If both modules provide a signal, arithmetic average will return 60, if it is higher than threshold 50, then the position will be opened.

If only Cross2MA triggers, the arithmetic mean is 40, greater than 30, the current position is closed; if it is less than 50, a new position is not opened.

Right?

Except SignalITF returns empty_value.


You are going in the right direction :) All controls for opening and closing thresholds are through the input parameters of the Expert Advisor generated by the MQL5 Wizard.