[Archive!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass it by. Couldn't go anywhere without you - 2. - page 357

 
Zhunko:
Would you mind posting the code? Maybe a pseudo code. How do you mean without start...? It's a basic function, like main() in C++.

It depends on what tasks you are going to solve and determine the location of your code in the body of the EA program. No one is obliging us to keep the code in the start() function!

The start() function is one of three predefined in our code template. It should be present at least!, because the parent program - the terminal program (this is main(), the main), performing its main tasks, receiving a fresh quote from the server (roughly) just look into your function start and execute the code, which is in it, if it will just return(), then the start function will stop here.

 
"Professionals, don't pass by", :) please share a link to your favourite (preferably Russian-language) news resource, where the economic calendar and other useful stuff is published. If such links cannot be posted here, then send them to me by PM.
 
help me write this.

if there is a buy signal
check open sell orders
if there is, close
if there is not, open buy

if there is a sell signal
check open buy orders
if there is, close
if there is not, open buy
 
frxmax:
help to write the following.

if there is a buy signal
Check open Sell orders
if so, close
if there is no signal to buy

if there is a sell signal
Check open buy orders
if yes we close
if no signal, reopen buy


Start writing yourself - we will help you

here is a starting point to help you https://book.mql4.com/ru/

 
abolk:


Start writing yourself - we'll help you

here you go https://book.mql4.com/ru/

I've already "started".

total=OrdersTotal();
if(CONDITION to open a buy order)
if(total<1)
{
ticket=OrderSend(Symbol(),OP_BUY,Lot,Ask,3,Bid-StopLoss*Point,Bid+100*Point, "Buy",1111,0,Green);
if(ticket<0)
{
Print("OrderSend failed with error #",GetLastError());
return(0);
}
}else
{
if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()==OP_SELL)
OrderClose(NULL,Lot,Ask,3);
return(0);
}
}

 
Almost right, just check first and then set it up.
 
frxmax:
help to write the following.

if there is a buy signal
Check open Sell orders
if yes, close
If there is no signal we may open a buy order
//  в ф-ции старт 
  
if(....условие покупки....){
     if(CalculateOrders(Symbol(),OP_SELL)>0) Ord_Close(Symbol(),OP_SELL);
     Ticket(OP_BUY);      }

//-----------------------
int CalculateOrders(string simbol,int type)
  {
   int count=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==simbol)
        {
         if(OrderType()==type)  count++;
        
        }
     }
     
 return(count);
  }

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

void Ord_Close(string simb,int type) {  double price;
   
   if(type==OP_BUY)  price=MarketInfo(simb, MODE_BID); else
   if(type==OP_SELL) price=MarketInfo(simb, MODE_ASK);

   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if( OrderSymbol()!=simb || OrderMagicNumber()!=mn) continue;
      //---- check order type 
      if(OrderType()==type)
        {  OrderClose(OrderTicket(),OrderLots(),price,3,White);   break;  }
        
        
     }   
     
     
 } 
//-----------------------------

int Ticket(int op){
      double pp = MarketInfo(Symbol(), MODE_POINT);
      double pBid = MarketInfo(Symbol(), MODE_BID);
      double pAsk = MarketInfo(Symbol(), MODE_ASK); 
      double open_price;
      double T;
      color col=NULL;
      int mn=0;
      
  if(op==0) {open_price=pAsk; col=Blue; T=pAsk+Take*pp;}
  if(op==1) {open_price=pBid; col=Red;  T=pBid-Take*pp;}
  int res=OrderSend(Symbol(),op,0.1,open_price,0,0,T,NULL,mn,0,col); 

   return(res);               }
 
KONDOR:

Thank you very much for your help, but I've done things a bit differently and your approach is a bit unclear to me.

Could you explain to me how to sell it all? There's just a lot I don't understand here.

You've been very helpful.

 
frxmax:

Tried flipping everything to sell - failed. no trades open

Error 130. - Stops are not right, so where do we have them there?

Now it opens an order for each bar, but I need only one.


total=OrdersTotal();

if(total<1)

etc.

 
KONDOR:


Hmm, I went deeper into the game - bids and asks to change etc... thanks for that)

I got the stops sorted out too.