• Welcome back! Thank you for being a part of this Traders Community. Let's discuss and share :)
    Selamat datang kembali! Trimakasih telah menjadi bagian dari Komunitas Trader ini. Mari berdiskusi dan berbagi :)

Tutorial MACD Close Order MQL4

Hayrman

New Member
Credits
0
Hello All,

Im totally new in MQL4 coding and really need help. Once i Open Buy and Sell, how do i code to close-order if

1. MACD Main is lesser than MACD Signal Line

The code for OPEN and SELL as below.


//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OpenBuy()
{
// Open Buy Order
int ticket=OrderSend(_Symbol,OP_BUY,LotSize,Ask,MySlippage,0,0,"BUY",MagicNumber);

if(ticket<0) Print("Buy Order Send failed with error #",GetLastError());
else Print("Buy Order placed successfully");

// Modify Buy Order
bool res=OrderModify(ticket,OrderOpenPrice(),Ask-StopLoss*MyPoint,Ask+TakeProfit*MyPoint,0);

if(!res) Print("Error in OrderModify. Error code=",GetLastError());
else Print("Order modified successfully.");
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OpenSell()
{
//Open Sell Order
int ticket=OrderSend(_Symbol,OP_SELL,LotSize,Bid,MySlippage,0,0,"SELL",MagicNumber);

if(ticket<0) Print("Sell Order Send failed with error #",GetLastError());
else Print("Sell Order placed successfully");

// Modify Sell Order
bool res=OrderModify(ticket,OrderOpenPrice(),Bid+StopLoss*MyPoint,Bid-TakeProfit*MyPoint,0);

if(!res) Print("Error in OrderModify. Error code=",GetLastError());
else Print("Order modified successfully.");
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+

Many Thanks in Advance. :)
 
Code:
int cekSignal(){
   int signal = -1;
   //your code to find a signal according to your rule
   
   if (...){
      signal   = OP_BUY;
   }else if (...){
      signal   = OP_SELL;
   }
   
   return (signal);
}

void closeOrder(int orderType){
   //buat fungsi utk close order
   
   Print ("Close Order Done!");
}

void OnTick(){

   static int lastSignal = -1;
   int signal  = cekSignal();
   if (signal != lastSignal){
      if (signal == OP_BUY){
         lastSignal = signal; 
         Print ("Signal BUY");
         closeOrder (OP_SELL);
      }else if (signal == OP_SELL){
         lastSignal = signal;
         Print ("Signal SELL");
         closeOrder (OP_BUY);
      }
   }

}
 
Many Thanks Onyx. I will try on it Thanks.

Also my current EA open new order on every candle. How can i open a new order when my previous trade hit TP or SL?

int err=0;
double price=Bid, sl=0, tp=0;
if(type == OP_BUY)
price =Ask;
//----
int ticket = OrderSend(Symbol(),type,LotSize,price,Slippage,0,0,"Trade",MagicNumber,0,Magenta);
if(ticket>0){
if(OrderSelect(ticket,SELECT_BY_TICKET)){

if (OrderType()==OP_SELL)
{
sl = OrderOpenPrice()+(StopLoss*pips);
tp = OrderOpenPrice()-(TakeProfit*pips);
}
else if(OrderType()==OP_BUY){
sl = OrderOpenPrice()-(StopLoss*pips);
tp = OrderOpenPrice()+(TakeProfit*pips);
}
if(!OrderModify(ticket,price,sl,tp,0,Magenta)) {
err = GetLastError();
Print("Encountered an error during modification!"+(string)err+" "+ErrorDescription(err) );
}
}
 
Back
Top