Not enought Money when trying to open trade

 

Hi,

I've write a EA which correctly find signals but, when it try to open a trade, broker respond not enought money. The most probable cause is that the numbre of lots my EA try to buy/sell is too big. So i search to find the maximum number of lots i can buy/sell if my robot calculate a too big number of lots.


i've see CTrade::CheckVolume that seemed to answer my problem, but I still get the same error message after using this function...


here, my code :

//+------------------------------------------------------------------+
//| opening trade                                                    |
//| true - buy, false - sell                                         |
//+------------------------------------------------------------------+
void Open(bool buy){
    //---
    //double sl           = buy ? m_symbol.NormalizePrice( iLow(m_symbol.Name(), Period(), 1) - extMarginSL ) : m_symbol.NormalizePrice( iHigh(m_symbol.Name(), Period(), 1) + extMarginSL );
    double sl           =  m_symbol.NormalizePrice( buy ? ( MMS4[1] - extMarginSL ) : ( MMS4[1] + extMarginSL ) );
    double market_price = ( buy ? m_symbol.Ask() : m_symbol.Bid() );
    double open_price   = m_symbol.NormalizePrice( buy ? market_price+extMarginOpen : market_price-extMarginOpen );
    double tp           = buy ? m_symbol.NormalizePrice( m_symbol.Ask() + (((m_symbol.Ask() - sl) * ratioRR) - extMarginTP)  ) : m_symbol.NormalizePrice( m_symbol.Bid() - (((sl - m_symbol.Bid()) * ratioRR) + extMarginTP)  );

    if(market_price == sl){
        Print( "ERREUR : Le SL doit être différent du prix de marché" );
        return;
    }

    //---
    double check_open_short_lot = 0.0;
    check_open_short_lot= buy ? m_money.CheckOpenLong( open_price, sl ) : m_money.CheckOpenShort( open_price, sl );
    if(check_open_short_lot <= 0.0){
        Print( "ERREUR : Le nombre de lot doit être suppérieur à 0.0" );
        return;
    }

    //---
    double check_volume_lot = 0.0;
    check_volume_lot        = m_trade.CheckVolume(m_symbol.Name(), check_open_short_lot, (open_price + (ExtSlippage *2)), ( buy ? ORDER_TYPE_BUY : ORDER_TYPE_SELL ) );
    bool trade_result       = false;
    if(check_volume_lot > 0.0){
        if( buy ){
            double buyLimit     = m_symbol.NormalizePrice( iClose(m_symbol.Name(), Period(), 1) + extMarginOpen );
            if(extMarginOpen>0.0 && m_symbol.Ask() < buyLimit ){
                trade_result        = m_trade.BuyStop(check_volume_lot, buyLimit , NULL, sl, tp, ORDER_TIME_SPECIFIED, TimeCurrent() +(InpOpenTime*60)+1 );
            }
            else{
                trade_result = m_trade.Buy(check_volume_lot,NULL,m_symbol.Ask(),sl,tp);
            }
        }
        else{
            double sellLimit    = m_symbol.NormalizePrice( iClose(m_symbol.Name(), Period(), 1) - extMarginOpen );
            if(extMarginOpen>0.0 && m_symbol.Bid() > sellLimit ){
                trade_result        = m_trade.SellStop(check_volume_lot, sellLimit , NULL, sl, tp, ORDER_TIME_SPECIFIED, TimeCurrent() +(InpOpenTime*60)+5 );
            }
            else{
                trade_result = m_trade.Sell(check_volume_lot,NULL,m_symbol.Bid(),sl,tp);
            }
        }
    }
    else{
        Print( "ERREUR : Le volume ne peut pas être égal à 0.0");
        return;
    }

    //---
    if(trade_result){
        if(m_trade.ResultDeal()==0){
            Print("#1 Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),
                  ", description of result: ",m_trade.ResultRetcodeDescription());
            PrintResultTrade(m_trade,m_symbol);
        }
        else{
            Print("#2 Sell -> true. Result Retcode: ",m_trade.ResultRetcode(),
                  ", description of result: ",m_trade.ResultRetcodeDescription());
            PrintResultTrade(m_trade,m_symbol);
        }
    }
    else{
        Print("#3 Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),
              ", description of result: ",m_trade.ResultRetcodeDescription());
        PrintResultTrade(m_trade,m_symbol);
    }
}


Is someone can help me to obtain programmatically the higher number of lots accepted by my broker ?


thank you !

 
It is not the volume, it is lack of margin.
 
William Roeder:
It is not the volume, it is lack of margin.

Sory, i'm a beginner, so I've probably not understanding something...

isn't lack of margin and volume linked ?

I thought that to solve a margin problem there were two solutions: Increase the account balance by adding money or reduce the volume of trades to require less margin. it's not that ?