Help in exiting with ATR readings

 

Hello to all,

I have a working EA. Not saying it is a profitable EA. It is for experimenting or for a better understanding to this MQL4.

Let's say a sell position is opened at .8555 AUDUSD. 12/15/2009 and that a 20 ATR was calculated day before at .0034. I want to set a stop for 2 times the ATR of the day before plus 1 pip. Which would be .0068 + .0001 + .8555 = .8624 stop.

The following day i would want the EA to recalculate the stop. Not to raise the price of the stop of course, and not to be just a trailing stop either. The EA to recalculate only at the end of each bar.

Hope this is clear enough. If not please contact for further understanding.

Thanks to all who reads this. and hope that this may help others.

Huckleberry

 
Huckleberry wrote >>

Hello to all,

I have a working EA. Not saying it is a profitable EA. It is for experimenting or for a better understanding to this MQL4.

Let's say a sell position is opened at .8555 AUDUSD. 12/15/2009 and that a 20 ATR was calculated day before at .0034. I want to set a stop for 2 times the ATR of the day before plus 1 pip. Which would be .0068 + .0001 + .8555 = .8624 stop.

The following day i would want the EA to recalculate the stop. Not to raise the price of the stop of course, and not to be just a trailing stop either. The EA to recalculate only at the end of each bar.

Hope this is clear enough. If not please contact for further understanding.

Thanks to all who reads this. and hope that this may help others.

Huckleberry

Hi,

somehow like this is working. for ATR you need to calculate your formula (2*Atr+1 pip ...). Also Change High[1] to what you need .. Close[1] or else

if (OrderStopLoss()==0||OrderStopLoss()>High[1]+ATR)
                  {
                  OrderModify(OrderTicket(),OrderOpenPrice(),High[1]+ATR,OrderTakeProfit(),0);
                  Print ("Trail Sell by ATR");
                  continue;
         }
 

Thank you EADeveloper,

Most of my day has been researching the correct way to declare the variables. 'pip', ATR, and understanding how to or why the Tester asks for a comma or semicolon at the stoploss declaration.

I will insert a portion of the code below, so as to give a better explaination.

//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
extern double ATR=20; //<<<<<<<<<<By declaring the ATR=20, this would explain
//to the program that I wish the ATR to be calculated by a 20 bar, as oppossed
//to ten or 14 bars.??
double pip=0.0001; //pip -expression on global scope not allowed
extern double StopLoss = 2*ATR+1 pip; //<<< Error, '*' -comma or semicolon expected, Also, the Tester does not like the way I declared the ATR. ATR

expression on global scope not allowed.
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

I thank you for taking the time to read this much. If you could explain the declarations, I would do further reading to further the coding.

Regards,

Huckleberry

 
Also, don't just paste in the code, use the SRC button.
extern double StopLoss = 2*ATR+1 pip;

1<space>pip is not valid code. Use extern only for read only constants to the EA

double ATR=iATR(Symbol(), 0, N, S); // Compute ATR over N bars, S bars ago
double SL = 2*ATR + 1*pip;


 

Thank you WHRoeder,

I was not familiar at all with the SRC button. Now may I can communicate better.

/+------------------------------------------------------------------+
//|                                                 LowestLow_ATR.mq4 |
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"


//----Declare EA Variables

string comment = "";    // no shortname
string currencies [1]; //only working on audusd
double currentTick; //the price of the audusd at this point and time (CURRENT)

//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
double ATR=iATR(Symbol(),20,1);         //Declaring that the ATR should be calculated by 20 bars,
//and that calculation be started from the bar prior to current bar.

double  SL=2 * ATR + 1* pip ;          //SL is 2 times ATR plus 1 pip. This calculation will be done
// after every completion of a bar. Not after every tick.
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

double iMaxDuplicateOrders;  //There will be additional positions with every 1/2 ATR.  


extern int TradeSize=20; int Slip=3;
extern int LongTP; extern int LongSL;

int init()

{
currencies [0]  = "AUDUSD";  //only working with one currency
  {
   return (0);
  }
}
//+------------------------------------------------------------------+
//| Entry Buy Condidtions                                   |
//+------------------------------------------------------------------+
int start()

//If curent bar moves one pip lower than the lowest low of the last
//twenty bars, then Buy at the market. Buy on the Low.
//The ATR value that is established for the bar prior to the current bar, 
//the entry bar, will be used for any additional positions.
//Calculation for additional positions --Entry minus 1/2 ATR. Note:
//Calculation is rounded up to a whole number.
//Example 85 ATR value divided by 2 equals 42.5 would be rounded up to 43.
//Once there has been an established Buy position, there can be up to 6 
//additional positions, but no more. This be continued under 'Exiting Buy Positions'. 



  { //this brace indicates the begining 'int start ()'  function
   
    
     if(OrdersTotal ()==0)
      {
       LaunchLong();   //see User Defined Custom Function
       
     } // End Bid < 20 bar Low
      

   
  
//+------------------------------------------------------------------+
//|  Exiting Buy Positions                                 |
//+------------------------------------------------------------------+
         
//There are two possible exits. 
//First exit is a hard stop. As mentioned in the 'Entry Buy Conditions', 
//additional positions are added on at 1/2 ATR value. Exampl: Entry .8555 with an 85 ATR.
 //.8555 -.0043 = .8512 then minus .0001 = .8511 For the second entry. 8511 - .0043 = .8468. .8468 - .0001 = 8467.
 //.8467 is the second addition. Sure you have the idea. I will insert entry and all additions,  
//then show the hard stop. Entry is .8555, 1st.8511, 2nd.8467, 3rd.8423, 4th.8379, 5th.8335,6th .8201, Hopefully the AUDUSD 
//will not drop another .0044 (.8247), but if it does, .8247 will be the hard stop. The last additonal
//position was .8201. The 6th addition. 
//The second stop (soft stop) is the 2*ATR + 1*pip, The soft stop for .8555, would be .0171 (.0085*2 + 1)= .8726.
//The soft stop for the first additional postion would be .8511 + .0171 = .8682, 2nd, .8467 - .0171 = .8638. etc.
if (OrderStopLoss()==0 ||OrderStopLoss()>High[1]+ATR) 
                          
     {
       OrderModify(OrderTicket(),OrderOpenPrice(),High[1]+30,OrderTakeProfit(),0);
         Print("Trail Sell by ATR");
          continue;
     }  //End start () function 
   
      
//-----END OF ALGORITHM-----

//-----Custom Functions------

//============================
//
//     Custom Function No. 1
//
//============================                                            |

//------------1-------------
bool LaunchLong()
  { //OpenBrace to Start the LaunchLong() function
double Min_Dist; double Min_Lot; double Step;
double Free; double Lot; double TP; double SL;   
double Dist_SL; int ticket; double Dist_TP;
int Error;
  
//-----------2--------------
TP=Bid+LongTP*Point;
SL=Ask-LongSL*Point;

while (true)
{  //OpenBrace for beginging of 'while' operator
Min_Dist=MarketInfo(Symbol(),MODE_STOPLEVEL);  //   Min. Distance
Min_Lot=MarketInfo(Symbol(),MODE_MINLOT); //Min. Volume (Min=0.1 @ Alpari)
Step=MarketInfo(Symbol(),MODE_LOTSTEP);  //Step to change lots (Lot Step=0.1 @ Alpari)
Free=AccountFreeMargin();   //Free Margin

//-----------3--------------

Lot=MathFloor(Free/(TradeSize*1000)*(1/Step))*Step;   //Calculates position size 
if (Lot < Min_Lot)  //If it is less than allowed
  {
   Alert("Not enough money for", Min_Lot, "lots");
    break;  //exit cycle
   }

//-----------4--------------

if ((SL+Bid)/Point < Min_Dist)   //If it is less than allowed
{
  SL=Bid-Min_Dist*Point;  //Set the allowed
   Alert("Increased the distance of SL = ",Dist_SL,"pt");
  }
  
//-----------5--------------

if ((Bid+TP)/Point < Min_Dist)  //If it is less than allowed
{
  TP=Ask+Min_Dist*Point;  //Set the allowed
   Alert("Increased the distance of TP = ",Dist_TP," pt");
  }    
  
//-----------6--------------

Alert("The request was sent to the server. Waiting for reply..");
ticket=OrderSend(Symbol(),OP_BUY,Lot, Ask, Slip, SL, TP, "", 0, 0, Red);

//-----------7--------------

if (ticket>0)    //Got it!
{
  Alert ("Opened order Sell ",ticket);
   break;   //exit cycle
  } 
//-------------8-----------

Error=GetLastError();         //Failed
switch(Error)               // Overcomable errors
  {
   case 135: Alert ("The price has change. Retrying..");
    RefreshRates();   //Update data
     continue;        //At the next iteration
   case 136: Alert ("No prices. Waiting for a new tick..");     
     while(RefreshRates()==false)     //Up to a new tick
       Sleep(1);                      //cycle a 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
     }
switch(Error)
     {
      case 2: Alert("Common error.");
        break;                         //exit 'switch'          
      case 5: Alert("Outdated version of the client terminal.");
        break;                         //Exit 'switch"
      case 64: Alert("The account is blocked.");
        break;                         //Exit 'switch'
      case 133: Alert("Trading forbidden");
        break;
      default: Alert("Occurred error ", Error); //Other alternatives  
    }
  break;   //Exit cycle
}  //End 'while' operator
}  //End 'LaunchLong()' function

//=========================================
//
//    CUSTOM FUNCTION 2
//
//=========================================

bool CloseTrade()
  {  //Begin CloseTrade
    if(OrdersTotal()>0)
     {
      OrderSelect(0,SELECT_BY_POS);
       OrderClose(OrderTicket(),OrderLots(),Bid,Slip);
     }  
   }    //End CloseTrade() function
Well, that is about the size of it. If there is anyone who could send info, or have further questions, you can 
also, PM me. Thanks again for reading. Regards Huckleberry          
Reason: