Set a stop loss order when the price crosses the line - source code | Trading script download - MT4/MT5 resources - MetaTrader 5 resources
Place a stop-loss order when the price crosses this line - the source EA creates two lines on the chart when launched: a green line (start) and a red line (stop). You can use the mouse to move the line to any location. At the same time, if the green line is above the red line, then we plan to place a buy stop order in the future, and if the green line is below the red line, a sell stop order will be placed accordingly. ```cpp //+------------------------------------------------------------------+ //| Trading Panel System.mq4 | //| Copyright © 2023-2025, AI Programming zblog | //| http://www.eawalk.com | //+----------------------------------------------------------------+ #property copyright"Copyright © 2024, AI Programming zblog" #property link "http://www.eawalk.com" #property version "1.00" #property strict #property description "When the expert advisor starts, he will draw two lines on the chart: green (start line) and red (stop loss line)." #property description "These lines can be dragged arbitrarily with the mouse. If the green line is higher than the red line, it will be planned to set a buy stop order; if the green line is lower than the red line, it will be planned to set a sell stop order." //+--------------------------------------------------------------------------------+ extern double Order lot size = 0.01; // Order lot size extern int Starting distance = 25; // The distance from the starting line to the pending order (unit: points) extern int stop loss distance = 25; // The distance from the stop loss line to the stop loss (unit: points) extern int magic number = 123321; // magic number //+--------------------------------------------------------------------------------+ int OnInit() { return(INIT_SUCCEEDED); } //+--------------------------------------------------------------------------------+ void OnDeinit(const int reason) { ObjectsDeleteAll(0,"cm"); } //+------------------------------------------------------------------+ void OnTick() { if(ObjectFind("cm start")==-1) { drawline("cm start",clrGreen,iHigh(NULL,PERIOD_D1,0)); drawtext("cm start ","start line",iHigh(NULL,PERIOD_D1,0)); } if(ObjectFind("cm stop")==-1) { drawline("cm stop",clrRed,iLow(NULL,PERIOD_D1,0)); drawtext("cm stop ","stoploss line",iLow(NULL,PERIOD_D1,0)); } doublePriceStart = ObjectGet("cm start",OBJPROP_PRICE1); ObjectSetDouble(0,"cm start ",OBJPROP_PRICE,PriceStart); doublePriceStop = ObjectGet("cm stop",OBJPROP_PRICE1); ObjectSetDouble(0,"cm stop ",OBJPROP_PRICE,PriceStop); //--- int tip,Ticket=0; for (int i=0;i1) break; Print("-----> The position was opened successfully and the operation ended"); SendNotification(WindowExpertName()+" Position opened successfully and ended"); ExpertRemove(); break; } } } if(PriceStart>PriceStop) { if(Ticket==0) { if (Bid>=PriceStart) if (OrderSend(Symbol(),OP_BUYSTOP, order lot size, NormalizeDouble(PriceStart+starting distancePoint,Digits),0,NormalizeDouble(PriceStop-stop distancePoint,Digits),0,NULL,magic number,0,CLR_NONE)==-1) Print("Error",GetLastError()); } else { if (Bid<=PriceStop) { if (OrderDelete(Ticket)) { Print("----->Delete the pending order and end the run"); SendNotification(WindowExpertName()+"Delete the pending order and end the run"); ExpertRemove(); } } } } else { if(Ticket==0) { if (Bid<=PriceStart) if (OrderSend(Symbol(),OP_SELLSTOP,order lot size,NormalizeDouble(PriceStart-starting distancePoint,Digits),0,NormalizeDouble(PriceStop+stop distancePoint,Digits),0,NULL,magic number,0,CLR_NONE)==-1) Print("Error",GetLastError()); } else { if (Bid>=PriceStop) { if (OrderDelete(Ticket)) { Print("----->Delete the pending order and end the run"); SendNotification(WindowExpertName()+"Delete the pending order and end the run"); ExpertRemove(); } } } } } //+--------------------------------------------------------------------------------+ void drawline(string NameL, colorcol, double Y1) { ObjectCreate(NameL,OBJ_HLINE, 0,0,Y1,0,0); ObjectSet (NameL, OBJPROP_COLOR, col); ObjectSet (NameL, OBJPROP_STYLE, STYLE_SOLID); ObjectSet (NameL, OBJPROP_WIDTH, 1); ObjectSet (NameL, OBJPROP_BACK, false); ObjectSet (NameL, OBJPROP_SELECTED,true); ObjectSet (NameL, OBJPROP_SELECTED,true); } //+------------------------------------------------------------------+ void drawtext(string Name, stringtxt, double Y1) { ObjectDelete (Name); ObjectCreate ObjectSetText 11.png
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •