hiddendiv_exp2

Author: Copyright � 2006, Nick Bilak
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself
Indicators Used
Money flow index
0 Views
0 Downloads
0 Favorites

Profitability Reports

AUD/USD Oct 2024 - Jan 2025
58.00 %
Total Trades 759
Won Trades 0
Lost trades 0
Win Rate 0.00 %
Expected payoff -1.39
Gross Profit 1452.00
Gross Loss -2508.00
Total Net Profit -1056.00
-100%
-50%
0%
50%
100%
NZD/USD Oct 2024 - Jan 2025
49.00 %
Total Trades 657
Won Trades 99
Lost trades 558
Win Rate 0.15 %
Expected payoff -1.74
Gross Profit 1089.00
Gross Loss -2232.00
Total Net Profit -1143.00
-100%
-50%
0%
50%
100%
hiddendiv_exp2
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                hiddendiv-exp.mq4 |
//|                                  Nick Bilak, beluck[AT]gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Nick Bilak"
#property link      "http://metatrader.50webs.com/"
#include <stdlib.mqh>

extern int myMagic = 1;
extern int NumTrades = 3;
extern double TakeProfit = 110;
extern double StopLoss = 40;  
extern double BreakEvenStop = 350;  
extern double TrailingStop = 350;  

extern int    slippage=2;   	//slippage for market order processing
extern int    shift=1;			//shift to current bar, 

extern double Lots = 0.1;
extern double MaximumRisk = 5;
extern bool   FixedLot = true;

extern int    MFIperiod=3;

extern int    OrderTriesNumber=2; //to repeate sending orders when got some error

extern string    EAName="hiddendiv"; 

bool buysig,sellsig,closebuy,closesell; int lastsig,tries,at,at2;


void start()  {

   //---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;

   //---- check for signals
   CheckForSignals();

   if (CalculateCurrentOrders(Symbol())==0)     //---- calculate open orders by current symbol
      CheckForOpen();
   else {
      CheckForClose();
      BreakEvenStop();
      TrailStop();
   }
        
}

double LotsRisk(int StopLoss)  { 
   double lot=Lots;
//---- select lot size
   if (!FixedLot)
      lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk*0.001/StopLoss,1);
   else
      lot=Lots;
//---- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
}

//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int ord;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==symbol && OrderMagicNumber()==myMagic) ord++;
     }
//---- return orders volume
   return(ord);
}

//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForSignals() {
      
      buysig=false;
      sellsig=false;
      //indicators variables
      double mfi1 = iMFI(NULL,0,MFIperiod,shift);
      double mfi2=mfi1;
      int i=shift+1;
      if (mfi1>=100) {
         while (mfi2>=100) {
            mfi2 = iMFI(NULL,0,MFIperiod,i);
            i++;
         }
         while (mfi2<100) {
            mfi2 = iMFI(NULL,0,MFIperiod,i);
            i++;
         }
         i--;
         if (High[shift]<High[i]) {
             sellsig=true;
         }
      }
      if (mfi1<=0) {
         while (mfi2<=0) {
            mfi2 = iMFI(NULL,0,MFIperiod,i);
            i++;
         }
         while (mfi2>0) {
            mfi2 = iMFI(NULL,0,MFIperiod,i);
            i++;
         }
         i--;
         if (Low[shift]>Low[i]) {
             buysig=true;
         }
      }
            
      //closebuy=sellsig;
      //closesell=buysig;
}

void CheckForOpen() {
   int    res,tr, cnt;
//---- sell conditions

   if(sellsig && lastsig!=Time[0])  {
   //if(buysig && lastsig!=Time[0])  {
   for (cnt = 0; cnt < NumTrades; cnt++)
   {
	   res=OpenAtMarket(OP_SELL,LotsRisk(StopLoss));
      if (res<=0) Print("Error SELL order : ",ErrorDescription(GetLastError()));
      lastsig=Time[0];
      if (cnt == NumTrades - 1) return;
   }
   }
   
//---- buy conditions
   //if(sellsig && lastsig!=Time[0])  {
   if(buysig && lastsig!=Time[0])  {
   for (cnt = 0; cnt < NumTrades; cnt++)
   {
	   res=OpenAtMarket(OP_BUY,LotsRisk(StopLoss));
      if (res<=0) Print("Error BUY order : ",ErrorDescription(GetLastError()));
      lastsig = Time[0];
      if (cnt == NumTrades - 1) return;
   }
   }
}
  
  
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()  {
   bool bres; int tr;
   for(int i=OrdersTotal() - 1; i>= 0; i--)  {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)  break;
      if(OrderMagicNumber()!=myMagic || OrderSymbol()!=Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY && closebuy) {
      //if(OrderType()==OP_BUY && closesell) {
         bres=CloseAtMarket(OrderTicket(),OrderLots());
         break;
      }
      if(OrderType()==OP_SELL && closesell)  {
      //if(OrderType()==OP_SELL && closebuy)  {
         bres=CloseAtMarket(OrderTicket(),OrderLots());
         break;
      }
   }
}


void TrailStop() {
   bool bres;
   double StopLoss;
   if ( TrailingStop > 2 ) {
      for (int i = 0; i < OrdersTotal(); i++) {
         if ( OrderSelect (i, SELECT_BY_POS) == false )  continue;
         if ( OrderSymbol() != Symbol() || OrderMagicNumber() != myMagic )  continue;
         if ( OrderType() == OP_BUY ) {
            if ( Bid < OrderOpenPrice()+TrailingStop*Point )  return;
            StopLoss = Bid-TrailingStop*Point;
            if ( StopLoss > OrderStopLoss() ) {
                  bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White);
					   if (!bres) Print("Error Modifying BUY order : ",ErrorDescription(GetLastError()));
            }
         }
   
         if ( OrderType() == OP_SELL ) {
            if ( Ask > OrderOpenPrice()-TrailingStop*Point )  return;
            StopLoss = Ask+TrailingStop*Point;
            if ( StopLoss < OrderStopLoss() ) {
                  bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, Gold);
					   if (!bres) Print("Error Modifying SELL order : ",ErrorDescription(GetLastError()));
            }
         }
      }
   }
   return;
}

void BreakEvenStop() {
   bool bres;
   double StopLoss;
   if ( BreakEvenStop > 2 ) {
      for (int i = 0; i < OrdersTotal(); i++) {
         if ( OrderSelect (i, SELECT_BY_POS) == false )  continue;
         if ( OrderSymbol() != Symbol() || OrderMagicNumber() != myMagic )  continue;
         if ( OrderType() == OP_BUY ) {
            if ( Bid < OrderOpenPrice()+BreakEvenStop*Point )  return;
            StopLoss = OrderOpenPrice();
            if ( StopLoss > OrderStopLoss() ) {
                  bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White);
					   if (!bres) Print("Error Modifying BUY order : ",ErrorDescription(GetLastError()));
            }
         }
   
         if ( OrderType() == OP_SELL ) {
            if ( Ask > OrderOpenPrice()-BreakEvenStop*Point )  return;
            StopLoss = OrderOpenPrice();
            if ( StopLoss < OrderStopLoss() ) {
                  bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, Gold);
					   if (!bres) Print("Error Modifying SELL order : ",ErrorDescription(GetLastError()));
            }
         }
      }
   }
   return;
}

int OpenAtMarket(int mode,double lot) {
   int    res,tr,col;
   double openprice,sl,tp;
   tries=0;
   while (res<=0 && tries<OrderTriesNumber) {
      tr=0; while (tr<5 && !IsTradeAllowed()) { tr++; Sleep(2000); }
      RefreshRates();
      if (mode==OP_SELL) {
         openprice=Bid; 
         sl=openprice+StopLoss*Point;
         tp=openprice-TakeProfit*Point;
         col=Red;
      } else {
         openprice=Ask;
         sl=openprice-StopLoss*Point;
         tp=openprice+TakeProfit*Point;
         col=Blue;
      }
      res=OrderSend(Symbol(),mode,lot,openprice,slippage,sl,tp,EAName+myMagic,myMagic,0,col);
      tries++;
   }
   return(res);
}


bool CloseAtMarket(int ticket,double lot) {
   bool bres=false; int tr;
   tries=0;
   while (!bres && tries<OrderTriesNumber) {
      tr=0; while (tr<5 && !IsTradeAllowed()) { tr++; Sleep(2000); }
      RefreshRates();
      bres=OrderClose(ticket,lot,OrderClosePrice(),slippage,White);
      tries++;
   }
   if (!bres) Print("Error closing order : ",ErrorDescription(GetLastError()));
}

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---