FT BillWillams Trader

Author: FORTRADER.RU
Price Data Components
Series array that contains the highest prices of each barSeries array that contains close prices for each barSeries array that contains the lowest prices of each barSeries array that contains close prices for each bar
Indicators Used
Moving average indicatorBill Williams Alligator
0 Views
0 Downloads
0 Favorites
FT BillWillams Trader
ÿþ//+------------------------------------------------------------------+

//|               FT BillWillams Trader(barabashkakvn's edition).mq5 |

//|                                                     FORTRADER.RU |

//|                                              http://FORTRADER.RU |

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

#property copyright "FORTRADER.RU"

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

#property version   "1.001"



#define MODE_LOW 1

#define MODE_HIGH 2



#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>  

#include <Trade\AccountInfo.mqh>

CPositionInfo  m_position;                   // trade position object

CTrade         m_trade;                      // trading object

CSymbolInfo    m_symbol;                     // symbol info object

CAccountInfo   m_account;                    // account info wrapper

input string   FT1="------0AB@>9:8 D@0:B0;0:----------";

input int      CountBarsFractal=5;//:>;8G5AB2> 10@>2 87 :>B>@KE A>AB>8B D@0:B0;

input int      ClassicFractal=1; //2:;NG5=85 2K:;NG5=85 :;0AA8G5A:>3> ?0BB5@=0

input int      MaxDistance=1000;//2:;NG5=85 :>=B@>;O @0AAB>O=8O >B 75;5=>9 ;8=88 4> B>G:8 2E>40

input string   FT2="------0AB@>9:8 B8?0 ?@>1>O D@0:B0;0:----------";

input ushort   InpIndent=1; //:>;8G5AB2> ?C=:B>2 4;O >BABC?0 >B <0:A8<C<0 8 <8=8<C<0

input int      TypeEntry=2; //B8? 2E>40 ?>A;5 ?@>1>O D@0:B0;0: 1 - =0 B5:CI5< 10@5, 2 - =0 10@5 70:@KB8O, 3 =0 >B:0B5 : B>G:5 2E>40 ?>A;5 ?@>1>O

input int      RedContol=1; //:>=B@>;8@>20BL =0E>48BAO ;8 ?@>1>9=0O F5=0 2KH5 =865 C@>2=O :@0A=>9 ;8=88

input string   FT3="------0AB@>9:8 0;;830B>@0:----------";

input int      jaw_period=13;  // ?5@8>4 CA@54=5=8O A8=59 ;8=88 (G5;NAB8 0;;830B>@0)

input int      jaw_shift=8;  // A<5I5=85 A8=59 ;8=88 >B=>A8B5;L=> 3@0D8:0 F5=K

input int      teeth_period=8;  // ?5@8>4 CA@54=5=8O :@0A=>9 ;8=88 (7C1>2 0;;830B>@0)

input int      teeth_shift=5;  // A<5I5=85 :@0A=>9 ;8=88 >B=>A8B5;L=> 3@0D8:0 F5=K

input int      lips_period=5; //  ?5@8>4 CA@54=5=8O 75;5=>9 ;8=88 (3C1 0;;830B>@0)

input int      lips_shift=3;  // A<5I5=85 75;5=>9 ;8=88 >B=>A8B5;L=> 3@0D8:0 F5=K

input ENUM_MA_METHOD ma_method=MODE_SMA;   // <5B>4 CA@54=5=8O. 

input ENUM_APPLIED_PRICE applied_price=PRICE_MEDIAN; // 8A?>;L7C5<0O F5=0

input string FT4="-------0AB@>9:8 :>=B@>;O B@5=40 ?> 0;;830B>@C:----------";

input int      TrendAligControl=0; // 2:;NG5=85 :>=B@>;O B@5=40 ?> 0;830B>@C

input ushort   InpJawTeethDistense=10; //@07=8F0 <564C 75;5=>9 8 :@0A=>9

input ushort   InpTeethLipsDistense=10;//@07=8F0 <564C :@0A=>9 8 A8=859

input string   FT5="-------0AB@>9:8 :>=B@>;O 70:@KB8O A45;:8:----------";

input int      CloseDropTeeth=2; // 2:;NG5=85 70:@KB8O A45;:8 ?@8 :>A0=88 8;8 ?@>1>5 G5;NAB8. 0 - >B:;NG5=85 1 - ?> :0A0=8N 2 ?> 70:@KB8N 10@0

input int      CloseReversSignal=2;// 2:;NG5=85 70:@KB8O A45;:8 ?@8 1- >1@07>20=88 >1@0B=>3> D@0:B0;0 2 - ?@8 A@010BK20=88 >1@0B=>3> D@0:B0;0 0 2K:;NG5=> 

input string   FT6="-------0AB@>9:8 A>?@>2>645=8O ExtStopLoss A45;:8:----------";

input int      TrailingGragus=1; //:;NG5=85 B@59;8=3 AB>?0 ?> F5=>2><C 3@04CAC =0:;>=0, 5A;8 A8;L=K9 C3>; B> B@59;8=3 ?> 75;5=>9, 5A;8 <0;K9 C3>; B> B@59;8=3 ?> :@0A=>9

input int      smaperugol=5;

input string   FT7="-------0AB@>9:8  ExtStopLoss 8 ExtTakeProfit 81L5<0 A45;:8:----------";

input ushort   InpStopLoss=50;

input ushort   InpTakeProfit=50;

input double   Lots=0.1;

//---

double         ExtIndent=0.0;

double         ExtJawTeethDistense=0.0;

double         ExtTeethLipsDistense=0.0;

double         ExtStopLoss=0.0;

double         ExtTakeProfit=0.0;

//---

double oldopb,opb,ops,oldops,otkatb,otkats;

int fractalnew,vpravovlevo,numsredbar,colish;

//---

ulong          m_magic=117062220;                  // magic number

int            handle_iMA;                         // variable for storing the handle of the iMA indicator 

int            handle_iAlligator;                  // variable for storing the handle of the iAlligator indicator 

ENUM_ACCOUNT_MARGIN_MODE m_margin_mode;

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

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

//--- tuning for 3 or 5 digits

   int digits_adjust=1;

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

      digits_adjust=10;

   ExtIndent            = InpIndent             * digits_adjust;

   ExtJawTeethDistense  = InpJawTeethDistense   * digits_adjust;

   ExtTeethLipsDistense = InpTeethLipsDistense  * digits_adjust;

   ExtStopLoss          = InpStopLoss           * digits_adjust;

   ExtTakeProfit        = InpTakeProfit         * digits_adjust;

//--- create handle of the indicator iMA

   handle_iMA=iMA(m_symbol.Name(),Period(),smaperugol,0,MODE_EMA,PRICE_CLOSE);

