vav_openlimitorder_manually
Download scriptThis script is a specialized financial trading tool designed to automate the placement of "Limit" orders (Buy Limit or Sell Limit) based on specific price coordinates and risk management rules. Instead of manually calculating complex lot sizes or ensuring that your stop-loss levels are mathematically consistent with current market prices, this tool handles those validations for you instantly.
Key Functionalities:
1. Intelligent Order Placement The script analyzes the relationship between your desired entry price and the current live market price to determine the correct type of order:
- Buy Limit: If your chosen entry price is below the current market price, the script prepares a "Buy Limit" order.
- Sell Limit: If your chosen entry price is above the current market price, the script prepares a "Sell Limit" order.
- Validation: If you attempt to set an entry price that is currently "in-between" the live Bid and Ask prices, the system will alert you that the price is invalid for a limit order.
2. Automated Risk & Lot Sizing One of the most significant features of this tool is its ability to calculate your position size based on your account balance.
- You provide a percentage value (e.g., 1% of your deposit).
- The script automatically calculates how many "lots" you can trade based on the distance between your Entry Price and your Stop Loss.
- This ensures that every trade is sized proportionally to your available capital, helping to maintain consistent risk management.
3. Safety Checks and Alerts To prevent manual entry errors that could lead to account losses, the script performs several automated "sanity checks":
- Stop Loss Validation: It ensures that your Stop Loss is placed on the correct side of your Entry Price (e.g., it won't let you set a Buy Limit with a Stop Loss higher than the entry price).
- Take Profit Correction: If the Take Profit price provided is logically impossible or invalid for the chosen direction, the script will alert you and default the Take Profit to zero.
- Insufficient Funds: If your desired trade size is too large for your current account balance (based on the risk percentage you set), the script will notify you immediately rather than attempting a faulty trade.
4. Execution Feedback Once all conditions are met, the script attempts to place the order with the trading server. It provides real-time feedback via pop-up alerts:
- It confirms successful placement and provides the unique Order ID.
- It alerts you if the order failed to open due to any external errors.
//+------------------------------------------------------------------+
//| vav_openlimitorder_manually.mq4 |
//| vav |
//| vav |
//+------------------------------------------------------------------+
#property copyright "vav"
#property link "vav"
#property version "1.00"
#property strict
#property script_show_inputs
//--- input parameters
input int Ident=123; //Èäåíòèôèêàòîð îðäåðà
input string coment=""; //Êîììåíòàðèé
input int PrSk=3; //Ïðîñêàëüçûâàíèå
input double CenaOp=0; //Price
input double Stop=0; //StopLoss
input double Tk=0; //TakeProfit
input double PrDepo=1; //Äîïóñòèìûé óáûòîê â % îò äåïîçèòà
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
int nZn = Digits; //Êîë-âî çíàêîâ
string SymB = Symbol(); //Ñèìâîë
double Lo; //Îáúåì ëîòà
double minLot = MarketInfo(SymB, MODE_MINLOT);
int ti;
double Take=Tk;
if(CenaOp<Ask)
{
if(Stop<CenaOp)
{
if(Take<=CenaOp || Take==0)
{
Take=0;
Alert("TakeProfit íå óñòàíîâëåí");
}
Lo=Lot(SymB,(int)((CenaOp-Stop)*MathPow(10,nZn)),PrDepo);
if(Lo<minLot)
{
Alert("Íåäîñòàòî÷íî ñðåäñòâ äåïîçèòà");
}
else
{
Lo=NormalizeDouble(Lo,2);
ti=OrderSend(SymB,OP_BUYLIMIT,Lo,CenaOp,PrSk,Stop,Take,coment,Ident,0,clrNONE);
if(ti!=-1)
{
Alert("Óñòàíîâëåí îðäåð",ti,"BuyStop");
}
else
Alert("Îðäåð íå óñòàíîâëåí");
}
}
else
{
Alert("Íåäîïóñòèìîå çíà÷åíèå StopLoss");
}
}
if(CenaOp>Bid)
{
if(Stop>CenaOp)
{
if(Take>=CenaOp || Take==0)
{
Take=0;
Alert("TakeProfit íå óñòàíîâëåí");
}
Lo=Lot(SymB,(int)((Stop-CenaOp)*MathPow(10,nZn)),PrDepo);
if(Lo<minLot)
{
Alert("Íåäîñòàòî÷íî ñðåäñòâ äåïîçèòà");
}
else
{
Lo=NormalizeDouble(Lo,2);
ti=OrderSend(SymB,OP_SELLLIMIT,Lo,CenaOp,PrSk,Stop,Take,coment,Ident,0,clrNONE);
if(ti!=-1)
{
Alert("Óñòàíîâëåí îðäåð",ti,"SellStop");
}
else
Alert("Îðäåð íå óñòàíîâëåí");
}
}
}
if(Bid<=CenaOp && CenaOp<=Ask)Alert("Íåäîïóñòèìàÿ öåíà îòêðûòèÿ");
}
//+------------------------------------------------------------------+
//Ôóíêöèÿ íàõîæäåíèÿ ëîòà îò äåïîçèòà
double Lot(string Sym,int Pn,double pD)
{
double L=(AccountBalance()/100*pD)/(MarketInfo(Sym, MODE_TICKVALUE)*(Pn));
return(L);
}
Comments