Trailing stops shouldn't trail downward

 

Hello All:

I'm trying to program my stops so that if profit is greater than 30 (pips), we will have a stop equal to the 55 Period MA, if profit is greater than 60, we'll use a 21 MA, if profit is greater then 90 we'll use an 8 MA. The problem is when profit goes above 60 (so it reverts to the 21 MA) and then falls back below 60, the moving average falls back to the 55 period MA (which we don't want). The stop should hold at the 21 MA.

Once the price leaves the 55ma tier, it should not go back there. Can anyone help?

ma1 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_CLOSE,0);
ma2 = iMA(NULL, 0, 21, 0, MODE_EMA, PRICE_CLOSE,0);
ma3 = iMA(NULL, 0, 55, 0, MODE_EMA, PRICE_CLOSE,0);

OOP30= OrderOpenPrice() + 30*Point;
OOP60= OrderOpenPrice() + 60*Point;
OOP90= OrderOpenPrice() + 90*Point;
OOP120= OrderOpenPrice() + 120*Point;

if(OrderType() == OP_BUY)
{
if( Ask > (OOP30) && Ask < (OOP60) && Ask > ma3)
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma3, OrderTakeProfit(),0,Blue);
}

if( Ask > (OOP60) && Ask < (OOP90) && Ask > ma2)
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma2, OrderTakeProfit(),0,Blue);
}

if( Ask > (OOP90) && Ask < (OOP120) && Ask > ma1)
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma1, OrderTakeProfit(),0,Blue);
}

Thanks,

Pat

 
FXpipclash:

Hello All:

I'm trying to program my stops so that if profit is greater than 30 (pips), we will have a stop equal to the 55 Period MA, if profit is greater than 60, we'll use a 21 MA, if profit is greater then 90 we'll use an 8 MA. The problem is when profit goes above 60 (so it reverts to the 21 MA) and then falls back below 60, the moving average falls back to the 55 period MA (which we don't want). The stop should hold at the 21 MA.

Once the price leaves the 55ma tier, it should not go back there. Can anyone help?

ma1 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_CLOSE,0);
ma2 = iMA(NULL, 0, 21, 0, MODE_EMA, PRICE_CLOSE,0);
ma3 = iMA(NULL, 0, 55, 0, MODE_EMA, PRICE_CLOSE,0);

OOP30= OrderOpenPrice() + 30*Point;
OOP60= OrderOpenPrice() + 60*Point;
OOP90= OrderOpenPrice() + 90*Point;
OOP120= OrderOpenPrice() + 120*Point;

if(OrderType() == OP_BUY)
{
if( Ask > (OOP30) && Ask < (OOP60) && Ask > ma3)
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma3, OrderTakeProfit(),0,Blue);
}

if( Ask > (OOP60) && Ask < (OOP90) && Ask > ma2)
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma2, OrderTakeProfit(),0,Blue);
}

if( Ask > (OOP90) && Ask < (OOP120) && Ask > ma1)
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma1, OrderTakeProfit(),0,Blue);
}

Thanks,

Pat

Im not the greatest coder but I might try help with a suggestion that might prompt other coders to help as well...


maybe you should try using flags...like bool firstmove,secondmove,thirdmove; at the top of your code and them implement it like this :


f(OrderType() == OP_BUY)
{
if( Ask > (OOP30) && Ask < (OOP60) && Ask > ma3 && secondmove==false && thirdmove==false)
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma3, OrderTakeProfit(),0,Blue);

firstmove=true;
}

if( Ask > (OOP60) && Ask < (OOP90) && Ask > ma2 && firstmove==true && thirdmove==false)
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma2, OrderTakeProfit(),0,Blue);

secondmove=true;
}

if( Ask > (OOP90) && Ask < (OOP120) && Ask > ma1 && firstmove==true && secondmove==true)
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma1, OrderTakeProfit(),0,Blue);

thirdmove=true;
}


hope this helps...if not I hope at least my attempt to help prompts some better coders to help.

cheers

 
23510 wrote >>

Im not the greatest coder but I might try help with a suggestion that might prompt other coders to help as well...

maybe you should try using flags...like bool firstmove,secondmove,thirdmove; at the top of your code and them implement it like this :

f(OrderType() == OP_BUY)
{
if( Ask > (OOP30) && Ask < (OOP60) && Ask > ma3 && secondmove==false && thirdmove==false)
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma3, OrderTakeProfit(),0,Blue);

firstmove=true;
}

if( Ask > (OOP60) && Ask < (OOP90) && Ask > ma2 && firstmove==true && thirdmove==false)
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma2, OrderTakeProfit(),0,Blue);

secondmove=true;
}

if( Ask > (OOP90) && Ask < (OOP120) && Ask > ma1 && firstmove==true && secondmove==true)
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma1, OrderTakeProfit(),0,Blue);

thirdmove=true;
}

hope this helps...if not I hope at least my attempt to help prompts some better coders to help.

cheers

23510:

Thank! It worked out great, I have another small problem though and since you've been so helpful thus far :) I figured I'd run it by you. Here is what I have:

if(OrderType() == OP_BUY)
{
if( Ask > (OOP30) && Ask < (OOP60) && Ask > ma3 && tier1 == false)
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma3, OrderTakeProfit(),0,Blue);
tier1=true;
}

if( Ask > (OOP60) && Ask < (OOP90) && Ask > ma2 && tier1 == true && tier2 ==false)
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma2, OrderTakeProfit(),0,Blue);
tier2 =true;
}

if( Ask > (OOP90) && Ask < (OOP120) && Ask > ma1 && tier1 == true && tier2 == true && tier3 ==false)
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma1, OrderTakeProfit(),0,Blue);
tier3 = true;
}

The problem I know have is that if say we get above 60 pips of profitability and the market falls back below that profit range, the stop stops moving where the 21 period MA was when it fell below the market. In other words, after it falls below 60 pips, the stop is left at the point where the 21 period MA was when the market fell, it no longer keeps refreshing to the 21 period MA. It's not a major discrepency, but a problem non the less. Thanks again for your help and any futher assistance you could be would be appreciated.

Thanks,

Pat

 

Hi Can I suggest using a Case statement to give a trailing stop sequencer and add the order value of Stoploss, OrderStopLoss(), to the test so if your new stoploss is closer to the current price it gets used otherwise it doesn't. switch Trailsequencer{ Case 1: if( current condition of big TS is still valid ){ Modify order else Trailsequencer++; } break; Case 2: if( current condition of middle TS is still valid ){ Modify order else Trailsequencer++; } break; Case 3: if( current condition of small TS is still valid ){ Modify order else Trailsequencer++; } break; Default : } Using this the sequence can't go backwards and everthing doesn't get tested every time just the next level gets checked.


Poxy editor has screwed up the layout I don't know why.

 
Ruptor:

Hi Can I suggest using a Case statement to give a trailing stop sequencer and add the order value of Stoploss, OrderStopLoss(), to the test so if your new stoploss is closer to the current price it gets used otherwise it doesn't. switch Trailsequencer{ Case 1: if( current condition of big TS is still valid ){ Modify order else Trailsequencer++; } break; Case 2: if( current condition of middle TS is still valid ){ Modify order else Trailsequencer++; } break; Case 3: if( current condition of small TS is still valid ){ Modify order else Trailsequencer++; } break; Default : } Using this the sequence can't go backwards and everthing doesn't get tested every time just the next level gets checked.


Poxy editor has screwed up the layout I don't know why.

Ruptor did you want it posted like this ? :

Hi Can I suggest using a Case statement to give a trailing stop sequencer and add the order value of Stoploss, OrderStopLoss(), to the test so if your new stoploss is closer to the current price it gets used otherwise it doesn't. 

switch Trailsequencer
{ 
Case 1: if( current condition of big TS is still valid ){ Modify order else Trailsequencer++; } 
break; 

Case 2: if( current condition of middle TS is still valid ){ Modify order else Trailsequencer++; } 
break; 

Case 3: if( current condition of small TS is still valid ){ Modify order else Trailsequencer++; } 
break; 

Default : } Using this the sequence can't go backwards and everthing doesn't get tested every time just the next level gets checked.
 
Ruptor wrote >>

Hi Can I suggest using a Case statement to give a trailing stop sequencer and add the order value of Stoploss, OrderStopLoss(), to the test so if your new stoploss is closer to the current price it gets used otherwise it doesn't. switch Trailsequencer{ Case 1: if( current condition of big TS is still valid ){ Modify order else Trailsequencer++; } break; Case 2: if( current condition of middle TS is still valid ){ Modify order else Trailsequencer++; } break; Case 3: if( current condition of small TS is still valid ){ Modify order else Trailsequencer++; } break; Default : } Using this the sequence can't go backwards and everthing doesn't get tested every time just the next level gets checked.

Poxy editor has screwed up the layout I don't know why.

23510
wrote
>>

Ruptor:

Here is what I have for the first series in the switch, please forgive me, I haven't used the switch operator before:

int trailsequencer = 30 //should this be the value that my stops increase by for each level?

switch(trailsequencer)
{
case 1 : if( Ask > (OOP30) && Ask < (OOP60) // If the current price is inbetween 30-60 pips of profitability
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma3, OrderTakeProfit(),0,Blue); //Modify the order to have a SL of the Big Trail (ma3)
}
else //otherwise
{
trailsequencer++; //increase the trailer by 30 (to 60) and pass control to the next case to check to see if profitability falls between 60
} &90
break;

Any elaboration you could give would be very helpful,

Thank you,

Pat

 

Hi Guys

Yes 23510 thanks that's better. Although seeing it like that I would have changed it slightly because I put the brackets in a misleading place.


Pat here is your case 1: arranged how I meant


The trailsequencer is just an integer number that goes 1,2,3 to switch between cases. As you can see once you increase it inside Case 1: then only Case 2: will be entered until trailsequence is increased again.

You have to initilaise trailsequencer when the trade is opened and possibly clear it when the trade is closed but these tasks are dependent upon your code structure.

The break command makes the code exit so no other case is executed until the next entry of the switch command without it every case would be executed and that would make the switch statement useless

 
Ruptor wrote >>

Hi Guys

Yes 23510 thanks that's better. Although seeing it like that I would have changed it slightly because I put the brackets in a misleading place.

Pat here is your case 1: arranged how I meant

The trailsequencer is just an integer number that goes 1,2,3 to switch between cases. As you can see once you increase it inside Case 1: then only Case 2: will be entered until trailsequence is increased again.

You have to initilaise trailsequencer when the trade is opened and possibly clear it when the trade is closed but these tasks are dependent upon your code structure.

The break command makes the code exit so no other case is executed until the next entry of the switch command without it every case would be executed and that would make the switch statement useless

Ruptor:

Thank you for all of your help thus far, however, I feel I may have to go back to the drawing board on this one. This is what I have below, I had to add an if(Ask > OOP30) before the switch operator because The operator would start, see that the market was not inbetween OOP30 & OOP60, and increase the trailsequence, then it would go to the next case, see that it doesn't fit the criteria and increase trailsequence again and then the trailsequence would be stuck at 3 and the stop would only move if the criteria of case 3 was met (Ask > OOP90 && Ask < OOP120.

The other issue I am having is that when the market falls in the second case, it will continue modifying as it is supposed to, but then when the market falls below the criteria for the second case, the If then else statement will increase the trailsequence (because it doesn't fit the criteria) and the trailsequence will again be stuck at 3rd case level. I've been mulling it over for a while and might come back to it monday with a cleared head :)

Thanks again for everything,

Pat

OOP30= OrderOpenPrice() + 30*Point;
OOP60= OrderOpenPrice() + 60*Point;
OOP90= OrderOpenPrice() + 90*Point;
OOP120= OrderOpenPrice() + 120*Point;

if(OrderType() == OP_BUY)
{
if(Ask > OOP30)
{
switch(trailsequencer)
{

case 1 :
if( Ask > (OOP30) && Ask < (OOP60))
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma3, OrderTakeProfit(),0,Blue);
}
else
{
trailsequencer++;
}
break;

case 2 :
if(Ask > (OOP60) && Ask < (OOP90))
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma2, OrderTakeProfit(),0,Blue);
}
else
{
trailsequencer++;
}
break;

case 3 :
if(Ask > (OOP90) && Ask < (OOP120))
{
OrderModify(OrderTicket(),OrderOpenPrice(),ma1, OrderTakeProfit(),0,Blue);
}
break;

}
}

 

HI Pat

Yes you do need the condition before the switch statement but I'm not sure yours is right because CASE 1: has it already. As I said your use of this sequencer is dependent upon the srrounding code you can just slap it in so it runs all the time. Although I did put the band condition in my example I think you can see it is not required from your tests. The sequencer steps up with profit so each step only requires a greater than test not a band test like this>>>>


OOP30= OrderOpenPrice() + 30*Point;
OOP60= OrderOpenPrice() + 60*Point;
OOP90= OrderOpenPrice() + 90*Point;
OOP120= OrderOpenPrice() + 120*Point;

if(OrderType() == OP_BUY){
if(Ask > OOP30){

switch(trailsequencer){
case 1 :
if( Ask < OOP60){
OrderModify(OrderTicket(),OrderOpenPrice(),ma3, OrderTakeProfit(),0,Blue);
}else{
trailsequencer++;
}
break;

case 2 :
if(Ask < OOP90){
OrderModify(OrderTicket(),OrderOpenPrice(),ma2, OrderTakeProfit(),0,Blue);
}else{
trailsequencer++;
}
break;
case 3 :
if(Ask < OOP120){
OrderModify(OrderTicket(),OrderOpenPrice(),ma1, OrderTakeProfit(),0,Blue);
}
break;
}

}

}

You should indent your code too it makes it easier to read and spot missing brackets and statements. Don't take this code as gospel I'm just trying to point you in the right direction not design it for you.

Oh putz! ignore the indent comment the post has messed up mine too.

 

Ruptor: Sure enough, you're right, it's working very well, thank you very much for your help, I now have to be sure the trailsequencer will be properly reset at the open of new trades.

Thanks again,

Pat

 
Ruptor wrote >>

HI Pat

Yes you do need the condition before the switch statement but I'm not sure yours is right because CASE 1: has it already. As I said your use of this sequencer is dependent upon the srrounding code you can just slap it in so it runs all the time. Although I did put the band condition in my example I think you can see it is not required from your tests. The sequencer steps up with profit so each step only requires a greater than test not a band test like this>>>>

OOP30= OrderOpenPrice() + 30*Point;
OOP60= OrderOpenPrice() + 60*Point;
OOP90= OrderOpenPrice() + 90*Point;
OOP120= OrderOpenPrice() + 120*Point;

if(OrderType() == OP_BUY){
if(Ask > OOP30){

switch(trailsequencer){
case 1 :
if( Ask < OOP60){
OrderModify(OrderTicket(),OrderOpenPrice(),ma3, OrderTakeProfit(),0,Blue);
}else{
trailsequencer++;
}
break;

case 2 :
if(Ask < OOP90){
OrderModify(OrderTicket(),OrderOpenPrice(),ma2, OrderTakeProfit(),0,Blue);
}else{
trailsequencer++;
}
break;
case 3 :
if(Ask < OOP120){
OrderModify(OrderTicket(),OrderOpenPrice(),ma1, OrderTakeProfit(),0,Blue);
}
break;
}

}

}

You should indent your code too it makes it easier to read and spot missing brackets and statements. Don't take this code as gospel I'm just trying to point you in the right direction not design it for you.

Oh putz! ignore the indent comment the post has messed up mine too.

Sorry,nm, it does it automatically when it places the trade.

Thanks again

Reason: