RSI_SAR_MACDmq4

Author: Copyright � 2011 Evgeny Evstegneev
Orders Execution
Checks for the total of closed ordersChecks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself
Indicators Used
Parabolic Stop and Reverse systemRelative strength indexMACD Histogram
0 Views
0 Downloads
0 Favorites
RSI_SAR_MACDmq4
//+------------------------------------------------------------------+
//|                                                   RSI_SAR_MACD.mq4 |
//|                             Copyright © 2011, Evgeny Evstegneev |
//+----                                      |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011 Evgeny Evstegneev"
#property link      " OCENute"

#define magic  020281
double Lots = 0.1;//íà÷àëüíûé ëîò
extern double MaximumRisk = 0.02;
extern double DecreaseFactor = 3.0;
extern double balans = 500.0; //øàã áàëàíñà

//---- input parameters
extern int period=25;
extern int period_1=20;
extern int tp=250;
extern int sl=27;
//extern double lot = 0.1;

double LotsOptimized() {
       double minlot = MarketInfo(Symbol(), MODE_MINLOT);
       double maxlot = MarketInfo(Symbol(), MODE_MAXLOT);       
       double lot = Lots;
       int orders = OrdersHistoryTotal();
       int losses = 0;
       lot = NormalizeDouble(AccountFreeMargin() * MaximumRisk / balans, 2);
       if (DecreaseFactor > 0.0) {
for (int i = orders - 1; i >= 0; i--) {
       if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) == FALSE) {
       Print("Error in history!");
       break;
       }
if (OrderSymbol() != Symbol() || OrderType() > OP_SELL) continue;
if (OrderProfit() > 0.0) break;
if (OrderProfit() < 0.0) losses++;
}
if (losses > 1) lot = NormalizeDouble(lot - lot * losses / DecreaseFactor, 2);
}
if(lot < minlot) lot = minlot;
if(lot > maxlot) lot = maxlot; 
return (lot);}
int start()
  {
//----
   double sar = iSAR(NULL,0,0.02,0.2,0);
   double rsi=iRSI(NULL ,0,period_1,PRICE_CLOSE,0);
   double macd=iMACD(NULL,0,9,19,19,PRICE_CLOSE,0,0);

   int ticket, cnt, total=OrdersTotal();
     
   if(total<1)
      {
         if(sar<Close[0] && rsi>50.0 && macd>0.0 )
             
            {
               ticket=OrderSend(Symbol(),OP_BUY,LotsOptimized() ,Ask,3,Bid-sl*Point,Ask+tp*Point,"RSI_SAR_MACD",magic,0,Green);
               if(ticket>0)
                  {
                     if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
                  }
               else
                  {
                     Print("Error opening BUY order : ",GetLastError());
                     return(0);
                  }
            }
         else if(sar>Close[0] && rsi<50.0 && macd<0.0)
              
            {
               ticket=OrderSend(Symbol(),OP_SELL,LotsOptimized() ,Bid,3,Ask+sl*Point,Bid-tp*Point,"CCI_TRADER",magic,0,Red);
               if(ticket>0)
                  {
                     if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
                  }
               else
                  {
                     Print("Error opening SELL order : ",GetLastError());
                     return(0);
                  }
            }
      }

for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
                                OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            
            if(sar>Close[0] && rsi<50.0 && macd<0.0)
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                 return(0); // exit
                }
           }
         else // go to short position
           {
            // should it be closed?
            if(sar<Close[0] && rsi>50.0 && macd>0.0)
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               return(0); // exit
              }
           }
        }
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+

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