SuperStochs v2

Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt Closes Orders by itself
Indicators Used
Stochastic oscillator
0 Views
0 Downloads
0 Favorites
SuperStochs v2
//+------------------------------------------------------------------+
//|                                                   SuperStochs EA |
//|                                                      version 1.0 |
//|                                       http://www.forexforums.org |
//|Check EA Main thread at:                                          |
//|http://www.forexforums.org/showthread.php?t=1888                  |
//+------------------------------------------------------------------+
 
extern int        Magic              = 42008;                       // 
extern double     LotSize            = 0.1;                         // 
extern int        TP                 = 400;                         // 
extern int        SL                 = 1800;                        // 
extern string     LongComment        = "SSL";                       // 
extern string     ShortComment       = "SSS";                       //  
extern double     BreakEven          = 100;                         // Profit Lock in pips
extern double     BEOffset           = 10;                          // BreakEven profit capture
extern double     TrailStop          = 400;                         // 
extern bool       MoneyManagement    = true;                        // Set to true to use Money Management 
extern double     MaxLotSize         = 1.5;                         // Max Lot size when using Money Management
extern double     Risk               = 15;                          // Risk factor when using MM
extern int        PreventPos         = 1;                           // Prevent the opening of a new position if there are other near



double S1M, S1S, S2M, S2S, S3M, S3S, S4M, S4S;
string Signal;
string Status;
bool AccountIsMicro;
double ticket, AccountSize;
double slippage=5;

int init()
{
   if (MarketInfo(Symbol(),MODE_LOTSTEP) == 0.01)
   {
     AccountIsMicro = true;
   }
   else
   {
     AccountIsMicro = false;
   }

   return(0);
}

int start()
{
   S1M=iStochastic(NULL,PERIOD_M5,5,3,3,MODE_SMA,0,MODE_MAIN,0);
   S1S=iStochastic(NULL,PERIOD_M5,5,3,3,MODE_SMA,0,MODE_SIGNAL,0);
   S2M=iStochastic(NULL,PERIOD_M30,5,3,3,MODE_SMA,0,MODE_MAIN,0);
   S2S=iStochastic(NULL,PERIOD_M30,5,3,3,MODE_SMA,0,MODE_SIGNAL,0);
   S3M=iStochastic(NULL,PERIOD_H1,5,3,3,MODE_SMA,0,MODE_MAIN,0);
   S3S=iStochastic(NULL,PERIOD_H1,5,3,3,MODE_SMA,0,MODE_SIGNAL,0);
   S4M=iStochastic(NULL,PERIOD_H1,14,3,3,MODE_SMA,0,MODE_MAIN,0);
   S4S=iStochastic(NULL,PERIOD_H1,14,3,3,MODE_SMA,0,MODE_SIGNAL,0);
   
   if (MoneyManagement) LotSize = LotSize();
   
   if (ScanOpenTrades() == 0) Status = "READY";
      
   Signal="NONE";
   if(S1M>S1S && S2M>S2S && S3M>S3S && S4M>S4S)
   {
      Signal="LONG";
   }
   if(S1M<S1S && S2M<S2S && S3M<S3S && S4M<S4S)
   {
     // Signal="SHORT";
   }
   
   
   if(Status=="READY" && Signal=="LONG" &&  !IsPosition(PreventPos)) //ScanOpenTrades() < MaxOpenTrades &&
   {
      ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,slippage,Ask-SL*Point,Ask+TP*Point,LongComment,Magic,0,Blue);
      if(ticket>-1)
      {
         Status="LONG ACTIVE";
      }
   }
   
   if(Status=="READY" && Signal=="SHORT" &&  !IsPosition(PreventPos)) //ScanOpenTrades() < MaxOpenTrades &&
   {
      ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,slippage,Bid+SL*Point,Bid-TP*Point,ShortComment,Magic,0,Red);
      if(ticket>-1)
      {
         Status="SHORT ACTIVE";
      }
   }
   
   if(Status=="LONG ACTIVE" && (Signal=="NONE" || Signal=="SHORT"))
   {
      if (Signal=="SHORT") OpenOrdClose();
      Status="READY";
   }
   
   if(Status=="SHORT ACTIVE" && (Signal=="NONE" || Signal=="LONG"))
   {
      if (Signal=="LONG") OpenOrdClose();
      Status="READY";
   }
   
    
    
    
    if (TrailStop > 0) 
    {
      TrailIt(TrailStop);
    }
   
    if (BreakEven > 0)
    {
      DoBE(BreakEven);
    }
   
   
   
   
   
   
   return(0);
}
   
int ScanOpenTrades()
{   
   int total = OrdersTotal();
   int numords = 0;
    
   for(int cnt=0; cnt<=total-1; cnt++) 
   {        
   OrderSelect(cnt, SELECT_BY_POS);
      if(OrderType()<=OP_SELL)
      {
      if(Magic > 0) if(OrderMagicNumber() == Magic) numords++;
      if(Magic == 0) numords++;
      }
   }   
   return(numords);
}  

