Code for buying and selling deals ....I care about your opinion

 
//+------------------------------------------------------------------+
//|                                                        try17.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+ 
  double open = iOpen(_Symbol, PERIOD_CURRENT, 0);
   double close = iClose(_Symbol, PERIOD_CURRENT, 0);
   double high = iHigh(_Symbol, PERIOD_CURRENT, 0);
   double low = iLow(_Symbol, PERIOD_CURRENT, 0);
   double  Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);

double  Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
    double m;
int OnInit()
  {
//--- create timer
   EventSetMillisecondTimer(200);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
   

double open = iOpen(_Symbol, PERIOD_CURRENT, 0);
   double close = iClose(_Symbol, PERIOD_CURRENT, 0);
   double high = iHigh(_Symbol, PERIOD_CURRENT, 0);
   double low = iLow(_Symbol, PERIOD_CURRENT, 0);
   

  /* Comment("open : "  + DoubleToString(open, _Digits), "\n",
           "close : "  + DoubleToString(close, _Digits), "\n",
           "high : "  + DoubleToString(high, _Digits), "\n",
           "ASK : " +  DoubleToString(Ask, _Digits), "\n",
              "Bid : " +  DoubleToString(Bid, _Digits), "\n",
           "low : "  + DoubleToString(low, _Digits));
        

      
      printf("module"+ DoubleToString (m));
      ObjectCreate(0,"Line1", OBJ_HLINE,0,0,m);
   ObjectGetInteger(0,"Line1",OBJPROP_COLOR,clrBeige);
   ObjectMove(0,"Line1", 0,0,m);*/
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+



  
void buy()
{

Print("buy");
}



void SELL()
{

Print("SELL");
}

void OnTick()
  {
 


  double open = iOpen(_Symbol, PERIOD_CURRENT, 0);
   double close = iClose(_Symbol, PERIOD_CURRENT, 0);
   double high = iHigh(_Symbol, PERIOD_CURRENT, 0);
   double low = iLow(_Symbol, PERIOD_CURRENT, 0);
   //double medual= NormalizeDouble ((open + close)/2 ,_Digits);
 double medual= (open + close)/2;
  medual= NormalizeDouble (medual ,_Digits);
//Comment("medual>>>>>>>>>>>>>>>>>>>>> : "  + DoubleToString(medual, _Digits));
        
double  Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);

double  Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   

   int SECOND;
   datetime    tm=TimeCurrent();
   MqlDateTime stm;
   tm=TimeCurrent();
   TimeToStruct(tm,stm);
   SECOND =(int)stm.sec;

Print("SECOND : "  + SECOND, "\n","medual>>>>>>>>>>>>>>>>>>>>> : "  + DoubleToString(Ask, _Digits));
           
  switch( SECOND)
  {
  case 1:
  
  if (open>Ask) 
   buy();
 else
 SELL();
 
 
   case 2 :        
  if (open>Ask)
   buy();
 else
 SELL();
 
 
    case 3:
  
   if (open>Ask)
   buy();
 else
 SELL();
 
  case 4:
  
   if (open>Ask)
   buy();
 else
 SELL();
 
  case 5:
  
  
   if (open>Ask)
   buy();
 else
 SELL();
 
 
 
 
  case 6:
  
   if (open>Ask)
   buy();
 else
 SELL();
 
 
  case 7:
  
    if (open>Ask)
   buy();
 else
 SELL();
 
  case 8:
  
    if (open>Ask)
   buy();
 else
 SELL();
 
 
  case 9:
  
  if (open>Ask)
   buy();
 else
 SELL();
 
 
  case 10:
  
   if (open>Ask)
   buy();
 else
 SELL();
 
 
 
 
 case 11:
  
   if (open>Ask)
   buy();
 else
 SELL();
 
 
 
   case 12 :        
  if (open>Ask)
   buy();
 else
 SELL();
 
 
 
    case 13:
  
  if (open>Ask)
   buy();
 else
 SELL();
 
 
 
 
  case 14:
  
    if (open>Ask)
   buy();
 else
 SELL();
 
 
  case 15:
  
    if (open>Ask)
   buy();
 else
 SELL();
 
 
  case 16:
    if (open>Ask)
   buy();
 else
 SELL();
 
 
  case 17:
  
    if (open>Ask)
   buy();
 else
 SELL();
 
  case 18:
  
   if (open>Ask)
   buy();
 else
 SELL();
 
 
  case 19:
   if (open>Ask)
   buy();
 else
 SELL();
 
  case 20:
  
    if (open>Ask)
   buy();
 else
 SELL();
 
   
   
   }
   
 /*for (int i =1 ; SECOND<59 ; i++)
 {
  Comment("SECOND : "  +(SECOND), "\n","i : "  + i);
        
 SECOND =(int)stm.sec;

}*/

  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
  OnTick();

  }
//+------------------------------------------------------------------+
//| Trade function                                                   |
//+------------------------------------------------------------------+
void OnTrade()
  {
//---
   
  }
//+------------------------------------------------------------------+

Hello all I made this code and I want to take your opinion about it. Will it succeed when executing attributes (buying or selling) and gives correct and tangible results?

Improperly formatted code removed by moderator.

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2023.07.18
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 

Improperly formatted code removed by moderator. Please EDIT your post and use the CODE button (Alt-S) when inserting code.

Code button in editor

Hover your mouse over your post and select "edit" ... 

 
Alain Verleyen #:

Improperly formatted code removed by moderator. Please EDIT your post and use the CODE button (Alt-S) when inserting code.

Hover your mouse over your post and select "edit" ... 

Well you can see now

 
Db Orc #:
No answer my sir
no. It won't. It will screw your account.

You should not use a switch statement. And you need to add a break; after each case x:

Else it will just jump to the case and continue execution from there on, through all your following cases... Opening a lot of positions...
 

How about explain what you're trying to accomplish. Is this meant to be bi-directional, long and short?  Or are the sells just closing the buy positions?  Not clear, and it makes a huge difference.

The logic of this code looks like it will:

  1. Buy if the price drops below the Open of the current candle within the first 20 seconds.
  2. Sell if it goes back up above the Open of the current candle within the first 20 seconds.

Is that correct? And what's supposed to happen if the price doesn't come back above the Open?

As it's written, without seeing the Buy() code, it could potentially open up to a huge number of positions, as @Dominik Christian Egert said. If the Buy() code limited you to one position, it might prevent that, but it seems safer to do that check earlier in the logic.

Also, I'm thinking this whole could be simplified. Why not just check if the # of seconds since open is < 20?  Or are you trying to make trades potentially every second? 

If that's the case, I think you also need to consider Bid/Ask/Close/spread differently. Close is based on Bid not Ask. So when Ask drops below the Open bid, and then immediately reverses to right above it, you will sell, at Bid price, which may not be in profit yet. You may want to wait to sell until Bid is above the Open.

Tl;dr:  This will make too many trades, and many of them may be losers because they haven't made up the spread.

You can easily verify this by adding the Bid and Ask prices to your print statements.