Can Someone Please have a look at this and Tell me hwere I am wrong ?

To add comments, please log in or register
MaxLM
7
MaxLM  

I m tying to make an EA with simple rules:

on H1

Buy Long  if Ask>HighOfLast4Bars + Volume[0]>NewVolumeHigh

Close Long if Ask<Low[1]

Sell Short if Bid< LowOfLast4Bars + Volume[0]>NewVolumeHigh

CLose Short if  Bid > High[1]


Here's what I've got, no errors in the code but no action either...

int Magic = 12345678;
int MaxTrades= 1;
double LotsToTrade = 0.01;
bool  RefreshRates();

bool CloseOrders=False;
double HighOfLast4Bars = iHigh(Symbol(), 0, iHighest(Symbol(), NULL, MODE_HIGH, 3, 0));
double LowOfLast4Bars = iLow(Symbol(), 0, iLowest(Symbol(), NULL, MODE_LOW, 3, 0));
double HighOfLast2Bars = iHigh(Symbol(), 0, iHighest(Symbol(), NULL, MODE_HIGH, 2, 0));
double LowOfLast2Bars = iLow(Symbol(), 0, iLowest(Symbol(), NULL, MODE_LOW, 2, 0));
double NewVolumeHigh= iVolume(Symbol(), 0, iHighest(Symbol(), NULL, MODE_VOLUME, 2, 0));

double NewHigh=Ask>HighOfLast4Bars;
double CloseLong=Ask> LowOfLast2Bars;
double NewLow=Bid< LowOfLast4Bars;
double CloseShort=Bid< HighOfLast2Bars;
int TradeDelayTimeSeconds = (1*24*60*60);
datetime LastTradePlacedTimestamp=0;
   int PositionIndex;  
   int TotalNumberOfOrders = OrdersTotal(); 
   


void OnTick()
  {
  

    if (GetTotalOpenTrades() < MaxTrades )
    {
    if( (TimeCurrent()- LastTradePlacedTimestamp) < TradeDelayTimeSeconds) return;
   
{
   
   if  (Ask>HighOfLast4Bars && Volume[0]>NewVolumeHigh)
         
              { 
          int OrderResult = OrderSend(Symbol(),OP_BUY,LotsToTrade, Ask,3,0,0,NULL,Magic,0, clrGreen);
            LastTradePlacedTimestamp =TimeCurrent();}
           
   if (Bid<LowOfLast4Bars &&Volume[0]>NewVolumeHigh)
    
               {
         int OrderResult = OrderSend(Symbol(),OP_SELL,LotsToTrade, Bid ,3,0,0,NULL,Magic,0, clrBlue );
         LastTradePlacedTimestamp =TimeCurrent();}
            
      }


if (CloseLong) CloseBuyTrades();

if( CloseShort) CloseSellTrades();
 
  
   }   }   
 

   
  int GetTotalOpenTrades()
   {
   int TotalTrades =0;
   {
      for (int t=0; t<OrdersTotal(); t++)
         {
      
         if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES))
            {
               if(OrderSymbol() !=Symbol()) continue;
               if(OrderMagicNumber() !=Magic) continue;
               if(OrderCloseTime() !=0)continue;
               
               TotalTrades = (TotalTrades +1);
             }
      
          }
          
      }
   return TotalTrades;

  }
 

  double GetTotalProfit()
 { 
   double TotalProfits=0.0;
   for (int t=0; t<OrdersTotal();t++)
      {  
         if( OrderSelect(t,SELECT_BY_POS, MODE_TRADES))
         {
            if(OrderMagicNumber() !=Magic) continue;
               if (OrderSymbol() !=Symbol()) continue;
               if (OrderCloseTime() !=0) continue;
               TotalProfits = ( TotalProfits + OrderProfit());
         }
      }
      
 
   return TotalProfits;
   }
   
     void CloseBuyTrades()
{

TotalNumberOfOrders = OrdersTotal();    // <-- we store the number of Orders in the variable

for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --)  //  <-- for loop to loop through all Orders . .   COUNT DOWN TO ZERO !
   {
   if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
   
   if( OrderMagicNumber() == Magic       // <-- does the Order's Magic Number match our EA's magic number ? 
      && OrderSymbol() == Symbol()         // <-- does the Order's Symbol match the Symbol our EA is working on ? 
      && ( OrderType() == OP_BUY ))       // <-- is the Order a Buy Order ?        // <-- or is it a Sell Order ?
   
      if (! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 3 ) )               // <-- try to close the order
         Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  // <-- if the Order Close failed print some helpful information 
      
   }
   
   }
   
   void CloseSellTrades()
{

TotalNumberOfOrders = OrdersTotal();    // <-- we store the number of Orders in the variable

for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --)  //  <-- for loop to loop through all Orders . .   COUNT DOWN TO ZERO !
   {
   if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
   
   if( OrderMagicNumber() == Magic       // <-- does the Order's Magic Number match our EA's magic number ? 
      && OrderSymbol() == Symbol()         // <-- does the Order's Symbol match the Symbol our EA is working on ? 
      && ( OrderType() == OP_SELL ))       // <-- is the Order a Buy Order ?        // <-- or is it a Sell Order ?
   
      if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 3 ) )               // <-- try to close the order
         Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  // <-- if the Order Close failed print some helpful information 
      
   }
   } //  end of For loop
William Roeder
20210
William Roeder  
double HighOfLast4Bars = iHigh(Symbol(), 0, iHighest(Symbol(), NULL, MODE_HIGH, 3, 0));
double LowOfLast4Bars = iLow(Symbol(), 0, iLowest(Symbol(), NULL, MODE_LOW, 3, 0));
double HighOfLast2Bars = iHigh(Symbol(), 0, iHighest(Symbol(), NULL, MODE_HIGH, 2, 0));
double LowOfLast2Bars = iLow(Symbol(), 0, iLowest(Symbol(), NULL, MODE_LOW, 2, 0));
double NewVolumeHigh= iVolume(Symbol(), 0, iHighest(Symbol(), NULL, MODE_VOLUME, 2, 0));

double NewHigh=Ask>HighOfLast4Bars;
double CloseLong=Ask> LowOfLast2Bars;
double NewLow=Bid< LowOfLast4Bars;
double CloseShort=Bid< HighOfLast2Bars;
Global and static variables work exactly the same way in MT4/MT5/C/C++.
  1. They are initialized once on program load.
  2. They don't update unless you assign to them.
  3. In C/C++ you can only initialize them with constants, and they default to zero.
  4. In MTx you should only initialize them with constants. There is no default in MT5 (or MT4 with strict which you should always use.)

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
  5. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
              external static variable - Inflation - MQL4 programming forum
To add comments, please log in or register