Combing lotsize-script with EA

 

Hello

At the buttom of this post is a script for lotsize-calculation based on free margin. I would very much like to combine it with the attached ParabolicSAR-EA.

Anyone can do that?

It would be a GREAT help.

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()));
    }

}

Files:
 
Here.
 
fj4klkd8: I would very much like to combine it with the attached ParabolicSAR-EA.
Since there are no slaves here, there are only two choices: learn to code or pay someone. We're not going to code it FOR you. We are willing to HELP you.