Need a little help with EA

 

I wrote an EA that does work but not the way I want it to. I would like it to close the buy position when the histogram goes red on the bar close and then the opposite so it would be in the market all the time. I can get it to stay in the market but it just wont sell or buy at the places i want it to. Attached is the indicator. Here is my EA code.


//---- input parameters
extern bool      AccountIsMini=true;
extern bool      MoneyManagement=true; //Lots will be determined by TradeSizePercent.
extern bool      Buy=true;
extern bool      Sell=true;
extern double    Lots=0.1;
extern double    StopLoss=100;
extern double    TradePercent=10.0;
extern double    Slippage=3;
extern double    EquityCutoff=0; //Expert will stop trading if equity level decreases to this level. Leave it as 0 if you want to disable this feature.
//internal
int bars;
int LastTrend=0;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
   return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
   return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{   
   int cnt;
   int ticket;
   int Trend;
   double b1;
   double b2;   
   bool TrendChanged=false;
 
   if (NewBar() == false)
   {
      return(0);
   }
   
   b1=iCustom(NULL, 0, "FX_FISH", 0, 1);
   b2=iCustom(NULL, 0, "FX_FISH", 1, 1);
   
   if (b1 > 0)
   {
      Trend=1;
   }
   else if (b2 < 0)
   {
      Trend=2;
   }
   else
   {
      Trend=0;
   }
 
   if (Trend == LastTrend)
   {
      TrendChanged=false;
      LastTrend=Trend;
   }
   else if (LastTrend == 0)
   {
      TrendChanged=false;
      LastTrend=Trend;
   }
   else if (Trend != LastTrend)
   {
      TrendChanged=True;
      LastTrend=Trend;
   }
 
   if (TrendChanged == false)
   {
      return(0);
   }
 
   cnt=OrdersTotal();
   
   if (Trend == 1) //up trend
   {
      if (cnt > 0) //open position
      {
         OrderSelect(cnt - 1, SELECT_BY_POS, MODE_TRADES);
         if (OrderType() == OP_SELL)
         {
            OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Green);
            Print(b1, ", ", b2);
         }
         else
         {
            return(0);
         }
      }
      if (Buy == true)
      {
         if(AccountFreeMargin()<(1000*LotSize()) || AccountFreeMargin() < EquityCutoff)
         {
            Print("Not enough margin for Buy");
            return(0);
         }
         Print(b1, ", ", b2);
         ticket=OrderSend(Symbol(), OP_BUY, LotSize(), Ask, Slippage, 0, 0, "", 0, Green);
         if (ticket<0)
         {
            Print("OrderSend failed with error #", GetLastError());
            return(0);
         }
      }
   }
   else if (Trend == 2) //down trend
   {
      if (cnt > 0) //open position
      {
         OrderSelect(cnt - 1, SELECT_BY_POS, MODE_TRADES);
         if (OrderType() == OP_BUY)
         {
            OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Red);
            Print(b1, ", ", b2);
         }
         else
         {
            return(0);
         }
      }
      if (Sell == true)
      {
         if(AccountFreeMargin()<(1000*LotSize()) || AccountFreeMargin() < EquityCutoff)
         {
            Print("Not enough margin for Sell");
            return(0);
         }
         Print(b1, ", ", b2);
         ticket=OrderSend(Symbol(), OP_SELL, LotSize(), Bid, Slippage, 0, 0, "", 0, Red);
         if (ticket<0)
         {
            Print("OrderSend failed with error #", GetLastError());
            return(0);
         }
      }
   }
   return(0);
}
//+------------------------------------------------------------------+
//| the sign of the new bar                                          |
//+------------------------------------------------------------------+
bool NewBar()
{
   if (bars!=Bars)
   {
      bars=Bars;
      return(true);
   }
   return(false);
}
//+------------------------------------------------------------------+
//| calculates lot size                                              |
//+------------------------------------------------------------------+
double LotSize()
{
   if(MoneyManagement)
   {
      Lots = NormalizeDouble(MathFloor(AccountFreeMargin()*TradePercent/10000)/10,1);
      if(AccountIsMini)
      {
         Lots = MathFloor(Lots)/10;    
         if(Lots<0.1)
         {
            Lots=0.1;
         }
      }
      return(Lots);
   }
   else 
   {   
      Lots = Lots;     
      if(AccountIsMini)
      {
         if (Lots >= 1.0)
         {
            Lots = Lots/10;
         }
         if (Lots < 0.1)
         {
            Lots = 0.1;
         }
      }
      return(Lots);
   }
}
//+------------------------------------------------------------------+
Files:
fx_fish.mq4  4 kb
Reason: