Download MetaTrader 5

Problem with a little part of my Holy Grail.

To add comments, please log in or register
How to test a program before buying?
robertj
6
robertj 2015.11.09 22:26 

Hi everyone! First, sorry for my english. My expert advisor don't make transactions in the way that I want. I've tried many things, but no one was working.

My ea should make transactions when actual price is higher/lower than the highest/lowest price from last x bars. The next transaction should be in the distance of x pips, when first condition is still valid.  Please for help.

  

//+------------------------------------------------------------------+
//|                                                R.A.J. spread.mq4 |
//|                                           Copyright 2015, R.A.J. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, R.A.J."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern double step_of_pips=2;
extern double TakeProfit=900;
double Change_LotSize;
extern double LotSize=0.1;
double bid_price;
double pips;
double wartosc;
extern int lastBar=4; 
extern int firstBar=1;
extern int StopBars=2;
datetime now;
extern double nad=4;



int OnInit()
  {
   double ticksize=MarketInfo(Symbol(),MODE_TICKSIZE);
   
   if(ticksize==0.00001 || ticksize==0.001)// Jeżeli format ceny ma 5 cyfr po przecinku lub tylko 3 to 
      pips = ticksize*10;                  // wartość pips zdefiniowana w zmiennych globalnych mnoży się razy 10 aby pips miała wartość rzeczywistego pipsa,
   else pips= ticksize;                    // w innym wypadku pips ma wartość 1, PS dla EUR/DOl u tego brokera warość pips mnoży się przez 10
        
   bid_price=Bid;
 
   return(INIT_SUCCEEDED);
  }

void OnTick()
  { 



{
   if( now != Time[0] )
   {
      now = Time[0];
   //...once per bar code goes here...
 
                                            
 
//------------------------------------------------------------------------------------
                        // Trailing distance
//------------------------------------------------------------------------------- 1 --
                                   // Special function 'start'
  {
   string Symb=Symbol();   // Symbol
//------------------------------------------------------------------------------- 2 --
   for(int i=1; i<=OrdersTotal(); i++)          // Cycle searching in orders
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
        {                                       // Analysis of orders:
         int Tip=OrderType();                   // Order type
         if(OrderSymbol()!=Symb||Tip>1)continue;// The order is not "ours"
         double SL=OrderStopLoss();             // SL of the selected order
         //---------------------------------------------------------------------- 3 --
         while(true)                            // Modification cycle
         {
            //------------------------------------------------------------------- 4 --
            bool Modify=false;                  // Not to be modified
            switch(Tip)                         // By order type
              {
               case 0 :                         // Order Buy
                 
                    {
                     SL=Low[iLowest(NULL,0,MODE_LOW,StopBars,1)]-Point;           // then modify it
                     string Text="Buy ";        // Text for Buy 
                     Modify=true;               // To be modified
                    }
                  break;                        // Exit 'switch'
               case 1 :                         // Order Sell
                 
                    {
                     SL=High[iHighest(NULL,0,MODE_HIGH,StopBars,1)]+Point;           // then modify it
                                 // Text for Sell 
                     Modify=true;               // To be modified
                    }
              }                                 // End of 'switch'
            if (Modify==false)                  // If it is not modified
               break;                           // Exit 'while'
            //------------------------------------------------------------------- 5 --
            double TP    =OrderTakeProfit();    // TP of the selected order
            double Price =OrderOpenPrice();     // Price of the selected order
            int    Ticket=OrderTicket();        // Ticket of the selected order
 
            Alert ("Modification ",Ticket,". Awaiting response..");
            bool Ans=OrderModify(Ticket,Price,SL,TP,0);//Modify it!
            //------------------------------------------------------------------- 6 --
            if (Ans==true)                      // Got it! :)
              {
               Alert ("Order ",Ticket," is modified:)");
               break;                           // From modification cycle.
              }
            //------------------------------------------------------------------- 7 --
            int Error=GetLastError();           // Failed :(
            switch(Error)                       // Overcomable errors
              {
                                 // At the next iteration
               case 136:Alert("No prices. Waiting for a new tick..");
                  while(RefreshRates()==false)  // To the new tick
                     Sleep(1);                  // Cycle delay
                  continue;                     // At the next iteration
               case 146:Alert("Trading subsystem is busy. Retrying ");
                  Sleep(500);                   // Simple solution
                  RefreshRates();               // Update data
                  continue;                     // At the next iteration
                  // Critical errors
               case 2 : Alert("Common error.");
                  break;                        // Exit 'switch'
               case 5 : Alert("Old version of the client terminal.");
                  break;                        // Exit 'switch'
               case 64: Alert("Account is blocked.");
                  break;                        // Exit 'switch'
               case 133:Alert("Trading is prohibited");
                  break;                        // Exit 'switch'
               default: Alert("Occurred error ",Error);//Other errors
              }
            break;                              // From modification cycle
           } 
           }                                   // End of modification cycle
         //---------------------------------------------------------------------- 8 --
        }                                       // End of order analysis
     }                                          // End of order search
//------------------------------------------------------------------------------- 9 --
                                     // Exit start()
    
  
  }
//...once per tick code goes here...
     {
//Warunek Kup 
     if (Ask+(nad*pips)>NewHigh && Ask>bid_price+step_of_pips*pips) //jezeli cena ask jest wieksza niż cena bid + step of pips(10) * 0.0001                                    
        buy();
     }
     
     {
//Warunek Sprzedaj   
     if (Bid-(nad*pips)<NewLow && Bid<bid_price-step_of_pips*pips)//jeżeli cena bid jest mniejsza niż cena bid - step of pips(10) * 0.0001                                  
         sell();
     }    
}
}
double NewHigh=High[iHighest(NULL,0,MODE_HIGH,lastBar,firstBar)];
double NewLow=Low[iLowest(NULL,0,MODE_LOW,lastBar,firstBar)];


void sell()
 
       {
       OrderSend(Symbol(),OP_SELL,LotSize+Change_LotSize,Bid,1,High[iHighest(NULL,0,MODE_HIGH,2,0)]+Point,Bid-(TakeProfit*pips),NULL,0,0,Red);
         bid_price=bid_price-step_of_pips*pips;
       }
 

 void buy()
      { 
        OrderSend(Symbol(),OP_BUY,LotSize+Change_LotSize,Ask,1,Low[iLowest(NULL,0,MODE_LOW,2,0)]-Point,Ask+(TakeProfit*pips),NULL,0,0,Green);
          bid_price=bid_price+step_of_pips*pips;
      }    
      

 example from strategy testerfrom strategy tester

To add comments, please log in or register