How to code? - page 302

Mladen Rakic
176464
Mladen Rakic  

...

Yes you can

The only limitation is that you do not run into "circular calling" when it would end with a dead loop. Other than that, no limitation for function calls at all

crsnape@btinternet.com:
Good point. Il have a look into that.

A question about functions, is it possible to call a function within a function? E.g. I have this function:

string GetWinLossPreviousShort (int LastOpenTicket, string WinLossPreviousShort)

{

if (... etc

Than later I call it:

double GetLotsLong (int LowRisk, int HighRisk, double SLDistanceLong, string GetWinLossPreviousShort)
Christopher Snape
167
Christopher Snape  

I've entered this code to retrieve the last opened order:

//--- Function to calculate ticket number of last opened Order

int LastOpenTicket()

{

datetime lastTime = 0;

int lastTicket = -1; // None open.

int pos;

for (pos = OrdersTotal() - 1; pos >= 0; pos--)

if (OrderSelect(pos, SELECT_BY_POS) // Only my orders w/

&& OrderMagicNumber() == MagicNumber // my magic number

&& OrderSymbol() == Symbol() // and my pair.

&& OrderOpenTime() > lastTime)

{

lastTime = OrderOpenTime();

lastTicket = OrderTicket();

}

return (lastTicket);

}

Its a function that is called from my GetLots function - the compiler is error free but when I backtest the results section is completely clear. No trades. Is this because the first trade has no previous trade to call upon in the code above?

In the journal it gets to loading the external variables (of which it loads magic number 42, but in my code its 42552...?) but then nothing thereafter.

Mladen Rakic
176464
Mladen Rakic  

...

What is the declaration of your magic number ( it should be int MagicNumber; )

crsnape@btinternet.com:
I've entered this code to retrieve the last opened order:

//--- Function to calculate ticket number of last opened Order

int LastOpenTicket()

{

datetime lastTime = 0;

int lastTicket = -1; // None open.

int pos;

for (pos = OrdersTotal() - 1; pos >= 0; pos--)

if (OrderSelect(pos, SELECT_BY_POS) // Only my orders w/

&& OrderMagicNumber() == MagicNumber // my magic number

&& OrderSymbol() == Symbol() // and my pair.

&& OrderOpenTime() > lastTime)

{

lastTime = OrderOpenTime();

lastTicket = OrderTicket();

}

return (lastTicket);

}

Its a function that is called from my GetLots function - the compiler is error free but when I backtest the results section is completely clear. No trades. Is this because the first trade has no previous trade to call upon in the code above?

In the journal it gets to loading the external variables (of which it loads magic number 42, but in my code its 42552...?) but then nothing thereafter.
Christopher Snape
167
Christopher Snape  

Yes it is int MagicNumber = 42557

Mladen Rakic
176464
Mladen Rakic  

...

Then I can not tell from the code you posted why is it doing wrong (why is it receiving wrong MagicNumber for example). The code should work if there is any order opened with specified MagicNumber (it is going to loop through opened orders) If there is no opened orders or the MagicNumbers and symbols do not match it will return -1 (as you expect it to) so that code is correct (tested it now, and it works as it should)

Sorry, but that as all I can do from the code you posted

crsnape@btinternet.com:
Yes it is int MagicNumber = 42557
Christopher Snape
167
Christopher Snape  

What would happen in the instance that no previous order had been opened and this is the first? I know it returns -1 but would the code EA fail to run because my GetLots function relies on calling the last opened order ticket number.

Mladen Rakic
176464
Mladen Rakic  

If it relies on existance of at least one previously opened order, then it will fail

Add something like this in your condition :

if (LastOpenOrder()==-1)

... then you can open order if all the rest of conditions are met

else

... check the last opened order as before plus the rest of the conditions

(just doing a wild guessing game how you wrote your EA conditions now)

crsnape@btinternet.com:
What would happen in the instance that no previous order had been opened and this is the first? I know it returns -1 but would the code EA fail to run because my GetLots function relies on calling the last opened order ticket number.
tkuan77
89
tkuan77  

Hi Mladen,

Do you know how to set open trade when the stochastic line crosses at level 20 or 80?

E.g.

When Stochastic crosses each other at level 20 (oversold), Long

When Stochastic crosses each other at level 80 (overbrought), Short

Regards

Ryan

Christopher Snape
167
Christopher Snape  

Ive added a few lines to this code - this is what uses the LastOpenTicket() function but it still doesnt like it. Ive added -1 option. Does anything look wrong to you here?

//--- Deterime whether previous sell order is profitable

string GetWinLossPreviousShort (int LastOpenTicket, string WinLossPreviousShort)

{

if (OrderSelect(LastOpenTicket(), SELECT_BY_TICKET, MODE_TRADES) == TRUE)

{

if (OrderProfit() > 0 && OrderType() == OP_SELL)

{

WinLossPreviousShort = "W";

return (WinLossPreviousShort);

}

if ((OrderProfit() < 0 && OrderType() == OP_SELL) || LastOpenTicket() == -1)

{

WinLossPreviousShort = "L";

return (WinLossPreviousShort);

}

}

}

Mladen Rakic
176464
Mladen Rakic  

...

Why don't you add one more possible result of that function : soomething like when LastOpenTicket() == -1 then WinLossPreviousShort="Na"

crsnape@btinternet.com:
Ive added a few lines to this code - this is what uses the LastOpenTicket() function but it still doesnt like it. Ive added -1 option. Does anything look wrong to you here?

//--- Deterime whether previous sell order is profitable

string GetWinLossPreviousShort (int LastOpenTicket, string WinLossPreviousShort)

{

if (OrderSelect(LastOpenTicket(), SELECT_BY_TICKET, MODE_TRADES) == TRUE)

{

if (OrderProfit() > 0 && OrderType() == OP_SELL)

{

WinLossPreviousShort = "W";

return (WinLossPreviousShort);

}

if ((OrderProfit() < 0 && OrderType() == OP_SELL) || LastOpenTicket() == -1)

{

WinLossPreviousShort = "L";

return (WinLossPreviousShort);

}

}

}