Coding Help Please ! EA with TakeProfit needs to be on middle BB.

 

Hi I am trying to adjust the take profit function on the EA shown below so it closes out a profitable trade when the price hits the middle bollinger band.

Any help in adjusting or rewriting the code would be appreciated.  thanks.......Mike

#property strict

#include <stderror.mqh>

#include <stdlib.mqh>


//--- input parameters

extern double StopLoss=6;

extern double TakeProfitShift=0;

input double LotSize=0.01;


//---user settings

extern bool show=true;//Show Indicators?


extern int Slippage=30;

extern int MagicNumber=1234;


double pips;

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

//| Expert initialization function                                   |

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

int OnInit()

  {

// Determine what a PIP is.

   pips=Point; //.00001 or .0001 .001 .01 

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

      pips*=10;

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

   Comment("  ");

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

   CheckForSignal();

   TakeProfit();

   TakeProfit = 0;

   {

   double iBands(NULL,0,20,2,TakeProfitShift,PRICE_TYPICAL,MODE_MAIN,1)

   return();

   }

  }

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

//|    Trigger Functions                                             |

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

void CheckForSignal()

  {

   static datetime candletime=0;

   if(candletime!=Time[0])

     {

      double UpperBB=iBands(NULL,0,20,2,0,PRICE_TYPICAL,MODE_UPPER,1);

      double LowerBB= iBands(NULL,0,20,2,0,PRICE_TYPICAL,MODE_LOWER,1);

      double RSI= iRSI(NULL,0,14,PRICE_TYPICAL,1);

      double ADX= iADX(NULL,0,7,PRICE_TYPICAL,MODE_MAIN,1);

      double Price=(High[1]+Low[1])/2;

      if(show==true)

         if(Price<LowerBB && RSI>30 && ADX<32)

            EnterTrade(OP_BUY);

      else if(Price>UpperBB && RSI<70 && ADX<32)

         EnterTrade(OP_SELL);


      candletime=Time[0];

     }

  }

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

//|   Trade Placing Functions                                        |

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


void EnterTrade(int type)

  {


   int err=0;

   double price=Bid,sl=0,tp=0;

   if(type==OP_BUY)

      price=Ask;

//----

   int ticket=OrderSend(Symbol(),type,LotSize,price,Slippage,0,0,"Scalp1",MagicNumber,0,Magenta);

   if(ticket>0)

     {

      if(OrderSelect(ticket,SELECT_BY_TICKET))

        {

         sl = OrderOpenPrice()+(StopLoss*pips);

         tp = OrderOpenPrice()-(TakeProfit*pips);

         if(OrderType()==OP_BUY)

           {

            sl = OrderOpenPrice()-(StopLoss*pips);

            tp = OrderOpenPrice()+(TakeProfit*pips);

           }

         if(!OrderModify(ticket,price,sl,tp,0,Magenta))

           {

            err=GetLastError();

            Print("Encountered an error during modification!"+(string)err+" "+ErrorDescription(err));

           }

        }

      else

        {//in case it fails to select the order for some reason 

         Print("Failed to Select Order ",ticket);

         err=GetLastError();

         Print("Encountered an error while seleting order "+(string)ticket+" error number "+(string)err+" "+ErrorDescription(err));

        }

     }

   else

     {//in case it fails to place the order and send us back a ticket number.

      err=GetLastError();

      Print("Encountered an error during order placement!"+(string)err+" "+ErrorDescription(err));

      if(err==ERR_TRADE_NOT_ALLOWED)MessageBox("You can not place a trade because \"Allow Live Trading\" is not checked in your options. Please check the \"Allow Live Trading\" Box!","Check Your Settings!");

     }

  }

 

The middle bollinger is the MA 20. Means, if you obtain the value of the price and the MA20, you can execute your take profit here.

i.E.

(...) 

double ma_20_price =iMA(Symbol(),PERIOD_CURRENT,20,0,MODE_SMA,PRICE_CLOSE,0)); 

if  (Price==ma_20_price) //--- round about of course

TakeProfit() //---your function name

(...) 

 

Doerk 

 
Please use the SRC button when posting code
 
Doerk:

The middle bollinger is the MA 20. Means, if you obtain the value of the price and the MA20, you can execute your take profit here.

i.E.

(...) 

double ma_20_price =iMA(Symbol(),PERIOD_CURRENT,20,0,MODE_SMA,PRICE_CLOSE,0)); 

if  (Price==ma_20_price) //--- round about of course

TakeProfit() //---your function name

(...) 

 

Doerk 

Thanks Doerck but where would I place this in the code?
 
mike green:
Thanks Doerck but where would I place this in the code?
Within OnTick(), before you check your criteria to open a position?
 
Stuart Browne:
Please use the SRC button when posting code
whats the src button?
 
Doerk:
Within OnTick(), before you check your criteria to open a position?
I have attempted to enter it where you suggested but I am getting compiling errors could you please adjust the code and highlight where you have adjusted it thanks: 
Files:
 
mike green:
I have attempted to enter it where you suggested but I am getting compiling errors could you please adjust the code and highlight where you have adjusted it thanks: 
I have removed the take profit from extern is this correct?
 
mike green:
whats the src button?

 

 

Click that, then paste in your code. Keeps it nice & neat and readable

 

yes

 it is just a great tool

it is just text box

but...

containing all the coding properties (such as colors in the codes)

using it will be very easy for readers to help you 

 

double BBmiddle=iBands(Symbol(),............

if(Close[0]>=BBmiddle) CloseBuy(); //close function

if(Close[0]<=BBmiddle) CloseSell(); //close function
Reason: