Amps

Tuesday, February 23, 2016

Moving Average EA Open Order with Modify Take profit and Stop Loss

Moving Average EA Open Order with Modify Take profit and Stop Loss

In previous chapter we guide moving average properties. In this chapster we would coding Moving Average EA MQL4 for modify take profit and Stop Loss.
Mention to Forex Broker have the exchange digits show 3,5 and 4 digits, which modify Take profit and Stop Loss that depend on broker we trading.

Refer to properties code in this part are:












extern string TP_SL = " --- TP and Stop Loss ---";
extern bool Broke3_5Digits = True;
extern double My_TP = 10;
extern double My_SL = 10;

And Moving Average Properties part we would always change as below
//Add extern parameter as Label
extern string MovingAvr = " --- Moving Average parameter ---";
//Change from original
extern int    MovingPeriod  =12;
extern int    MovingShift   =6;
//Add properties
extern ENUM_TIMEFRAMES TimeFrameUsed = PERIOD_CURRENT;
extern ENUM_MA_METHOD   EMA_method1 = MODE_SMA;
extern ENUM_APPLIED_PRICE Applied_Price1 = PRICE_CLOSE;

In checking the open order we would add the condition to design broker digits using as follow

void CheckForOpen()
  {
   double ma;
   int    res;

// calculate TP and Stop Loss   
   double BrokerTPSell,BrokerTPBuy;
   double BrokerSLSell,BrokerSLBuy;
 if(Broke3_5Digits=True)
 { 
   if (My_TP!=0)
   {
    BrokerTPSell = Bid-((My_TP*Point)*10);
    BrokerTPBuy = Ask+((My_TP*Point)*10);
   }
   
   if (My_TP==0)
   {
    BrokerTPSell = 0;
    BrokerTPBuy = 0;
   }
   
   if (My_SL!=0)
   {
    BrokerSLSell = Bid+((My_SL*Point)*10);
    BrokerSLBuy = Ask-((My_SL*Point)*10);
   }
   
   if (My_SL==0)
   {
    BrokerSLSell = 0;
    BrokerSLBuy = 0;
   }
   
 }
 if(Broke3_5Digits=False)
 { 
   if (My_TP!=0)
   {
    BrokerTPSell = Bid-(My_TP*Point);
    BrokerTPBuy = Ask+(My_TP*Point);
   }
   
   if (My_TP==0)
   {
    BrokerTPSell = 0;
    BrokerTPBuy = 0;
   }
   
   if (My_SL!=0)
   {
    BrokerSLSell = Bid+(My_SL*Point);
    BrokerSLBuy = Ask-(My_SL*Point);
   }
   
   if (My_SL==0)
   {
    BrokerSLSell = 0;
    BrokerSLBuy = 0;
   }
   
 }

So the open order we would put the value from Moving Average EA as below

//--- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//--- get Moving Average 
  // original ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
  //new
  ma=iMA(NULL,TimeFrameUsed,MovingPeriod,MovingShift,EMA_method1,Applied_Price1,0);
//--- sell conditions
   if(Open[1]>ma && Close[1]<ma)
     {
      res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,BrokerSLSell,BrokerTPSell,"",MAGICMA,0,Red);
     
      return;
     }
//--- buy conditions
   if(Open[1]<ma && Close[1]>ma)
     {
      res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,BrokerSLBuy,BrokerTPBuy,"",MAGICMA,0,Blue);
      return;
     }
//---
  }

In next chapster we would detail for auto close order

No comments:

Post a Comment