Trailing stop initiation...

 

I notice that when trailing stops initiate it seems that they only increase when the price moves above the OrderOpenPrice()

I would like to get a trailing stop to start trailing from the current price regardless of where that is in relation to the OpenOrderPrice()

am I misinterpreting where it initiates?

 

Look for something similar to the following statement:

profit=NormalizeDouble((Bid-OrderOpenPrice())/Point,0);

if(profit<start)

continue;

basically, when the profit is higher than the start (an amount designated by you) the trailing stop starts. You can take the If... continue statement out and see what it does. Experiment with it.

 

Trailing Stop

Are you asking about trailing stops with MT4? If so, the trailing stop feature only works when:

a) it's at XX plus 1

b) your computer is on or the trading platform is loaded on a server

 
Maji:
Look for something similar to the following statement:

profit=NormalizeDouble((Bid-OrderOpenPrice())/Point,0);

if(profit<start)

continue;

basically, when the profit is higher than the start (an amount designated by you) the trailing stop starts. You can take the If... continue statement out and see what it does. Experiment with it.

you know your really are a genius and like most geniuses you leave just enough gaps in your suggestions to the common folk to leave us puzzling...

So the puzzlement is first...where do I look for this statement? the only place I see the profit variable is in this plain vanilla CloseOrder() that you made before...which I think I almost understand now btw. I wouldn't mind seeing a version of that function used to turn the trailing stop on or to tighten it in steps at specified times.

but getting back to this...the second puzzlement is the 'start' specifed...that would just be the current price level when the thing triggers. or would it? hum...

i like the idea of tightening the trailing near the lower tolerance extreme and letting it trail wider if it's away from the tolerance edge.

what function calls the current price? is that just point()? maybe something with the orderprofit()???hum

 
Aaragorn:
what function calls the current price? is that just point()?

"Bid" and "Ask" give the current price. (By default, MT4 shows the Bid price on the chart).

"Point" (not "point()") is the value of 1 pip of a currency pair. For GBPUSD, Point=0.0001, for USDJPY it is 0.01 etc.

So if you want to do something, say, 50 pips below My_Price, you test:

if( Bid < MyPrice - 50*Point ) {...}

PS: price isn t updated within the start() function. if the start function takes a long time to implement (or if an OderSend failed due to requotes), at some point you may want to refresh the price by using the RefreshRates() function.

 
Aaragorn:
you know your really are a genius and like most geniuses you leave just enough gaps in your suggestions to the common folk to leave us puzzling...

So the puzzlement is first...where do I look for this statement? the only place I see the profit variable is in this plain vanilla CloseOrder() that you made before...which I think I almost understand now btw. I wouldn't mind seeing a version of that function used to turn the trailing stop on or to tighten it in steps at specified times.

but getting back to this...the second puzzlement is the 'start' specifed...that would just be the current price level when the thing triggers. or would it? hum...

i like the idea of tightening the trailing near the lower tolerance extreme and letting it trail wider if it's away from the tolerance edge.

what function calls the current price? is that just point()? maybe something with the orderprofit()???hum

I am not a genius by any stretch of imagination. I can as well modify a trailingstop routine and give it to you, but then you won't learn. My intention all along is to force you to learn. That is the way I learnt and that is the way I want to help others... really help themselves Remember the saying about giving a fish and showing how to fish?

You should check the code, not closeall, but your code where the trailingstop method is implemented. Study it and it will scream back to you on how to modify it.

 
Maji:
I am not a genius by any stretch of imagination. I can as well modify a trailingstop routine and give it to you, but then you won't learn. My intention all along is to force you to learn. That is the way I learnt and that is the way I want to help others... really help themselves Remember the saying about giving a fish and showing how to fish? You should check the code, not closeall, but your code where the trailingstop method is implemented. Study it and it will scream back to you on how to modify it.

Ok, you are a modest genius who likes to teach. Granted. The only thing I call into question in your methods is the assertion that I don't learn from you doing it. I will conceed that perhaps I might not learn on the same curve as if I am 'forced' to slog it out myself, but I maintain that I learn both ways. If I were of a mind to not apply any effort myself and totally not pay any attention I would agree but that is not the case.

Since your choice in how to teach is your option and not mine, I shall look for screaming code.

 

First, what is the function of a trailing stop?

Second, what is the difference between a trailing stop and the initial stop loss?

Third, when does a trailing stop kick in?

See if you can figure these yourself. After you are done with that, then see how the program logic handles these three items. I see that you are forcing yourself to learn, and that is clearing things up in your own mind. Of course, there will be some confusion, but it will gradually clear up as you understand the functioning of the trailing stop system better.

Sorry for my brusque replies.

 

here is what I understand of the code...

//Trailing stop

if(TrailingStopMode && TrailingStop > 0) {[/PHP]

trailing stop mode is the bool variable which if true = 1 would satisfy the condition alone being greater than zero && (odd use of &&?) trailing stop is whatever external user value defined. so this line asks if the trailing stop is being used or not basically as determined by >0. I assume that if this condion returns false that it proceed to the 'else' and moves on...

no screaming so far...next

if(Bid - OrderOpenPrice() > Point * TrailingStop) {[/PHP]

if the Bid (which is the current price to close a long position) minus the order open price...(which would scream 'profit' to me assuming the position is profitable) is > normalized trailing stop value.

so now we know the trailing stop is being used and that the price is currently higher than the trailing stop value to get an affirmative from this condition. I assume that if this condion returns false that it proceed to the 'else' and moves on...

next line..

if(OrderStopLoss() < Bid - Point * TrailingStop) {

if Orderstoploss() ok that's a new one...returns the value of the open order stop loss...< current price to close a long position - normalized external user defined trailing stop value. This says to me that it should close if this returns true. I assume that if this condion returns false that it proceed to the 'else' and moves on...I admit it this one confuses me. ok I get it...if the current stop loss value is less than what the current stop loss value shoud be now...

next

[PHP]OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);

modifies the order to the current price to close a long position - the normalized trailing stop value...in other word the new stop loss level.

so I see one screamer there...you were right it did scream...

sooooo

[PHP]if(Bid - OrderOpenPrice() < OrderOpenPrice - Point * trigger1)

TrailingStop=T1; {

should be saying

if the current value of the long position is less than the normalized user defined trigger value....assign trailing stop this value

that should get it assigned at the trigger level...now to get the modification done and the code in the right place with the rest....

so, the lines say..

1- if we are using the trailing stop...

2- if the position's current value is higher than the trailing stop now...

3- if the current stop loss is less than the trailing stop parameter setting...

4- modify the order to the present level trailing setting.

well I definitely see that line two is where change is needed...

what i did so far assigns the trailing stop a new value that I can define at a certain level...

but what happens to the logic of the sequence when I do that...

so, IF the lines say..

1- if we are using the trailing stop...

2- if the position's current value is less than I will allow it to be without a trailing stop.../*higher than the trailing stop now*/...give the trailing stop this value...

3- if the current stop loss is less than the trailing stop parameter setting...

4- modify the order to the present level trailing setting.

see there's a problem with this..

2- if the position's current value is less than I will allow it to be without a trailing stop.../*higher than the trailing stop now*/...give the trailing stop this value...

that will trigger it the first time but that will not continue to trail once the price rises above me set level......will it??

what happens the second time it askes 'is the current value < I will allow it to be without a trailingstop? ...and it says NO! and proceeds to the else clause...

oy

I don't want to destroy the functionality of the trailing I just want to control where it engages in relation to the openorderprice() and how tightly it engages in stepping levels in relation to the openorderprice()...

I've got to think this thru so more...how do I put this in the loop without destroying the loop?

 

Timeline anatomy of a losing position...

Let me see if I can draw a hypothetical situation out with traditional stop loss tools and what I want to make instead.

The first thing that happens is the order opens. With traditional tools it can have a stop loss limit and or a trailing stop parameter.

Let's assume this is a long position for our discussion.

So the trade opens and in process of time it loses part of it's value. Let's say that a traditional stop loss would close at 50 pips. The traditional trailing stop of 20 pips won't engage until the trade has recovered all it's original value plus 21 pips. Otherwise it sits there doing nothing leaving a range of 71 pips exposed to the ups and downs of the market as long as the position is open. The range of 71 pips is above the stop loss and below the first place where the trailing stop would engage.

What I'm saying is that is far too much territory to leave unprotected.

The stop loss sets a bottom limit much like the air bag on the car. But it is also like a car stuck on the railroad tracks. It can't move. If I am sitting in that car on the railroad tracks I don't really want to have to rely on my airbag. I want to get off the tracks before it gets ugly. And I don't especially want to have to wait until the position has recovered all of it's original value plus the trailing stop range before I make a move. I want to protect every gain I can even below the original position value because it may or may not recover any part of it's full value. I want to protect whatever it gives me even if that's below the original value of the position.

so it has to trail not be a static bottom but it has to engage below the original value of the position.

does that make sense?

I want to initiate the stop loss trailing with x=10 pips when the position is x=40 or so pips below the openorderprice.

my question is HOW do we do that?

ok let's break this into pieces...

we have no problem with the traditional trailing stop once the order has become profitable enough to make it trail up. The problem is that it doesn't engage when the order has lost value.

It can't be traditionally started until it has exceeded the original openorderprice....hence the title of the thread...initiation.

so we need to make it able to engage before the position has recovered all it's value by a specified factor to make it responsive to the specified range BELOW the openorderprice.

...and of course do that without messing up it's normal function once it reaches the level it would normally engage at.

I want to make a better trailing stop that isn't afraid to start it's life in the basement rather than wait around on the ground floor for signs of progress.

 

ok time to roll up my sleeves...

first step is to get it's engagement price just like we would an openorderprice() we need to know where the opentrailingstopprice()

that can be returned as the result of whatever trigger we use to enage it as the "initiationlevel"

the initiation level will always be some degree toward the loss of value in the position. If it's a long position the initiationlevel() will always be below the openorderprice() and visa versa for shorts positions.

so we know that the initiationlevel() is a specific distance below the openorderprice()

we know that above the openorderprice() traditional trailing code will take over once the current price exceeds the openorderprice() + trailingstop()

we know that the traditional trailing code bases it's comparison to the openorderprice() as to wheather or not to move the trailing stop...

therefore we need to use the (openorderprice() - (openorderprice() - initiationlevel())) as the basis for the trailing movement for 0>price values>initiationlevel()

that still leaves the range 0<price values<where the traditional trailing stop kicks in....

hum.....maybe one step at a time is enough...lets' work thru the range 0>price values>initiationlevel() first....

come to think of it..that's too complicated...all it really needs to do is to consider the initiationlevel() IS the openorderprice() as far as the trailing stop loss is concerned.

we need a function that will return the initiationlevel() just like the orderopenprice()

then regulate the trailing stop by a condition that says if we are <orderopenprice()+trailingstop() then use this alternate initiationlevel() as the relative comparision instead of the openorderprice()

that should do it eh? it sounds right to me.

Reason: