Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 22

 
Artyom Trishkin:

О! Thank you. (chuckles) Didn't realise it myself in the morning... Still need to do a check for filling arrays though. In Quatermass did not meet, and in Five quite often not filled in the data the first time because of the lack of historical data.

ZS. You should sleep more - your thoughts will work in that direction.

Well, you can put it in a loop

do while(CopyOpen(_Symbol, PERIOD_CURRENT, 1, 15, openCandle) < 0);

or even

do while(CopyOpen(_Symbol, PERIOD_CURRENT, 1, 15, openCandle) < 15);

to copy exactly the amount requested.


ps; While I went to pour tea, I had another idea, to use CopyRates() and an array of structures MqlRates rates[] but I'm too lazy to rewrite something.

 
Alexey Viktorov:

Well, you could also put it in a loop

do while(CopyOpen(_Symbol, PERIOD_CURRENT, 1, 15, openCandle) < 0);

or even

do while(CopyOpen(_Symbol, PERIOD_CURRENT, 1, 15, openCandle) < 15);

to copy exactly the number of copies requested.


ps; While I was drinking tea, I had another idea - to use CopyRates() and array of structures MqlRates rates[] but I'm too lazy to rewrite it.

Isn't it the way I proposed?
 
giannis1386:
Help me check other Sell conditions.
int start()
{
//+--------------------------------------------------------------------+
//|   -= stop loss в без убыток =-                                      |
//+--------------------------------------------------------------------+
bool   result;
double stop;
int    cmd,error;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderProfit()>pOPCS)
{
cmd=OrderType();
double blevel=OrderStopLoss()<Bid-Point*TS;
double slevel=OrderStopLoss()>Ask+Point*TS;
//---
if(cmd==OP_BUY || cmd==OP_SELL)
{
while(true)
{
if(cmd==OP_BUY && blevel) stop=Bid-Point*TS;
else                      stop=Ask+Point*TS;
result=OrderModify(OrderTicket(),OrderOpenPrice(),stop,0,0,Orange);
if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
else error=0;
if(error==135) RefreshRates();
else break;
}
}
}
}

It has an odd logic. But even if you don't look at the strange logic, then:

Here you have two conditions for Buy, and all the rest for Sell.

if(cmd==OP_BUY && blevel) stop=Bid-Point*TS;
else                      stop=Ask+Point*TS;
 
Artyom Trishkin:

Your logic is strange. But even if you don't look at the strange logic, then:

Here you have two conditions checked for Buy, and all the rest for Sell

if(cmd==OP_BUY && blevel) stop=Bid-Point*TS;
else                      stop=Ask+Point*TS;

I tried to attachslevel=OrderStopLoss()>Ask+Point*TS for Sell;

But since I'm not a clue about programming, nothing useful has come out. I poked around the forums for a week.

Why the weird logic?) The code is not entirely mine.

 
giannis1386:

I tried to addslevel=OrderStopLoss()>Ask+Point*TS;

I've been poking around the forums for a week. All my attempts ended up with the Sell Stop moving together with the price.

Why the weird logic?) The code is not all mine. I reworked it as best I could.

Why don't you first write the conditions under which the Sell and Buy stops should be shifted?

And then, after you've thought about it, write in your code what's written on the slip.

 
Artyom Trishkin:

First, write down the conditions under which the Buy and Sell stops must be shifted on a piece of paper with a pencil.

And then, after you have thought about it, write in the code what is written on the piece of paper.

slevel=OrderStopLoss()>Ask+Point*TS; this is what it looks like for a Sell position.
 
giannis1386:
slevel=OrderStopLoss()>Ask+Point*TS; well, it seems to be the right one. Or is it wrong? I'm new to this.

Do you even understand what is written in this condition? This is an assignment of zero or one to a variable of type double

What do you want to get?

 
Artyom Trishkin:

Do you even understand what is written in this condition? It's an assignment of zero or one to a variable of type double

What do you want to achieve?

here's what I proofread.

Check condition - not to move SL back and forth, but only in one direction.

For example, for a BUY order, your formula

OrderStopLoss()<bid-point*TrailingStop

Using this example, I have made it for Sel.

 
giannis1386:

here's what I've read.

the check condition - not to move SL back and forth, but only in one direction

For example, for a BUY order, your formula

OrderStopLoss()<bid-point*TrailingStop

Using this example, I pasted it for a Sell position.

So you need it like this:

in Russian... if the stop order is less than the Bid price minus the trailing stop distance, then ... your action

And you assign to the variable the result of this logical expression - i.e. either zero or one.

 
Artyom Trishkin:

So you need this:

in Russian... if the stop order is less than the Bid price minus the trailing stop distance, then ... your actions

And you assign to the variable the result of this boolean expression - i.e. either zero or one.

I'm completely confused.

double blevel=OrderStopLoss()<Bid-Point*TS; works for me. SL goes after the price only in profit.

double slevel=OrderStopLoss()>Ask+Point*TS; I do not know how to add this one to else

they are not a bool.