//--- if the handle is not created 

   if(handle_iMA==INVALID_HANDLE)

     {

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

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

                  m_symbol.Name(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early 

      return(INIT_FAILED);

     }

//--- create handle of the indicator iAlligator

   handle_iAlligator=iAlligator(m_symbol.Name(),Period(),

                                jaw_period,jaw_shift,

                                teeth_period,teeth_shift,

                                lips_period,lips_shift,

                                ma_method,applied_price);

//--- if the handle is not created 

   if(handle_iAlligator==INVALID_HANDLE)

     {

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

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

                  m_symbol.Name(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early 

      return(INIT_FAILED);

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {



   ClassicFractal();

   return;

  }

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

//|                                                                  |

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

void  ClassicFractal()

  {

   int buy=0,sell=0;

   double sl=0.0,tp=0.0;



//--- C?@02;5=85 ?>78F8O<8

   ClassicFractalPosManager();



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

      if(m_position.SelectByIndex(i))

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

           {

            if(m_position.PositionType()==POSITION_TYPE_BUY)

               buy=1;



            if(m_position.PositionType()==POSITION_TYPE_SELL)

               sell=1;

           }



//---  =0945< A:>;L:> A<>B@5BL 2?@02> 8 2;52>

   vpravovlevo=(CountBarsFractal-1)/2;

   numsredbar=(CountBarsFractal-vpravovlevo);

   colish=numsredbar-1;



//--- # ---



//---  =0945< D@0:B0; =0 ?>:C?:C

   if(iHigh(m_symbol.Name(),Period(),numsredbar)>

      iHigh(m_symbol.Name(),Period(),iHighest(m_symbol.Name(),Period(),MODE_HIGH,colish,numsredbar+1)) &&

      iHigh(m_symbol.Name(),Period(),numsredbar)>

      iHigh(m_symbol.Name(),Period(),iHighest(m_symbol.Name(),Period(),MODE_HIGH,colish,1)) &&

      (RedContol(iClose(m_symbol.Name(),Period(),1),0)==true && RedContol==1))

     {

      opb=NormalizeDouble(iHigh(m_symbol.Name(),Period(),numsredbar)+ExtIndent*Point(),4);

     }



   if(!RefreshRates())

      return;



//---  ?@>25@:0 2E>40 =0 :0A0=88 8;8 ?> 70:@KB8N 10@0

   if(buy==0 && ((m_symbol.Ask()>opb && TypeEntry==1) || (iClose(m_symbol.Name(),Period(),1)>opb && TypeEntry==2))

      && opb!=oldopb && MaxDistance(opb)==true && opb>0

      && ((RedContol(iClose(m_symbol.Name(),Period(),1),0)==true && RedContol==1) || RedContol==0)

      && ((TrendAligControl(0)==true && TrendAligControl==1) || TrendAligControl==0))

     {

      oldopb=opb;

      sl=NormalizeDouble(m_symbol.Ask()-ExtStopLoss*Point(),4);

      tp=NormalizeDouble(m_symbol.Ask()+ExtTakeProfit*Point(),4);

      m_trade.Buy(Lots,m_symbol.Name(),m_symbol.Ask(),sl,tp,"FORTRADER.RU");

     }



//---   ---



//--- =0945< D@0:B0; =0 ?@>406C

   if(iLow(m_symbol.Name(),Period(),numsredbar)<iLow(m_symbol.Name(),Period(),iLowest(m_symbol.Name(),Period(),MODE_LOW,colish,numsredbar+1)) &&

      iLow(m_symbol.Name(),Period(),numsredbar)<iLow(m_symbol.Name(),Period(),iLowest(m_symbol.Name(),Period(),MODE_LOW,colish,0)) && (RedContol(iClose(m_symbol.Name(),Period(),1),1)==true && RedContol==1))

     {

      ops=NormalizeDouble(iLow(m_symbol.Name(),Period(),numsredbar)-ExtIndent*Point(),4);

     }

//--- ?@>25@:0 2E>40 =0 :0A0=88 8;8 ?> 70:@KB8N 10@0

   if(sell==0 && ((m_symbol.Bid()<ops && TypeEntry==1) || (iClose(m_symbol.Name(),Period(),1)<ops && TypeEntry==2))

      && oldops!=ops && MaxDistance(ops)==true

      && ((RedContol(iClose(m_symbol.Name(),Period(),1),1)==true && RedContol==1) || RedContol==0)

      && ((TrendAligControl(1)==true && TrendAligControl==1) || TrendAligControl==0))

     {

      oldops=ops;

      sl=NormalizeDouble(m_symbol.Bid()+ExtStopLoss*Point(),4);

      tp=NormalizeDouble(m_symbol.Bid()-ExtTakeProfit*Point(),4);

      m_trade.Sell(Lots,m_symbol.Name(),m_symbol.Bid(),sl,tp,"FORTRADER.RU");

     }

   return;

  }

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

//|                                                                  |

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

bool MaxDistance(double entryprice)

  {

   double lips=iAlligatorGet(GATORLIPS_LINE,1);

   if(MathAbs(entryprice-lips)<MaxDistance*Point())

     {

      return(true);

     }

   return(false);

  }

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

//|                                                                  |

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

bool RedContol(double entryprice,int  type)

  {

   double teeth=iAlligatorGet(GATORTEETH_LINE,1);



   if(entryprice>teeth && type==0)

     {

      return(true);

     }

   if(entryprice<teeth && type==1)

     {

      return(true);

     }

   return(false);

  }

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

//|                                                                  |

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

bool TrendAligControl(int type)

  {

   double teeth=iAlligatorGet(GATORTEETH_LINE,1);

   double lips=iAlligatorGet(GATORLIPS_LINE,1);

   double jaw=iAlligatorGet(GATORJAW_LINE,1);



   if(type==0 && lips-teeth>ExtTeethLipsDistense*Point() && teeth-jaw>ExtJawTeethDistense*Point())

     {

      return(true);

     }

   if(type==1 && teeth-lips>ExtTeethLipsDistense*Point() && jaw-teeth>ExtJawTeethDistense*Point())

     {

      return(true);

     }



   return(false);

  }

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

//|                                                                  |

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

void ClassicFractalPosManager()

  {

   int buy=0,sell=0;

   double jaw     = iAlligatorGet(GATORJAW_LINE,1);

   double teeth   = iAlligatorGet(GATORTEETH_LINE,1);

   double lips    = iAlligatorGet(GATORLIPS_LINE,1);

   double lipsl   = iAlligatorGet(GATORLIPS_LINE,2);

   double sma     = iMAGet(1);

   double smal    = iMAGet(2);



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

      if(m_position.SelectByIndex(i))

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

           {

            if(m_position.PositionType()==POSITION_TYPE_BUY)

               buy++;



            if(m_position.PositionType()==POSITION_TYPE_SELL)

               sell++;

           }

   if(!RefreshRates())

      return;



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

      if(m_position.SelectByIndex(i))

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

           {

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               if((CloseDropTeeth==1 && m_symbol.Bid()<=jaw) || (CloseDropTeeth==2 && iClose(m_symbol.Name(),Period(),1)<=jaw))

                 {

                  m_trade.PositionClose(m_position.Ticket());

                  return;

                 }

               //--- CloseReversSignal -> 2:;NG5=85 70:@KB8O A45;:8 ?@8: 

               //--- 1 - >1@07>20=88 >1@0B=>3> D@0:B0;0, 2 - ?@8 A@010BK20=88 >1@0B=>3> D@0:B0;0, 0 2K:;NG5=>

               if((CloseReversSignal==1 && iLow(m_symbol.Name(),Period(),numsredbar)<iLow(m_symbol.Name(),Period(),iLowest(m_symbol.Name(),Period(),MODE_LOW,colish,numsredbar+1)) && 

                  iLow(m_symbol.Name(),Period(),numsredbar)<iLow(m_symbol.Name(),Period(),iLowest(m_symbol.Name(),Period(),MODE_LOW,colish,0))) || (CloseReversSignal==2 && sell==1))

                 {

                  m_trade.PositionClose(m_position.Ticket());

                  return;

                 }



               if(TrailingGragus==1 && lips-lipsl>sma-smal && m_position.Profit()>0)

                 {

                  if(m_position.StopLoss()<lips)

                    {

                     m_trade.PositionModify(m_position.Ticket(),lips,m_position.TakeProfit());

                     return;

                    }

                 }



               if(TrailingGragus==1 && lips-lipsl<=sma-smal && m_position.Profit()>0)

                 {

                  if(m_position.StopLoss()<teeth || lips>teeth)

                    {

                     m_trade.PositionModify(m_position.Ticket(),teeth,m_position.TakeProfit());

                     return;

                    }

                 }

              }

            if(m_position.PositionType()==POSITION_TYPE_SELL)

              {

               if((CloseDropTeeth==1 && m_symbol.Ask()>=jaw) || (CloseDropTeeth==2 && iClose(m_symbol.Name(),Period(),1)>=jaw))

                 {

                  m_trade.PositionClose(m_position.Ticket());

                  return;

                 }



               //--- CloseReversSignal -> 2:;NG5=85 70:@KB8O A45;:8 ?@8: 

               //--- 1 - >1@07>20=88 >1@0B=>3> D@0:B0;0, 2 - ?@8 A@010BK20=88 >1@0B=>3> D@0:B0;0, 0 2K:;NG5=>

               if(((CloseReversSignal==1 && iHigh(m_symbol.Name(),Period(),numsredbar)>iHigh(m_symbol.Name(),Period(),iHighest(m_symbol.Name(),Period(),MODE_HIGH,colish,numsredbar+1)) && 

                  iHigh(m_symbol.Name(),Period(),numsredbar)>iHigh(m_symbol.Name(),Period(),iHighest(m_symbol.Name(),Period(),MODE_HIGH,colish,1))) || (CloseReversSignal==2 && buy==1)))

                 {

                  m_trade.PositionClose(m_position.Ticket());

                  return;

                 }



               if(TrailingGragus==1 && lipsl-lips<smal-sma && m_position.Profit()>0)

                 {

                  if(m_position.StopLoss()>lips)

                    {

                     m_trade.PositionModify(m_position.Ticket(),lips,m_position.TakeProfit());

                     return;

                    }

                 }



               if(TrailingGragus==1 && lipsl-lips>smal-sma && m_position.Profit()>0)

                 {

                  if(m_position.StopLoss()>teeth || lips<teeth)

                    {

                     m_trade.PositionModify(m_position.Ticket(),teeth,m_position.TakeProfit());

                     return;

                    }

                 }

              }

           }

  }

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

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

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

double iMAGet(const int index)

  {

   double MA[1];

//--- reset error code 

   ResetLastError();

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

   if(CopyBuffer(handle_iMA,0,index,1,MA)<0)

     {

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

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

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

      return(0.0);

     }

   return(MA[0]);

  }

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

//| Get value of buffers for the iAlligator                          |

//|  the buffer numbers are the following:                           |

//|   0 - GATORJAW_LINE, 1 - GATORTEETH_LINE, 2 - GATORLIPS_LINE     |

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

double iAlligatorGet(const int buffer,const int index)

  {

   double Alligator[1];

//--- reset error code 

   ResetLastError();

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

   if(CopyBuffer(handle_iAlligator,buffer,index,1,Alligator)<0)

     {

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

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

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

      return(0.0);

     }

   return(Alligator[0]);

  }

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

//|                                                                  |

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

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

  }

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

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