_SET_SELL_STOP
Download scriptThis script is designed to automate the execution of sell orders on the MetaTrader platform by monitoring specific price conditions. It acts as a "smart" execution tool that ensures an order is placed only when market conditions align with your predefined requirements.
How it Works
1. Preliminary Checks and Setup Before any action is taken, the script performs a series of safety checks to ensure you have provided the necessary information:
- Lot Size: It looks for a specific "lot size" value stored in your global settings. If this is missing or set to zero, it will alert you to define your trading volume first.
- Order Placement: The script requires you to manually place a pending order on the chart first. It scans the screen for this object; if it can't find one, it will notify you to set one up before proceeding.
2. Real-Time Market Monitoring Once the requirements are met, the script enters a continuous loop where it constantly watches the current market price (the "Bid") and compares it against the price of your pending order. It performs several automated evaluations:
- Execution Distance: The script calculates how far the current market price is from your target order price. It checks if this distance complies with the broker's minimum allowed limits (Stop Levels).
- Smart Order Execution:
- If the market price moves significantly beyond your target in one direction, it triggers a Sell order.
- If the price is within a specific "buffer zone" near your target, it attempts to execute a Buy order (depending on the price relationship), ensuring that the trade isn't triggered prematurely or incorrectly due to small fluctuations.
- Error Handling: If the script fails to place an order due to a technical issue or a connection problem, it will provide an alert and print an error code so you can troubleshoot the issue.
3. User Feedback To keep you informed of what is happening behind the scenes:
- Audio Confirmation: The script plays a notification sound (
expert.wav) successfully every time an order is executed. - Visual Tracking: While it is waiting for the price to hit your target, it briefly displays the exact distance (in points) between the current market price and your order price on the chart.
Summary for the User
Think of this script as a vigilant assistant. You tell it how many units to trade and where you want your "price point." The script then sits and watches the market 24/7. Instead of just placing an order at one exact millisecond, it analyzes the distance and volatility to ensure the order is executed correctly according to your target, providing a sound notification once the task is complete.
//+------------------------------------------------------------------+
//| SELL_SET.mq4 |
//| Copyright © 2009, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
int magic=327;
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
void start(){
//GlobalVariablesDeleteAll();
double lot;
int tick;
if(!GlobalVariableCheck("lot")){
GlobalVariableSet("lot",0);
Alert("Ââåäèòå ðàçìåð ëîòà â ãëîáàëüíóþ ïåðåìåííóþ");
return;
}else{
if(GlobalVariableGet("lot")==0){
Alert("Ââåäèòå ðàçìåð ëîòà â ãëîáàëüíóþ ïåðåìåííóþ");
return;
}
lot=GlobalVariableGet("lot");
if(ObjectFind("order")!=0){
Alert("Óñòàíîâèòå ëèíèþ îðäåðà");
return;
}
while(!IsStopped()&&tick==0){
double prise=NormalizeDouble(ObjectGet("order",OBJPROP_PRICE1),Digits);
int stlw=MarketInfo(Symbol(),MODE_STOPLEVEL);
int spr =MarketInfo(Symbol(),MODE_SPREAD);
if(Bid-prise>stlw*Point){
tick=OrderSend(Symbol(),5,lot,prise,spr,0,0,NULL,magic,0,Red);
if(tick<1){
Print("Îøèáêà óñòàíîâêè îðäåðà ¹",GetLastError());
Alert("Îøèáêà óñòàíîâêè îðäåðà ¹",GetLastError());
return;
}else{PlaySound("expert.wav");}
}else{
RefreshRates();
if(prise-Bid>=spr*Point*2){
Alert("ÍÅ ïðàâèëüíàÿ öåíà");
return;
}
if(prise-Bid>=0&&prise-Bid<spr*Point){
tick=OrderSend(Symbol(),1,lot,prise,spr,0,0,NULL,magic,0,Red);
if(tick<1){
Print("Îøèáêà óñòàíîâêè îðäåðà ¹",GetLastError());
Alert("Îøèáêà óñòàíîâêè îðäåðà ¹",GetLastError());
return;
}else{PlaySound("expert.wav");}
}
Comment("SET_SELL DISTANSE = ",DoubleToStr((Bid-prise)/Point,0));
Sleep(200);
Comment("");
}
}
}
Comment("");
return;}
//+------------------------------------------------------------------+
Comments