Перейти к содержимому


Фотография

Увеличение лота при увеличении депозита


  • Please log in to reply
3 ответы в этой теме

#1 zlorik

zlorik

    мало говорим

  • Пользователь
  • 4 Сообщений:

Отправлено 07 November 2012 - 02:06 PM

В общем, нужно сделать так, чтобы советник выставлял лот в зависимости от баланса.


#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

extern string MAF="---------------- MA Filter";
extern double Ma_Period=200;
extern double Ma_Mode=1; //0=sma, 1=ema, 2=smoothed, 3=linear weighted
extern string DBO="---------------- Distance between orders";
extern double step=25;
extern int StepMode=0;
extern string MAR="---------------- Martingale";
extern bool Use_with_win=true;
extern bool Use_with_loss=true;
extern double mult=1.5;
extern double lotsbuy=0.01;
extern double lotssell=0.01; 
extern string SLTP="---------------- SL TP";
extern double TP=1000;
extern double SL=1000;
extern double proffactor=10;
extern string STO="---------------- Stochastic";
extern double per_K=200;
extern double per_D=20;
extern double slow=20;
extern string STOL="---------------- Stoch Levels";
extern double zoneBUY=50;
extern double zoneSELL=50;
extern string EXT="---------------- Extras";
extern int MaxOpenBuyOrders=100; //1=disabled
extern int MaxOpenSellOrders=100; //1=disabled
extern int StopIfEquityOver=15000; //max profit in pips allowed per day
extern int Slippage=3;
extern bool Use_MaxSpread=false;
extern int MaxSpread=2; //max spread
double Magicbuy=555;
double Magicsell=556;
double openpricebuy,openpricesell,lotsbuy2,lotssell2,lastlotbuy,lastlotsell,tpb,tps,cnt,smbuy,smsell,lotstep,
ticketbuy,ticketsell,maxLot,free,balance,lotsell,lotbuy,dig,sig_buy,sig_sell,ask,bid;                           
double Balance=0.0;int err=0;

int OrdersTotalMagicbuy(int Magicbuy){
    int j=0;int r;
    for(r=0;r<OrdersTotal();r++){
        if(OrderSelect(r,SELECT_BY_POS,MODE_TRADES)){
          if(OrderMagicNumber()==Magicbuy) j++;
          }
        }   
    return(j); 
}

int OrdersTotalMagicsell(int Magicsell){
    int d=0;int n;
    for(n=0;n<OrdersTotal();n++){
        if(OrderSelect(n,SELECT_BY_POS,MODE_TRADES)){
          if(OrderMagicNumber()==Magicsell) d++;
        }
    }    
    return(d);
}     

int orderclosebuy(int ticketbuy){
    string symbol = Symbol();int cnt;
    for(cnt = OrdersTotal(); cnt >= 0; cnt--){
        OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);       
        if(OrderSymbol() == symbol && OrderMagicNumber()==Magicbuy) {
           ticketbuy=OrderTicket();OrderSelect(ticketbuy, SELECT_BY_TICKET, MODE_TRADES);lotsbuy2=OrderLots() ;                         
           double bid = MarketInfo(symbol,MODE_BID); 
           RefreshRates();
           OrderClose(ticketbuy,lotsbuy2,bid,3,Magenta); 
        }
    }
    lotsbuy2=lotsbuy;return(0);
} 

int orderclosesell(int ticketsell){
    string symbol = Symbol();int cnt;   
    for(cnt = OrdersTotal(); cnt >= 0; cnt--){
        OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);       
        if(OrderSymbol() == symbol && OrderMagicNumber()==Magicsell) {
           ticketsell=OrderTicket();OrderSelect(ticketsell, SELECT_BY_TICKET, MODE_TRADES);lotssell2=OrderLots() ;                         
           double ask = MarketInfo(symbol,MODE_ASK); 
           RefreshRates();
           OrderClose(ticketsell,lotssell2,ask,3, Lime); 
        }
    }
    lotssell2=lotssell;return(0); 
}

int start(){
//----
    if(Balance!=0.0){
       if(Use_with_win==false){if(Balance<AccountBalance())mult=1;}
       else if(Use_with_loss==false){if(Balance>AccountBalance())mult=1;}
       else mult=mult;
    }Balance=AccountBalance();
    if(Use_MaxSpread==true)MaxSpread=MaxSpread; else if(Use_MaxSpread==false)MaxSpread=100;
    double MA=iMA(Symbol(),0,Ma_Period,0,Ma_Mode,PRICE_CLOSE,1); double profitbuy=0;double profitsell=0;string symbol = OrderSymbol();
    double spread = MarketInfo(symbol,MODE_SPREAD);double minLot = MarketInfo(symbol,MODE_MINLOT);
    if (minLot==0.01){dig=2;maxLot=MarketInfo(symbol,MODE_MAXLOT);}
    if (minLot==0.1){dig=1;maxLot=((AccountBalance()/2)/1000);}
    if(AccountEquity()<=StopIfEquityOver) {
    if(OrdersTotalMagicbuy(Magicbuy)>0){
       double smbuy;
       for(cnt=0;cnt<OrdersTotal();cnt++){
            OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
            if(OrderSymbol() == Symbol() && OrderMagicNumber () == Magicbuy) {
               ticketbuy = OrderTicket();OrderSelect(ticketbuy,SELECT_BY_TICKET, MODE_TRADES);
               smbuy = smbuy+OrderLots();openpricebuy = OrderOpenPrice();lastlotbuy = OrderLots();
            }
       }
       {   
       if (smbuy+(NormalizeDouble((lastlotbuy*mult),dig))<maxLot){     
           if(StepMode==0){
              if(Ask<=openpricebuy-step*Point && Ask>MA && OrdersTotalMagicbuy(Magicbuy)<MaxOpenBuyOrders && (Ask-Bid)<MaxSpread){
                 lotsbuy2=lastlotbuy*mult;
                 RefreshRates();ticketbuy=OrderSend(Symbol(),OP_BUY,NormalizeDouble(lotsbuy2,dig),Ask,Slippage,Ask - SL * Point, Ask + TP * Point,"MartingailExpert",Magicbuy,0,Blue);
              }
           }
           if(StepMode==1){
              if(Ask<=openpricebuy-(step+OrdersTotalMagicbuy(Magicbuy)+OrdersTotalMagicbuy(Magicbuy)-2)*Point && Ask>MA && OrdersTotalMagicbuy(Magicbuy)<MaxOpenBuyOrders && (Ask-Bid)<MaxSpread){
                 lotsbuy2=lastlotbuy*mult;
                 RefreshRates();ticketbuy=OrderSend(Symbol(),OP_BUY,NormalizeDouble(lotsbuy2,dig),Ask,Slippage,Ask - SL * Point, Ask + TP * Point,"MartingailExpert",Magicbuy,0,Blue);
              } 
           }
       }
       }
    }
    if(OrdersTotalMagicsell(Magicsell)>0){
       double smsell;
       for(cnt=0;cnt<OrdersTotal();cnt++){
           OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
           if(OrderSymbol() == Symbol() && OrderMagicNumber () == Magicsell){
              ticketsell = OrderTicket();OrderSelect(ticketsell,SELECT_BY_TICKET, MODE_TRADES);
              smsell = smsell + OrderLots();openpricesell = OrderOpenPrice();lastlotsell = OrderLots();
           }     
       }
       {
       if(smsell+(NormalizeDouble((lastlotsell*mult),dig))<maxLot){
          if(StepMode==0){
             if(Bid>=openpricesell+step*Point && Bid<MA && OrdersTotalMagicsell(Magicsell)<MaxOpenSellOrders && (Ask-Bid)<MaxSpread){
                lotssell2=lastlotsell*mult;
                RefreshRates();ticketsell=OrderSend(Symbol(),OP_SELL,NormalizeDouble(lotssell2,dig),Bid,Slippage,Bid + SL * Point,Bid - TP * Point,"MartingailExpert",Magicsell,0,Red);
             }
          }
          if(StepMode==1){
             if(Bid>=openpricesell+(step+OrdersTotalMagicsell(Magicsell)+OrdersTotalMagicsell(Magicsell)-2)*Point && Bid<MA && OrdersTotalMagicsell(Magicsell)<MaxOpenSellOrders && (Ask-Bid)<MaxSpread){
                lotssell2=lastlotsell*mult;
                RefreshRates();ticketsell=OrderSend(Symbol(),OP_SELL,NormalizeDouble(lotssell2,dig),Bid,Slippage,Bid + SL * Point,Bid - TP * Point,"MartingailExpert",Magicsell,0,Red);
             }
          }
       }
    }}  
}

if(AccountEquity()<=StopIfEquityOver) {
if(Use_MaxSpread==true)MaxSpread=MaxSpread; else if(Use_MaxSpread==false)MaxSpread=100;
if(OrdersTotalMagicbuy(Magicbuy)<1){ 
   if(iStochastic(NULL,0,per_K,per_D,slow,MODE_LWMA,1,0,1)>iStochastic(NULL,0,per_K,per_D,slow,MODE_LWMA,1,1,1)
   && iStochastic(NULL,0,per_K,per_D,slow,MODE_LWMA,1,1,1)>zoneBUY && Ask>MA && (Ask-Bid)<MaxSpread)ticketbuy = OrderSend(Symbol(),OP_BUY,lotsbuy,Ask,Slippage,Ask - SL * Point, Ask + TP * Point,"MartingailExpert",Magicbuy,0,Blue);
}
if(OrdersTotalMagicsell(Magicsell)<1){
   if(iStochastic(NULL,0,per_K,per_D,slow,MODE_LWMA,1,0,1)<iStochastic(NULL,0,per_K,per_D,slow,MODE_LWMA,1,1,1)
   && iStochastic(NULL,0,per_K,per_D,slow,MODE_LWMA,1,1,1)<zoneSELL && Bid<MA && (Ask-Bid)<MaxSpread)ticketsell = OrderSend(Symbol(),OP_SELL,lotssell,Bid,Slippage,Bid + SL * Point,Bid - TP * Point,"MartingailExpert",Magicsell,0,Red);
}}

for(cnt=0;cnt<OrdersTotal();cnt++){
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if(OrderSymbol()==Symbol() && OrderMagicNumber () == Magicbuy){
       ticketbuy = OrderTicket();OrderSelect(ticketbuy,SELECT_BY_TICKET, MODE_TRADES);profitbuy = profitbuy+OrderProfit() ;
       openpricebuy = OrderOpenPrice();
    }
}  

tpb = (OrdersTotalMagicbuy(Magicbuy)*proffactor*Point)+openpricebuy;
double bid = MarketInfo(Symbol(),MODE_BID);
if(profitbuy>0 || AccountEquity()>=StopIfEquityOver){
   if (Bid>=tpb) orderclosebuy(ticketbuy);
}
for(cnt=0;cnt<OrdersTotal();cnt++){   
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magicsell){
        ticketsell = OrderTicket();OrderSelect(ticketsell,SELECT_BY_TICKET, MODE_TRADES);profitsell = profitsell+OrderProfit();
        openpricesell = OrderOpenPrice(); 
    }
}
tps = openpricesell-(OrdersTotalMagicsell(Magicsell)*proffactor*Point);
double ask = MarketInfo(Symbol(),MODE_ASK);    
if(profitsell>0 || AccountEquity()>=StopIfEquityOver){
   if (Ask<=tps)orderclosesell(ticketsell);    
}
free = AccountFreeMargin();balance = AccountBalance();    
for(cnt=0;cnt< OrdersTotal();cnt++){   
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if(OrderSymbol()==Symbol() && OrderMagicNumber () == Magicbuy)  ticketbuy = OrderTicket();
    if(OrderSymbol()==Symbol() && OrderMagicNumber () == Magicsell) ticketsell = OrderTicket();
}
if(OrdersTotalMagicbuy(Magicbuy)==0){
   profitbuy=0;ticketbuy=0;tpb=0;
}
if (OrdersTotalMagicsell(Magicsell)==0){
    profitsell=0;ticketsell=0;tps=0;
}
Comment("FreeMargin = ",NormalizeDouble(free,0),"  Balance = ",NormalizeDouble(balance,0),"  maxLot = ",NormalizeDouble(maxLot,dig),"\n",
"Totalbuy = ",OrdersTotalMagicbuy(Magicbuy),"  Lot = ",smbuy,"  Totalsell = ",OrdersTotalMagicsell(Magicsell),"  Lot = ",smsell,"\n",
"---------------------------------------------------------------","\n","Profitbuy = ",profitbuy,"\n",
"Profitsell = ",profitsell);
//----
for(int ii=0; ii<2; ii+=2){
    ObjectDelete("rect"+ii);
    ObjectCreate("rect"+ii,OBJ_HLINE, 0, 0,tps);
    ObjectSet("rect"+ii, OBJPROP_COLOR, Red);
    ObjectSet("rect"+ii, OBJPROP_WIDTH, 1);
    ObjectSet("rect"+ii, OBJPROP_RAY, False);
}    
for(int rr=0; rr<2; rr+=2){
    ObjectDelete("rect1"+rr);
    ObjectCreate("rect1"+rr,OBJ_HLINE, 0, 0,tpb);      
    ObjectSet("rect1"+rr, OBJPROP_COLOR, Blue);
    ObjectSet("rect1"+rr, OBJPROP_WIDTH, 1);
    ObjectSet("rect1"+rr, OBJPROP_RAY, False);     
}
if(ticketbuy<0||ticketsell<0){if (GetLastError()==134){err=1;Print("NOT ENOGUGHT MONEY!!");}return (-1);}

return(0);
}  
//+------------------------------------------------------------------+

  • zlorik это понравилось

#2 Guest_Кир.илл_*

Guest_Кир.илл_*
  • Гости

Отправлено 08 November 2012 - 01:06 PM

Приветсвтую!
Делается это так:

extern string  L1                   = "Настройки Larry Williams Method";
extern bool    LarryWilliamsMethod  = false; // использовать метод управления капиталом Ларри Вильямса
extern double  LWrisk               = 10;    // [%] процент риска. определяется трейдером. рекомендуется от 5% до 20%
extern double  LWpart               = 100;   // [%] какая часть депозита отводится в распоряжение данного робота
extern double  LossMax              = 1000;  // Макс. потеря на 1лот. В USD. PS: если валюта депозита != USD - надо переделывать
extern string  L2                   = "-------------------------------";

//--------------------------------------- LarryWilliams ---------------------------------
double LarryWilliams()
{
   double xlots;
   double LotMin  = MarketInfo(Symbol(), MODE_MINLOT);
   double LotMax  = MarketInfo(Symbol(), MODE_MAXLOT);
   double LotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
   xlots  = (LWpart / 100) * AccountBalance() * (LWrisk / 100) / LossMax;
   xlots = MathRound(xlots / LotStep)  * LotStep;
   if(xlots == 0) Print("Not enough funds to open order");
   else if(NormalizeDouble(xlots-LotMin,2) < 0) {Print("Rounding lot up to LotMin"); xlots = LotMin;}
   else if(NormalizeDouble(xlots-LotMax,2) > 0) {Print("Rounding lot down to LotMax"); xlots = LotMax;}
   return (xlots);
}

Очень подробный разбор теории и этого содержится в серии уроков "Управление Капиталом" в Курсе MQL4 на нашем форуме:
http://www.fx4u.ru/t...post__p__133983
http://www.fx4u.ru/t...post__p__134926

С уважением,
Кирилл

#3 zlorik

zlorik

    мало говорим

  • Пользователь
  • 4 Сообщений:

Отправлено 08 November 2012 - 03:18 PM

Большое спасибо :ok:
  • zlorik это понравилось

#4 Guest_Кир.илл_*

Guest_Кир.илл_*
  • Гости

Отправлено 11 December 2012 - 11:36 AM

Большое спасибо :ok:


Здравсуйте!
Рад был помочь :drinks:
Хотел поинтересоваться - у Вас все получилось? Нужна еще помощь с этим кодом?

Кирилл




0 человек читают эту тему

0 пользователей, 0 гостей, 0 скрытых пользователей