void DoBE(int byPips)
  {
    for (int i = 0; i < OrdersTotal(); i++) {
     OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
     if ( OrderSymbol()==Symbol() && ( (OrderMagicNumber() == Magic) ) )  

        {
            if (OrderType() == OP_BUY && (OrderStopLoss() < (OrderOpenPrice() +  MarketInfo(OrderSymbol(), MODE_POINT)*BEOffset))) if ((Bid - OrderOpenPrice() > byPips * MarketInfo(OrderSymbol(), MODE_POINT)) && (OrderOpenPrice()-OrderStopLoss() > 0)) if (OrderStopLoss() != (OrderOpenPrice() +  MarketInfo(OrderSymbol(), MODE_POINT))) OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() +  MarketInfo(OrderSymbol(), MODE_POINT)*BEOffset, OrderTakeProfit(), Red);
            if (OrderType() == OP_SELL && (OrderStopLoss() > (OrderOpenPrice() +  MarketInfo(OrderSymbol(), MODE_POINT)*BEOffset))) if ((OrderOpenPrice() - Ask > byPips * MarketInfo(OrderSymbol(), MODE_POINT)) && (OrderOpenPrice()-OrderStopLoss() > 0)) if (OrderStopLoss() != (OrderOpenPrice() -  MarketInfo(OrderSymbol(), MODE_POINT))) OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() -  MarketInfo(OrderSymbol(), MODE_POINT)*BEOffset, OrderTakeProfit(), Red);
        }
    }
  }

 void TrailIt( int byPips )                   // based on trailing stop code from MT site... thanks MT!
  {
  if (byPips >=5)
  {
  for (int i = 0; i < OrdersTotal(); i++) {
     OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
     if ( OrderSymbol()==Symbol() && ( (OrderMagicNumber() == Magic)) ) 
        {
            if (OrderType() == OP_BUY) {

               if (Bid - OrderOpenPrice() > byPips * MarketInfo(OrderSymbol(), MODE_POINT)) {
                  if (OrderStopLoss() < Bid - byPips * MarketInfo(OrderSymbol(), MODE_POINT)) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Bid - byPips * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);
                  }
               }
            } else if (OrderType() == OP_SELL) {
               if (OrderOpenPrice() - Ask > byPips * MarketInfo(OrderSymbol(), MODE_POINT)) {
                  if ((OrderStopLoss() > Ask + byPips * MarketInfo(OrderSymbol(), MODE_POINT)) || 
                        (OrderStopLoss() == 0)) {
                     OrderModify(OrderTicket(), OrderOpenPrice(),
                        Ask + byPips * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);
                  }
               }
            }
        }
	  }
	  }

  } // proc TrailIt()


double LotSize()
{

     double Account = 0;
  
     double lotMM = MathCeil(AccountFreeMargin() *  Risk / 1000) / 1000;
     
     if(AccountIsMicro)  {
     
     if(lotMM < 0.01) lotMM = 0.01;
     if(lotMM >= MaxLotSize) lotMM = MaxLotSize;
     
     AccountSize=2;
     
     } else {
     
     if(lotMM < 0.1) lotMM = 0.1;
     if(lotMM >= MaxLotSize) lotMM = MaxLotSize;
     
     AccountSize=1;
     
     }
     
     lotMM = NormalizeDouble(lotMM,AccountSize);
     
	  return (lotMM);
}


// Closing of Open Orders      
void OpenOrdClose()
{
    int total=OrdersTotal();
    
    for (int cnt=0;cnt<total;cnt++)
    { 
    OrderSelect(total-cnt-1, SELECT_BY_POS);
    int mode=OrderType();
    bool res = false; 
    bool condition = false;
    if (OrderMagicNumber()==Magic ) condition = true;
      if (condition && ( mode==OP_BUY || mode==OP_SELL ))
      { 
       
// - BUY Orders         
         if(mode==OP_BUY)
         {  
         res = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,Yellow);
               
            if( !res )
            {
            Print(" BUY: OrderClose failed with error #",GetLastError());
            Print(" Ticket=",OrderTicket());
            Sleep(3000);
            }
         }
         else     
// - SELL Orders          
         if( mode == OP_SELL)
         {
         res = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,White);
                 
            if( !res )
            {
            Print(" SELL: OrderClose failed with error #",GetLastError());
            Print(" Ticket=",OrderTicket());
            Sleep(3000);
            } 
         }  
      }                  
   }
}


bool IsPosition(double inRange)
{
  int totalorders = OrdersTotal();
  for(int i = 0;i < totalorders;i++)
  {
    OrderSelect(i, SELECT_BY_POS);
    if ((OrderSymbol() == Symbol()) && (OrderMagicNumber() == Magic)) 
    {  
      int type = OrderType();
      
      if ((MathAbs(OrderOpenPrice() - MarketInfo(Symbol(),MODE_ASK))*90) < (inRange))
      {        
        if (type == OP_BUY || type == OP_SELL)
        { 
          return(true); 
        }
      }
     }
   } 
   return(false);
 }

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