[WARNING CLOSED!] Any newbie question, so as not to clutter up the forum. Professionals, don't go by. Can't go anywhere without you. - page 47

 

Dear forum specialists, who also used to understand almost nothing.

I am trying to write a simple Expert Advisor that uses Parabolic Sar indicator as a training tool. Its action is supposed to be elementary: if price was below the Sar value and then got higher, then we should buy exactly at the moment when the price crosses the indicator and exactly once with setting stop loss and take profit of 10 points. And if it is vice versa, then sell.

The problem is that it always buys and sells, but it does it while there is free money on the deposit, not once. In other words, as soon as the price rises above the level of Sar, he is continuously buying until the price is higher, and as soon as the price is below Sar, he is selling all the time until the price is below.

In fact, the question is: how to make our EA make only one deal when these conditions appear, and not an endless set of them?

Here is the code of my Expert Advisor.

//+------------------------------------------------------------------+
//|                                    Current_sar_value_summary.mq4 |
//|                                         Орлов Денис Владимирович |
//|                                                 orlovkem@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Орлов Денис Владимирович"
#property link      "orlovkem@mail.ru"

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
                     //объявляем переменные
   double _sar_value; //текущий уровень Сар
   double _price;       //текущий уровень цены
   int _ticket;      //возвращает количество открытых позиций
   
   _sar_value=iSAR(0,0,0.02,0.2,0);//приравнял переменную _sar_value к текущему значению индикатора Сар
   _price=Bid;                      //приравнял переменную _price к текущему уровню цены Бид
   
   //описываем условие покупки: если уровень цены был ниже значения сар
   if(_price<_sar_value)
           {
           if(_price>=_sar_value)// а потом стал выше, то покупаем
                {
                   Alert("Надо покупать");
                   _ticket=OrderSend(Symbol(),OP_BUY,1,Ask,1,Bid-10*Point,Ask+10*Point,NULL,0,0,Red);
                     
                          Print("OrderSend failed with error #",GetLastError());
                           return(0);
                 
                }
                 return(0);
           }
 
         
   if(_price>_sar_value)//описываем условие покупки: если уровень цены был выше значения сар
         {
           if(_price<=_sar_value)// а потом стал ниже, то продаем
            {
                   Alert("Надо продавать");
                   _ticket=OrderSend(Symbol(),OP_SELL,1,Bid,1,Ask+10*Point,Bid-10*Point,NULL,0,0,Red);
                     
                          Print("OrderSend failed with error #",GetLastError());
                           return(0);
                   
            }
            return(0);
            
         }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

I want to ask you to help figure it out, I promise that when I become a guru, I will not ignore the requests of people like me.

Regards, Denis Orlov.

 
orlovkem >> :

Dear forum specialists, who also used to understand almost nothing.

I am trying to write a simple Expert Advisor that uses Parabolic Sar indicator as a training tool. Its action is supposed to be elementary: if price was below the Sar value and then got higher, then we should buy exactly at the moment when the price crosses the indicator and exactly once with setting stop loss and take profit of 10 points. And if it is vice versa, then sell.

The problem is that it always buys and sells, but it does it while there is free money on the deposit, not once. In other words, as soon as the price rises above the level of Sar, he is continuously buying until the price is higher, and as soon as the price is below Sar, he is selling all the time until the price is below.

In fact, the question is: how to make our EA make only one deal when these conditions appear, and not an endless set of them?

Here is the code of my Expert Advisor.

I want to ask you to help. I promise that when I become a guru, I will not ignore requests of other dummies like me.

Sincerely, Denis Orlov.

Where do you keep the predictive value of the indicator? (in what variable...?) the same thing with the price value

double _sar_value; //текущий уровень Сар
   double _price;       //текущий уровень цены

 
lascu.roman >> :

and where do you have the indicator's previous value? (in which variable...?) the same with the price value

The point is that the previous indicator value is on the previous candle and the price crosses the indicator value on the current candle.

It's the same with price, what does "previous price value" mean, the price of the previous tick? or the close of the previous candle, but that's not it.

I understand that I have to add something, but what exactly I can't understand. Please help, this is my first experience.

>> Respectfully, Orlov Denis.

 
orlovkem >> :

The point is that the previous indicator value is on the previous candle and the price crosses the indicator value on the current candle.

It's the same with the price, what does "previous price value" mean, the price of the previous tick? or the close of the previous candle, but that's not it.

I understand that I have to add something, but what exactly I can't understand. Help please, this is my first experience.

Sincerely, Orlov Denis.

Did you understand what you said, then? You are checking the value at a given time.

 if(_price<_sar_value)
           {
           if(_price>=_sar_value)// а потом стал выше, то покупаем

Two mutually exclusive conditions. When did he then become taller?

 
Roger >> :

Did you understand what you said, then? You're checking the value at a given time.

Two mutually exclusive conditions. When did it get higher?

 if(_price<_sar_value)
           {
           if(_price>=_sar_value)// а потом стал выше, то покупаем

Maybe I formulated the comment wrong, but the essence is still true, the price level has become higher than the indicator value, you have to buy and it buys, but you have to do it once, and the EA buys as long as there is money on the deposit. You just need to limit the number of deals. Make no more than one at a time, that is what is not working.

 
xruss >> :

but if there was a Sell order in the history and then the Expert Advisor closed it (in the history type, it (its close) is shown as t/p or s/l - will it be considered as the last one?

If an order (buy or sell) was closed last (the Expert Advisor closed it, or it triggered t/p or s/l) and passes under the conditions of

if(OrderSymbol()==Symbol()){
if(OrderMagicNumber()==MagicNumber){

then accordingly it will be considered last. What particular effect can't you achieve?
 
Everlost >> :

If an order (buy or sell) was closed last (Expert Advisor closed it, or t/p or s/l was triggered) and passes the conditions

if(OrderSymbol()==Symbol()){
if(OrderMagicNumber()==MagicNumber){

then, accordingly, it will be considered as the last one. And what exactly is the effect that you cannot achieve?

Yes, it's probably because I excluded OrderMagicNumber() from the code

I want to avoid reopening orders after they have been closed. I want to make it so my orders wouldn't reopen after closing (it sometimes happens that conditions for reopening still exist). The same story with sell. I thought it would be cool to state in my conditions if I had a Buy position and had profit - not to reopen Buy position.)

((

 
xruss писал(а) >>

Yes, that's probably because I excluded OrderMagicNumber() from the code

I want to make it so that orders would not reopen after closing. I thought it would be cool if I specified in the conditions, if I had a Buy position and had a profit - not to reopen Buy again. But it still opens - bastard!)))

((

The easiest way to control the number of orders opened at the same time

if(OrdersTotal()<1){

   .................
   .................
   условия установки ордеров 

}
 
goldtrader >> :

Read the description of OrderSend( ... ).

The magic number (magik) is assigned to an order when it is sent to the server.

This is mainly used to identify pending orders/positions.

I.e., so that the EA does not touch "alien" orders. This is done by an EA or a script.

The Magician cannot be set manually. You set the rules in the EA/Script yourself.

The magician is usually a static one but it can be dynamic if necessary.

Look at some simple examples from Kodobase, and it should become clear to you.

Why cannot you identify "your" orders by ticket number? They are unique no matter how many Expert Advisors are running.

For example, an EA opens an order, remembers the ticket, closes it, and forgets it.

 
mukata >> :

Why can't you identify "your" orders by the ticket number? They are unique, no matter how many Expert Advisors are running.

For example, an EA opens an order, remembers the ticket and forgets about it, after that it closes it.

Chubais will cut off the electricity and then find out where the others are and where they are.