Lot increase

 

Hi, would you please help me with the code for increasing lot if market  order closed with a loss and lot to stay the same if market order closed with profit.

for an example if EA start with (0.1 lot ) to trade, lot must increase to 0.2 if market order closed with a loss and to 0.3 lot if market order still closed in a loss. But lot must stay as 0.1 lot as long as market order closed with profit.

I will be glad if I can get a tip  on how to solve such a problem.

Thanks 

 
Velly123:

Hi, would you please help me with the code for increasing lot if market  order closed with a loss and lot to stay the same if market order closed with profit.

for an example if EA start with (0.1 lot ) to trade, lot must increase to 0.2 if market order closed with a loss and to 0.3 lot if market order still closed in a loss. But lot must stay as 0.1 lot as long as market order closed with profit.

I will be glad if I can get a tip  on how to solve such a problem.

Thanks 

Loop through the Order History to find the last closed order that matches your Symbol and Magic Number (if used),  then if OrderProfit() is negative change your position size for the next order . . .  what is the problem ?
 
Velly123:

Hi, would you please help me with the code for increasing lot if market  order closed with a loss and lot to stay the same if market order closed with profit.

for an example if EA start with (0.1 lot ) to trade, lot must increase to 0.2 if market order closed with a loss and to 0.3 lot if market order still closed in a loss. But lot must stay as 0.1 lot as long as market order closed with profit.

I will be glad if I can get a tip  on how to solve such a problem.

Thanks 


Hi Velly,

a comment which probably doesn't belong in this forum (as it does not deal with MQL): Martingale is [i]not[/i] an advisable strategy.  :)

You might want to reconsider.


Cheers,

P.

 
Velly123: Hi, would you please help me with the code for increasing lot if market  order closed with a loss

  1. Martingale strategy will kill your account eventually
  2. Martingale strategy goes back to the starting lot size on a win, not stay the the last size
  3. Since there are no slaves here, there are only three choices: Search for it, learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt and the nature of your problem.
 
RaptorUK:
Loop through the Order History to find the last closed order that matches your Symbol and Magic Number (if used),  then if OrderProfit() is negative change your position size for the next order . . .  what is the problem ?


Thank you very much RaptorUK that was helpful.
 
WHRoeder:

  1. Martingale strategy will kill your account eventually
  2. Martingale strategy goes back to the starting lot size on a win, not stay the the last size
  3. Since there are no slaves here, there are only three choices: Search for it, learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt and the nature of your problem.


I get it WHRoeder, I'm still learning. Anyway thanks for the advice.

 
PaladinFX:

Hi Velly,

a comment which probably doesn't belong in this forum (as it does not deal with MQL): Martingale is [i]not[/i] an advisable strategy.  :)

You might want to reconsider.


Cheers,

P.


Thank you.
 
Velly123:

Hi, would you please help me with the code for increasing lot if market  order closed with a loss and lot to stay the same if market order closed with profit.

for an example if EA start with (0.1 lot ) to trade, lot must increase to 0.2 if market order closed with a loss and to 0.3 lot if market order still closed in a loss. But lot must stay as 0.1 lot as long as market order closed with profit.

I will be glad if I can get a tip  on how to solve such a problem.

Thanks 


try this code.

//----------------------- Externals ----------------------------------------------------------------
extern string Money_Management  = "==== Money Management ====";
extern bool   MoneyManagement   = true;     // Auto lot size
extern double RiskFactor        = 10;       // Risk for money management (from 0.1 to 50)
extern double ManualLotSize     = 0.01;     // Manual lot size
extern bool   RecoveryLot       = false;    // Recovery lot if loss order
extern double MultiplierLot     = 2;        // Multiplier lot recovery
extern int    MagicNumber       = 777;      // Identifier
//===================================================================================================================//
int start()
{
double LotSize=CalcLots();
//---------
//your code......
//---------
return(0);
}
//===================================================================================================================//
//lot size
double CalcLots()
{
double MinLot=NormalizeDouble(MarketInfo(Symbol(),MODE_MINLOT),2);
double MaxLot=NormalizeDouble(MarketInfo(Symbol(),MODE_MAXLOT),2);
double LotStep=NormalizeDouble(MarketInfo(Symbol(),MODE_LOTSTEP),2);
double LotValue=MarketInfo(Symbol(),MODE_LOTSIZE);
double LotSize,Lot;
int LotDigit;
double Multiplier=1;
double LastLot=0;
//------------------------------------------------------
//Limit risk level
RiskFactor=MathMin(MathMax(RiskFactor,0.1),50);//Risk between 0.1 - 50
//------------------------------------------------------
//Auto lot size
if(MoneyManagement==true) LotSize=(AccountFreeMargin()/LotValue)*RiskFactor;
//------------------------------------------------------
//Manual lot size
if(MoneyManagement==false) LotSize=ManualLotSize;
//------------------------------------------------------
//Lot digit
if(LotStep==1)LotDigit=0;
if(LotStep==0.1)LotDigit=1;
if(LotStep==0.01)LotDigit=2;
//------------------------------------------------------
//Check last order for loss or profit
if(RecoveryLot==true)
{
for(int iCnt=0; iCnt<OrdersHistoryTotal(); iCnt++)
{
if(OrderSelect(iCnt,SELECT_BY_POS,MODE_HISTORY))
{
LastLot=OrderLots();
if((OrderMagicNumber()==MagicNumber)&&(OrderProfit()<0)) Multiplier=MultiplierLot;
if((OrderMagicNumber()==MagicNumber)&&(OrderProfit()>=0)) Multiplier=1;
}
}
}
//------------------------------------------------------
//lot size
if(Multiplier>1) Lot=NormalizeDouble(MathMin(MathMax(LastLot*Multiplier,MinLot),MaxLot),LotDigit);
else
Lot=NormalizeDouble(MathMin(MathMax(LotSize*Multiplier,MinLot),MaxLot),LotDigit);
//------------------------------------------------------
return(Lot);
}
 
pannek: try this code.

Do not try that code.

  1. Never use NormalizeDouble Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 forum
  2. if(LotStep==1)LotDigit=0;
    if(LotStep==0.1)LotDigit=1;
    if(LotStep==0.01)LotDigit=2;
    This assumes a lotStep a power of ten and fails for all others Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 forum
  3. Account free margin has nothing to do with risk. Wanted risk = account balance * percentage. Actual risk = lot size * (orderOpenPrice - ISL) * deltaPerPoint. Set your SL, then solve for lot size. THEN verify you have enough free margin at the most adverse excursion (i.e. SL) for ALL open orders to avoid a margin call. See my code
Reason: