cm_SL NL TP

Author: Copyright 2019, cmillion@narod.ru
Orders Execution
It automatically opens orders when conditions are reached
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
cm_SL NL TP
ÿþ//+------------------------------------------------------------------+

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

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

#property version   "1.0"

#property description "!>25B=8: 2KAB02;O5B 2A5< >@45@0< AB>?;>AA B59:?@>D8B 8 ?5@52>48B 8E 2 157C1KB>:."

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

input int     Stoploss             = 500,           //AB>?;>AA

              Takeprofit           = 200,           //B59:?@>D8B

              NoLoss               = 50,            //?5@52>4 2 157C1KB>:, 5A;8 0, B> =5B ?5@52>40 2 157C1KB>:

              MinProfitNoLoss      = 10;            //<8=8<0;L=0O ?@81K;L ?@8 ?5@52>45 2157C1KB>:

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

MqlTick tick;

MqlTradeRequest request;

MqlTradeResult result;

MqlTradeCheckResult check;

MqlDateTime tm;

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

int OnInit()

{

    Comment("Start ");

   return(INIT_SUCCEEDED);

}

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

void OnDeinit(const int reason)

{

}

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

void OnTick()

{

   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(Bid - 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(Ask + 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;

               }

            }

            //else request.sl = OSL;

            

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

            {

               request.action = TRADE_ACTION_SLTP;

               Alert("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(Ask + 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(Bid - 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;

               }

            }

            //else request.sl = OSL;

            

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

            {

               request.action = TRADE_ACTION_SLTP;

               Alert("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 ---