Author: Copyright © 2020, Vladimir Karputov
Price Data Components
0 Views
0 Downloads
0 Favorites
Fixed Loss
ÿþ//+------------------------------------------------------------------+

//|                                                   Fixed Loss.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

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

#property copyright "Copyright © 2020, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "1.000"

//---

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>

#include <Trade\AccountInfo.mqh>

#include <Expert\Money\MoneyFixedRisk.mqh>

//---

CTrade         m_trade;                      // trading object

CSymbolInfo    m_symbol;                     // symbol info object

CAccountInfo   m_account;                    // account info wrapper

CMoneyFixedRisk *m_money;                    // object of CMoneyFixedRisk class

//--- input parameters

input ENUM_POSITION_TYPE   InpPositions   = POSITION_TYPE_BUY;    // Position Type

input ushort               InpStopLoss    = 1000;                 // Stop Loss, in points (1.00045-1.00055=10 points)

input double               InpLoss        = 100;                  // Loss, in money

input bool                 InpPrintLog    = false;                // Print log

//---

double   m_stop_loss                      = 0.0;                  // Stop Loss   -> double

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   if(!m_symbol.Name(Symbol())) // sets symbol name

     {

      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");

      return(INIT_FAILED);

     }

   RefreshRates();

//--- tuning for 3 or 5 digits

   int digits_adjust=1;

   if(m_symbol.Digits()==3 || m_symbol.Digits()==5)

      digits_adjust=10;

   m_stop_loss = InpStopLoss  * m_symbol.Point();

//---

   if(m_money!=NULL)

      delete m_money;

   m_money=new CMoneyFixedRisk;

   if(m_money!=NULL)

     {

      if(!m_money.Init(GetPointer(m_symbol),Period(),m_symbol.Point()*digits_adjust))

         return(INIT_FAILED);

      double balance=m_account.Balance();

      /*

      balance  -  100%

      InpLoss  -  x%

      x = InpLoss * 100.0 / balance

      */

      m_money.Percent(InpLoss*100.0/balance);

     }

   else

     {

      Print(__FILE__," ",__FUNCTION__,", ERROR: Object CMoneyFixedMargin is NULL");

      return(INIT_FAILED);

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

   if(!RefreshRates())

      return;

//--- protection against the return value of "zero"

   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)

      return;



   if(PositionsTotal()==0)

     {

      double balance=m_account.Balance();

      /*

      balance  -  100%

      InpLoss  -  x%

      x = InpLoss * 100.0 / balance

      */

      m_money.Percent(InpLoss*100.0/balance);

      //--- getting lot size for open long position (CMoneyFixedRisk)

      double sl=0.0;

      double check_open_long_lot=0.0;

      //--- variant #1: StopLoss=0.0

      sl=0.0;

      check_open_long_lot=m_money.CheckOpenLong(m_symbol.Ask(),sl);

      if(check_open_long_lot==0.0)

         return;

      if(InpPrintLog)

         Print("sl=0.0",

               ", CheckOpenLong: ",DoubleToString(check_open_long_lot,2),

               ", Balance: ",    DoubleToString(m_account.Balance(),2),

               ", Equity: ",     DoubleToString(m_account.Equity(),2),

               ", FreeMargin: ", DoubleToString(m_account.FreeMargin(),2));

      //--- variant #2: StopLoss!=0.0

      sl=m_symbol.Ask()-m_stop_loss;

      check_open_long_lot=m_money.CheckOpenLong(m_symbol.Ask(),sl);

      if(check_open_long_lot==0.0)

         return;

      if(InpPrintLog)

         Print("sl=",DoubleToString(sl,m_symbol.Digits()),

               ", CheckOpenLong: ",DoubleToString(check_open_long_lot,2),

               ", Balance: ",    DoubleToString(m_account.Balance(),2),

               ", Equity: ",     DoubleToString(m_account.Equity(),2),

               ", FreeMargin: ", DoubleToString(m_account.FreeMargin(),2));





      //--- check volume before OrderSend to avoid "not enough money" error (CTrade)

      double chek_volime_lot=m_trade.CheckVolume(m_symbol.Name(),check_open_long_lot,m_symbol.Ask(),ORDER_TYPE_BUY);



      if(chek_volime_lot!=0.0)

        {

         if(chek_volime_lot>=check_open_long_lot)

           {

            m_trade.Buy(chek_volime_lot,NULL,m_symbol.Ask(),m_symbol.Ask()-m_stop_loss,m_symbol.Ask()+m_stop_loss);

           }

         else

           {

            if(InpPrintLog)

               Print("CMoneyFixedRisk lot = ",DoubleToString(check_open_long_lot,2),

                     ", CTrade lot = ",DoubleToString(chek_volime_lot,2));

           }

        }

      //---

     }

  }

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

//| Refreshes the symbol quotes data                                 |

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

bool RefreshRates()

  {

//--- refresh rates

   if(!m_symbol.RefreshRates())

      return(false);

//--- protection against the return value of "zero"

   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)

      return(false);

//---

   return(true);

  }

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

Comments