AccountFreeMarginCheck & AccountFreeMargin MT4 problem

 
void OnTick()
  {
      double vol=0.01;
      Print("AccountFreeMargin before:",DoubleToStr(AccountFreeMargin()));
      Print("AccountFreeMarginCheck:",DoubleToStr(AccountFreeMarginCheck(Symbol(),OP_BUY,vol)));
      OrderSend(Symbol(),OP_BUY,vol,Ask,0,0,0,"test",0,0,Blue);
      Print("AccountFreeMargin immediately after:",DoubleToStr(AccountFreeMargin()));

   return;
  }


output:

0       17:01:20        2014.01.02 02:00  test2 AUDUSD,M1: AccountFreeMargin before:100.00000000
0       17:01:20        2014.01.02 02:00  test2 AUDUSD,M1: AccountFreeMarginCheck:98.00000000
2       17:01:20        2014.01.02 02:00  test2 AUDUSD,M1: open #1 buy 0.01 AUDUSD at 0.88851 ok
0       17:01:20        2014.01.02 02:00  test2 AUDUSD,M1: AccountFreeMargin immediately after:97.77000000


https://docs.mql4.com:

AccountFreeMarginCheck() - Returns free margin that remains after the specified order has been opened at the current price on the current account.

AccountFreeMargin() - Returns free margin value of the current account.

As I understand AccountFreeMarginCheck() call before opening an order should return the same value as AccountFreeMargin() straight after the order was opened


Why AccountFreeMarginCheck() does not match AccountFreeMargin()? What am I doing wrong?

Thanks!

 
You are forgetting about the spread. The moment you open a buy, you are now down by the spread. Open a sell and they would match because you pay the spread when it closes.
 
0       15:47:56        2014.01.02 02:00  test2 AUDUSD,M1: AccountFreeMargin before:100.00000000
0       15:47:56        2014.01.02 02:00  test2 AUDUSD,M1: AccountFreeMarginCheck:98.00000000
2       15:47:56        2014.01.02 02:00  test2 AUDUSD,M1: open #1 buy 0.01 AUDUSD at 0.88851 ok
0       15:47:56        2014.01.02 02:00  test2 AUDUSD,M1: AccountFreeMargin immediately after:97.77000000
0       15:47:56        2014.01.02 02:00  test2 AUDUSD,M1: Spread:23.00000000


Hm, now it makes sence.

Thank you for clarification, you helped alot!


Does it mean that AccountFreeMargin() would be different across time whith floating spread broker?

 
zversky:


Hm, now it makes sence.

Thank you for clarification, you helped alot!


Does it mean that AccountFreeMargin() would be different across time whith floating spread broker?

Yes
 

I'm attempting to write an EA (with ChatGPT's assistance)

We've done quite well (I have zero coding knowledge), OK I think we have done quite well!

However when running back test the trades aren't executing because of a 130 error (Error 130: Invalid stops (stop loss or take profit too close or not allowed)   |   apparently the "Required Margin: -10000000000"
Chat GPT suggests its to do with the use of specific functions like AccountFreeMarginCheck and trade commands ( OP_BUYLIMIT ). Hence this post


this is an extract of the code (hopefully the relevant piece!)


// Define minimum distance in points
double minDistancePoints = 5 * Point;

// Function to place limit order
void PlaceLimitOrder() {
    double entryPrice = High[1] + 2 * Point;
    double stopLoss = Low[1] - 10 * Point;
    double takeProfit = High[iHighest(NULL, PERIOD_D1, MODE_HIGH, lookbackPeriod, 0)] - 10 * Point;
    double calculatedLotSize = CalculateLotSize(stopLoss, entryPrice);
    
    // Debugging statements
    Print("Calculated Lot Size: ", calculatedLotSize);
    Print("Entry Price: ", entryPrice);
    Print("Stop Loss: ", stopLoss);
    Print("Take Profit: ", takeProfit);
    
    // Check if stops are valid
    if (stopLoss <= entryPrice - 0.0005 && takeProfit >= entryPrice + 0.0005) {
        // Perform AccountFreeMarginCheck
        double requiredMargin = AccountFreeMarginCheck(_Symbol, OP_BUYLIMIT, calculatedLotSize);
        double freeMargin = AccountFreeMargin();

        // Debugging statements
        Print("Required Margin: ", requiredMargin);
        Print("Free Margin: ", freeMargin);

        // Validate margin and free margin
        if (requiredMargin >= 0 && freeMargin >= requiredMargin) {
            int ticket = OrderSend(_Symbol, OP_BUYLIMIT, calculatedLotSize, entryPrice, 2, stopLoss, takeProfit);
            if (ticket < 0) {
                Print("OrderSend failed with error #", GetLastError());
            } else {
                tradeOpened = true;
            }
        } else {
            Print("Insufficient margin to place order.");
        }
    } else {
        Print("Invalid stop loss or take profit levels.");
    }
}

and this is the journal output from back testing journal (dont worry, its a demo account set up to see if without any training I could develop & test trading strategys)


2024.07.06 16:56:37.367    EURUSD,Daily: 2563 tick events (2562 bars, 5224 bar states) processed in 0:00:00.035 (total time 0:00:00.036)
2024.07.06 16:56:37.365    2021.10.08 00:00:00  Trend long + Daily pinbar EURUSD,Daily: Insufficient margin to place order.
2024.07.06 16:56:37.365    2021.10.08 00:00:00  Trend long + Daily pinbar EURUSD,Daily: Free Margin: 10000
2024.07.06 16:56:37.365    2021.10.08 00:00:00  Trend long + Daily pinbar EURUSD,Daily: Required Margin: -10000000000
2024.07.06 16:56:37.365    2021.10.08 00:00:00  Trend long + Daily pinbar EURUSD,Daily: invalid trade cmd 2 for FreeMarginCheck function
2024.07.06 16:56:37.365    2021.10.08 00:00:00  Trend long + Daily pinbar EURUSD,Daily: Take Profit: 1.1844
2024.07.06 16:56:37.365    2021.10.08 00:00:00  Trend long + Daily pinbar EURUSD,Daily: Stop Loss: 1.1545
2024.07.06 16:56:37.365    2021.10.08 00:00:00  Trend long + Daily pinbar EURUSD,Daily: Entry Price: 1.1571
2024.07.06 16:56:37.365    2021.10.08 00:00:00  Trend long + Daily pinbar EURUSD,Daily: Calculated Lot Size: 0.77
2024.07.06 16:56:37.365    2021.10.07 00:00:00  Trend long + Daily pinbar EURUSD,Daily: Insufficient margin to place order.


If any human being can help me to help ChatGPT that'd be much appreciated

Thanks!
Paul