Scalper Mars

Author: Copyright 2018, Igor Bulgakov
Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
Orders Execution
It automatically opens orders when conditions are reachedIt can change open orders parameters, due to possible stepping strategyChecks for the total of open orders
Indicators Used
Moving average indicatorBollinger bands indicatorMACD HistogramRelative strength index
0 Views
0 Downloads
0 Favorites
Scalper Mars
ÿþ//+------------------------------------------------------------------+

//|                                                 Scalper Mars.mq4 |

//|                                    Copyright 2018, Igor Bulgakov |

//|                                                http://bigfexp.ru |

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

#property copyright "Copyright 2018, Igor Bulgakov"

#property link      "http://bigfexp.ru"

#property version   "4.00"

#property description "Advisor Scalper Mars intraday scalper on timeframe M15." 

#property description "The input parameters are optimized for EURUSD, USDJPY, EURCHF."         

#property strict

//----------------------------------------------------

extern double Lots         = 0.1;

extern int    MaxStopLoss  = 50; //MaxStopLoss (15-100)



extern int    StartTime    = 00; //StartTime (00-23)

extern int    EndTime      = 23; //EndTime (00-23)



int magic=20180512;

double SL,TP;

int ticket,sbars=10;

double minprice=999999,maxprice=-999999;

string g_comment_132="Scalper Mars";

int maxstoploss, starttime, endtime, ma_period, bb_period, fast_emaperiod, slow_emaperiod, signal_smaperiod, rsi_period, minsl;



//---- input parameters EMA

extern int Ma_period=5; //Ma_period (2-10)



//---- input parameters BB

extern int Bb_period=25; //Bb_period (15-30)



//---- input parameters MACD

extern int Fast_emaperiod   = 10; //Fast_emaperiod (5-10)

extern int Slow_emaperiod   = 19; //Slow_emaperiod (15-20)

extern int Signal_smaperiod = 11; //Signal_smaperiod (5-15)



//---- input parameters RSI

extern int Rsi_period=15; //Rsi_period (10-20)

extern int Minsl=13; //Minsl (5-15)



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

//| Expert initialization function                                   |

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

int OnInit()

  {

   maxstoploss=MaxStopLoss;

   if(maxstoploss < 15) maxstoploss = 15;

   if(maxstoploss > 100) maxstoploss = 100;



   starttime=StartTime;

   if(starttime < 0) starttime = 0;

   if(starttime > 23) starttime = 23;



   endtime=EndTime;

   if(endtime < 0) endtime = 0;

   if(endtime > 23) endtime = 23;



   ma_period=Ma_period;

   if(ma_period < 2) ma_period = 2;

   if(ma_period > 10) ma_period = 10;



   bb_period=Bb_period;

   if(bb_period < 15) bb_period = 15;

   if(bb_period > 30) bb_period = 30;



   fast_emaperiod=Fast_emaperiod;

   if(fast_emaperiod < 5)  fast_emaperiod = 5;

   if(fast_emaperiod > 10) fast_emaperiod = 10;



   slow_emaperiod=Slow_emaperiod;

   if(slow_emaperiod < 15)  slow_emaperiod = 15;

   if(slow_emaperiod > 20) slow_emaperiod = 20;



   signal_smaperiod=Signal_smaperiod;

   if(signal_smaperiod < 5)  signal_smaperiod = 5;

   if(signal_smaperiod > 15) signal_smaperiod = 15;



   rsi_period=Rsi_period;

   if(rsi_period < 10)  rsi_period = 10;

   if(rsi_period > 20) rsi_period = 20;



   minsl=Minsl;

   if(minsl < 5)  minsl = 5;

   if(minsl > 15) minsl = 15;



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

     {

      maxstoploss *= 10;

      minsl       *= 10;

     }

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

   if(DayOfWeek() == 0 || DayOfWeek() == 6) return;

   if(!IsTradeAllowed()) return;



   double lots;

   lots=GetLot(Lots);



   string sd;

   bool vallors=CheckVolumeValue(lots,sd);

   if(vallors == false) return;



   bool cmft1 = CheckMoneyForTrade(_Symbol, lots, OP_BUY);

   bool cmft2 = CheckMoneyForTrade(_Symbol, lots, OP_SELL);



   double ema1  = iMA(NULL, 0, ma_period, 0, MODE_EMA, PRICE_CLOSE, 1);

   double ema3  = iMA(NULL, 0, ma_period, 0, MODE_EMA, PRICE_CLOSE, 3);

   double bands = iBands(NULL, 0, bb_period, 2, 0, PRICE_CLOSE, 0, 1);

   double macd  = iMACD(NULL, 0, fast_emaperiod, slow_emaperiod, signal_smaperiod, PRICE_CLOSE, 0, 1);

   double rsi   = iRSI(NULL, 0, rsi_period, PRICE_CLOSE, 1);



   int cc1 = CountCmd(OP_BUY, magic);

   int cc2 = CountCmd(OP_SELL, magic);



   maxprice = GetMaxPrice(sbars, maxprice);

   minprice = GetMinPrice(sbars, minprice);

   double slbuy  = (Ask - minprice)/Point;

   double slsell = (maxprice - Bid)/Point;



//*------------------------

   datetime  l_datetime_108=TimeCurrent();

   if(TimeHour(l_datetime_108)>=starttime && TimeHour(l_datetime_108)<=endtime) 

     {



      if(cmft1==true && cc1+cc2==0 && ema1>bands && ema3<bands && macd>0 && rsi>50 && slbuy>=minsl && slbuy<=minsl*5)



        {

         ticket=OrderSend(Symbol(),OP_BUY,lots,Ask,2,0,0,g_comment_132,magic,0,Green);



         if(ticket>0)

           {

            SL=minprice;

            if(Bid-minprice>maxstoploss*Point) SL=Bid-maxstoploss*Point;

            TP=Bid+(Bid-minprice);

            if(OrderSelect(ticket,SELECT_BY_TICKET))

               if(!OrderModify(ticket,OrderOpenPrice(),SL,TP,0))

                  Print("Error of modification of a buy order");

           }

         else Print("Error opening a buy order");



        }

      if(cmft2==true && cc1+cc2==0 && ema1<bands && ema3>bands && macd<0 && rsi<50 && slsell>=minsl && slsell<=minsl*5)



        {

         ticket=OrderSend(Symbol(),OP_SELL,lots,Bid,2,0,0,g_comment_132,magic,0,Red);



         if(ticket>0)

           {

            SL=maxprice;

            if(Ask+maxprice>maxstoploss*Point) SL=Ask+maxstoploss*Point;

            TP=Ask -(maxprice-Ask);

            if(OrderSelect(ticket,SELECT_BY_TICKET))

               if(!OrderModify(ticket,OrderOpenPrice(),SL,TP,0))

                  Print("Error modifying the sell order!");

           }

         else Print("Error opening a sell order!");



        }



     }



  }

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

double GetLot(double Lot)

  {

   double Free=AccountFreeMargin();

   double One_Lot =MarketInfo(Symbol(),MODE_MARGINREQUIRED);

   double Min_Lot =MarketInfo(Symbol(),MODE_MINLOT);

   double Max_Lot =MarketInfo(Symbol(),MODE_MAXLOT);

   if(Lot<Min_Lot) Lot=Min_Lot;

   if(Lot>Max_Lot) Lot=Max_Lot;

   if(Lot*One_Lot>Free) return(0.0);

   return(Lot);

  }

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

int CountCmd(int Cmd,int fMagic)

  {

   int count=0;

   for(int trade=OrdersTotal()-1; trade>=0; trade--)

     {

      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))

        {

         if(OrderSymbol()==Symbol() && OrderMagicNumber()==fMagic && OrderType()==Cmd)

            count++;

        }



     }

   return(count);

  }

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

double GetMaxPrice(int bc,double maxpr)

  {

   for(int i=1; i<=bc; i++)

     {

      double mp=iHigh(NULL,0,i);

      if(mp>maxpr)

         maxpr=mp;

     }

   return(maxpr);

  }

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

double GetMinPrice(int bc,double minpr)

  {

   for(int i=1; i<=bc; i++)

     {

      double mp=iLow(NULL,0,i);

      if(mp<minpr)

         minpr=mp;

     }

   return(minpr);

  }

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

bool CheckMoneyForTrade(string symb,double lots,int type)

  {

   double free_margin=AccountFreeMarginCheck(symb,type,lots);

//-- if money is not enough

   if(free_margin<0)

     {

      string oper=(type==OP_BUY)? "Buy":"Sell";

      Print("Not enough money for ",oper," ",lots," ",symb," Error code=",GetLastError());

      return(false);

     }

//-- the check was successful

   return(true);

  }

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

bool CheckVolumeValue(double volume,string &description)

  {



   double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);

   if(volume<min_volume)

     {

      description=StringFormat("Volume is less than the minimum allowable SYMBOL_VOLUME_MIN=%.2f",min_volume);

      return(false);

     }



   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);

   if(volume>max_volume)

     {

      description=StringFormat("Volume is more than the maximum allowable SYMBOL_VOLUME_MAX=%.2f",max_volume);

      return(false);

     }



   double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);



   int ratio=(int)MathRound(volume/volume_step);

   if(MathAbs(ratio*volume_step-volume)>0.0000001)

     {

      description=StringFormat("The volume is not a multiple of the minimum gradation SYMBOL_VOLUME_STEP=%.2f, closest correct volume %.2f",

                               volume_step,ratio*volume_step);

      return(false);

     }

   description="Correct value of volume";

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