cm_SL-NL-TP

Author: Copyright 2022, cmillion@narod.ru
Orders Execution
It automatically opens orders when conditions are reached
0 Views
0 Downloads
0 Favorites
cm_SL-NL-TP
ÿþ//+------------------------------------------------------------------+

#property copyright "Copyright 2022, cmillion@narod.ru"

#property link      "http://www.cmillion.ru"

#property version   "1.2"

#property description "!>25B=8: 2KAB02;O5B 2A5< >@45@0< AB>?;>AA B59:?@>D8B 8 ?5@52>48B 8E 2 157C1KB>:. The ADVISER puts all orders stop loss take profit and transfers them to breakeven."

#property description "---"

#property description "Telegram:  @ea_cmillion - #Convenient trading tools ##4>1=K5 8=AB@C<5=BK B>@3>2;8."

#property description "WhatsApp   +79283690333"

#property description "---"

#property description "0@0<5B@K "

#property description "NoLoss - ?5@52>4 2 157C1KB>:, 5A;8 0, B> =5B ?5@52>40 2 157C1KB>:"

#property description "MinProfitNoLoss - <8=8<0;L=0O ?@81K;L ?@8 ?5@52>45 2157C1KB>:"



//--------------------------------------------------------------------

input int     Stoploss             = 500,

              Takeprofit           = 105,

              NoLoss               = 100,

              MinProfitNoLoss      = 20;

//--------------------------------------------------------------------

MqlTick tick;

MqlTradeRequest request;

MqlTradeResult result;

MqlTradeCheckResult check;

MqlDateTime tm;

bool LANGUAGE;

//+------------------------------------------------------------------+

int OnInit()

{

   LANGUAGE=TerminalInfoString(TERMINAL_LANGUAGE)=="Russian";

   Comment(LANGUAGE?"!>25B=8: 2KAB02;O5B 2A5< >@45@0< AB>?;>AA B59:?@>D8B 8 ?5@52>48B 8E 2 157C1KB>:.":"The ADVISER puts all orders stop loss take profit and transfers them to breakeven.");

   return(INIT_SUCCEEDED);

}

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{

   Comment("");

}

//+------------------------------------------------------------------+

void OnTick()

