Pinball machine

Author: Copyright © 2017, Vladimir Karputov
Price Data Components
Miscellaneous
It plays sound alerts
0 Views
0 Downloads
0 Favorites
Pinball machine
ÿþ//+------------------------------------------------------------------+

//|                     Pinball machine(barabashkakvn's edition).mq5 |

//|                              Copyright © 2017, Vladimir Karputov |

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

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

#property copyright "Copyright © 2017, Vladimir Karputov"

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

#property version   "1.001"

//---

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>  

#include <Trade\AccountInfo.mqh>

#include <Expert\Money\MoneyFixedMargin.mqh>

CPositionInfo  m_position;                   // trade position object

CTrade         m_trade;                      // trading object

CSymbolInfo    m_symbol;                     // symbol info object

CAccountInfo   m_account;                    // account info wrapper

CMoneyFixedMargin m_money;                   // 

//--- input parameters

input double   InpPercentRisk=10;          // % risk (from 1 to 90)

//---

ulong          m_magic        = 28081975;    // 038G5A:>5 G8A;>  

ulong          m_slippage     = 30;          // @>A:0;L7K20=85 F5=K

int            NumberOfTry    = 5;           // >;8G5AB2> B>@3>2KE ?>?KB>:

bool           UseSound       = true;        // A?>;L7>20BL 72C:>2>9 A83=0;

string         NameSound      = "expert.wav";// 08<5=>20=85 72C:>2>3> D09;0

//---

ENUM_ACCOUNT_MARGIN_MODE m_margin_mode;

double         m_adjusted_point;             // point value adjusted for 3 or 5 points

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   SetMarginMode();

   if(!IsHedging())

     {

      Print("Hedging only!");

      return(INIT_FAILED);

     }

//---

   m_symbol.Name(Symbol());                  // sets symbol name

   if(!RefreshRates())

     {

      Print("Error RefreshRates. Bid=",DoubleToString(m_symbol.Bid(),Digits()),

            ", Ask=",DoubleToString(m_symbol.Ask(),Digits()));

      return(INIT_FAILED);

     }

   m_symbol.Refresh();

//---

   m_trade.SetExpertMagicNumber(m_magic);

//---

   m_trade.SetDeviationInPoints(m_slippage);

//--- tuning for 3 or 5 digits

   int digits_adjust=1;

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

      digits_adjust=10;

   m_adjusted_point=m_symbol.Point()*digits_adjust;

//---

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

      return(INIT_FAILED);

   m_money.Percent(InpPercentRisk); // 10% risk

//---

   MathSrand(GetTickCount());

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//--- open positions

   double sl=0.0,tp=0.0;



   int value1  = GetRand(0,100);

   int value2  = GetRand(0,100);

   int value3  = GetRand(0,100);

   int value4  = GetRand(0,100);



   int    StopLoss      = 0;

   int    TakeProfit    = 0;

   if(value1==value2)

     {

      StopLoss      = GetRand(10,100);             //  07<5@ AB>?0 2 ?C=:B0E

      TakeProfit    = GetRand(10,100);             //  07<5@ B59:0 2 ?C=:B0E



      int count=0;

      bool result_refresh=false;

      //--- NumberOfTry ?>?KB>: >1=>28BL F5=K

      do

        {

         result_refresh=RefreshRates();

         count++;

        }

      while(!result_refresh || count>NumberOfTry);

      if(!result_refresh)

         return;



      sl=m_symbol.Ask()-StopLoss*m_adjusted_point;

      tp=m_symbol.Ask()+TakeProfit*m_adjusted_point;



      OpenBuy(sl,tp);

     }



   if(value3==value4)

     {

      StopLoss       = GetRand(10,100);            //  07<5@ AB>?0 2 ?C=:B0E

      TakeProfit     = GetRand(10,100);            //  07<5@ B59:0 2 ?C=:B0E



      int count=0;

      bool result_refresh=false;

      //--- NumberOfTry ?>?KB>: >1=>28BL F5=K

      do

        {

         result_refresh=RefreshRates();

         count++;

        }

      while(!result_refresh || count>NumberOfTry);

      if(!result_refresh)

         return;



      sl=m_symbol.Bid()+StopLoss*m_adjusted_point;

      tp=m_symbol.Bid()-TakeProfit*m_adjusted_point;



      OpenSell(sl,tp);

     }

  }

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

//| 2B>@ - 2AB@0B5=:> <8B@89  (35=5@0B>@ A;CG09=KE G8A5;)         |

//| ?@8<5@ 2K7>20 DC=:F88        int value1  = GetRand(0,10);        |

//| 2 8=8B 2AB028BL     MathSrand(TimeLocal());                      |

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

int GetRand(int vFrom,int vTo)

  {

   int vV=0;

   while(true)

     {

      vV=MathRand();

      if(vV>=vFrom && vV<=vTo)

         break;

     }

   return(vV);

  }

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

//|                                                                  |

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

void SetMarginMode(void)

  {

   m_margin_mode=(ENUM_ACCOUNT_MARGIN_MODE)AccountInfoInteger(ACCOUNT_MARGIN_MODE);

  }

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

//|                                                                  |

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

bool IsHedging(void)

  {

   return(m_margin_mode==ACCOUNT_MARGIN_MODE_RETAIL_HEDGING);

  }

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

//| 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);

  }

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

//| Open Buy position                                                |

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

void OpenBuy(double sl,double tp)

  {

   sl=m_symbol.NormalizePrice(sl);

   tp=m_symbol.NormalizePrice(tp);



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

//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));

   if(check_open_long_lot==0.0)

      return;



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

        {

         if(m_trade.Buy(check_open_long_lot,NULL,m_symbol.Ask(),sl,tp))

           {

            if(m_trade.ResultDeal()==0)

              {

               Print("Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),

                     ", description of result: ",m_trade.ResultRetcodeDescription());

              }

            else

              {

               Print("Buy -> true. Result Retcode: ",m_trade.ResultRetcode(),

                     ", description of result: ",m_trade.ResultRetcodeDescription());

               if(UseSound)

                  PlaySound(NameSound);

              }

           }

         else

           {

            Print("Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),

                  ", description of result: ",m_trade.ResultRetcodeDescription());

           }

        }

//---

  }

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

//| Open Sell position                                               |

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

void OpenSell(double sl,double tp)

  {

   sl=m_symbol.NormalizePrice(sl);

   tp=m_symbol.NormalizePrice(tp);



   double check_open_short_lot=m_money.CheckOpenShort(m_symbol.Bid(),sl);

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

//      ", CheckOpenLong: ",DoubleToString(check_open_short_lot,2),

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

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

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

   if(check_open_short_lot==0.0)

      return;



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

   double chek_volime_lot=m_trade.CheckVolume(m_symbol.Name(),check_open_short_lot,m_symbol.Bid(),ORDER_TYPE_SELL);



   if(chek_volime_lot!=0.0)

      if(chek_volime_lot>=check_open_short_lot)

        {

         if(m_trade.Sell(check_open_short_lot,NULL,m_symbol.Bid(),sl,tp))

           {

            if(m_trade.ResultDeal()==0)

              {

               Print("Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),

                     ", description of result: ",m_trade.ResultRetcodeDescription());

              }

            else

              {

               Print("Sell -> true. Result Retcode: ",m_trade.ResultRetcode(),

                     ", description of result: ",m_trade.ResultRetcodeDescription());

               if(UseSound)

                  PlaySound(NameSound);

              }

           }

         else

           {

            Print("Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),

                  ", description of result: ",m_trade.ResultRetcodeDescription());

           }

        }

//---

  }

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

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