DropAdjust_SL-TP

Author: Fernando Morales
Orders Execution
It automatically opens orders when conditions are reached
0 Views
0 Downloads
0 Favorites
DropAdjust_SL-TP
ÿþ//+------------------------------------------------------------------+

//|                                             DropAdjust_SL-TP.mq5 |

//|                                                 Fernando Morales |

//|                                             https://www.mql5.com |

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

#property copyright "Fernando Morales"

#property link      "https://www.mql5.com"

#property version   "1.00"

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

//| Script program start function                                    |

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

void OnStart()

  {

//---



//--- declare and initialize the trade request and result of trade request

   MqlTradeRequest request;

   MqlTradeResult  result;



   double priceTarget=ChartPriceOnDropped();



   ulong ticket;

   int pos_total=PositionsTotal();

   double price_TP=0,price_SL=0;



   printf("Found %d open positions",pos_total);

   Print("Target price is ",priceTarget);



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



      if((ticket=PositionGetTicket(i))>0)

        {

         if(PositionGetSymbol(i)!=_Symbol) continue;



         double price_bid = SymbolInfoDouble( _Symbol, SYMBOL_BID );

         double price_ask = SymbolInfoDouble( _Symbol, SYMBOL_ASK );



         // Obtaining current millisecond count

         ulong ms_inicial=GetTickCount();



         switch((int)PositionGetInteger(POSITION_TYPE))

           {

            case 0: // BUY



               if(priceTarget>price_bid)

                 {

                  price_SL = PositionGetDouble( POSITION_SL );

                  price_TP = priceTarget;

                 }

               else if(priceTarget<price_bid)

                 {

                  price_SL = priceTarget;

                  price_TP = PositionGetDouble( POSITION_TP );

                 }

               break;



            case 1:   // SELL



               if(priceTarget<price_ask)

                 {

                  price_SL = PositionGetDouble( POSITION_SL );

                  price_TP = priceTarget;

                 }

               else if(priceTarget>price_ask)

                 {

                  price_SL = priceTarget;

                  price_TP = PositionGetDouble( POSITION_TP );

                 }



               break;

           }



         //--- zeroing the request and result values

         ZeroMemory(request);

         ZeroMemory(result);



         //--- setting the operation parameters

         request.action= TRADE_ACTION_SLTP;// type of trade operation

         request.position= ticket;         // ticket of the position

         request.symbol= _Symbol;         // symbol 

         request.sl      = price_SL;      // Stop Loss of the position

         request.tp      = price_TP;      // Take Profit of the position



         //--- output information about the modification

         PrintFormat("Updating trade #%I64d: SL=%f .. TP= %f",ticket,price_SL,price_TP);



         //--- send the request

         if(!OrderSend(request,result))

            printf("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code



         //--- information about the operation   

         printf("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);

         printf("####---> Updating the trade took %d ms",GetTickCount()-ms_inicial);



        }

  }

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

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