Author: FORTRADER.RU
Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
Orders Execution
Checks for the total of open orders
Indicators Used
Parabolic Stop and Reverse system
1 Views
0 Downloads
0 Favorites
Fibo iSAR
ÿþ//+---------------------------------------------------------------------------0+|

//|                                      Fibo iSAR(barabashkakvn's edition).mq5 |

//|                             07@01>B0=> 4;O 39 2K?CA:0 6C@=0;0  FORTRADER.RU |

//|                                                                             |

//| ?8A0=85 AB@0B5388:         http://www.forexsystems.ru/showthread.php?t=5495|

//| ?8A0=85 =5:>B>@KE DC=:F89: http://fxnow.ru/blog/programming_mql4/          |

//| >=B0:BK:                   yuriy@fortrader.ru                              |

//+----------------------------------------------------------------------------+|

//|4-150

#property copyright "FORTRADER.RU"

#property link      "http://FORTRADER.RU"

#property version   "1.001"



#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>  

#include <Trade\AccountInfo.mqh>

#include<Trade\OrderInfo.mqh>

CPositionInfo  m_position;                   // trade position object

CTrade         m_trade;                      // trading object

CSymbolInfo    m_symbol;                     // symbol info object

CAccountInfo   m_account;                    // account info wrapper

COrderInfo     m_order;                      // pending orders object



#define MODE_LOW 1

#define MODE_HIGH 2



input int      InpControlTime       =false;  // Control Time

input uchar    IntStartTime         =7;      // Start Time (hour)

input uchar    IntStopTime          =17;     // Stop Time (hour)

input ushort   InpBBUSize           =0;      // Breakeven

input ushort   InpTrailingStop      =10;     // Trailing Stop

input ushort   InpTrailingStep      =5;      // Trailing Step

input double   InpStepFast          =0.02;   // Step Fast_SAR

input double   InpMaximumFast       =0.2;    // Maximum Fast_SAR

input double   InpStepSlow          =0.01;   // Step Slow_SAR

input double   InpMaximumSlow       =0.1;    // Maximum Slow_SAR

input int      InpCountBarSearch    =3;      // Count Bar Search

input int      InpIndentStopLoss    =30;     // Indent Stop Loss

input double   InpFiboEntranceLevel =50;     // Fibo Entrance Level

input double   InpFiboProfitLevel   =161;    // Fibo Profit Level



int nummodb,nummods;

int bars=0;

int err1;

int countbars,countbarv;



double ExtTrailingStop=0.0;

double ExtTrailingStep=0.0;

double ExtIndentStopLoss=0.0;



int    handle_iFastSAR;                      // variable for storing the handle of the iSAR indicator 

int    handle_iSlowSAR;                      // variable for storing the handle of the iSAR indicator 



string fibo_name="FiboLevels";

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

//| Expert initialization function                                   |

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

int OnInit()

  {

   if(IntStartTime>24 || IntStopTime>24)

     {

      Print("Start Time or Stop Time can't be more than 24");

      return(INIT_FAILED);

     }



   m_symbol.Name(Symbol());

   if(!RefreshRates())

     {

      Print("Ask: ",DoubleToString(m_symbol.Ask(),Digits()),

            ",Bid: ",DoubleToString(m_symbol.Bid(),Digits()));

      return(INIT_FAILED);

     }



//--- tuning for 3 or 5 digits

   int digits_adjust=1;

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

      digits_adjust=10;



   ExtTrailingStop=InpTrailingStop*digits_adjust;

   ExtTrailingStep=InpTrailingStep*digits_adjust;

   ExtIndentStopLoss=InpIndentStopLoss*digits_adjust;



//--- create handle of the indicator iSAR

   handle_iFastSAR=iSAR(Symbol(),Period(),InpStepFast,InpMaximumFast);

//--- if the handle is not created 

   if(handle_iFastSAR==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code 

      PrintFormat("Failed to create handle of the iSAR indicator for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early 

      return(INIT_FAILED);

     }



//--- create handle of the indicator iSAR

   handle_iSlowSAR=iSAR(Symbol(),Period(),InpStepSlow,InpMaximumSlow);

//--- if the handle is not created 

   if(handle_iSlowSAR==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code 

      PrintFormat("Failed to create handle of the iSAR indicator for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early 

      return(INIT_FAILED);

     }

   return(INIT_SUCCEEDED);

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//if(bars!=Bars(Symbol(),Period()) && ((!TimeControl(IntStartTime,IntStopTime) && InpControlTime) || !InpControlTime))

   if((!TimeControl(IntStartTime,IntStopTime) && InpControlTime) || !InpControlTime)

     {

      //bars=Bars(Symbol(),Period());

      err1=ScalpParabolicPattern();

     }



   if(ExtTrailingStop>0)

     {

      err1=Trailing();

     }

   if(InpBBUSize>0)

     {

      err1=BBU();

     }

  }

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

//|                                                                  |

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

int ScalpParabolicPattern()

  {

   bool err;

   double op,sl,tp,max,min,FiboLevel50,FiboLevel116;

   int sarmax=0,sarmin=0;



   if(!RefreshRates())

      return(-1);



   double SarSlow=iSARGet(handle_iSlowSAR,1);

   double SarFast=iSARGet(handle_iFastSAR,1);



//--- 2 A0<>< =87C SarSlow, 2KH5 5Q =0E>48BAO SarFast 8 F5=0 m_symbol.Bid() 2KH5 SarFast

   if(SarSlow<SarFast && SarFast<m_symbol.Bid())

     {

      sarmax=0;

      min=MaximumMinimum(0);   //=0945< B5:CI89 <8=8<C<

      max=iHigh(m_symbol.Name(),Period(),1);



      FiboLevel50=GetFiboUr(max,min,InpFiboEntranceLevel/100.0);//?>;CG8< 50% 7=0G5=85 D81>

      FiboLevel116=GetFiboUr(max,min,InpFiboProfitLevel/100.0); //?>;CG8< 116% 7=0G5=85 D81>



      op=FiboLevel50;

      sl=min-ExtIndentStopLoss*Point();

      tp=FiboLevel116;

      if((m_symbol.Ask()-op)<5*Point() || (m_symbol.Ask()-sl)<5*Point() || (tp-m_symbol.Ask())<5*Point())

        {

         return(0);

        }

      if(ChLimitOrder(1)==0 && ChPositions(1)==0)

        {

         err=m_trade.BuyLimit(0.1,NormalizeDouble(op,Digits()),Symbol(),sl,tp,

                              ORDER_TIME_SPECIFIED,TimeCurrent()+3*60,"FORTRADER.RU");

         if(!err)

           {

            Print("ScalpParabolicPattern()-  H81:0 CAB0=>2:8 >B;>65==KE >@45@>2 OP_BUYLIMIT.  op ",

                  DoubleToString(op,Digits())," sl ",

                  DoubleToString(sl,Digits())," tp ",

                  DoubleToString(tp,Digits())," ",GetLastError());

            return(-1);

           }

         nummodb=0;

        }

     }

//--- 2 A0<>< 25@EC SarSlow, =865 5Q =0E>48BAO SarFast 8 F5=0 m_symbol.Bid() =865 SarFast

   if(SarSlow>SarFast && SarFast>m_symbol.Bid())

     {

      sarmin=0;

      max=MaximumMinimum(1);   //=0945< B5:CI89 <0:A8<C<

      min=iLow(m_symbol.Name(),Period(),1);



      FiboLevel50=GetFiboUr(min,max,InpFiboEntranceLevel/100);//?>;CG8< 50% 7=0G5=85 D81>

      FiboLevel116=GetFiboUr(min,max,InpFiboProfitLevel/100); //?>;CG8< 116% 7=0G5=85 D81>



      op=FiboLevel50;

      sl=max+ExtIndentStopLoss*Point();

      tp=FiboLevel116;

      if((op-m_symbol.Ask())<5*Point() || (sl-m_symbol.Ask())<5*Point() || (m_symbol.Ask()-tp)<5*Point())

        {

         return(0);

        }

      if(ChLimitOrder(0)==0 && ChPositions(0)==0)

        {

         err=m_trade.SellLimit(0.1,NormalizeDouble(op,Digits()),Symbol(),sl,tp,

                               ORDER_TIME_SPECIFIED,TimeCurrent()+3*60,"FORTRADER.RU");

         if(!err)

           {

            Print("ScalpParabolicPattern()-  H81:0 CAB0=>2:8 >B;>65==KE >@45@>2 OP_SELLLIMIT.  op ",

                  DoubleToString(op,Digits())," sl ",

                  DoubleToString(sl,Digits())," tp ",

                  DoubleToString(tp,Digits())," ",GetLastError());

            return(-1);

           }

         nummods=0;

        }

     }

   return(0);

  }

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

//|                                                                  |

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

bool DeleteLimitOrder(int type)

  {

   bool err=false;

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

     {

      if(m_order.SelectByIndex(i))

        {

         if(m_order.OrderType()==ORDER_TYPE_BUY_LIMIT && m_order.Symbol()==Symbol() && type==1)

           {

            err=m_trade.OrderDelete(m_order.Ticket());

           }

         if(m_order.OrderType()==ORDER_TYPE_SELL_LIMIT && m_order.Symbol()==Symbol() && type==0)

           {

            err=m_trade.OrderDelete(m_order.Ticket());

           }

        }

     }

   return(err);

  }

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

//|                                                                  |

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

int ChLimitOrder(int type)

  {

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

     {

      if(m_order.SelectByIndex(i))

        {

         if(m_order.OrderType()==ORDER_TYPE_BUY_LIMIT && m_order.Symbol()==Symbol() && type==1)

           {

            return(1);

           }

         if(m_order.OrderType()==ORDER_TYPE_SELL_LIMIT && m_order.Symbol()==Symbol() && type==0)

           {

            return(1);

           }

        }

     }

   return(0);

  }

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

//|                                                                  |

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

int ChPositions(int type)

  {

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

     {

      if(m_position.SelectByIndex(i))

        {

         if(m_position.PositionType()==POSITION_TYPE_BUY && m_position.Symbol()==Symbol() && type==1)

           {

            return(1);

           }

         if(m_position.PositionType()==POSITION_TYPE_SELL && m_position.Symbol()==Symbol() && type==0)

           {

            return(1);

           }

        }

     }

   return(0);

  }

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

//|                                                                  |

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

double MaximumMinimum(int type)

  {//>?8A0=85 DC=:F88 http://fxnow.ru/blog/programming_mql4/3.html

   int x=0,stop=0;

   double minmax=0.0;



   if(type==0)

     {

      while(stop==0)

        {

         minmax=iLow(m_symbol.Name(),Period(),iLowest(Symbol(),Period(),MODE_LOW,InpCountBarSearch,x));

         if(minmax>iLow(m_symbol.Name(),Period(),iLowest(Symbol(),Period(),MODE_LOW,InpCountBarSearch,x+InpCountBarSearch)))

           {

            minmax=iLow(m_symbol.Name(),Period(),iLowest(Symbol(),Period(),MODE_LOW,InpCountBarSearch,x+InpCountBarSearch));

            x=x+InpCountBarSearch;

           }

         else

           {

            stop=1;

            return(minmax);

           }

        }

     }



   if(type==1)

     {

      while(stop==0)

        {

         minmax=iHigh(m_symbol.Name(),Period(),iHighest(Symbol(),Period(),MODE_HIGH,InpCountBarSearch,x));

         if(minmax<iHigh(m_symbol.Name(),Period(),iHighest(Symbol(),Period(),MODE_HIGH,InpCountBarSearch,x+InpCountBarSearch)))

           {

            minmax=iHigh(m_symbol.Name(),Period(),iHighest(Symbol(),Period(),MODE_HIGH,InpCountBarSearch,x+InpCountBarSearch));

            x=x+InpCountBarSearch;

           }

         else{stop=1;return(minmax);}

        }

     }

   return(0);

  }

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

//|                                                                  |

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

double GetFiboUr(double high,double low,double ur)

  {//>?8A0=85 DC=:F88 http://fxnow.ru/blog/programming_mql4/4.html

   double Fibo=NormalizeDouble(low+(high-low)*ur,Digits());

   return(Fibo);

  }

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

//|                                                                  |

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

int  Trailing()

  {//>?8A0=85 DC=:F88 http://fxnow.ru/blog/programming_mql4/1.html

   bool err=false;

   if(ExtTrailingStop<=0)

      return(0);

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

     {

      if(m_position.SelectByIndex(i))

        {

         if(m_position.PositionType()==POSITION_TYPE_BUY && m_position.Symbol()==Symbol())

           {

            if((m_symbol.Bid()-m_position.PriceOpen())>=ExtTrailingStop*Point() && 

               (m_symbol.Bid()-Point()*ExtTrailingStop)>m_position.StopLoss())

              {

               if(((m_symbol.Bid()-Point()*ExtTrailingStop)-m_position.StopLoss())>=ExtTrailingStep*Point())

                 {

                  Print(""  BUY");

                  err=m_trade.PositionModify(m_position.Ticket(),

                                             m_symbol.Bid()-Point()*ExtTrailingStop,m_position.TakeProfit());

                  if(!err)

                    {

                     return(-1);

                    }

                 }

              }

           }

         if(m_position.PositionType()==POSITION_TYPE_SELL && m_position.Symbol()==Symbol())

           {

            if((m_position.PriceOpen()-m_symbol.Ask())>=ExtTrailingStop*Point() && 

               m_position.StopLoss()>(m_symbol.Ask()+ExtTrailingStop*Point()))

              {

               if((m_position.StopLoss()-(m_symbol.Ask()+ExtTrailingStop*Point()))>ExtTrailingStep*Point())

                 {

                  Print(""  SELL");

                  err=m_trade.PositionModify(m_position.Ticket(),

                                             m_symbol.Ask()+ExtTrailingStop*Point(),m_position.TakeProfit());

                  if(!err)

                    {

                     return(-1);

                    }

                 }

              }

           }

        }

     }

   return(0);

  }

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

//|                                                                  |

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

int  BBU()

  {//>?8A0=85 DC=:F88 http://fxnow.ru/blog/programming_mql4/2.html

   bool err=false;

   if(InpBBUSize<=0)

      return(0);

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

     {

      if(m_position.SelectByIndex(i))

        {

         if(m_position.PositionType()==POSITION_TYPE_BUY && m_position.Symbol()==Symbol() && 

            m_position.StopLoss()<m_position.PriceOpen())

           {

            if(m_symbol.Bid()-m_position.PriceOpen()>=InpBBUSize*Point())

              {

               Print("  Buy  57CKB>:");

               err=m_trade.PositionModify(m_position.Ticket(),

                                          m_position.PriceOpen()+1*Point(),m_position.TakeProfit());

               if(err==false)

                 {

                  return(-1);

                 }

              }

           }



         if(m_position.PositionType()==POSITION_TYPE_SELL && m_position.Symbol()==Symbol() && 

            m_position.StopLoss()>m_position.PriceOpen())

           {

            if(m_position.PriceOpen()-m_symbol.Ask()>=InpBBUSize*Point())

              {

               Print("  Sell  57CKB>:");

               err=m_trade.PositionModify(m_position.Ticket(),

                                          m_position.PriceOpen()-1*Point(),m_position.TakeProfit());

               if(err==false)

                 {

                  return(-1);

                 }

              }

           }

        }

     }

   return(0);

  }

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

//| Time Control                                                     |

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

bool TimeControl(uchar Start,uchar Stop)

  {//>?8A0=85 DC=:F88 http://fxnow.ru/blog/programming_mql4/5.html

   MqlDateTime str1;

   TimeToStruct(TimeCurrent(),str1);

   if(str1.hour>=Start && str1.hour<=Stop)

     {

      return(false);

     }

   return(true);

  }

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

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

  }

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

//| Get value of buffers for the iSAR                                |

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

double iSARGet(const int handle,const int index)

  {

   double SAR[];

   ArraySetAsSeries(SAR,true);

//--- reset error code 

   ResetLastError();

//--- fill a part of the iSARBuffer array with values from the indicator buffer that has 0 index 

   if(CopyBuffer(handle,0,0,index+1,SAR)<0)

     {

      //--- if the copying fails, tell the error code 

      PrintFormat("Failed to copy data from the iSAR indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated 

      return(0.0);

     }

   return(SAR[index]);

  }

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

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