Need help using % of equity rather than a % of free margin, code included..

 

I need to make this order function use a % of the AccountEquity rather than a % of the AccountFreeMargin. This code allows the risk to be adjusted depending on a win/loss, but calculates risk depending on free margin when I need it to calculate based on equity. In the 2nd post of this thread I have posted an example of an order function using a % of equity, but the % is fixed and does not have the if "profit >|< 0" code as per the code below.

void BuyOrderRiskMgm4()
{
    double profit = 0;
    datetime lastCloseTime = 0;
    int cnt = OrdersHistoryTotal();
    for (int i=0; i < cnt; i++)
    {
        if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == 1 && lastCloseTime < OrderCloseTime())
        {
            lastCloseTime = OrderCloseTime();
            profit = OrderProfit();
            CurrentBuyRisk4 = (AccountFreeMargin()/ 100 * CurrentBuyRisk4 * 0.001);
        }
    }
    
    if (profit > 0)
    {
        CurrentBuyRisk4 = CurrentBuyRisk4 * RiskBuyMpOnProfit4 + RiskBuyChOnProfit4;
        if (RiskResetOnProfit4)
        CurrentBuyRisk4 = BuyRisk4;
    }
    else if (profit < 0)
    {
        CurrentBuyRisk4 = CurrentBuyRisk4 * RiskBuyMpOnLoss4 + RiskBuyChOnLoss4;
        if (RiskResetOnLoss4) CurrentBuyRisk4 = BuyRisk4;
    }
    if (CurrentBuyRisk4 > MaxBuyRisk4)
    {
        CurrentBuyRisk4 = MaxBuyRisk4;
    }
    // calculate lot size based on current risk
    double lotvalue = 0.001;
    double minilot = MarketInfo(Symbol(), MODE_MINLOT);
    int powerscount = 0;
    while (minilot < 1)
    {
        minilot = minilot * MathPow(10, powerscount);
        powerscount++;
    }
    lotvalue = NormalizeDouble(AccountFreeMargin() / 100 * CurrentBuyRisk4 * 0.001, powerscount - 1);
    
    if (lotvalue < MarketInfo(Symbol(), MODE_MINLOT))    // make sure lot is not smaller than allowed value
    {
        lotvalue = MarketInfo(Symbol(), MODE_MINLOT);
    }
    if (lotvalue > MarketInfo(Symbol(), MODE_MAXLOT))    // make sure lot is not greater than allowed value
    {
        lotvalue = MarketInfo(Symbol(), MODE_MAXLOT);
    }
    double SL = Ask - BuyStoploss4*PipValue*Point;
    if (BuyStoploss4 == 0) SL = 0;
    double TP = Ask + BuyTakeprofit4*PipValue*Point;
    if (BuyTakeprofit4 == 0) TP = 0;
    FirstBuyRiskMgm4 = false;
    int ticket = -1;
    if (true)
    ticket = OrderSend(Symbol(), OP_BUY, lotvalue, Ask, 4, 0, 0, "My Expert", 1, 0, Blue);
    else
    ticket = OrderSend(Symbol(), OP_BUY, lotvalue, Ask, 4, SL, TP, "My Expert", 1, 0, Blue);
    if (ticket > -1)
    {
        if (true)
        {
            OrderSelect(ticket, SELECT_BY_TICKET);
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, Blue);
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
            
    }
    else
    {
        Print("OrderSend() error - ", ErrorDescription(GetLastError()));
    }
}

 

This code will place an order with a % of equity used, but it is only for fixed %. A need to be able to increase the risk depending on win/loss as per the code in the first post (that code would be perfect but it uses a % of free margin instead of equity). I believe just a little chop and changing is required but I have tried for a few days now to no avail. I have been trying to squeeze "if profit <|> 0" section of the code above but no luck as of yet so looking for some help.

void BuyOrderRiskFixed2()
{
    double lotsize = MarketInfo(Symbol(),MODE_LOTSIZE) / AccountLeverage();
    double pipsize = 1 * 10;
    double maxlots = AccountFreeMargin() / 100 * BalanceRiskPercent2 / lotsize * pipsize;
    if (BuyStoploss2 == 0) Print("OrderSend() error - stoploss can not be zero");
    double lots = maxlots / BuyStoploss2 * 10;
    
    // calculate lot size based on current risk
    double lotvalue = 0.001;
    double minilot = MarketInfo(Symbol(), MODE_MINLOT);
    int powerscount = 0;
    while (minilot < 1)
    {
        minilot = minilot * MathPow(10, powerscount);
        powerscount++;
    }
    lotvalue = NormalizeDouble(lots, powerscount - 1);
    
    if (lotvalue < MarketInfo(Symbol(), MODE_MINLOT))    // make sure lot is not smaller than allowed value
    {
        lotvalue = MarketInfo(Symbol(), MODE_MINLOT);
    }
    if (lotvalue > MarketInfo(Symbol(), MODE_MAXLOT))    // make sure lot is not greater than allowed value
    {
        lotvalue = MarketInfo(Symbol(), MODE_MAXLOT);
    }
    double SL = Ask - BuyStoploss2*PipValue*Point;
    if (BuyStoploss2 == 0) SL = 0;
    double TP = Ask + BuyTakeprofit2*PipValue*Point;
    if (BuyTakeprofit2 == 0) TP = 0;
    
    int ticket = -1;
    if (true)
    ticket = OrderSend(Symbol(), OP_BUY, lotvalue, Ask, 4, 0, 0, "My Expert", 1, 0, Blue);
    else
    ticket = OrderSend(Symbol(), OP_BUY, lotvalue, Ask, 4, SL, TP, "My Expert", 1, 0, Blue);
    if (ticket > -1)
    {
        if (true)
        {
            OrderSelect(ticket, SELECT_BY_TICKET);
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, Blue);
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
            
    }
    else
    {
        Print("OrderSend() error - ", ErrorDescription(GetLastError()));
    }
}
 

After hours of trying I have finally solved my own problem. I will share the solution below just incase it will benefit someone in the future:

void BuyOrderRiskFixed2()
{
    double profit = 0;
    datetime lastCloseTime = 0;
    int cnt = OrdersHistoryTotal();
    for (int i=0; i < cnt; i++)
    {
        if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && lastCloseTime < OrderCloseTime())
        {
            lastCloseTime = OrderCloseTime();
            profit = OrderProfit();
        }
    }
    if (profit > 0)
    {
        CurrentBuyRisk4 = CurrentBuyRisk4 * RiskBuyMpOnProfit4 + RiskBuyChOnProfit4;
        if (RiskResetOnProfit4)
        CurrentBuyRisk4 = RiskPercent;
    }
    else if (profit < 0)
    {
        CurrentBuyRisk4 = CurrentBuyRisk4 * RiskBuyMpOnLoss4 + RiskBuyChOnLoss4;
        if (RiskResetOnLoss4) CurrentBuyRisk4 = RiskPercent;
    }
    if (CurrentBuyRisk4 > MaxBuyRisk4)
    {
        CurrentBuyRisk4 = MaxBuyRisk4;
    }
    double lotsize = MarketInfo(Symbol(),MODE_LOTSIZE) / 1;
    double pipsize = 100 * 10;
    double maxlots = AccountEquity() / 100 * CurrentBuyRisk4 / lotsize * pipsize;
    if (StopLoss == 0) Print("OrderSend() error - stoploss can not be zero");
    double lots = maxlots / StopLoss * 10;
    
    // calculate lot size based on current risk
    double lotvalue = 0.001;
    double minilot = MarketInfo(Symbol(), MODE_MINLOT);
    int powerscount = 0;
    while (minilot < 1)
    {
        minilot = minilot * MathPow(10, powerscount);
        powerscount++;
    }
    lotvalue = NormalizeDouble(lots, powerscount - 1);
    
    if (lotvalue < MarketInfo(Symbol(), MODE_MINLOT))    // make sure lot is not smaller than allowed value
    {
        lotvalue = MarketInfo(Symbol(), MODE_MINLOT);
    }
    if (lotvalue > MarketInfo(Symbol(), MODE_MAXLOT))    // make sure lot is not greater than allowed value
    {
        lotvalue = MarketInfo(Symbol(), MODE_MAXLOT);
    }
    double SL = Ask - StopLoss*PipValue*Point;
    if (StopLoss == 0) SL = 0;
    double TP = Ask + TakeProfit*PipValue*Point;
    if (TakeProfit == 0) TP = 0;
    
    int ticket = -1;
    if (true)
    ticket = OrderSend(Symbol(), OP_BUY, lotvalue, Ask, Slippage, 0, 0, ExpertName, MagicNumber, 0, Blue);
    else
    ticket = OrderSend(Symbol(), OP_BUY, lotvalue, Ask, Slippage, SL, TP, ExpertName, MagicNumber, 0, Blue);
    if (ticket > -1)
    {
        if (true)
        {
            OrderSelect(ticket, SELECT_BY_TICKET);
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, Blue);
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
            
    }
    else
    {
        Print("OrderSend() error - ", ErrorDescription(GetLastError()));
    }
}
Reason: