Question for connoisseurs

 

Hello. Programming experts can help.

Expert Advisor: Buy - crossing of stochastics and the signal at oversold area (below 25), under the condition that

Pivot is below the price level (a kind of support); on the contrary, Pivot is above the price level.

Testing has shown some programming flaws. 1) How to open only one trade per signal (during testing I lost everything, when the signal is still active (false), and the position closing condition has already occurred). 2) whether I have correctly set the condition that Pivot is above / below the price level. I have doubts, looking at the graph of testing.

If i can help with specific examples. I know programming at the level of simple compiling !!!!!!!.

extern double Lots = 0.1;

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double P_up0,P_down0,P_up1,P_down1;
double st_m1,st_s1,st_m2,st_s2,Pivot,ma_s1;
int cnt, ticket, total;

if(Bars<100)
{
Print("bars less than 100");
return(0);
}
P_up0=iCustom(0,0, "Price channel",11,0,0);
P_down0=iCustom(0,0, "Price channel",11,1,0);
P_up1=iCustom(0,0, "Price channel",11,0,1);
P_down1=iCustom(0,0, "Price channel",11,1,1);
st_m1=iStochastic(0,0,13,5,8,MODE_SMA,0,MODE_MAIN,1);
st_s1=iStochastic(0,0,13,5,8,MODE_SMA,0,MODE_SIGNAL,1);
st_m2=iStochastic(0,0,13,5,8,MODE_SMA,0,MODE_MAIN,2);
st_s2=iStochastic(0,0,13,5,8,MODE_SMA,0,MODE_SIGNAL,2);
Pivot=iCustom(0,0,Pivot,0,1);
ma_s1=iMA(0,0,4,0,MODE_SMA,PRICE_CLOSE,1);
//set all data

total=OrdersTotal();
if(total<1)
{
// Check for free margin
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin();
return(0);
}
// condition of BUY position opening
if(st_m2>st_s2&&st_m1>st_s1&&st_m2<25&&Pivot<ma_s1)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,",0,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
//open SELL position condition
if(st_m2<st_s2&&st_m1<st_s1&&st_m2>75&&Pivot>ma_s1)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,",0,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice())
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL &&
OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) // long position opened
{
// condition of long position closing
if(P_down1>P_down0)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
return(0);
}
}
else
{
// condition of short position closing
if(P_up1<P_up0)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
return(0);
}
}
}
}
return(0);
}




Files:
pivot.mq4  2 kb
 
Kostay:

Hello. Programming experts can help.

Expert Advisor: Buy - crossing of stochastics and the signal at oversold area (below 25), under the condition that

Pivot is below the price level (a kind of support); on the contrary, Pivot is above the price level.

Testing has shown some programming flaws. 1) How to open only one trade per signal (during testing I lost everything, when the signal is still active (false), and the position closing condition has already occurred). 2) whether I have correctly set the condition that Pivot is above / below the price level. I have doubts, looking at the graph of testing.

If i can help with specific examples. I know how to do programming on the level of simple compilation!!!!!!!

extern double Lots = 0.1;

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double P_up0,P_down0,P_up1,P_down1;
double st_m1,st_s1,st_m2,st_s2,Pivot,ma_s1;
int cnt, ticket, total;

if(Bars<100)
{
Print("bars less than 100");
return(0);
}
P_up0=iCustom(0,0, "Price channel",11,0,0);
P_down0=iCustom(0,0, "Price channel",11,1,0);
P_up1=iCustom(0,0, "Price channel",11,0,1);
P_down1=iCustom(0,0, "Price channel",11,1,1);
st_m1=iStochastic(0,0,13,5,8,MODE_SMA,0,MODE_MAIN,1);
st_s1=iStochastic(0,0,13,5,8,MODE_SMA,0,MODE_SIGNAL,1);
st_m2=iStochastic(0,0,13,5,8,MODE_SMA,0,MODE_MAIN,2);
st_s2=iStochastic(0,0,13,5,8,MODE_SMA,0,MODE_SIGNAL,2);
Pivot=iCustom(0,0,Pivot,0,1);
ma_s1=iMA(0,0,4,0,MODE_SMA,PRICE_CLOSE,1);
//set all data

total=OrdersTotal();
if(total<1)
{
// Check for free margin
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin();
return(0);
}
// condition of BUY position opening
if(st_m2>st_s2&&st_m1>st_s1&&st_m2<25&&Pivot<ma_s1)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,",0,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
//open SELL position condition
if(st_m2<st_s2&&st_m1<st_s1&&st_m2>75&&Pivot>ma_s1)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,",0,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice())
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL &&
OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) // long position opened
{
// condition of long position closing
if(P_down1>P_down0)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
return(0);
}
}
else
{
// condition of short position closing
if(P_up1<P_up0)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
return(0);
}
}
}
}
return(0);
}

1) how to make only one deal open at one signal

I need it like this:

if(total<1) 
     {
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);  
        }
         

if(st_m2>st_s2&&st_m1>st_s1&&st_m2<25&&Pivot<ma_s1)
{

         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"",0,0,Green);

         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               Alert("Покупка: ",OrderOpenPrice());                      
           }
         else Alert("ошибка:по цене ",OrderOpenPrice()); 
         return(0); 
        }
        
        
     return(0);
     }
 
I lost my entire deposit on the same spot!
 

I still don't understand what I've changed in this code. it seems to be the same!

And still, how can I program it to open only one position per signal?

And if a position was closed on the same bar (i.e., the signal is still active), then a new one should not open.

 
Kostay:

...However, how can I program it to open only one position per signal?

And if a position on the same bar was closed (i.e. the signal is still active), then a new position should not be opened.

If the time of opening of orders, including closed ones, belongs to the current bar (on which the signal has appeared), then we are smoking on the fence.

Approximately so:

bool flag=true;
int cnt=OrdersTotal()-1;
for(int i=cnt;i>=0;i--) {
   if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
   if(OrderOpenTime()>=Time[0]) { // Time[0] - если позиция открывается на нулевом баре текущего символа
      flag=false;
      break;
   }
}
cnt=OrdersHistoryTotal()-1;
for(i=cnt;i>=0;i--) {
   if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY )) continue;
   if(OrderOpenTime()>=Time[0]) { // Time[0] - если позиция открывается на нулевом баре текущего символа
      flag=false;
      break;
   }
}
if(!flag) return; 
 

How can more than 1 position be open at a time if this is present:

total=OrdersTotal();
if(total<1)

?
 

Inserted your code in the EA. but the result is the same.

I must have inserted the frame wrong. please look at it.

extern double Lots = 0.1;
bool flag=true;
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
int start()
{
double P_up0,P_down0,P_up1,P_down1;
double st_m1, st_s1, st_m2, st_s2,Pivot,ma_s1;
int cnt, ticket, total;

if(Bars<100)
{
Print("bars less than 100");
return(0);
}
P_up0=iCustom(0,0, "Price channel",11,0,0);
P_down0=iCustom(0,0, "Price channel",11,1,0);
P_up1=iCustom(0,0, "Price channel",11,0,1);
P_down1=iCustom(0,0, "Price channel",11,1,1);
st_m1=iStochastic(0,0,10,3,3,MODE_SMA,0,MODE_MAIN,1);
st_s1=iStochastic(0,0,10,3,3,MODE_SMA,0,MODE_SIGNAL,1);
st_m2=iStochastic(0,0,10,3,3,MODE_SMA,0,MODE_MAIN,2);
st_s2=iStochastic(0,0,10,3,3,MODE_SMA,0,MODE_SIGNAL,2);
Pivot=iCustom(0,0, "Pivot",0,1);
ma_s1=iMA(0,0,4,0,MODE_SMA,PRICE_CLOSE,1);
//set all data

total=OrdersTotal();
if(total<1)
{
// Check free margin
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
// Condition of BUY position opening
if(st_m2>st_s2&st_m1>st_s1&st_m2<25&&Pivot<ma_s1)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"",0,0,Green);

if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)
Alert("Purchase: ",OrderOpenPrice());
}
else Alert("error:price ",OrderOpenPrice());
return(0);
}
int cn=OrdersTotal()-1;
for(int i=cn;i>=0;i--) {
if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if(OrderOpenTime()>=Time[0]) { // Time[0] - if position opens on the zero bar of the current symbol
flag=false;
break;
}
}
cnt=OrdersHistoryTotal()-1;
for(i=cn;i>=0;i--) {
if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY )) continue;
if(OrderOpenTime()>=Time[0]) { // Time[0] - if the position is opened on the zero bar of the current symbol
flag=false;
break;
}
}
if(!flag) return;
// Condition of SELL position opening
if(st_m2<st_s2&st_m1<st_s1&st_m2>75&&Pivot>ma_s1)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,",0",0,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)
Alert("Purchase: ",OrderOpenPrice());
}
else Alert("error:price ",OrderOpenPrice());
return(0);
}
int c=OrdersTotal()-1;
for(int a=c;a>=0;a--) {
if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if(OrderOpenTime()>=Time[0]) { // Time[0] - if position opens on the zero bar of the current symbol
flag=false;
break;
}
}
cnt=OrdersHistoryTotal()-1;
for(a=c;a>=0;a--) {
if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY )) continue;
if(OrderOpenTime()>=Time[0]) { // Time[0] - if position opens on the zero bar of the current symbol
flag=false;
break;
}
}
if(!flag) return;
return(0);
}
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL &&
OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) // long position opened
{
// condition to close long position
if(P_down1>P_down0)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet)
return(0);
}
}
else
{
// short position close condition
if(P_up1<P_up0)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
return(0);
}
}
}
}
return(0);
}




 
D500_Rised:

How can there be more than 1 position open at the same time if this is present:

total=OrdersTotal();
if(total<1)

?

You misunderstand. no more than 1 position is opened simultaneously!!!!!!! signal appeared - position opened. on the same zero bar price went in the opposite direction and the condition of closing the position is in effect. it is closed, accordingly, on the same zero bar. But the signal action has not been cancelled. the position opens and closes again, as it goes in the opposite direction.

So the question is how to make one signal to open only one position, even if it closes with a new tick!?

 
Then you need to make a ban on intrabar trade go through the search with these keywords, you'll find it.
 
Kostay:

Inserted your code in the EA. but the result is the same.

I must have inserted the code incorrectly.

Please, but next time paste the code through the button

There should not be any errors but I have not tested it.

extern double Lots = 0.1;

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start() {
   double P_up0,P_down0,P_up1,P_down1;
   double st_m1, st_s1, st_m2, st_s2,Pivot,ma_s1;
   int i, cnt, ticket, total;
   bool flag=true;

   if(Bars<100) {
      Print("bars less than 100");
      return(0); 
   }
// Проверяем стоит ли открываться
   
   cnt=OrdersHistoryTotal()-1;
   for(i=cnt;i>=0;i--) {
      if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY )) continue;
      if(OrderOpenTime()>=Time[0]) { // Time[0] - если позиция открывается на нулевом баре текущего символа
         flag=false;
         break;
      }
   }
   cnt=OrdersTotal()-1;
   for(i=cnt;i>=0;i--) {
      if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
      if(OrderOpenTime()>=Time[0]) { // Time[0] - если позиция открывается на нулевом баре текущего символа
         flag=false;
         break;
      }
   }
   if(!flag) return(0); 
//-----------------------------------------------
   P_up0=iCustom(0,0,"Ценовой канал",11,0,0);
   P_down0=iCustom(0,0,"Ценовой канал",11,1,0);
   P_up1=iCustom(0,0,"Ценовой канал",11,0,1);
   P_down1=iCustom(0,0,"Ценовой канал",11,1,1);
   st_m1=iStochastic(0,0,10,3,3,MODE_SMA,0,MODE_MAIN,1);
   st_s1=iStochastic(0,0,10,3,3,MODE_SMA,0,MODE_SIGNAL,1);
   st_m2=iStochastic(0,0,10,3,3,MODE_SMA,0,MODE_MAIN,2);
   st_s2=iStochastic(0,0,10,3,3,MODE_SMA,0,MODE_SIGNAL,2);
   Pivot=iCustom(0,0,"Pivot",0,1);
   ma_s1=iMA(0,0,4,0,MODE_SMA,PRICE_CLOSE,1);
//задали все данные 

// Проверка свободной маржи
   if(AccountFreeMargin()<(1000*Lots)) {
      Print("We have no money. Free Margin = ", AccountFreeMargin());
      return(0); 
   }
      
// Условие открытие позиции BUY
   if(st_m2>st_s2&&st_m1>st_s1&&st_m2<25&&Pivot<ma_s1) {
      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"",0,0,Green);
      if(ticket>0) {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
            Alert("Покупка: ",OrderOpenPrice()); 
      }  else Alert("ошибка:по цене ",OrderOpenPrice()); 
      return(0); 
   }
      
// Условие открытие позиции SELL
   if(st_m2<st_s2&&st_m1<st_s1&&st_m2>75&&Pivot>ma_s1) {
      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"",0,0,Red);
      if(ticket>0) {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
            Alert("Покупка: ",OrderOpenPrice()); 
      }  else Alert("ошибка:по цене ",OrderOpenPrice()); 
      return(0); 
   }
  
   for(i=cnt;i>=0;i--) {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) {
         if(OrderType()==OP_BUY) {// длинная позиция открыта
// условие закрытие длинной позиции
            if(P_down1>P_down0) {
               OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); 
               return(0); 
            }
         }  else {
// условие закрытия короткой позиции
               if(P_up1<P_up0) {
                  OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); 
                  return(0); 
               }
            }
      }
   }
   return(0);
}
 

The strange thing is going on with the Expert Advisor now. it's not working with the position closures. there may be several positions of the same type open and active at the same time. And the result: everything is losing on the same problem, albeit in a modified version!

Reason: