Чувак Хачинбек
Чувак Хачинбек личный блог
01 февраля 2023, 22:37

Исходный код рабочего торгового робота на mql4

//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
#property version   "1.00"
//+------------------------------------------------------------------+
input double    Risk             =1;         //          //
input double    Exponenta        =1.3;
input double    TPproc           =0.2;
input int       Step             =5; 
input int       n                =100; 
input int       Magic            =2017; 
//+------------------------------------------------------------------+
string comment ="System";
int r, D;
datetime NewBar =0;
double NewLot;
//+------------------------------------------------------------------+
int OnInit(){
D=1;
if (Digits==5 || Digits==3)D=10;
return(INIT_SUCCEEDED);}
//+------------------------------------------------------------------+
void OnDeinit(const int reason){}
//+------------------------------------------------------------------+
void OnTick(){
//+------------------------------------------------------------------+
double Lot=0;
Lot=NormalizeDouble(AccountBalance()/100*Risk/(MarketInfo(Symbol(),MODE_TICKVALUE)*100*D),2);
if (Lot<MarketInfo(Symbol(),MODE_MINLOT))Lot=MarketInfo(Symbol(),MODE_MINLOT);
//+------------------------------------------------------------------+
if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.01) int dig =2;
if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.10)     dig =1;
if(MarketInfo(Symbol(),MODE_LOTSTEP)==1.00)     dig =0;
//+------------------------------------------------------------------+
if(NewBar!= iTime(Symbol(),0,0) ) 
{NewBar = iTime(Symbol(),0,0) ;
//+------------------------------------------------------------------+
bool Sell=false; bool Buy=false;
if(Open[n+1]<Close[n+1] && Open[n]<Close[n]) {Buy=true;}
if(Open[n+1]>Close[n+1] && Open[n]>Close[n]) {Sell=true;}
//+------------------------------------------------------------------+
bool minus=false, plus=false;

if(LastProfit()<0)
{minus=true;}
if(LastProfit()>=0)
{plus=true;}
//+------------------------------------------------------------------+
if(plus || CountH(-1)==0)
{NewLot=Lot;}

if(minus && CountH(-1)>0)
{NewLot=NormalizeDouble(LastLot()*Exponenta, dig);}
//+------------------------------------------------------------------+
double P_Max=(AccountBalance()/100)*TPproc;

if(Count(OP_SELL)==0 && Sell && LastType()!=OP_SELL && (Profit(OP_BUY)>P_Max*Count(OP_BUY) || CountH(-1)==0))
  {r=OrderSend(Symbol(),OP_SELL,NewLot,Bid,10,0,0,comment,Magic,0,Red);
  CloseBuy();}
if(Count(OP_BUY)==0 && Buy && LastType()!=OP_BUY && (Profit(OP_SELL)>P_Max*Count(OP_SELL) || CountH(-1)==0))
  {r=OrderSend(Symbol(),OP_BUY,NewLot,Ask,10,0,0,comment,Magic,0,Green);
  CloseSell();}
//+------------------------------------------------------------------+
if(Count(OP_BUY)>0 && Ask+Step*D*Point<=BuyPric())
   {r=OrderSend(Symbol(),OP_BUY,NewLot,Ask,10,0,0,comment,Magic,0,Green);}  
if(Count(OP_SELL)>0 && Bid-Step*D*Point>=SellPric())
   {r=OrderSend(Symbol(),OP_SELL,NewLot,Bid,10,0,0,comment,Magic,0,Red);}
//+------------------------------------------------------------------+
}}
//+------------------------------------------------------------------+ 
//| Считаем количество ордеров по типу                               | 
//+------------------------------------------------------------------+ 
int Count(int type)
{int count=0;
 for(int i=OrdersTotal()-1;i>=0;i--)
 if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
  {if(Symbol()==OrderSymbol() && Magic==OrderMagicNumber() && (type==-1 || OrderType()==type)) count++;}
   return(count);}
//+------------------------------------------------------------------+ 
//| Функция закрытия ордеров                                         |
//+------------------------------------------------------------------+
void CloseBuy()
{double priceB;
for(int i=OrdersTotal()-1;i>=0;i--)
{if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{if(Symbol()==OrderSymbol() && OrderType()==OP_BUY && Magic==OrderMagicNumber())
{priceB=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID), Digits);
bool clos=OrderClose(OrderTicket(),OrderLots(),priceB,100,0);}}}
return;}
//+------------------------------------------------------------------+
void CloseSell()
{double priceS;
for(int i=OrdersTotal()-1;i>=0;i--)
{if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{if(Symbol()==OrderSymbol() && OrderType()==OP_SELL && Magic==OrderMagicNumber())
{priceS=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK), Digits);
bool clos=OrderClose(OrderTicket(),OrderLots(),priceS,100,0);}}}
return;}
//+------------------------------------------------------------------+ 
//| Определяем тип последнего ордера                                 | 
//+------------------------------------------------------------------+ 
int LastType()
{int type=-1;
datetime dt=0;
for(int i=OrdersTotal()-1;i>=0;i--)
 if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
  {if(Symbol()==OrderSymbol() && OrderMagicNumber()==Magic) 
   {if(OrderOpenTime()>dt)
   {dt=OrderOpenTime();
    type=OrderType();}}}
return(type);}
//+------------------------------------------------------------------+ 
//| Определяем лот последнего ордера                                 | 
//+------------------------------------------------------------------+ 
double LastLot()
{int type=-1;
double lots;
datetime dt=0;
for(int i=OrdersTotal()-1;i>=0;i--)
 if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
  {if(Symbol()==OrderSymbol() && OrderMagicNumber()==Magic) 
   {if(OrderOpenTime()>dt)
   {dt=OrderOpenTime();
    type=OrderType();
    lots=OrderLots();}}}
return(lots);}
//+------------------------------------------------------------------+ 
//| Определяем профит последнего ордера                                 | 
//+------------------------------------------------------------------+ 
double LastProfit()
{int type=-1;
double profit;
datetime dt=0;
for(int i=OrdersTotal()-1;i>=0;i--)
 if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
  {if(Symbol()==OrderSymbol() && OrderMagicNumber()==Magic) 
   {if(OrderOpenTime()>dt)
   {dt=OrderOpenTime();
    type=OrderType();
    profit=OrderProfit();}}}
return(profit);}
//+------------------------------------------------------------------+ 
//| Определяем цену последнего ордера бай                            | 
//+------------------------------------------------------------------+ 
double BuyPric() {
   double oldorderopenprice;
   int oldticketnumber;
   double unused = 0;
   int ticketnumber = 0;
   for (int cnt = OrdersTotal() - 1; cnt >= 0; cnt--) {
      bool clos=OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == OP_BUY) {
         oldticketnumber = OrderTicket();
         if (oldticketnumber > ticketnumber) {
            oldorderopenprice = OrderOpenPrice();
            unused = oldorderopenprice;
            ticketnumber = oldticketnumber;}}}
   return (oldorderopenprice);}
//+------------------------------------------------------------------+ 
//| Определяем цену последнего ордера селл                           | 
//+------------------------------------------------------------------+ 
double SellPric() {
   double oldorderopenprice;
   int oldticketnumber;
   double unused = 0;
   int ticketnumber = 0;
   for (int cnt = OrdersTotal() - 1; cnt >= 0; cnt--) {
      bool clos=OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == OP_SELL) {
         oldticketnumber = OrderTicket();
         if (oldticketnumber > ticketnumber) {
            oldorderopenprice = OrderOpenPrice();
            unused = oldorderopenprice;
            ticketnumber = oldticketnumber;}}}
   return (oldorderopenprice);}
//+------------------------------------------------------------------+ 
int CountH(int type)
{int count=0;
 for(int i=OrdersHistoryTotal()-1;i>=0;i--)
 if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
  {if(Symbol()==OrderSymbol() && Magic==OrderMagicNumber() && (type==-1 || OrderType()==type)) count++;}
   return(count);}
//--------------------------------------------------------------------+
double Profit(int type) 
{double Profit = 0;
   for (int cnt = OrdersTotal() - 1; cnt >= 0; cnt--) {
      if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
      {if (Symbol()==OrderSymbol() && OrderMagicNumber()==Magic && (OrderType() == type || type==-1)) Profit += OrderProfit()+OrderSwap()+OrderCommission();}}
       return (Profit);} 
3 Комментария

Активные форумы
Что сейчас обсуждают

Старый дизайн
Старый
дизайн