Download MetaTrader 5
To add comments, please log in or register
All activity of the MQL5.community members is displayed on a single page!
windowshopr
230
windowshopr 2014.09.18 01:43 

Hey all,

Having some troubles implementing a lots management strategy. I'm basically just wanting to implement a risk per trade. I have a simple RSI EA that I'm trying to add it to. Found a code that looked simple enough, but I'm getting Error 4051 - invalid lots amount for OrderSend function. Any help would be greatly appreciated. I'm using a 5 digit broker.

extern int MagicNumber=10101;
extern double Lots =0.1;
extern double LotPercent = 2.0; // mean 2.0%
extern double RSIPeriod=4;
extern double Upper=80;
extern double Lower=20;
extern double StopLoss=10;
extern double TakeProfit=10;
extern int TrailingStop=0;
extern int Slippage=3;
//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+
int BarsCount = 0;

//-- pass variable
double LotSize;

void LotsCalc() {
   if(MarketInfo(Symbol(),MODE_MINLOT) == 0.1) {int LotsDigit = 1;}
   else if(MarketInfo(Symbol(),MODE_MINLOT) == 0.01) { LotsDigit = 2;}
   double MinLots = NormalizeDouble(MarketInfo(Symbol(),MODE_MINLOT),LotsDigit);
   double MaxLots = NormalizeDouble(MarketInfo(Symbol(),MODE_MAXLOT),LotsDigit);
   double AcFrMar = NormalizeDouble(AccountFreeMargin(),2);
   
   LotSize = (AcFrMar*(LotPercent/100))/1000;
   
   if(LotSize > MaxLots) LotSize = MaxLots;
   if(LotSize < MinLots) LotSize = MinLots; 
}

int start()
{
  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;
  
  double TheStopLoss=0;
  double TheTakeProfit=0;
  
  if (Bars > BarsCount)
  
  {
  
  if( TotalOrdersCount()==0 ) 
  {
     int result=0;
     if((iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,1)<Lower)&&(iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,0)>Lower)) // Here is your open buy rule
     {
        result=OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Blue);
        BarsCount = Bars;  
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
     if((iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,1)>Upper)&&(iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,0)<Upper)) // Here is your open Sell rule
     {
        result=OrderSend(Symbol(),OP_SELL,LotSize,Bid,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Red);
        BarsCount = Bars;
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
  }
  }
   return(0);
}

int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);
}
whroeder1
13596
whroeder1 2014.09.18 19:35  
  1. You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  2. Account Balance * percent = RISK = (OrderOpenPrice - OrderStopLoss)*DIR * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)
  3. Do NOT use TickValue by itself - DeltaPerlot
  4. You must normalize lots properly and check against min and max.
  5. You must also check FreeMargin to avoid stop out
/
To add comments, please log in or register