Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 538

 
evillive:

I'm trying to write a simple EA that closes all positions on my account when it reaches specified profit or loss values in the deposit currency (open by several symbols), but it closes positions as soon as they appear (((

I do not understand what is wrong:



Check this out.


extern int profit = 30;

extern int Magic=0;

int CurProfit;

extern bool CurSymbolOnly = false;


int start() // Special function start

{

//counter of profit of all orders

double p1;

int i1, kk1=OrdersTotal(), pr1=0;

for (i1=0; i1<kk1; i1++)

{

if(OrderSelect(i1, SELECT_BY_POS, MODE_TRADES))

{

if (!CurSymbolOnly || OrderSymbol()==Symbol())

{

p1=MarketInfo(OrderSymbol(), MODE_POINT);

if (p1==0) if (StringFind(OrderSymbol(), Symbol())<0) p1=0.0001; else p1=0.01;

if (OrderType()==OP_BUY)

{

pr1+=NormalizeDouble((OrderClosePrice()-OrderOpenPrice())/p1, 0);

}

if (OrderType()==OP_SELL)

{

pr1+=NormalizeDouble((OrderOpenPrice()-OrderClosePrice())/p1, 0);

}

}

}

}

CurProfit=pr1;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

if (profit<=pr1)

{

ClosePosWithMaxProfitInCurrency();

}

return(0);

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


///closes from the most expensive to the cheapest

void ClosePosWithMaxProfitInCurrency(string sy="", int op=-1, int mn=-1)

{

double pr=0;

int i5, k=OrdersTotal(), np=-1;

if (sy=="0") sy=Symbol();

for (i5=k-1; i5>=0; i5--)

{

if (OrderSelect(i5, SELECT_BY_POS, MODE_TRADES))

{

if ((OrderSymbol()==sy || sy==") && (op<0 || OrderType()==op))

{

if (mn<0 || OrderMagicNumber()==mn)

{

if (pr<OrderProfit()+OrderSwap())

{

pr=OrderProfit()+OrderSwap();

np=i5;

}

}

}

}

}

if (np>=0)

{

if (OrderSelect(np, SELECT_BY_POS, MODE_TRADES))

{

{for (int i=OrdersTotal()-1; i>=0; i--)

{

if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) break;

if (OrderType()==OP_BUY ) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),9);

if (OrderType()==OP_SELL) OrderClose (OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),9);

}

}

}

}

closeotlojnie();

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//closeotlojnie

void closeotlojnie()

{

for (int i=OrdersTotal()-1; i>=0; i--)

{

if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) break;

if (OrderType()==OP_BUYSTOP ) OrderDelete(OrderTicket());

if (OrderType()==OP_SELLSTOP ) OrderDelete(OrderTicket());

if (OrderType()==OP_BUYLIMIT ) OrderDelete(OrderTicket());

if (OrderType()==OP_SELLLIMIT) OrderDelete(OrderTicket());

}

}

 
GSB:
I have worked out a single function for you to create linked arrays of symbols and number of orders (open positions) to buy and sell, test the script by opening several positions on a demo


Thank you for your time, but it's a bit complicated for me, sorry for the intrusion. Explain to me what, where, how? As they say on the fingers.


I have never been used to trade on this kind of scripts.

 
GSB, pako, thank you, I've been absent-minded for some time now, not noticing things like this...
 
denis77515:

Thank you for your time, but it's a bit complicated for me, sorry for the intrusion. Explain to me what, where, how? As they say on the fingers.


I have tried it and have traded it manually on a demo account.


How did you test it on a demo account if there are only errors? The code that was presented could not trade. I have added comments to the code. You have to do all the work yourself, without knowledge and hard work... You cannot trade on marketplaces, especially forex. The result will be unambiguous. In addition to the above-mentioned problems, you should use the trading system that you have chosen. After 2-3 months the results will show, then you will decide whether this system is suitable for you personally and for trading in principle.
 
Thank you, we will work hard!
 
Question to moderators or administrators: Can I change my nickname? I tried to register at MQL5, but it turned out that my nickname was already used there, so I had to register under another one. I want to have the same nickname there and here. Of course, I can reregister here as well, but I've started to post something in Code Base and I want to keep the authorship of my works (and posts) simply by "changing my name". Is it possible?
 
Spy:
Question to moderators or administrators: Can I change my nickname? I tried to register at MQL5, but it turned out that my nickname was already used there, so I had to register under another one. I want to have the same nickname there and here. Of course, I can reregister here as well, but I've started to post something in Code Base and I want to keep the authorship of my works (and posts) simply by "changing my name". Is it possible?

Write to Rosha in a private message. I hope you will find her. State the problem
 

OK, thank you.

 

Hello! Having EAs on a previous version of MetaTrader I upgraded the terminal. After that I went back to 509. So the EAs have "been" in 60... Bild, they returned to the terminal version they were written on.

The question is: What to do with those EAs that stop opening deals in the Strategy Tester after "coming back" and those that cannot be checked anymore (the start button in the Strategy Tester simply does not respond to a click)?

 
tatianati:

Thank you! It worked, I was happy as a child, but the advisor stopped displaying "new. Can you tell me where I went wrong?

Huh, found the mistake, not where I was originally looking. I can't cope with the next task myself.

Please advise how to set the deviation range - removal of price from MA.

extern string s10                = ">>>>>>>>>>>>>>>>>>>>>>>>>>>>";
extern string s11                = ">>> Вход против МА";
extern int    iMA_Period         = 365;
extern int    iMA_OpenDistance   = 60;     // как задать диапазон? например 60-85 за его пределами не открывать ордеров

Here's the entry part

int GetSignal()
{
  int Signal = 0;

  double iMA_Signal = iMA(Symbol(), 0, iMA_Period, 0, MODE_SMMA, PRICE_CLOSE, 0);
  
  int Ma_Bid_Diff = MathAbs(iMA_Signal - Bid)/vPoint;
  
  if(Ma_Bid_Diff > iMA_OpenDistance && Bid > iMA_Signal) Signal = -1;
  if(Ma_Bid_Diff > iMA_OpenDistance && Bid < iMA_Signal) Signal = 1;
  
  return(Signal);
}
Reason: