RandomEA_v8

Author: Pete
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
0 Views
0 Downloads
0 Favorites
RandomEA_v8
//+------------------------------------------------------------------+
//|                                                     RandomEA.mq4 |
//|                                                             Pete |
//|                                                  www.izimoney.ru |
//+------------------------------------------------------------------+
#property copyright "Pete"
#property link      "www.mql5.com"
#property version   "24.1"
#property strict
//--- input parameters
//input int      progon=100;      //for backtesting optimaze
input int      maxsprd=30;
input double   lot_persent=0.5; //lot in percent of balance
input int      tp_and_sl=100;   //takeprofit and stoploss
input double   k=2.1;           //martingale koficient
input int      slip=10;         //slipage

double minlot,maxlot,lot,acc=0,lot2;
bool b;
int h;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   MathSrand(GetTickCount());   //turn on randomize function
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

   minlot=MarketInfo(Symbol(),MODE_MINLOT);                        //
   maxlot=MarketInfo(Symbol(),MODE_MAXLOT);                        //
   lot=NormalizeDouble(lot_persent*AccountBalance()/100000,2);     // calculating lots
   if(lot<minlot){lot=minlot;}                                     //
   if(lot>maxlot){lot=maxlot;}                                     //
   lot=NormalizeDouble(lot,2);                                     //

   
   
   
   
   //if there are no orders open trade according signal
   if(OrdersTotal()==0)
     {
     
      if (maxsprd<MarketInfo(Symbol(),MODE_SPREAD)){return;}
     
      if(signal()==1){if(AccountBalance()<acc){lot=lot2*k;}buy_(lot);return;}

      if(signal()==-1){if(AccountBalance()<acc){lot=lot2*k;}sell_(lot);return;}
     }

   
   // if there is one order we are turn take profit and stop loss on
   if(OrdersTotal()==1)
     {
      acc=AccountBalance();
      b=OrderSelect(0,SELECT_BY_POS);
      lot2=OrderLots();
      if((OrderStopLoss()==0)&&(OrderType()==OP_BUY)) 
         {b=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-tp_and_sl*Point,Digits),NormalizeDouble(OrderOpenPrice()+tp_and_sl*Point,Digits),0,Blue);}
      if((OrderStopLoss()==0)&&(OrderType()==OP_SELL))
         {b=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+tp_and_sl*Point,Digits),NormalizeDouble(OrderOpenPrice()-tp_and_sl*Point,Digits),0,Red);}

     }
   
   
   // if there are more than one trade is on then close all positions (becouse some error with it)
   if(OrdersTotal()>1){CloseAll();}


  }
//+------------------------------------------------------------------+

int signal() //return random signal , buy or sell
  {
   int r0;
   double r1;

   r0=TimeCurrent();
   r1=MathMod(r0,2);
   if(r1!=0){return(-1);}
   if(r1==0){return(1);}
   return(0);
  }
//*****************************************************************

//+------------------------------------------------------------------+
void buy_(double lot998) // opening buy function
  {
   lot=NormalizeDouble(lot998,2);
//   sl_=NormalizeDouble(Ask-sl*Point,Digits);
//   tp_=NormalizeDouble(Ask+tp*Point,Digits);
   b=OrderSend(Symbol(),OP_BUY,NormalizeDouble(lot,2),Ask,slip,0,0,"",0,0,Blue);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void sell_(double lot999) //opening sell function
  {
   lot=NormalizeDouble(lot999,2);
//   sl_=NormalizeDouble(Bid+sl*Point,Digits);
//   tp_=NormalizeDouble(Bid-tp*Point,Digits);
   b=OrderSend(Symbol(),OP_SELL,NormalizeDouble(lot,2),Bid,slip,0,0,"",0,0,Red);
  }
//*************************************************************

void CloseAll() // close all trades function
  {
   int i,tik;
   i=OrdersTotal();
   while(i!=0)
     {
      b=OrderSelect(i-1,SELECT_BY_POS);
      if(b==true)
        {
         tik=OrderTicket();
         if(OrderType()==OP_BUY) {b=OrderClose(OrderTicket(),OrderLots(),Ask,slip,clrNONE);}
         if(OrderType()==OP_SELL){b=OrderClose(OrderTicket(),OrderLots(),Bid,slip,clrNONE);}
         if((OrderType()==OP_BUYSTOP) || (OrderType()==OP_SELLSTOP) || (OrderType()==OP_BUYLIMIT) || (OrderType()==OP_SELLLIMIT)){b=OrderDelete(tik,clrNONE);}
        }
      i=i-1;
     }
  }

//+------------------------------------------------------------------+

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