//+------------------------------------------------------------------+
//|                                                          TFX.mq4 |
//|                                           Copyright © 2008 Wolfe |
//|                                                www.forex-tsd.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008 Wolfe"
#property link      "www.forex-tsd.com"




extern double        Start_Lot_Size=0.1;//starting lot size for cycle
extern double        Lot_Size_Increment=0.1;//Additional orders will increase by this amount
extern bool          Double_Lotsize=false;//set to true if you want to just double every lotsize, Lot_Size_Increment ignored if true
extern int           Max_Trades=20;//maximum number of trades allowed
extern bool          Close_All_Max=false;//select true if you want ALL orders to close if Max_Trades is hit 
extern int           Next_Trade=10;//next trade pip increment, (will trade this many pips above OR below last order)
extern double        TP_Percent=5;//will take profit if profit is greater than percent of account balance (close all orders)
extern double        SL_Percent=10;//will stop loss if profit is less than percent of account balance (close all orders)
extern int           MA_Period=7;//moving average period for calculation
extern int           Timeframe=5;//timeframe used for MA calculation, 1=1m, 2=5m, 3=15m, 4=30m, 5=1h, 6=4h, 7=1d
extern int           Slippage=5;
extern int           Magic_Number=9009;


double Lot_Size;
int MA_Timeframe;
double MA_Current;
double MA_Previous;
bool Read_Last;
static int Ticket=-1;
static double Open_Last;
static double Last_Lotsize;
bool Get_Lotsize;
double Take_Profit;
double Stop_Loss;

#include <TFX_Include.mqh>


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
  
if (Timeframe==1){MA_Timeframe=1;}
if (Timeframe==2){MA_Timeframe=5;}
if (Timeframe==3){MA_Timeframe=15;}
if (Timeframe==4){MA_Timeframe=30;}
if (Timeframe==5){MA_Timeframe=60;}
if (Timeframe==6){MA_Timeframe=240;}
if (Timeframe==7){MA_Timeframe=1440;}

Take_Profit=TP_Percent*0.01;
Stop_Loss=SL_Percent*0.01;


//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  
//////////////////////////////////////////Take Profit or Stop Loss by Percent of Account Balance/////////////////////////////////

if (Close_All_Max==true)
 {
  if (OTBM(Magic_Number)==Max_Trades)
   {
    CBM(Magic_Number);
    Print("                                                                               ***MAX TRADE HIT CLOSE ALL PERFORMED***");
   }
 }
 
if (AccountProfit() >= AccountBalance()*(Take_Profit))
 {
  CBM(Magic_Number);
  Print("                                                                                 ***TAKE PROFIT CLOSE ALL PERFORMED***");
 }

if (AccountProfit() <= AccountBalance()*(-Stop_Loss))
 {
  CBM(Magic_Number);
  Print("                                                                                   ***STOP LOSS CLOSE ALL PERFORMED***");
 }

///////////////////////////////////////////Call Moving Average for Initial Starting Direction//////////////////////////////////// 

MA_Current=iMA(NULL,MA_Timeframe,MA_Period,0,MODE_EMA,PRICE_CLOSE,0);
MA_Previous=iMA(NULL,MA_Timeframe,MA_Period,0,MODE_EMA,PRICE_CLOSE,1);

///////////////////////////////////////////Lot Size Incrementation///////////////////////////////////////////////////////////////

if (OTBM(Magic_Number)==0){Lot_Size=Start_Lot_Size;}
if (Double_Lotsize==false)
{
if (OTBM(Magic_Number)>0){Lot_Size=Last_Lotsize+Lot_Size_Increment;}
}
if (Double_Lotsize==true)
{
if (OTBM(Magic_Number)>0){Lot_Size=Last_Lotsize*2;}
}

//////////////////////////////////////////////////////////Get Open Price of Last Order///////////////////////////////////////////

if (Read_Last==true)
  {
   if(OrderSelect(Ticket, SELECT_BY_TICKET)==true)
   { 
    Open_Last=OrderOpenPrice();
    Print("                                                                                         Open_Last =",Open_Last);
    Read_Last=false;
    Get_Lotsize=True;
   }
}

//////////////////////////////////////////////////////Get Lot Size of Last Order////////////////////////////////////////////////

if (Get_Lotsize==True)
 {
  if(OrderSelect(Ticket, SELECT_BY_TICKET)==true)
   {
    Last_Lotsize=OrderLots();
    Print("                                                                                    Last_Lotsize =",Last_Lotsize);
    Get_Lotsize=false;
   }
 }

///////////////////////////////////////////Opening of Additional Orders After Initial Order/////////////////////////////////////////////////////////

if ((OTBM(Magic_Number)>0) && (OTBM(Magic_Number)<Max_Trades) && (Read_Last==false) && (Get_Lotsize==false))
 {
  if (Ask >= Open_Last+Next_Trade*Point)
    {
      Ticket=OrderSend(Symbol(),OP_BUY,Lot_Size,Ask,Slippage,NULL,NULL,"TFX_LONG",Magic_Number,0,Green);
      Read_Last=true;
    }
  if (Bid <= Open_Last-Next_Trade*Point)
    {
      Ticket=OrderSend(Symbol(),OP_SELL,Lot_Size,Bid,Slippage,NULL,NULL,"TFX_SHORT",Magic_Number,0,Red);
      Read_Last=true;
    }
 }

//////////////////////////////////////////////////////Open First Order//////////////////////////////////////////////////////////  

if (OTBM(Magic_Number)==0)
 {
  if (MA_Current > MA_Previous)
   {
    Ticket=OrderSend(Symbol(),OP_BUY,Lot_Size,Ask,Slippage,NULL,NULL,"TFX_LONG_START",Magic_Number,0,Green);
    Read_Last=true;
   }
  if (MA_Current < MA_Previous)
   {
    Ticket=OrderSend(Symbol(),OP_SELL,Lot_Size,Bid,Slippage,NULL,NULL,"TFX_SHORT_START",Magic_Number,0,Red);
    Read_Last=true; 
   }
 }
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+