How to code? - page 303

 

another beginner's questions

Hi, I am new to programming but not new to trading. I did not post anything for a while and now, since I started developing my EA, I have lots of questions. I started with building an indicator first, and once it does what I want I will go further. I read all the manuals I could find, so now it is time for me to move ahead with coding.

Sorry, if it is a wrong thread - please let me know where I should post such questions then.

I have 2, probably, basic questions for now, please:

1. In many indicators using MAs I see lines checking for how many bars are on the chart. So, if I need MA200 and the chart only has, say, 100 bars, would iMA200 work then? I think yes - I checked and any MA is drawn without any problems on any chart, so I think this is an old bug that Metaquotes fixed now. So, is there a point of checking how many bars are there on the chart for drawing MA or using iMA function?

2. What is a difference between Close and iClose? My understanding is that Close can return a close of the bars on the current chart only, and iClose can return any close of any currency pair and any timeframe. And in both cases, in order to get a last closed bar I have to use index [1]. Am I seeing it correct?

Thank you.

 

Ive done that already. See highlighted red below:

//--- 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);

}

}

}

Still completely blank.

Scratching my head. I know your help is limited because I havnt pasted the code, but does the above code look OK to you?

Also with the first line below, do I need to reference LastOpenTicket with () after?

string GetWinLossPreviousShort (int LastOpenTicket, string WinLossPreviousShort)

 

...

If you are calling that function with the ticket number of the last opened ticket as an argument, then the code after the blue line will never be executed (the OrderSelect() fails if the LastOpenTicket number is -1 (which is in that moment equivalent to LastOpenTicket() function result), and the code does not get to execute the block after the blue line)

crsnape@btinternet.com:
Ive done that already. See highlighted red below:

//--- 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);

}

}

}

Still completely blank.

Scratching my head. I know your help is limited because I havnt pasted the code, but does the above code look OK to you?

Also with the first line below, do I need to reference LastOpenTicket with () after?

string GetWinLossPreviousShort (int LastOpenTicket, string WinLossPreviousShort)
 

Im not sure if I understand.

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

So because I am calling the function LastOpenTicket() in the line above, the rest of that function does not get executed and the OrderSelect() fails. Am I following you right?

 

...

Lets assume the following :

- you had no previous orders

- LastOpenedTicket() returned -1 (no previous orders) as a result

- you call the function with that result

OrderSelect() fails since order ticket -1 does not exist and none of the code behind the order select is executed. Even in the case when LastOpenTicket is > -1, the "blue line condition" that LastOpenTicket() == -1 can never be true since the only way it can get to that part of code is when LastOpenTicket is > -1

That is why I suggested the "3rd state : the "Na", since in fact you do have 3 states because of the starting "no previous orders" state

crsnape@btinternet.com:
Im not sure if I understand.

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

So because I am calling the function LastOpenTicket() in the line above, the rest of that function does not get executed and the OrderSelect() fails. Am I following you right?
 

some coding questions, probably beginner's

Hi, I am new to programming but not new to trading. I am building my EA but started with building an indicator first, and once it does what I want I will go further. I read all the manuals I could find and now coding it.

Would appreciate any advice - you all started where I am now...

I have 2, probably, basic questions for now, please:

1. In some indicators using MAs I see lines checking for how many bars are on the chart. So, if I need MA200 and the chart only has, say, 100 bars, would iMA200 work then? I think yes - I checked and any MA is drawn without any problems on any chart, so I think this is an old problem that Metaquotes fixed now. So, is there a point of checking how many bars are there on the chart for drawing MA or using iMA function?

2. What is a difference between Close and iClose? My understanding is that Close can return a close of the bars on the current chart only, and iClose can return any close of any currency pair and any timeframe. And in both cases, in order to get a last closed bar I have to use index [1]. Is that correct?

Thank you.

 

Like this?

//--- 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)

{

WinLossPreviousShort = "L";

return (WinLossPreviousShort);

}

if (LastOpenTicket() == -1)

{

WinLossPreviousShort = "NA";

return (WinLossPreviousShort);

}

}

}

OR THIS? changes in red.

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

string GetWinLossPreviousShort (int LastOpenTicket, int LastTicket,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)

{

WinLossPreviousShort = "L";

return (WinLossPreviousShort);

}

if (LastTicket == -1)

{

WinLossPreviousShort = "NA";

return (WinLossPreviousShort);

}

}

}

 

ah its finally clicked, I cant use OrderSelect on a ticket number thats -1 because -1 doesnt exist. Tickets go from 0 upwards.

I need to declare LastOpenTicket outside of the OrderSend function right?

 

...

Try like this :

string GetWinLossPreviousShort (int LastOpenTicket, int LastTicket, string WinLossPreviousShort)

{

if (LastTicket == -1)

{

WinLossPreviousShort = "NA";

return (WinLossPreviousShort);

}

//

//

// if LastTicket == -1 no need even to try to execute the code bellow

// you already have exited for case when it is -1, so the code bellow

// will be executed properly

//

//

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)

{

WinLossPreviousShort = "L";

return (WinLossPreviousShort);

}

}
 

Thanks mladen, does the code below basically equate to the same as yours? I typed this as you posted your last! :-)

EDITED-

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

string GetWinLossPreviousShort (int LastOpenTicket, int LastTicket, 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)

{

WinLossPreviousShort = "L";

return (WinLossPreviousShort);

}

}

if (LastTicket == -1)

{

WinLossPreviousShort = "NA";

return (WinLossPreviousShort);

}

}

Reason: