[ARCHIVE]Any rookie question, so as not to clutter up the forum. Professionals, don't pass it by. Can't go anywhere without you - 5. - page 180

 
solnce600:

Good day to you all!

I'm a newbie. This is my first time trying to put an idea into code in the tester.

The idea is simple - to open one order at opening price of each hourly candlestick, alternately up and down.

I have written a simple code .... But the problem is that the program sometimes opens more than 1 order at the opening of one candlestick.

It seems to me it happens when more than 1 tick comes within 1 second.

As an alternative, I tried to put in the if( ) condition header...

OrdersTotal()==0

Only one order really opens.



But this variant does not suit me because the next day the given candlestick is not opened, since at the moment of opening of this candlestick

there is already a random number of orders in the trade.

I also tried to interrupt OrderSend(Symbol(),OP_BUY,0.1,Price,3,SL,TP); interrupt start() execution with return; ..... did not help.


I would appreciate if you tell me how to make only 1 order is opened at any tick within 1 second.

int start()

{
double Price=Ask;
double SL=Price-300*Point;
double TP=Price+300*Point;
if( Hour()==10 && Minute()== 00 && Seconds()==00)
OrderSend(Symbol(),OP_BUY,0.1,Price,3,SL,TP);
}

Try this variant:
int TimeNow, TimePrev, PrevType;

int start()   
 {
  double Price,SL,TP;
     int Ticket;

  TimeNow=iTime(NULL,0,0);
  if(TimePrev==TimeNow) return(0);

  if(PrevType!=1) {
   Price=NormalizeDouble(Ask,Digits);    
   SL=NormalizeDouble(Price-300*Point,Digits);    
   TP=NormalizeDouble(Price+300*Point,Digits);
   Ticket=OrderSend(Symbol(),OP_BUY,0.1,Price,3,SL,TP);
   if(Ticket!=-1) { TimePrev=TimeNow; PrevType=1; } }

  else if(PrevType!=-1) {
   Price=NormalizeDouble(Bid,Digits);    
   SL=NormalizeDouble(Price+300*Point,Digits);    
   TP=NormalizeDouble(Price-300*Point,Digits);
   Ticket=OrderSend(Symbol(),OP_SELL,0.1,Price,3,SL,TP);
   if(Ticket!=-1) { TimePrev=TimeNow; PrevType=-1; } }
 }
 
alsu:


Thank you very much for your prompt reply.
 
Diubakin:
Try this option:
Thank you very much for your prompt reply.
 
Dear Professionals! Where can I find the SloseBy() function that works in manual mode? I want to add it to my owls, so that I could close the position with maximal minus to the one with maximal plus, not manually, but automatically according to the conditions. I could not find it in codebase. I have not found it in codebase. Thank you!
 
borilunad:
Ev. pros! Where can I find the СloseBy() function, which works in manual mode? I want to add it to my Owls, so it would not be possible to close position with maximal minus to the opposite with maximal plus, but automatically, according to the conditions. I could not find it in codebase. I have not found it in codebase. Thank you!

Try this option:

bool LockOFF(int EA_Magic) {
 double Result, PrevLoss, PrevProfit;
    int pos, orders_total, order_type, MaxProfitTicket, MaxLossTicket;
   bool Ans;

 MaxProfitTicket=-1; MaxLossTicket=-1;

 orders_total=OrdersTotal();
 for(pos=orders_total-1; pos>=0; pos--) {
  if(!OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)) continue;
  if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=EA_Magic) continue; // не наш ордер
  if(OrderType()>1) continue;
  Result=OrderProfit()+OrderSwap()+OrderCommission();
  if(Result<0.0 && (PrevLoss==0.0 || Result<PrevLoss)) {
   PrevLoss=Result; MaxLossTicket=OrderTicket(); order_type=OrderType(); } } // end of for

 if(MaxLossTicket==-1) return(false); // нет убыточной позиции
 if(order_type==OP_BUY) order_type=OP_SELL; else order_type=OP_BUY; 

 orders_total=OrdersTotal();
 for(pos=orders_total-1; pos>=0; pos--) {
  if(!OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)) continue;
  if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=EA_Magic) continue; // не наш ордер
  if(order_type!=OrderType()) continue;
  Result=OrderProfit()+OrderSwap()+OrderCommission();
  if(Result>0.0 && (PrevProfit==0.0 || Result>PrevProfit)) {
   PrevProfit=Result; MaxProfitTicket=OrderTicket(); } } // end of for

 if(MaxProfitTicket==-1) return(false); // нет противоположной прибыльной позиции

 Ans=OrderCloseBy(MaxLossTicket, MaxProfitTicket);
 if(!Ans) { Print("Ошибка при встречном закрытие"); return(false); }
 
 return(true); }
 

Sergei, at a cursory glance it's worthwhile! I'll screw it on, make the conditions and try it out. Thank you very much!

Tried it, it works without errors! Now only to experiment with the conditions, to get the effect, and will be fine! But that's for tomorrow!

Thank you very much. And interesting dreams!

 
borilunad:
Sergei, at a cursory glance it's worthwhile! I'll screw it on, make the conditions and try it out. Thank you very much!
Only instead of a counter-close command, insert a full-fledged counter-close function - otherwise it's just for the tester. And initialize the variables in the function.
 
artmedia70:
Only instead of a counter-close command, insert a full-fledged counter-close function - otherwise it's only for the tester. And initialise the variables in the function.
Thanks Artyom, it was too late. I'll make everything according to the rules for online today!
 
borilunad:
Thank you, Artyom! It was too late. Will do everything by the rules for online today!
Good luck...
 

Good afternoon. I have such a question.

I have redesigned this indicator for my own needs with great difficulty from pieces of code of other Expert Advisors and Inductors. It is not a big code and it works the way I want it to.

The matter is that it performs some calculations internally and shows up or down arrows on the chart.

Please advise how to make an Expert Advisor to open an order in the right direction when arrow appears on the chart

More precisely, I just need to know which arrow is active now, I think I can do the rest

Here is a piece of code to make it clearer, it's an initialization in the indicator

   SetIndexBuffer(1, Vverh);

   SetIndexStyle(1,DRAW_ARROW);

   SetIndexArrow(1,233);

   

   SetIndexBuffer(2,Vniz);

   SetIndexStyle(2,DRAW_ARROW);

   SetIndexArrow(2,234); 

Thanks in advance