[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 369

 
Color MA Orlova.
 
Thank you
 

I'm still having trouble figuring out the trading functions, but I know a bit about programming.

it would be good for me to implement this kind of algorithm with your help:

if (buy condition)

if there is an open order

if this order is on the pair in which the buy condition has triggered

if it is a sell order

close it

open a buy order.

Help me write this, I'm getting tired. I've read a lot, I can't understand it yet.

 
frxmax:

I'm still having trouble figuring out the trading functions, but I know a bit about programming.

It would be good for me to implement this kind of algorithm with your help:

1. if (buy condition)

2. if there is an open order

2.1. if this order is on the pair where the buy condition has been triggered

2.2. if it is a sell order

3. close it

4. open a Buy order.

I have read a lot but I still can't understand it.


1. What is the condition to buy?

2. We make a loop by market orders of the terminal

2.1 In the loop (point 2) we check on which pair the selected order is open and if it is the right one, then ...

2.2. check the type of the selected order and if it is Sell ...

3. Close the selected order

Open Buy order

Or do you need the code completely? This is not a help, but a bearish favor. However... If it does not work - I will write, but no problem...

 

Yes I have written and helped. and "everyone has their own truth" on this issue.

here is my version

if (LoCurent>LoPrevious && FtCurent>FtPrevious)
      {
       ticket=OrderSend(Symbol(),OP_BUY,Lot,Ask,3,Bid-StopLoss*Point,Bid+TakeProfit*Point,"Buy",1111,0,Green);
       if(ticket>0)
          {
           if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
          }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
      }
// условие ПРОДАЖИ 
   if(LoCurent<LoPrevious && FtCurent<FtPrevious)
       {
        ticket=OrderSend(Symbol(),OP_SELL,Lot,Bid,3,Ask+StopLoss*Point,Ask-TakeProfit*Point,"Sell",2222,0,Red);
        if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
        else Print("Error opening SELL order : ",GetLastError()); 
        return(0); 
        }    
    return(0);
 // ЗАКРЫТИЕ СДЕЛОК
 for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
           if(LoCurent<LoPrevious || FtCurent<FtPrevious || LoCurent<-30 || LoPrevious<-30)
               {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                 return(0); // exit
               }
           }else
               {
                 if(LoCurent>LoPrevious || FtCurent>FtPrevious || LoCurent>30 || LoPrevious>30)
                  {
                     OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
                     return(0); // exit
                  }
                }
     }
        
     
 }
 

}

Here's KONDOR's version

if(LoCurent<LoPrevious)
     if(CalculateOrders(Symbol(),OP_SELL)>0) Ord_Close(Symbol(),OP_SELL);
     if(total<1)
     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()!=111) 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+TakeProfit*pp;}
  if(op==1) {open_price=pBid; col=Red;  T=pBid-TakeProfit*pp;}
  int res=OrderSend(Symbol(),op,Lot,open_price,1,Bid-StopLoss*Point,Bid+TakeProfit*Point,NULL,mn,0,col); 

   return(res);               
 }
 

The EA still opens trades at every price change when the order opening condition is satisfied.

Another problem with closing orders on a reverse signal (covering a buy order when there is a sell signal and vice versa).

 
frxmax:

The EA still opens trades at every price change when the order opening condition is satisfied.

Another problem with closing orders on a reverse signal (covering a buy order when there is a sell signal and vice versa).

This is another way to do it. Write a ToR (as Sergeev and Composter ask). And then get a working code. In fact, there is a solution to all the problems. And more than once was posted on this forum.
 

Hi all. Heard about forex but didn't go into details, I know the basics of C programming.

I want to learn how to program forex robots that would generate income.

Can I really make money on this? What is the minimum prerequisite for starting out? What do I need to learn?

 
Vinin:

There is another way to do it. Write the ToR (as Sergeev and Composter ask). And then get a working code. In fact, there is a solution to all the problems. And more than once laid out on the forum.
The ToR for the task I understand correctly?
 
frxmax:

1. The advisor still opens trades on every price change when the condition to open an order is satisfied.

2. Another problem with closing orders on a reverse signal (covering a buy order when there is a sell signal and vice versa).

Prerequisite:

1. If there is a condition to open an order (Buy); If there is no open order (Buy), open a Buy order. Similarly for Sell

2. If there is a condition to open a Buy order, if there is an open Sell order - close Sell.

Thus we obtain:

1. If there is a signal to open a Buy ->> order

2. If there is an open Sell order ->>.
2.1 Close it.

3. If there is no open Buy order ->> 3.
3.1 Open it.

Similarly for Sell

Reason: