Trailing Equity Money

Author: Copyright © 2019, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Trailing Equity Money
ÿþ//+------------------------------------------------------------------+

//|                                        Trailing Equity Money.mq5 |

//|                              Copyright © 2019, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

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

#property copyright "Copyright © 2019, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.000"

/*

   barabashkakvn Trading engine 3.109

*/

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\AccountInfo.mqh>

//---

CPositionInfo  m_position;                   // object of CPositionInfo class

CTrade         m_trade;                      // object of CTrade class

CAccountInfo   m_account;                    // object of CAccountInfo class

//--- input parameters

input double   InpEquityStart    = 5000;     // Get started with Equity >=

input double   InpEquityDecrease = 300;      // Close all with a decrease of

//---

double   m_prev_max_equity;                  // previous maximum Equity

bool     m_need_close_all        = false;    // close all positions

string   m_global_variable_name  = "TEM";

bool     m_pause                 = false;

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   Print("Balance: ",DoubleToString(m_account.Balance(),2)," Equity: ",DoubleToString(m_account.Equity(),2));

   if(GlobalVariableCheck(m_global_variable_name))

      m_prev_max_equity=GlobalVariableGet(m_global_variable_name);

   else

      GlobalVariableSet(m_global_variable_name,m_account.Equity());



   if(m_pause)

      m_pause=false;

   Print("START");

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

   if(m_need_close_all)

     {

      if(IsPositionExists())

        {

         CloseAllPositions();

         return;

        }

      else

         m_need_close_all=false;

     }



   if(m_pause)

      return;



   if(PositionsTotal()==0)

      return;

   double current_equity=m_account.Equity();

   if(current_equity>m_prev_max_equity)

     {

      m_prev_max_equity=current_equity;

      GlobalVariableSet(m_global_variable_name,m_prev_max_equity);

      GlobalVariableDel(m_global_variable_name);

     }

   else

     {

      if(current_equity<=m_prev_max_equity-InpEquityDecrease)

        {

         m_need_close_all=true;

         m_pause=true;

         Print("PAUSE");

        }

     }

  }

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

//| Is position exists                                               |

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

bool IsPositionExists(void)

  {

   for(int i=PositionsTotal()-1; i>=0; i--)

      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

         return(true);

//---

   return(false);

  }

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

//| Close all positions                                              |

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

void CloseAllPositions(void)

  {

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions

      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

         m_trade.PositionClose(m_position.Ticket()); // close a position by the specified m_symbol

  }

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

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