FarhadCrab61.1

Author: Copyright � 2007, Farhad Farshad
Price Data Components
Series array that contains open time of each barSeries array that contains open prices of each bar
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt automatically opens orders when conditions are reachedIt Closes Orders by itself
Indicators Used
Relative strength indexMovement directional indexMoving average indicator
0 Views
0 Downloads
0 Favorites

Profitability Reports

AUD/USD Oct 2024 - Jan 2025
34.00 %
Total Trades 41
Won Trades 0
Lost trades 0
Win Rate 0.00 %
Expected payoff -52.10
Gross Profit 1091.00
Gross Loss -3227.00
Total Net Profit -2136.00
-100%
-50%
0%
50%
100%
GBP/USD Oct 2024 - Jan 2025
68.00 %
Total Trades 64
Won Trades 17
Lost trades 47
Win Rate 0.27 %
Expected payoff -30.54
Gross Profit 4091.00
Gross Loss -6045.40
Total Net Profit -1954.40
-100%
-50%
0%
50%
100%
FarhadCrab61.1
//+------------------------------------------------------------------+
//|     FarhadCrab6.mq4 |
//|     Copyright © 2007, Farhad Farshad |
//|     farhadfarshad11@yahoo.com
//|     ***** PLEASE NOTE *****
//|     This EA best works on EURUSD (2 pip spread broker) 1M TimeFrame. 
//|     If you get money from this EA please donate some to poor people of your country.
//+-----------------------------------------------------------------+
#property copyright "Copyright © 2007, Farhad Farshad"
#property link      "http://www.farhadsalimi.com"
#include <stdlib.mqh>

extern double lTakeProfit    = 300;   // recomended  no more than 20
extern double sTakeProfit    = 300;   // recomended  no more than 20
extern double takeProfit     = 300;            // recomended  no more than 20
extern double pr             = 300;      //take profit in sideway markets.
extern double stopLoss       = 0;             // 
extern int magicEA           = 124;        // Magic EA identifier. Allows for several co-existing EA with different input values
extern double lTrailingStop  = 10;   // trail stop in points
extern double sTrailingStop  = 10;   // trail stop in points
extern color clOpenBuy       = Blue;  //Different colors for different positions
extern color clCloseBuy      = Aqua;  //Different colors for different positions
extern color clOpenSell      = Red;  //Different colors for different positions
extern color clCloseSell     = Violet;  //Different colors for different positions
extern color clModiBuy       = Blue;   //Different colors for different positions
extern color clModiSell      = Red;   //Different colors for different positions
extern int Slippage          = 3;
extern string nameEA         = "FarhadCrab6.mq4";// To "easy read" which EA place an specific order and remember me forever :)



double maLongCurrent, maShortCurrent, maLongPrevious, maShortPrevious, faRSICurrent, deMark;
double  closeCurrentD, closePreviousD;
int cnt, ticket;
double initialDeposit; //First of All Specify your initial Deposit.
double lLots , sLots;// you can change the lot but be aware of margin. Its better to trade with 1/4 of your capital. 






//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start() {

 
/*   
 */  


if (AccountProfit()>0)
{
CloseBuyPositions();
CloseSellPositions();
return(0);
}

   // Check for invalid bars and takeprofit
   if(Bars < 200) {
      Print("Not enough bars for this strategy - ", nameEA);
      return(0);
      }

    calculateIndicators();                      // Calculate indicators' value 
    //Check for TakeProfit Conditions  
   if(lTakeProfit<1){
      Print("TakeProfit less than 1 on this EA with Magic -", magicEA );
      return(0);
   }
   if(sTakeProfit<1){
      Print("TakeProfit less than 1 on this EA with Magic -", magicEA);
      return(0);
   }
   
   //Introducing new expressions
double rsi                 = iRSI(NULL,PERIOD_M1,9,PRICE_CLOSE,0);


double n;

if ((AccountEquity()-AccountBalance()==0) || (AccountEquity()-AccountBalance()>-((OrderLots())*500)))
   
   n = 0;
   else 
    n = 1;

if (OrdersTotal()>n)
{
return(0);
}

int bnt;
   
   for(bnt=0;bnt<OrdersTotal();bnt++)
   {
      OrderSelect (bnt, SELECT_BY_POS);
      
      if (OrderProfit()>0)
      {
      CloseBuyPositions();
      CloseSellPositions();
      return(0);
      }
      if (TimeCurrent()-OrderOpenTime()<500) {
 //if((OrdersTotal()>1)){
      return(0);
   }
   }

    

//Check Margin Requirement

   if(AccountFreeMargin()<0){
   
      
      Print("We have no money. Free Margin = ", AccountFreeMargin());
      return(0);
   }
   

   //Buy Condition
   if ((!takeBuyPositions()) && (n == 0) ){
   

      if (
      
     
    ((maLongCurrent<closeCurrentD)) //In downward direction is off
     && (rsi < 30)         
)


      {

         OpenBuy();
        
         
         return(0);
      }
      }
      
      
         if ((!takeBuyPositions()) && (n == 1) ){
   

      if (

         (OrderType()==OP_SELL)
)


      {

         hedgeOpenBuy();
        
         
         return(0);
      }
      }

//Sell Condition
   if ((!takeSellPositions()) && (n == 0)){
      if ( 
 
      
   // ((maLongCurrent>closeCurrentD)) -In upward direction is off.
   (rsi > 70)
)
{


         OpenSell();
        
         return(0);
      }
      }
      
      
         if ((!takeSellPositions()) && (n == 1)){
      if (

   (OrderType()==OP_BUY)
)
{


         hedgeOpenSell();
        
         return(0);
      }
      }
   
   
   
   //Trailing Expressions
   TrailingPositionsBuy(lTrailingStop);
   TrailingPositionsSell(sTrailingStop);
   return (0);
}
//Number of Buy Positions

bool takeBuyPositions() {

for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol() && OrderMagicNumber() == magicEA) {
return(0);
}
} 
}

return(0);
}

//Number of Sell Positions

bool takeSellPositions() {

for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol() && OrderMagicNumber() == magicEA) {
return(0);
}
} 
}

return(0);
}

void TrailingPositionsBuy(int trailingStop) { 
   for (int i=0; i<OrdersTotal(); i++) { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { 
         if (OrderSymbol()==Symbol() && OrderMagicNumber() == magicEA) { 
            if (OrderType()==OP_BUY) { 
               if (Bid-OrderOpenPrice()>trailingStop*Point) { 
                  if (OrderStopLoss()<Bid-trailingStop*Point) 
                     ModifyStopLoss(Bid-trailingStop*Point); 
               } 
            } 
         } 
      } 
   } 
} 
void TrailingPositionsSell(int trailingStop) { 
   for (int i=0; i<OrdersTotal(); i++) { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { 
         if (OrderSymbol()==Symbol() && OrderMagicNumber() == magicEA) { 
            if (OrderType()==OP_SELL) { 
               if (OrderOpenPrice()-Ask>trailingStop*Point) { 
                  if (OrderStopLoss()>Ask+trailingStop*Point || 
OrderStopLoss()==0)  
                     ModifyStopLoss(Ask+trailingStop*Point); 
               } 
            } 
         } 
      } 
   } 
} 
void ModifyStopLoss(double ldStopLoss) { 
   bool fm;
   fm = OrderModify(OrderTicket(),OrderOpenPrice
(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE); 
  
} 

void OpenBuy() { 
   double ldLot, ldStop, ldTake; 
   string lsComm; 
   ldLot = GetSizeLot(); 
   ldStop = GetStopLossBuy(); 
   ldTake = GetTakeProfitBuy(); 
   lsComm = GetCommentForOrder(); 
   if ((AccountEquity()-AccountBalance()==0) || (AccountEquity()-AccountBalance()>-((OrderLots())*500)))
   
   lLots = NormalizeDouble((MathRound((AccountBalance()-500)/1000)/10),1);

   else 
   lLots = NormalizeDouble((MathRound((AccountBalance()-500)/1000)/5),1);

   OrderSend(Symbol(),OP_BUY,lLots,Ask,Slippage,ldStop,ldTake,nameEA,magicEA,0,clOpenBuy);  

    
} 
void OpenSell() { 
   double ldLot, ldStop, ldTake; 
   string lsComm; 

   ldLot = GetSizeLot(); 
   ldStop = GetStopLossSell(); 
   ldTake = GetTakeProfitSell(); 
   lsComm = GetCommentForOrder(); 
   if ((AccountEquity()-AccountBalance()==0) || (AccountEquity()-AccountBalance()>-((OrderLots())*500)))
   
   sLots = NormalizeDouble((MathRound((AccountBalance()-500)/1000)/10),1);

   else 
   sLots = NormalizeDouble((MathRound((AccountBalance()-500)/1000)/5),1);

   
   

   OrderSend(Symbol(),OP_SELL,sLots,Bid,Slippage,ldStop,ldTake,nameEA,magicEA,0,clOpenSell); 
  
} 

void hedgeOpenBuy() { 
   double ldLot, ldStop, ldTake; 
   string lsComm; 
   ldLot = GetSizeLot(); 
   ldStop = GetStopLossBuy(); 
   ldTake = GetTakeProfitBuy(); 
   lsComm = GetCommentForOrder(); 
   //if ((AccountEquity()-AccountBalance()==0) || (AccountEquity()-AccountBalance()>-(MathSqrt(OrderLots())*500)))
   
   //lLots = 0.2; //MathRound(AccountBalance()/4000);

   //else 
   lLots = NormalizeDouble((MathRound((AccountBalance()-500)/1000)/5),1);

   OrderSend(Symbol(),OP_BUY,lLots,Ask,Slippage,Ask-60*Point,ldTake,nameEA,magicEA,0,clOpenBuy);  

    
} 
void hedgeOpenSell() { 
   double ldLot, ldStop, ldTake; 
   string lsComm; 

   ldLot = GetSizeLot(); 
   ldStop = GetStopLossSell(); 
   ldTake = GetTakeProfitSell(); 
   lsComm = GetCommentForOrder(); 
  // if ((AccountEquity()-AccountBalance()==0) || (AccountEquity()-AccountBalance()>-(MathSqrt(OrderLots())*500)))
   
   //sLots = 0.2; //MathRound(AccountBalance()/4000);

  // else 
   sLots = NormalizeDouble((MathRound((AccountBalance()-500)/1000)/5),1);

   
   

   OrderSend(Symbol(),OP_SELL,sLots,Bid,Slippage,Bid+60*Point,ldTake,nameEA,magicEA,0,clOpenSell); 
  
} 
string GetCommentForOrder() { return(nameEA); } 
double GetSizeLot() { return(lLots); } 
double GetTakeProfitBuy() { if (iADX(NULL,0,14,PRICE_MEDIAN,MODE_MAIN,0)<25)  return(Ask+lTakeProfit*Point); else return(Ask+pr*Point); } 
double GetTakeProfitSell() { if (iADX(NULL,0,14,PRICE_MEDIAN,MODE_MAIN,0)<25) return(Bid-sTakeProfit*Point); else return (Bid-pr*Point);} 
double GetStopLossBuy() { if (stopLoss==0) return(0); else  return(Ask - stopLoss*Point);}
double GetStopLossSell() { if (stopLoss==0) return(0); else return(Bid + stopLoss*Point);}
  void calculateIndicators() {  
    // Calculate indicators' value   

   closeCurrentD       = iOpen(NULL,PERIOD_M1,0); //Close Price Current for D1 TimeFrame
   closePreviousD      = iOpen(NULL,PERIOD_M1,1); //Close Price Previous for D1 TimeFrame
   maLongCurrent       = iMA(NULL,PERIOD_M1,3,1,MODE_SMMA,PRICE_OPEN,0); //Current Long Term Moving Average 
   maLongPrevious      = iMA(NULL,PERIOD_M1,3,1,MODE_SMMA,PRICE_OPEN,1); //Previous Long Term Moving Average 
   
}
void CloseBuyPositions(){ 
   for (int i=0; i<OrdersTotal(); i++) { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { 
         if (OrderSymbol()==Symbol() && OrderMagicNumber() == magicEA) { 
            if (OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clCloseBuy);
         } 
      } 
   } 
} 
void CloseSellPositions(){ 
   for (int i=0; i<OrdersTotal(); i++) { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { 
         if (OrderSymbol()==Symbol() && OrderMagicNumber() == magicEA) { 
            if (OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clCloseSell);
         } 
      } 
   } 
}


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 ---