//+------------------------------------------------------------------+
//|                                                 StopOutProbe.mq5 |
//|                                       Copyright 2026, Ali Rajput |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Ali Rajput"
#property version   "1.00"
#property description "Values a 1-lot buy at the bid and at 5% and 10% below it with OrderCalcProfit(),"
#property description "and prints OrderCalcMargin() at the same prices, for six symbols."
//+------------------------------------------------------------------+
//| Print one symbol: profit and margin at the market and far away   |
//+------------------------------------------------------------------+
void ProbeSymbol(const string sym)
  {
   if(!SymbolSelect(sym,true))
     {
      PrintFormat("StopOutProbe %s: not available",sym);
      return;
     }
   MqlTick t;
   for(int i=0; i<50 && !SymbolInfoTick(sym,t); i++)
      Sleep(100);
   double contract=SymbolInfoDouble(sym,SYMBOL_TRADE_CONTRACT_SIZE);
   double tv=SymbolInfoDouble(sym,SYMBOL_TRADE_TICK_VALUE);
   double ts=SymbolInfoDouble(sym,SYMBOL_TRADE_TICK_SIZE);
   PrintFormat("StopOutProbe %s: bid %.5f ask %.5f contract %.0f tick value %.6f tick size %.6f base %s profit %s margin %s calc %d",
               sym,t.bid,t.ask,contract,tv,ts,
               SymbolInfoString(sym,SYMBOL_CURRENCY_BASE),SymbolInfoString(sym,SYMBOL_CURRENCY_PROFIT),
               SymbolInfoString(sym,SYMBOL_CURRENCY_MARGIN),(int)SymbolInfoInteger(sym,SYMBOL_TRADE_CALC_MODE));
//--- a buy of 1 lot opened at the ask, closed at bid, bid-5%, bid-10%
   double shifts[]={0.0,0.05,0.10};
   for(int i=0; i<ArraySize(shifts); i++)
     {
      double close=t.bid*(1.0-shifts[i]);
      double profit=0.0,margin=0.0;
      bool okp=OrderCalcProfit(ORDER_TYPE_BUY,sym,1.0,t.ask,close,profit);
      bool okm=OrderCalcMargin(ORDER_TYPE_BUY,sym,1.0,close,margin);
      PrintFormat("StopOutProbe %s: close %.5f (-%.0f%%) profit %.2f (%s) margin at that price %.2f (%s)",
                  sym,close,100*shifts[i],profit,okp ? "ok" : "fail",margin,okm ? "ok" : "fail");
     }
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   PrintFormat("StopOutProbe account: currency %s leverage %d so mode %d margin call %.2f stop out %.2f margin mode %d",
               AccountInfoString(ACCOUNT_CURRENCY),(int)AccountInfoInteger(ACCOUNT_LEVERAGE),
               (int)AccountInfoInteger(ACCOUNT_MARGIN_SO_MODE),AccountInfoDouble(ACCOUNT_MARGIN_SO_CALL),
               AccountInfoDouble(ACCOUNT_MARGIN_SO_SO),(int)AccountInfoInteger(ACCOUNT_MARGIN_MODE));
   string syms[]={"EURUSD","USDJPY","GBPJPY","EURGBP","XAUUSD","US500"};
   for(int i=0; i<ArraySize(syms); i++)
      ProbeSymbol(syms[i]);
  }
//+------------------------------------------------------------------+
