I need help on the Ea. I want develop trend follower using bars

Back to topics list To post a new topic, please log in or register
avatar
4
icoword 2015.09.14 12:04
 

I want my system to detect Last High and Last Low with loop bars. It should place order when high-low grater than 20 pips.

I want to ea to place buy when market reach new high , that is two swing high must be seen. or two swing low for sell order

Here is my codes. I have problem of subtracting Minimum from maximum. 

 //--------------------------------------------------------------------

// extremumprice.mq4 

// The code is for scalping market structure

//--------------------------------------------------------------------

extern int Quant_Bars=30;                       // Amount of bars

//--------------------------------------------------------------------

int start()                                     // Special funct. start()

  {

   int i;                                       // Bar number 

   double Minimum=Bid,                          // Minimal price

          Maximum=Bid,                          // Maximal price

          Trend = (-Maximum+Minimum),

          TradeNow = Trend;

   for(i=0;i<=Quant_Bars-1;i++)                 // From zero (!) to..

     {                                          // ..Quant_Bars-1 (!)

      if (Low[i]< Minimum)                      // If < than known

         Minimum=Low[i];                        // it will be min

      if (High[i]> Maximum)                     // If > than known

         Maximum=High[i];                       // it will be max

      if ( Trend>TradeNow)

          TradeNow=Trend;

     }

   Alert("For the last ",Quant_Bars,            // Show message  

         " bars Min= ",Minimum," Max= ",Maximum, "New Trend=",TradeNow);

   return(0);                                      // Exit start()

  }

//--------------------------------------------------------------------

 


avatar
Moderator
3404
GumRai 2015.09.14 12:53 #
 
 double Minimum=Bid,                          // Minimal price

          Maximum=Bid,                          // Maximal price

          Trend = (-Maximum+Minimum),

          TradeNow = Trend;

   for(i=0;i<=Quant_Bars-1;i++)                 // From zero (!) to..

     {                                          // ..Quant_Bars-1 (!)

      if (Low[i]< Minimum)                      // If < than known

         Minimum=Low[i];                        // it will be min

      if (High[i]> Maximum)                     // If > than known

         Maximum=High[i];                       // it will be max

      if ( Trend>TradeNow)

          TradeNow=Trend;

     }
Trend cannot possibly be bigger than TradeNow, you give them the same value which is 0
avatar
4
icoword 2015.09.16 15:04 #
 

I really appreciate te correction, 

I have tried ti limit my ea to 6 order using extern input but it open several position continuously. 

I need further help ..I want my ea to open order using external setting. I want it to limit open order to specified external input

 

 

extern int Max_Bull = 3;  // this ea will open 3 bull order only

extern int Max_Sell = 3;  // this ea will open 3 sell order only

// 

 Max_Bull=0;

   Max_Sell=0;

   for(int cnt=0; cnt<OrdersTotal(); cnt++)

     {

      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))

         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderComment()==Koment)

           {

            if(OrderType()==OP_BUY) Max_Bull++;

            if(OrderType()==OP_SELL) Max_Sell++;

           }

     }

 

avatar
12983
WHRoeder 2015.09.16 20:44 #
 
Don't paste code
Play video
Please edit your post.
For large amounts of code, attach it.
avatar
Moderator
3404
GumRai 2015.09.16 23:14 #
 
extern int Max_Bull = 3;  // this ea will open 3 bull order only

extern int Max_Sell = 3;  // this ea will open 3 sell order only

// 

 Max_Bull=0;

   Max_Sell=0;
Why have extern inputs that you immediately change the variable value to 0?
avatar
4
icoword 2015.09.18 20:21 #
 

GumRai:
Why have extern inputs that you immediately change the variable value to 0?

Thatk alot Gumrai, you gave me headway.

I came up with this code, I want to develop ea that wll detect previous three low, and previous three high.

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

//|                                                       RSI EA.mq4 |

//|                        Copyright 2015, MetaQuotes Software Corp. |

//|                           http://free-bonus-deposit.blogspot.com |

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

#property copyright "Copyright 2015, dXerof"

#property link      "http://free-bonus-deposit.blogspot.com"

#property version   "1.00"

#property strict

extern int Total_Bars1=15;                       // Amount of bars

extern int Total_Bars2=30;                       // Amount of bars

extern int Total_Bars3=45;                       // Amount of bars

bool New_Bar=false;                             // Flag of a new bar

extern int Max_Buy = 3;

extern int Max_Sell = 3;

input int    MagicNumber=123; 

input string Koment="RSIea";


input double StopLoss=100;

input double TakeProfit=100;

input int RSIperiod=14;

input double BuyLevel=30;

input double SellLevel=70;

input bool   AutoLot=True;

input double Risk=10;

input double ManualLots=0.1;

input int Slippage=10;

int OrderBuy,OrderSell;

int ticket;

int LotDigits;

double

   Level_1=1.2850,                          // Set level 1

   Level_2=1.2800,                          // Set level 2

   Price=Bid;                               // Request price

   double Minimum1,                              // Minimal price

          Minimum2,ref=0.8810,

          Maximum1, buy1,buy2,buy3,                             // Maximal price

          Maximum2,sell1,sell2,sell3,          

          Minimum3,dif1,dif2, dif3,

          Maximum3;

//-

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

//| Expert initialization function                                   |

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

int start()

  {

//--------------------------------------------------------------------

   int Ind_max1 =ArrayMaximum(High,Total_Bars1,0);// Bar index of max. price 

   int Ind_min1 =ArrayMinimum(Low, Total_Bars1,0);// Bar index of min. price 

   int Ind_max2 =ArrayMaximum(High,Total_Bars2,0);// Bar index of max. price 

   int Ind_min2 =ArrayMinimum(Low, Total_Bars2,0);// Bar index of min. price 

   int Ind_max3 =ArrayMaximum(High,Total_Bars3,0);// Bar index of max. price 

   int Ind_min3 =ArrayMinimum(Low, Total_Bars3,0);// Bar index of min. price 

   Maximum1=High[Ind_max1];                       // Desired max. price

   Minimum1=Low[Ind_min1];                        // Desired min. price

   Maximum2=High[Ind_max2];                       // Desired max. price

   Minimum2=Low[Ind_min2];                        // Desired min. price

   Maximum3=High[Ind_max3];                       // Desired max. price

   Minimum3=Low[Ind_min3];                        // Desired min. price

   double Total =3;

   // (Max_Buy + Max_Sell);

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

//| Calculate open positions                                         |

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


   for(int i=0;i<Total;i++)

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;

      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)

        {

         if(OrderType()==OP_BUY)  Max_Buy++;

         if(OrderType()==OP_SELL) Max_Sell++;

        }

        if(Max_Buy>0) return(Max_Buy);

          else return(-Max_Sell);


     }

//--- return orders volume

     

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

//.................................................................................

   dif3= (Maximum3-Minimum3); 

   if (Maximum1<=Maximum2 && Maximum2 <= Maximum3)

   if (dif3>=ref && Bid >=Maximum3)

   sell3=dif3;OPSELL(); 

 

   //..................................................................................

   if (Minimum1<=Minimum2 && Minimum2 <= Minimum3)

   if (dif3>=ref && Bid >=Minimum3)

    buy3=dif3; OPBUY();

   

 //...................................................................................  

   dif2= (Maximum2-Minimum2); 

   if (Maximum2<=Maximum1)

   if (dif2>=ref && Bid >=Maximum2)

   sell2=dif2;OPSELL();

 

 //...................................................................................  

   dif2= (Maximum2-Minimum2); 

   if (Minimum2>=Minimum1)

   if (dif2>=ref && Bid >=Minimum2)

   buy2=dif2;OPBUY();

   //.....................................................................  

   dif1= (Maximum1-Minimum1); 

   if (dif1>=ref && Bid >=Maximum1)

   sell1=dif1;OPSELL();

    //................................................................... 

   dif1= (-Maximum1+Minimum1); 

  if (dif1>=ref && Bid <=Minimum1)

      buy1=dif1; OPSELL();

   // Alert("For the last ",Total_Bars1,            // Show message  

  // " Pip_Div = ",buy1,"Min bar=",Minimum1,"High bar=","Max bar=",Maximum1); 

//.............................................................

  

double rsi=iRSI(Symbol(),0,RSIperiod,PRICE_CLOSE,0);//feature not used

double rsi1=iRSI(Symbol(),0,RSIperiod,PRICE_CLOSE,1); //feature not used


//open position

double MA1=iMA(Symbol(),PERIOD_M5,6,0,MODE_EMA,PRICE_TYPICAL,0); //Defines the moving average.

double MA2=iMA(Symbol(),PERIOD_M5,3,0,MODE_EMA,PRICE_TYPICAL,0); //Defines the moving average.


  if(Max_Sell<1 && Price <= MA1)CloseSell();

  if(Max_Buy<1 && Price >= MA1)CloseBuy(); 

   

//close position by signal

//double minProfit = -100000000;

//int minTicket = 0;


int timeout=5; // minutes

for (int cnt=0; cnt<Total; cnt++) {

   if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))

   if ((TimeCurrent()-OrderOpenTime())>=(60*timeout)) {

      if (OrderType()==OP_BUY) {

         ticket=OrderClose(OrderTicket(),OrderLots(),Bid,0,Red);

      }

      if (OrderType()==OP_SELL) {

        ticket= OrderClose(OrderTicket(),OrderLots(),Ask,0,Red);

      }

   }

}

//if (OP_BUY>=minProfit) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(), MODE_BID), 5, Red); 

//if ( OrderType() == OP_SELL ) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ); 

     

return(0);

     

  }

  

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

//|                                                                  |

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

void OPBUY()

  {

   double StopLossLevel;

   double TakeProfitLevel;

   if(StopLoss>0) StopLossLevel=Bid-StopLoss*Point; else StopLossLevel=0.0;

   if(TakeProfit>0) TakeProfitLevel=Ask+TakeProfit*Point; else TakeProfitLevel=0.0;


   ticket=OrderSend(Symbol(),OP_BUY,LOT(),Ask,Slippage,StopLossLevel,TakeProfitLevel,Koment,MagicNumber,0,DodgerBlue);

  }

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

//|                                                                  |

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

void OPSELL()

  {

   double StopLossLevel;

   double TakeProfitLevel;

   if(StopLoss>0) StopLossLevel=Ask+StopLoss*Point; else StopLossLevel=0.0;

   if(TakeProfit>0) TakeProfitLevel=Bid-TakeProfit*Point; else TakeProfitLevel=0.0;


   ticket=OrderSend(Symbol(),OP_SELL,LOT(),Bid,Slippage,StopLossLevel,TakeProfitLevel,Koment,MagicNumber,0,DeepPink);


  }

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

//|                                                                  |

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

void CloseSell()

  {

   int  total=OrdersTotal();

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

     {

      if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))

         if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderMagicNumber()==MagicNumber)

           {

            ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Black);

           }

     }

  }

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

//|                                                                  |

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

void CloseBuy()

  {

   int  total=OrdersTotal();

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

     {

      if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))

         if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderMagicNumber()==MagicNumber)

           {

            ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Black);

           }

     }

  }

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

//|                                                                  |

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

double LOT()

  {

   double lotsi;

   double ilot_max =MarketInfo(Symbol(),MODE_MAXLOT);

   double ilot_min =MarketInfo(Symbol(),MODE_MINLOT);

   double tick=MarketInfo(Symbol(),MODE_TICKVALUE);


   double  myAccount=AccountBalance();


   if(ilot_min==0.01) LotDigits=2;

   if(ilot_min==0.1) LotDigits=1;

   if(ilot_min==1) LotDigits=0;


   if(AutoLot)

     {

      lotsi=NormalizeDouble((myAccount*Risk)/1000000,LotDigits);

        } else { lotsi=ManualLots;

     }


   if(lotsi>=ilot_max) { lotsi=ilot_max; }


   return(lotsi);

  }



avatar
12983
WHRoeder 2015.09.19 00:26 #
 
Don't paste code
Play video
Please edit your post.
For large amounts of code, attach it.
Back to topics list  

To add comments, please log in or register