Using a Moving Average cross for take profit once target has been reached - page 2

 
simoncs:


sure no problem, i thought it might be something fundamental, about the way functions should be used, so was hoping not to have to post loads of code for you to read, but here are the relevant parts of code below.

this assumes there is an open trade... and i have no issues with getting into the trade at the correct point. Just closing it.

calling the TakeProfit() running in Start.

then all my other functions are outside the Start function, and just referenced in start as necessary.

the closeall()

the TakeProfit() - why does this not close the trade after price has passed the TP?

What is TakeProfit ? a function ? a bool ? a local bool that needs to be returned ? or a global that doesn't ? don't mix you variable names with your function names, don't return globally declared variables . . . there is no need.

Where is your OrderSelect() ? what order has been selected when you call OrderType() ? Bearing in mind that TakeProfit is globally declared . . . where is it set to false ?

simoncs:

the question is why does Takeprofit() not close the trade when price moves past the TP if i set TakeProfit =true and then try to close it, as opposed to using closeall() inside the TakeProfit() which does work?

Can't tell for certain, you didn't post all the relevant code . . . if I had to guess . . . .

TakeProfit, the variable, not the function, doesn't get set back to false, so once set to true is always true, so if price doesn't go past the TP and closeall() is called from inside TakeProfit() then it isn't called . . . but if closeall() is commented out, as above, and TakeProfit is always true then closeall() will be called even if price hasn't passed TP.

If you want to know if your TP is passed add a Print() that will be called only when your condition is met . . .

if (sellprice() >= (TP()+pips2dbl+Spread()))
        {
         Print("sellprice >= TP()  ! ! !");    // <--  add this line

         TakeProfit=true;
         //closeall();
         //CloseReason = 1;
        }



By the way, pips2dbl is meant to be used by multiplying a value in pips, for example, 3 pips, into a double value such as 0.0003

 
RaptorUK:

What is TakeProfit ? a function ? a bool ? a local bool that needs to be returned ? or a global that doesn't ? don't mix you variable names with your function names, don't return globally declared variables . . . there is no need.

Where is your OrderSelect() ? what order has been selected when you call OrderType() ? Bearing in mind that TakeProfit is globally declared . . . where is it set to false ?

Can't tell for certain, you didn't post all the relevant code . . . if I had to guess . . . .

TakeProfit, the variable, not the function, doesn't get set back to false, so once set to true is always true, so if price doesn't go past the TP and closeall() is called from inside TakeProfit() then it isn't called . . . but if closeall() is commented out, as above, and TakeProfit is always true then closeall() will be called even if price hasn't passed TP.

If you want to know if your TP is passed add a Print() that will be called only when your condition is met . . .



By the way, pips2dbl is meant to be used by multiplying a value in pips, for example, 3 pips, into a double value such as 0.0003


Thanks Raptor!!!

I think that's the problem - somehow i completely forgot to set TakeProfit back to false anywhere. I did have Print statement to prove exactly that but deleted them before posting.

With regard to the OrderSelect() - I thought i could have this as a function to determine the TicketNumber and the Ordertyp, and then just use TicketNumber and Ordertyp in other parts of code (these variables are declared globally). Does this not work, and one should really use OrderSelect() in every relevant part?

I do use pips2dbl correctly elsewhere, but it is also a way for me to add 1 pip to my stoploss.

 
simoncs: I think that's the problem - somehow i completely forgot to set TakeProfit back to false anywhere.
So do your bools correctly
if (sellprice() >= (TP()+pips2dbl+Spread()))
        {
         Print("sellprice >= TP()  ! ! !");    // <--  add this line

         TakeProfit=true;
TakeProfit = (sellprice() >= (TP()+pips2dbl+Spread()));
if(TakeProfit)
        {
         Print("sellprice >= TP()  ! ! !");    // <--  add this line

         // TakeProfit=true;
Reason: