CloseAllPositions_v1

Author: Wen Tao Xiong
Orders Execution
It automatically opens orders when conditions are reached
0 Views
0 Downloads
0 Favorites
CloseAllPositions_v1
ÿþ//+------------------------------------------------------------------+

//|                                                   |

//|                                         |

//|                                        http://www.popoding.club/ |

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

#property copyright "Wen Tao Xiong"

#property link      "http://www.popoding.club/"

#property version   "1.00"

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

//| Script program start function                                    |

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

void OnStart()

  {

//---

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

     {



      ulong pos_ticket = PositionGetTicket(i);

      ulong pos_magic = PositionGetInteger(POSITION_MAGIC);

      string pos_symbol = PositionGetString(POSITION_SYMBOL);

      double pos_volume = PositionGetDouble(POSITION_VOLUME);

      double pos_profit = PositionGetDouble(POSITION_PROFIT);

      ENUM_POSITION_TYPE pos_type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);



      if(pos_magic == 0 && pos_symbol == "EURUSD" && pos_profit < 0)

        {



         MqlTradeRequest requst = {};

         MqlTradeResult result = {};



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

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



         requst.action = TRADE_ACTION_DEAL;

         requst.position = pos_ticket;

         requst.symbol = pos_symbol;

         requst.volume = pos_volume;

         requst.deviation = 50;

         requst.magic = pos_magic;



         int filling_mode = (int)SymbolInfoInteger(Symbol(),SYMBOL_FILLING_MODE);

         if(filling_mode == 1)

           {

            requst.type_filling = ORDER_FILLING_FOK;

           }

         else

            if(filling_mode == 2)

              {

               requst.type_filling = ORDER_FILLING_IOC;

              }

            else

               if(filling_mode == 3)

                 {

                  requst.type_filling = ORDER_FILLING_RETURN;

                 }



         if(pos_type == POSITION_TYPE_BUY)

           {

            requst.type = ORDER_TYPE_SELL;

            requst.price = bid;

           }

         else

            if(pos_type == POSITION_TYPE_SELL)

              {

               requst.type = ORDER_TYPE_BUY;

               requst.price = ask;

              }



         if(!OrderSend(requst,result))

           {

            printf("OrderSend Error %d",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 ---