Automatic Stop Loss Setter

Automatic Stop Loss Setter
Orders Execution
Checks for the total of open orders
0 Views
0 Downloads
0 Favorites
Automatic Stop Loss Setter

   extern int StopLoss = 18;
   extern int TakeProfit = 25;
   extern int ZeroStopLossProfit = 13; //ýòî óðîâåíü ïåðåâîäà ïîçèöèè â áåçóáûòîê +1 ïèïñ
   extern bool MakeSound = true; 
   
   double MaxDeltaPrice;
   string SymbolString;
   bool Error;
   
/*=================================================================================================*/

   void init ()
   {
   Error = false;
   SymbolString = Symbol();
   if ( (StopLoss < 13) ||
        (TakeProfit < 13) ||
        (ZeroStopLossProfit < 13) )
         { Comment ("Error: inputs cannot be less than 13. Change."); Error = true;  return; }
   else Comment ("The Expert is loaded and waiting data.");
   }

/*=================================================================================================*/

   void start ()
   {
   int i;
   double OpenPrice;
   double CurrentPrice;
   double DeltaPrice;
   double StopLossPrice;
   double TakeProfitPrice;
   int OrderDir;
   int OrderCounter;
   if (Error == true) return;
   Comment (SymbolString,": The Expert is working...");
   OrderCounter = OrdersTotal();
   if (OrderCounter==0) return;
   for (i=0; i<OrderCounter; i++)
      {
      OrderSelect (i,SELECT_BY_POS);
      OpenPrice = OrderOpenPrice();
      OrderDir = OrderType();
      if (OrderSymbol() != Symbol())  continue;
      if (OrderStopLoss() != 0.0)
            { if ( (OrderDir == OP_SELL) && (OrderOpenPrice() > OrderStopLoss()) ) continue;
              if ( (OrderDir == OP_BUY) && (OrderOpenPrice() < OrderStopLoss()) )  continue;
              if (OrderProfit() <= 0.0) continue;
              CurrentPrice = OrderClosePrice();
              DeltaPrice = MathAbs(CurrentPrice-OpenPrice);              
              if ((DeltaPrice/Point) < ZeroStopLossProfit) 
                  { if (DeltaPrice > MaxDeltaPrice)
                        { if (OrderDir == OP_SELL) StopLossPrice = CurrentPrice + (StopLoss*Point);
                          if (OrderDir == OP_BUY)  StopLossPrice = CurrentPrice - (StopLoss*Point);
                          OrderModify (OrderTicket(),OrderOpenPrice(),StopLossPrice,OrderTakeProfit(),0,CLR_NONE);
                          if (MakeSound == true) PlaySound ("tick.wav"); }
                    MaxDeltaPrice = MathMax(MaxDeltaPrice,DeltaPrice);
                    continue; }
              if (OrderDir == OP_SELL) StopLossPrice = OpenPrice - Point;
              if (OrderDir == OP_BUY)  StopLossPrice = OpenPrice + Point;
              OrderModify (OrderTicket(),OrderOpenPrice(),StopLossPrice,OrderTakeProfit(),0,CLR_NONE);
              if (MakeSound == true) PlaySound ("tick.wav");
              continue; }
      if (OrderDir == OP_SELL)  
         { StopLossPrice = OpenPrice + (StopLoss * Point);  TakeProfitPrice = OpenPrice - TakeProfit*Point; }
      if (OrderDir == OP_BUY)  
         { StopLossPrice = OpenPrice - (StopLoss * Point);  TakeProfitPrice = OpenPrice + TakeProfit*Point; }
      OrderModify (OrderTicket(),OrderOpenPrice(),StopLossPrice,TakeProfitPrice,0,CLR_NONE);
      if (MakeSound == true) PlaySound ("tick.wav");
      } 
   }

/*=================================================================================================*/

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---