Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 756

 

Good afternoon.

I've written an exercise programme. It's not working for some reason.

I tested it in the strategy tester between September 29 and October 15.

The program should work like this: I make a buy. When the price falls to 0.010 it should sell with a loss.

A buy occurs. There is no sale. I start visualization and see that the price is not just 0.010 lower but much higher. Strange.

// -------- variables --- Start -----
extern double L1=0.01; // lot size for buying
extern double L1n=0.01; // lot size for buying (normalized lot)
extern double AktualAsk=0.0; // current bid price
extern double AktualBid=0.0; // actual selling price
extern int int ticketlackmus=1; // indicator of successful order opening.
extern int int ticket1=0; // ticket
extern bool PozOtk1=true;
// -------- variables --- End -----
int init()
{
return(0);
}


int start()
{
if (PozOtk1==true)
{
//open position
ticketlackmus=1;
while (ticketlackmus>0)
{
Alert (" Open first position ");
RefreshRates();// Refresh data
AktualAsk=NormalizeDouble(Ask,Digits); //for price
L1n=NormalizeDouble(L1,2); //for the lot.
ticket1=OrderSend ("GBPUSD", OP_BUY, L1n, AktualAsk, 5, 0, 0, 0, 0, CLR_NONE);
if (ticket1>0) ticketlackmus=-1;
{
Alert (" ORDER OPEN ", ticket1, ", ticketlackmus);
}
}
PozOtk1=false;
Alert (" PozOtk1 ", PozOtk1);
}

RefreshRates();// RefreshRates()
AktualBid=NormalizeDouble(Bid,Digits); //for price
if (AktualBid<=AktualAsk-0.010 && PozOtk1==false)
{
RefreshRates(); // Refresh data
OrderSelect(ticket1, SELECT_BY_TICKET;)
L1n=NormalizeDouble(L1,2); //for the lot
OrderClose (ticket1, L1n, AktualBid, 5);
PozOtk1=true;
}

return(0);
}

int deinit()
{
return(0);
}


Ozero.

 

Please use the magic SRC button when inserting code:

Insert code correctly in the forum

 
// -------- переменные --- Н -----
extern double L1=0.01;            // размер лота для покупки 
extern double L1n=0.01;           // размер лота для покупки 
extern double AktualAsk=0.0;
extern double AktualBid=0.0;
extern int ticketlackmus=1;       // "лакмусовая бумажка" успешности откарытия ордера
extern int ticket1=0;             // тикет 
extern bool PozOtk1=true;
// -------- переменные --- К ----- 
int init() 
   {
      return(0);
   }


int start()    
   {
      if (PozOtk1==true) 
         {     
// Открытие позиции
             ticketlackmus=1;
             while (ticketlackmus>0)
                {
     Alert (" Открытие первой позиции ");
                   RefreshRates();                                        // Обновление данных 
                   AktualAsk=NormalizeDouble(Ask,Digits); //для цены
                   L1n=NormalizeDouble(L1,2); //для лота.
                   ticket1=OrderSend ("GBPUSD", OP_BUY, L1n, AktualAsk, 5, 0, 0, 0, 0, CLR_NONE);    
                   if (ticket1>0) ticketlackmus=-1;
                   {
                   Alert (" ОРДЕР ОТКРЫТ ", ticket1, "  ", ticketlackmus);
                   }
                }
             PozOtk1=false; 
             Alert (" PozOtk1 ", PozOtk1);
          }

      RefreshRates();                                        // Обновление данных 
      AktualBid=NormalizeDouble(Bid,Digits); //для цены
           if (AktualBid<=AktualAsk-0.010 && PozOtk1==false)
       {                              
                     RefreshRates();    // Обновление данных 
                     OrderSelect(ticket1, SELECT_BY_TICKET);   
                     L1n=NormalizeDouble(L1,2); //для лота
                     OrderClose (ticket1, L1n, AktualBid, 5);
                     PozOtk1=true; 
       }

return(0);
   }



int deinit()                                   
   {
      return(0);
   } 

 

If a position is opened at a certain symbol (in this case - "GBPUSD"), then it is advisable to take all data on this symbol through marketinfo, i.e. not just AktualAsk=NormalizeDouble(Ask,Digits ); aktualAsk=MarketInfo( "GBPUSD",MODE_ASK);

And so any market data should be taken, in order to avoid.

 
// -------- переменные --- Н -----
extern double L1=0.01;            // размер лота для покупки 
extern double L1n=0.01;           // размер лота для покупки 
extern double AktualAsk=0.0;
extern double AktualBid=0.0;
extern int ticketlackmus=1;       // "лакмусовая бумажка" успешности откарытия ордера
extern int ticket1=0;             // тикет 
extern bool PozOtk1=true;
// -------- переменные --- К ----- 
int init() 
   {
      return(0);
   }


int start()    
   {
      if (PozOtk1==true) 
         {     
// Открытие позиции
             ticketlackmus=1;
             while (ticketlackmus>0)
                {
     Alert (" Открытие первой позиции ");
                   RefreshRates();                                        // Обновление данных 
                   //AktualAsk=NormalizeDouble(Ask,Digits); //для цены
                   AktualAsk=MarketInfo( "GBPUSD",MODE_ASK);
                   L1n=NormalizeDouble(L1,2); //для лота.
                   ticket1=OrderSend ("GBPUSD", OP_BUY, L1n, AktualAsk, 5, 0, 0, 0, 0, CLR_NONE);    
                   if (ticket1>0) ticketlackmus=-1;
                   {
                   Alert (" ОРДЕР ОТКРЫТ ", ticket1, "  ", ticketlackmus);
                   }
                }
             PozOtk1=false; 
             Alert (" PozOtk1 ", PozOtk1);
          }

      RefreshRates();                                        // Обновление данных 
      AktualBid=NormalizeDouble(Bid,Digits); //для цены
           if (AktualBid>=AktualAsk+0.010 && PozOtk1==false)
       {                              
                     RefreshRates();    // Обновление данных 
                     OrderSelect(ticket1, SELECT_BY_TICKET);   
                     L1n=NormalizeDouble(L1,2); //для лота
                     OrderClose (ticket1, L1n, AktualBid, 5);
                     PozOtk1=true; 
       }

return(0);
   }



int deinit()                                   
   {
      return(0);
   } 
evillive:

If a position opens on a specific symbol (in this case - "GBPUSD"), then it is advisable to take all data also on this symbol via marketinfo, i.e. not just AktualAsk=NormalizeDouble(Ask,Digits); aktualAsk=MarketInfo( "GBPUSD",MODE_ASK);

And so any market data should be taken, to avoid.

Thank you Evillive!

I have made changes. But I have already started buying. No sale after the changes. M15 test from September 30, 2014 to mid-October.



 
Ozero:

Thanks, Evillive!

Made the change. But, the purchase was going on anyway. After the changes, no sale takes place. M15 test from 30 September 2014 to mid October.



Find where in the above code there might be a sale
 
OrderClose (ticket1, L1n, AktualBid, 5);
 
Ozero:
This is the close of the order. Where is the opening of the sell order. It is not in the above code
 
I meant the order doesn't close :( Sorry if I didn't put it that way, I'm a newbie.
 
Ozero:
I meant the order doesn't close :( Sorry if I didn't put it that way, I'm still a beginner.

It's working quite well.

//+------------------------------------------------------------------+
//|                                                        Ozero.mq4 |
//|                                            Copyright 2014, Vinin |
//|                                             http://vinin.ucoz.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, Vinin"
#property link      "http://vinin.ucoz.ru"
#property version   "1.00"
#property strict
// -------- переменные --- Н -----
extern double gLot=0.1;            // размер лота для покупки 
extern double gPoint=0.001;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
// -------- переменные --- К ----- 
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {

// Открытие позиции
   if(OrdersTotal()==0)
     {
      Print(" Открытие первой позиции ");
      double _Ask=MarketInfo("GBPUSD",MODE_ASK);
      int ticket=OrderSend("GBPUSD",OP_BUY,gLot,_Ask,15,0,0);
      if(ticket>0)
        {
         Print(" ОРДЕР ОТКРЫТ ",ticket);
        }
     }

   double _Bid=MarketInfo("GBPUSD",MODE_BID); //для цены
   for(int pos=OrdersTotal()-1;pos>=0;pos--)
     {
      if(OrderSelect(pos,SELECT_BY_POS))
        {
         if(OrderOpenPrice()+gPoint<_Bid)
           {
            OrderClose(OrderTicket(),OrderLots(),_Bid,15);
           }
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+
Files:
ozero.mq4  2 kb