//+------------------------------------------------------------------+
//|                                             RiskBudgetScript.mq5 |
//|                                                       Ali Rajput |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Ali Rajput"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property script_show_inputs
//+------------------------------------------------------------------+
//| Includes                                                         |
//+------------------------------------------------------------------+
#include <RiskBudget\RiskBudget.mqh>
//+------------------------------------------------------------------+
//| Inputs                                                           |
//+------------------------------------------------------------------+
input double InpRiskPct   =1.0;     // Risk per trade, % of balance
input double InpBudgetPct =3.0;     // Open risk budget, % of balance
input double InpMarginPct =30.0;    // Share of free margin one trade may use, %
input double InpStopPct   =0.5;     // Stop loss distance, % of the price
input double InpCommission=0.0;     // Commission per lot and side, account currency
input int    InpSlipTicks =0;       // Slippage allowed for at the stop, ticks
//+------------------------------------------------------------------+
//| Script program start function: a buy quote for every symbol in   |
//| Market Watch with the stop InpStopPct below the Ask              |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- a terminal started from a command line needs a few seconds to log in
   for(int wait=0; wait<300 && AccountInfoDouble(ACCOUNT_BALANCE)<=0.0 && !IsStopped(); wait++)
      Sleep(100);
   if(AccountInfoDouble(ACCOUNT_BALANCE)<=0.0)
     {
      Print("RiskBudget quotes: no account connected");
      return;
     }
   CRiskBudget budget;
   budget.SetRisk(InpRiskPct,InpBudgetPct);
   budget.SetMarginShare(InpMarginPct);
   budget.SetCosts(InpCommission,InpSlipTicks);
   int unprotected=0,unpriced=0;
   double open=budget.OpenRisk(unprotected,unpriced);
   PrintFormat("RiskBudget quotes: balance %.2f, free margin %.2f, open risk %.2f, without stop %d, not priced %d",
               AccountInfoDouble(ACCOUNT_BALANCE),AccountInfoDouble(ACCOUNT_MARGIN_FREE),open,unprotected,unpriced);
   PrintFormat("%-8s %-9s %-12s %9s %10s %9s %9s %s","symbol","status","cap","volume",
               "risk/lot","risk","margin","tick value");
   int total=SymbolsTotal(true);
   for(int i=0; i<total; i++)
     {
      string sym=SymbolName(i,true);
      double ask =SymbolInfoDouble(sym,SYMBOL_ASK);
      double size=SymbolInfoDouble(sym,SYMBOL_TRADE_TICK_SIZE);
      if(ask<=0.0 || size<=0.0)
        {
         PrintFormat("%-8s no quote",sym);
         continue;
        }
      double dist=MathMax(size,MathRound(ask*InpStopPct/100.0/size)*size);
      SRiskQuote q;
      budget.Quote(sym,ORDER_TYPE_BUY,ask,ask-dist,q);
      if(q.status==RB_NO_CALC)
        {
         PrintFormat("%-8s %-9s",sym,CRiskBudget::StatusText(q.status));
         continue;
        }
      PrintFormat("%-8s %-9s %-12s %9.2f %10.2f %9.2f %9.2f %s %.3f",sym,
                  CRiskBudget::StatusText(q.status),CRiskBudget::CapText(q.cap),q.volume,
                  q.risk_per_lot,q.trade_risk,q.margin,
                  budget.TickValueAgrees(q) ? "ok" : "DIFFERS",q.tv_ratio);
     }
  }
//+------------------------------------------------------------------+
