SetStopLoss

Download script
SetStopLoss

This script is designed to act as an automated safety net for your trading account by ensuring that every active trade has a predefined "exit plan." Its primary purpose is to automatically calculate and apply a risk limit (Stop Loss) to any open position that currently lacks one.

How the Logic Works

The script operates by continuously scanning your active trading list. It looks at every open trade one by one to determine if it meets specific criteria for automation.

1. Identifying "Unprotected" Trades The script specifically hunts for trades where you have entered a position but have not yet set a Stop Loss price. It ignores any trades that already have protection in place, ensuring it doesn't interfere with your manual settings.

2. Automated Risk Calculation Once the script finds an unprotected trade, it identifies whether you are currently "Buying" or "Selling." It then automatically calculates the correct distance for your safety net based on a fixed numerical value defined in the settings (the "StopLoss" variable).

  • For Buy Orders: If you are buying a currency or asset, the script places the safety net below your entry price. It subtracts the designated amount from your opening price to ensure that if the market moves against you, the trade will close automatically to prevent further losses.
  • For Sell Orders: If you are selling, the script places the safety net above your entry price. It adds the designated amount to your opening price, providing a ceiling that triggers an automatic exit if the market rises unexpectedly.

3. Seamless Execution The script performs these adjustments instantly in the background. By looping through all open positions, it ensures that even if you open multiple trades rapidly, each one will be checked and updated with the appropriate protection without requiring you to manually calculate the price points for every single trade.

In short, this tool automates the "defense" side of your trading strategy, ensuring that no trade remains exposed to unlimited risk by automatically applying a consistent safety margin the moment a trade is opened.

Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategy
12 Views
0 Downloads
0 Favorites
SetStopLoss
extern int StopLoss=50;

int start()
  {
   for(int cnt=OrdersTotal();cnt>=0;cnt--)
     { 
       if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)==true) 
          {
              if(OrderType()==OP_BUY && OrderStopLoss()==0)   
                 {
                     OrderModify(OrderTicket(),0,OrderOpenPrice()-StopLoss*Point,OrderTakeProfit(),0,CLR_NONE);
                 }
              if(OrderType()==OP_SELL && OrderStopLoss()==0)   
                 {
                     OrderModify(OrderTicket(),0,OrderOpenPrice()+StopLoss*Point,OrderTakeProfit(),0,CLR_NONE);
                 }
          }                       
     }
   }
   

Comments