{

   if (!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED)) 

   { 

      Comment(LANGUAGE?"@>25@LB5 2 =0AB@>9:0E B5@<8=0;0 @07@5H5=85 =0 02B><0B8G5A:CN B>@3>2;N!":"Check the automatic trading permission in the terminal settings!");

      Sleep(2000);

      return;

   }

   

   //---

   

   if(!MQLInfoInteger(MQL_TRADE_ALLOWED)) 

   {

      Comment(LANGUAGE?"2B><0B8G5A:0O B>@3>2;O 70?@5I5=0 2 A2>9AB20E ?@>3@0<<K 4;O ":"Automatic trading is prohibited in the program properties for ",__FILE__); 

      Sleep(2000);

      return;

   }

   

   //---

   

   if(SymbolInfoInteger(_Symbol,SYMBOL_TRADE_MODE)!=SYMBOL_TRADE_MODE_FULL)

   {

      Comment(LANGUAGE?"">@3>2;O ?> 2K1@0==K< 8=AB@C<5=B0< 70?@5I5=0":"Trading on the selected instruments is prohibited");

      Sleep(2000);

      return;

   }

   

   //---

   

   if(!AccountInfoInteger(ACCOUNT_TRADE_EXPERT)) 

   {

      Comment(LANGUAGE?"2B><0B8G5A:0O B>@3>2;O 70?@5I5=0 4;O AG5B0 ":"Automatic trading is prohibited for the account ",AccountInfoInteger(ACCOUNT_LOGIN), 

      LANGUAGE?" =0 AB>@>=5 B>@3>2>3> A5@25@0":"on the side of the trading server");

      Sleep(2000);

      return;

   }

   

   //---

   

   if(!AccountInfoInteger(ACCOUNT_TRADE_ALLOWED))

   {

      Comment(LANGUAGE?"">@3>2;O 70?@5I5=0 4;O AG5B0 ":"Trading is prohibited for the account ",AccountInfoInteger(ACCOUNT_LOGIN));

      Sleep(2000);

      return;

   }

   

   //---

   

   double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);

   double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);

   double StLo,OOP,OSL,OTP;

   for(int i=0; i<PositionsTotal(); i++)

   {

      if(_Symbol==PositionGetSymbol(i))

      {

         ZeroMemory(request);

         ZeroMemory(result);

         request.position = PositionGetInteger(POSITION_TICKET);

         OOP = PositionGetDouble(POSITION_PRICE_OPEN); 

         OSL = PositionGetDouble(POSITION_SL); request.sl = OSL;

         OTP = PositionGetDouble(POSITION_TP); request.tp = OTP;

         long tip = PositionGetInteger(POSITION_TYPE);

         request.sl=OSL;request.tp=OTP;

         if (tip==POSITION_TYPE_BUY ) 

         {

            if (Stoploss!=0)

            {

               request.sl = NormalizeDouble(OOP - Stoploss * _Point,_Digits);

               if (request.sl>=Bid) request.sl = NormalizeDouble(OOP - Stoploss * _Point,_Digits);

               if (request.sl < OSL && OSL!=0) request.sl = OSL;

            }

            else if (OSL<OOP) request.sl=0;

            //---

            if (Takeprofit!=0)

            {

               request.tp = NormalizeDouble(OOP + Takeprofit * _Point,_Digits);

               if (request.tp<=Ask) request.tp = NormalizeDouble(OOP + Stoploss * _Point,_Digits);

               if (request.tp > OTP && OTP!=0) request.tp = OTP;

            }

            else request.tp=0;

            //---

            if (OSL<OOP)

            {

               if (NoLoss!=0)

               {

                  StLo = NormalizeDouble(OOP+MinProfitNoLoss*_Point,_Digits); 

                  if (StLo > OSL && StLo <= NormalizeDouble(Bid - NoLoss * _Point,_Digits)) 

                     request.sl = StLo;

               }

            }

            

            if (request.sl != OSL || request.tp != OTP)

            {

               request.action = TRADE_ACTION_SLTP;

               Comment(TimeToString(TimeCurrent())," Modify Buy ",request.position,",  SL ",DoubleToString(OSL,_Digits)," -> ",DoubleToString(request.sl,_Digits)," (",DoubleToString((OOP-request.sl)/_Point,0),"),  TP ",DoubleToString(OTP,_Digits)," -> ",request.tp," (",DoubleToString((request.tp-OOP)/_Point,0),")");

               if(!OrderSend(request,result)) Print("error ",GetLastError());

            }

         }                     

         if (tip==POSITION_TYPE_SELL) 

         {

            if (Stoploss!=0)

            {

               request.sl = NormalizeDouble(OOP + Stoploss   * _Point,_Digits);

               if (request.sl<=Ask) request.sl = NormalizeDouble(OOP + Stoploss   * _Point,_Digits);

               if (request.sl > OSL && OSL!=0) request.sl = OSL;

            }

            else if (OSL>OOP) request.sl=0;

            //---

            if (Takeprofit!=0)

            {

               request.tp = NormalizeDouble(OOP - Takeprofit * _Point,_Digits);

               if (request.tp>=Bid) request.tp = NormalizeDouble(OOP - Stoploss   * _Point,_Digits);

               if (request.tp < OTP && OTP!=0) request.tp = OTP;

            }

            else request.tp=0;

            //---

            if (OSL>OOP)

            {

               if (NoLoss!=0)

               {

                  StLo = NormalizeDouble(OOP-MinProfitNoLoss*_Point,_Digits); 

                  if ((StLo < OSL || OSL==0) && StLo >= NormalizeDouble(Ask + NoLoss * _Point,_Digits)) 

                     request.sl = StLo;

               }

            }

            

            if (request.sl != OSL || request.tp != OTP)

            {

               request.action = TRADE_ACTION_SLTP;

               Comment(TimeToString(TimeCurrent())," Modify Sell ",request.position,",  SL ",DoubleToString(OSL,_Digits)," -> ",DoubleToString(request.sl,_Digits)," (",DoubleToString((request.sl-OOP)/_Point,0),"),  TP ",DoubleToString(OTP,_Digits)," -> ",request.tp," (",DoubleToString((OOP-request.tp)/_Point,0),")");

               if(!OrderSend(request,result)) Print("error ",GetLastError());

            }

         }  

      }  

   }

}

//+------------------------------------------------------------------+

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 ---