Who can help with the robot, why isn't it working? - page 3

 
For GIM
If you just want to open orders, then specify 0 instead of SL / TP. If SL or TP is 0, you will have to enter 0 in your request, and if you enter a value, you will have to calculate the price at which the order will be closed.
 
Dmitriy Gizlyk:
For GIM
If you just want to open orders, then specify 0 instead of SL / TP. And for the future, you have to check: if SL or TP is 0, then you have to specify 0 in the request. And if you specify a value, then calculate the closing price of the order.
I would like the Expert Advisor to open and close orders according to total profit for all trades....
 
AdikAmir:

extern double Lots=0.1;
extern intnal double FastMAPeriod=5;
extern int FastMAMethod=0; // 0-SMA, 1-EMA, 2-SMMA, 4-LWMA
extern int FastMAPrice=0; // 0-Close, 1-Open, 2-High, 3-Low, 4-Median, 5-Typical, 6-Weighted
extern int SlowMAPeriod=75;
extern intn SlowMAMethod=3; // 0-SMA, 1-EMA, 2-SMMA, 4-LWMA
extern int SlowMAPrice=3; // 0-Close, 1-Open, 2-High, 3-Low, 4-Median, 5-Typical, 6-Weighted
extern int TP=500; //takeprofit
extern int SL=250; //stop loss
double ticet;

int start(){

double fast_ma1=iMA(NULL,0,FastMAPeriod,0,FastMAMethod,FastMAPrice,1);
double slow_ma1=iMA(NULL,0,SlowMAPeriod,0,SlowMAMethod,SlowMAPrice,1);
double fast_ma2=iMA(NULL,0,FastMAPeriod,0,FastMAMethod,FastMAPrice,2);
double slow_ma2=iMA(NULL,0,SlowMAPeriod,0,SlowMAMethod,SlowMAPrice,2);

if (OrdersTotal()==0)
{
if (fast_ma1>slow_ma1 && (fast_ma2<slow_ma2))
{ ticet = OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Ask-SL*Point,Ask+TP*Point,NULL,0,0,CLR_NONE);
if (ticet==0) Print("ERROR");
}
if (slow_ma1>fast_ma1 && (slow_ma2<fast_ma2))
{
ticet = OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid-SL*Point,Bid+TP*Point,NULL,0,0,CLR_NONE);
if (ticet==0) Print("ERROR");
}

}

return(0);
}




Read the log. Maybe there are messages about errors which were not detected during compilation.

There is an error here OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid-SL*Point,Bid+TP*Point,NULL,0,0,CLR_NONE);

you need to do the following OrderSend(Symbol(),OP_SELL,Lots,Bid,Bid,3,Ask+SL*Point,Bid-TP*Point,NULL,0,0,CLR_NONE);

When you sell, the stop loss should be higher than the current price and take a lower one. In your case, on the contrary, you put the stop below the price and take a higher one.

 
GIM:
I would like the Expert Advisor to open and close orders by total profit for all trades....

In this case, when opening a position, put SL=0, TP=0.

To close a position, we need to compare the obtained profit with the set value on each tick. And when the profit reaches the specified value, we need to close all orders in the loop through OrderClose(....).

As far as the determination of the current profit is concerned, there can be variations depending on the task set. If you look at the profit of the account, you can simply take AccountProfit(), but the more labor-intensive way is probably through a loop (then we can calculate profit only for the necessary orders).

   double profit=0;
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==_Symbol && OrderMagicNumber()== /* Ваш магик */)
        {
         profit+=OrderProfit()+OrderSwap()+OrderCommission();
        }
     }
 
Dmitriy Gizlyk:

In this case, when opening a position, put SL=0, TP=0.

To close a position, we need to compare the obtained profit with the set value on each tick. And when the profit reaches the specified value, we need to close all orders in the loop through OrderClose(....).

As far as the determination of the current profit is concerned, there can be variations depending on the task set. If you look at profit per account, you can simply take AccountProfit(), but probably the more labor-intensive way is through a cycle (then you can calculate profit only for the necessary orders).

extern double  lots       = 0.01;
extern int  stop_loss      = 0;  
extern int take_profit    = 0;

int            last_bar       = 0;

int start(){
   if (last_bar == Bars) return(0);
   last_bar = Bars;
   if (OrdersTotal() == 0){
          OrderSend(Symbol(), OP_BUY, lots ,Ask, 50, Bid - stop_loss * Point, Ask + take_profit * Point,  "", 1, 0, clrBlue);
          OrderSend(Symbol(), OP_SELL, lots ,Bid, 50, Ask+ stop_loss * Point, Bid - take_profit * Point,  "", 1, 0, clrRed);      
}
   return(0); 
}
 double profit=0;
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==_Symbol && OrderMagicNumber()== /* Ваш магик */)
        {
         profit+=OrderProfit()+OrderSwap()+OrderCommission();
        }
     }
 
GIM:
If stoploss and take profit are in the currency of the deposit
extern double  lots       = 0.01;
extern int  stop_loss      = 10;  /* Указываем в валюте депозита */
extern int take_profit    = 20;   /* Указываем в валюте депозита */
extern int Magic          = 1;

int            last_bar       = 0;

int start(){
   if (last_bar == Bars) return(0);
   last_bar = Bars;
   if (OrdersTotal() == 0){
          OrderSend(Symbol(), OP_BUY, lots ,Ask, 50, 0, 0,  "", Magic, 0, clrBlue);
          OrderSend(Symbol(), OP_SELL, lots ,Bid, 50, 0, 0,  "", Magic, 0, clrRed);      
}
 double profit=0;
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==_Symbol && OrderMagicNumber()== Magic)
        {
         profit+=OrderProfit()+OrderSwap()+OrderCommission();
        }
     }
 int requot=0;
 if(profit>=take_profit || (-profit)>=stop_loss)
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==_Symbol && OrderMagicNumber()== Magic)
        {
         if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),50,clrRed))
               requot=0;
            else
              {
               requot++;
              }
         if(requot>10)
              {
               i++;
               requot=0;
              }
            i--;
        }
     }
   return(0); 
}
 
Dmitriy Gizlyk:
If stoploss and take profit are specified in the deposit currency

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

Error: "i"

 
Who knows?
extern int Magic          = 1;


что означает??
 
GIM:
Who knows

The identifier of the Expert Advisor's orders, so that it only works with its own orders.

 
Vladimir Zubov:

The identifier of the Expert Advisor's orders, so that it only works with its own orders.

What do you mean, only with its own orders....