Bull vs Medved

Author: Copyright © 2008, Andrey Kuzmenko (Foxbat)
Price Data Components
Series array that contains open time of each barSeries array that contains open prices of each barSeries array that contains close prices for each bar
Orders Execution
Checks for the total of open orders
Miscellaneous
It plays sound alerts
0 Views
0 Downloads
0 Favorites
Bull vs Medved
ÿþ//+------------------------------------------------------------------+

//|                      Bull vs Medved(barabashkakvn's edition).mq5 |

//|                       Copyright © 2008, Andrey Kuzmenko (Foxbat) |

//|                                          mailto:foxbat-b@mail.ru |

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

//| 5:>B>@K5 ">1I5?>;57=K5" DC=:F88 :>40, B@59;8=3 AB>?, =0?@8<5@,  |

//| 1K;8 ;N157=> ?>708<AB2>20=K 87 4@C38E M:A?5@B>2.                 |

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

#property copyright "Copyright © 2008, Andrey Kuzmenko (Foxbat)"

#property link      "foxbat-b@mail.ru"

#property version   "1.001"

//---

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>  

#include <Trade\AccountInfo.mqh>

#include <Trade\OrderInfo.mqh>

CTrade         m_trade;                      // trading object

CSymbolInfo    m_symbol;                     // symbol info object

CAccountInfo   m_account;                    // account info wrapper

COrderInfo     m_order;                      // pending orders object

//---

input double   Lots           = 0.10;        // @07<5@ ;>B0

input ushort   InpCandleSize  = 75;          // @07<5@ B5;0 A25G8

input ushort   InpStopLoss    = 60;          // StopLoss

input ushort   InpTakeProfit  = 60;          // TakeProfit

input ushort   InpIndentUP    = 16;          // >BABC? 4;O Buylimit

input ushort   InpIndentDOWN  = 20;          // >BABC? 4;O Selllimit

input string   StartTime      = "0:05";      // @5<O AB0@B0 ?> 3@8=28GC

input string   StartTime1     = "4:05";

input string   StartTime2     = "8:05";

input string   StartTime3     = "12:05";

input string   StartTime4     = "16:05";

input string   StartTime5     = "20:05";

//---

bool           trade          = false;

bool           trade1         = false;

double         Limit          = 400;

string         Name_Expert    = "Bull vs Medved";

bool           UseSound       = true;

string         NameFileSound  = "alert.wav";

//---

ulong          m_magic=612453;               // magic number

ulong          m_slippage=0;                 // slippage

ENUM_ACCOUNT_MARGIN_MODE m_margin_mode;

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

double         ExtCandleSize  = 0;

double         ExtIndentUP    = 0;

double         ExtIndentDOWN  = 0;

double         ExtStopLoss    = 0;

double         ExtTakeProfit  = 0;

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

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

   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;

   ExtCandleSize  = InpCandleSize * m_adjusted_point;

   ExtIndentUP    = InpIndentUP   * m_adjusted_point;

   ExtIndentDOWN  = InpIndentDOWN * m_adjusted_point;

   ExtStopLoss    = InpStopLoss   * m_adjusted_point;

   ExtTakeProfit  = InpTakeProfit * m_adjusted_point;

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//--- ?5@52>48< 2@5<O 87 AB@>G=>9 25;8G8=K StartTime 2> 2@5<5==#N TimeStart

   datetime TimeStart=StringToTime(StartTime);

   datetime TimeStart1=StringToTime(StartTime1);

   datetime TimeStart2=StringToTime(StartTime2);

   datetime TimeStart3=StringToTime(StartTime3);

   datetime TimeStart4=StringToTime(StartTime4);

   datetime TimeStart5=StringToTime(StartTime5);

//--- 5A;8 B5:CI55 2@5<O <5=LH5 AB0@B>2>3> 8;8 1>;LH5 53> =0 5 <8=CB, B> 2KE>48< 8 =8G53> =5 45;05<.

//--- => ?@5420@8B5;L=> 45;05< ?5@5<5==CN trade ;>6=>9. @>AB> A1@0AK205< 8=D>@<0F8N > B><, GB> C65 >B:@K20;8AL.

   if((TimeCurrent()<TimeStart || TimeCurrent()>TimeStart+300) && 

      (TimeCurrent()<TimeStart1 || TimeCurrent()>TimeStart1+300) && 

      (TimeCurrent()<TimeStart2 || TimeCurrent()>TimeStart2+300) && 

      (TimeCurrent()<TimeStart3 || TimeCurrent()>TimeStart3+300) && 

      (TimeCurrent()<TimeStart4 || TimeCurrent()>TimeStart4+300) && 

      (TimeCurrent()<TimeStart5 || TimeCurrent()>TimeStart5+300))

     {

      trade=false;

      return;

     }



   if(trade)

      return;



   if(m_account.FreeMargin()<(1000*Lots))

     {

      Print("We have no money. Free Margin = ",m_account.FreeMargin());

      return;

     }

   if(!IsPendingOrders())

     {

      if(IsBull()==true && IsBadBull()==false)

        {

         PlacementBuyLimit();

         return;

        }

     }



   if(!IsPendingOrders())

     {

      if(IsCoolBull()==true)

        {

         PlacementBuyLimit();

         return;

        }

     }



   if(!IsPendingOrders())

     {

      if(IsBear()==true)

        {

         PlacementSellLimit();

         return;

        }

     }



   return;

  }

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

//|                                                                  |

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

bool IsBull()

  {

   if((iClose(m_symbol.Name(),Period(),3)>iOpen(m_symbol.Name(),Period(),2)) &&

      (iClose(m_symbol.Name(),Period(),2)-iOpen(m_symbol.Name(),Period(),2)>=10*m_symbol.Point()) &&

      (iClose(m_symbol.Name(),Period(),1)-iOpen(m_symbol.Name(),Period(),1)>=ExtCandleSize))

      return(true);

   else

      return(false);

  }

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

//|                                                                  |

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

bool IsBadBull()

  {

   if((iClose(m_symbol.Name(),Period(),3)-iOpen(m_symbol.Name(),Period(),3)>=10*m_symbol.Point()) &&

      (iClose(m_symbol.Name(),Period(),2)-iOpen(m_symbol.Name(),Period(),2)>=10*m_symbol.Point()) &&

      (iClose(m_symbol.Name(),Period(),1)-iOpen(m_symbol.Name(),Period(),1)>=ExtCandleSize))

      return(true);

   else

      return(false);

  }

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

//|                                                                  |

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

bool IsCoolBull()

  {

   if((iOpen(m_symbol.Name(),Period(),2)-iClose(m_symbol.Name(),Period(),2)>=20*m_symbol.Point()) && 

      (iClose(m_symbol.Name(),Period(),2)<=iOpen(m_symbol.Name(),Period(),1)) && 

      (iClose(m_symbol.Name(),Period(),1)>iOpen(m_symbol.Name(),Period(),2)) && 

      (iClose(m_symbol.Name(),Period(),1)-iOpen(m_symbol.Name(),Period(),1)>=0.4*ExtCandleSize))

      return(true);

   else

      return(false);

  }

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

//|                                                                  |

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

bool IsBear()

  {

   if((iOpen(m_symbol.Name(),Period(),1)-iClose(m_symbol.Name(),Period(),1)>=ExtCandleSize))

      return(true);

   else

      return(false);

  }

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

//| >8A: >B;>65==KE >@45@>2                                           |

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

bool IsPendingOrders()

  {

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

      if(m_order.SelectByIndex(i))     // selects the pending order by index for further access to its properties

         if(m_order.Symbol()==m_symbol.Name() && m_order.Magic()==m_magic)

            return(true);



   return(false);

  }

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

//|  07<5I05< Buy Limit:                                             |

//| |                                                                |

//| ||                                                               |

//|  ||                                                              |

//|  ||                                                              |

//|   | - B5:CI0O F5=0 Ask                                           |

//|   | - B5:CI0O F5=0 Bid                                           |

//|   ||                                                             |

//|    ||                                                            |

//|     |                                                            |

//|     | - 745AL C@>25=L Buy Limit                                  |

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

void PlacementBuyLimit()

  {

//--- 4;O >B;>65==KE >@45@>2 2KAB02;O5< 2@5<O 8AB5G5=8O @02=>5 + G5BK@5 G0A0

   if(!RefreshRates())

      return;



   double price=m_symbol.Ask()-ExtIndentUP;

   double sl   =price-ExtStopLoss;

   double tp   =price+ExtTakeProfit;



   if(!m_trade.BuyLimit(Lots,price,NULL,sl,tp,ORDER_TIME_SPECIFIED,TimeCurrent()+4*60*60,Name_Expert))

     {

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

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

     }

   else

     {

      if(UseSound)

         PlaySound(NameFileSound);

     }

  }

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

//|  07<5I05< Sell Limit:                                            |

//|     | - 745AL C@>25=L Sell Limit                                 |

//|     |                                                            |

//|    ||                                                            |

//|   ||                                                             |

//|   | - B5:CI0O F5=0 Ask                                           |

//|   | - B5:CI0O F5=0 Bid                                           |

//|  ||                                                              |

//|  ||                                                              |

//| ||                                                               |

//| |                                                                |

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

void PlacementSellLimit()

  {

//--- 4;O >B;>65==KE >@45@>2 2KAB02;O5< 2@5<O 8AB5G5=8O @02=>5 + G5BK@5 G0A0

   if(!RefreshRates())

      return;



   double price=m_symbol.Bid()+ExtIndentUP;

   double sl   =price+ExtStopLoss;

   double tp   =price-ExtTakeProfit;



   if(!m_trade.SellLimit(Lots,price,NULL,sl,tp,ORDER_TIME_SPECIFIED,TimeCurrent()+4*60*60,Name_Expert))

     {

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

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

     }

   else

     {

      if(UseSound)

         PlaySound(NameFileSound);

     }

  }

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

//|                                                                  |

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

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

  }

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

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