Trailing stop initiation... - page 2

 
Maji:
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.

apology easily and happily accepted. I appreciate your apologizing. Really thankyou! If your brusque perhaps I am sometimes overly intent. Stay with me on this ok? I know I'm not always the best at explaining.

The function of a trailing stop is to establish a moving stop loss in tow with the increasing profitability of the position...

the difference between a trailing stop and an initial stop loss is that the trailing stop loss moves in response to the price movement keeping a specific distance from the current price whereas the initial stop loss is simply a fixed point below the openorderprice.

When does the trailing stop kick in? well that's my whole point here. based on the traditional trailing stop function it kicks in when the price is enough greater than the openorderprice to warrant changing the initial setting...

what I'm saying is that I want the thing to kick in WHEN I WANT IT TO.

It's initial setting keeps it to that range but I want it to end with a different range setting than it starts with. I also want it to start below what it's typical initial setting would be...I have see trailing stops modified to get smaller as time goes on but I want the reverse of that.

it's a little unconventional what I'm thinking of and I don't know how to do it. But I want to be able to trigger it off the price action rather than from a static initial level....the backcross of two moving averages isn't something I can exactly say is going to be this many pips below the open order price, it is whereever it is. once it triggers I don't want to allow it as much room to trail when it's at that extreme but as it goes more profitable I want to allow it a little more wiggle room. I don't know if I'm explaining this very well...

see the thing is that when I see the backcross of two moving averages I want to basically pull the last ditch effort to save the position. I want this to be the place at which the stop loss engages and says this is the most I will allow this trade to go bad....BUT...I want to salvage as much as possible of the trade at this point so I want it to trail not just sit there static on the outer limit.

 

I only want one initiatonlevel() returned per order. how am I going to do this?

for long positions it will be the Ask() at the time the moving averages cross just like opening a long position would be. How do I store that value for future comparision like the openorderprice() is stored? So it has to be a variable that can be assigned one time and one time only per trade on the first occurance of the trigger condition.

thinking out loud again....

bool InitiatedTS = False; //outside function loop

double Initiationlevel = 0; // outside function loop

void Initiationlevel()

{

int total = OrdersTotal();

for (int cnt = 0 ; cnt < total ; cnt++)

{

OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && Buy1_1 <= CloseBuy1_2 && InitiatedTS = False) // Buy1_1 and CloseBuy1_2 are previously defined as moving averages in the variables section

{

Initiationlevel = Ask();

InitiatedTS = True;

return (Initiationlevel);

}

}

}[/PHP]would this InitiatedTS work like some kind of a magic number and attach to the order so it would only allow one return per order? ...

naw of course it wouldn't so how to I make something like this attach to the order for reference?

round two...

double MagicNumber = 0;// outside function loop, replaces typical extern int MagicNumber

void Initiationlevel()

{

int total = OrdersTotal();

for (int cnt = 0 ; cnt < total ; cnt++)

{

OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && Buy1_1 <= CloseBuy1_2 && 0 = OrderMagicNumber() ) // Buy1_1 and CloseBuy1_2 are previously defined as moving averages in the variables section

{

OrderMagicNumber() = Ask();

}

if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && Sell1_1 >= CloseSell1_2 && 0 = OrderMagicNumber() ) // Sell1_1 and CloseSell1_2 are previously defined as moving averages in the variables section

{

OrderMagicNumber() = Bid();

}

return (OrderMagicNumber());

}

}

how'd I do coach?

one thing I notice about this whole magic number thing is that the expert advisor builder made a variable called MagicNumber but this function seems to be looking for OrderMagicNumber. Are they going to give compiler errors if they aren't the same? Or will the two find each other? Do I need to change the variable to OrderMagicNumber?

[PHP]double OrderMagicNumber = 0;

If I put this on global, will this just initialize the variable, I don't want it to set it and not have the function change it.

 

ok this just exploded....

Compiling 'Gogetter.mq4'...

'Ilevel' - function returns no result C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (119, 61)

'

')' - left parenthesis expected C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (293, 110)

'=' - left parenthesis expected C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (295, 28)

'=' - semicolon expected C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (295, 28)

'=' - unexpected token C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (295, 28)

'Ask' - unexpected token C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (295, 30)

'(' - assignment expected C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (295, 33)

')' - unexpected token C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (295, 34)

';' - semicolon unexpected C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (295, 35)

'}' - unbalanced parentheses C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (296, 11)

')' - left parenthesis expected C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (297, 113)

'=' - left parenthesis expected C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (299, 27)

'=' - semicolon expected C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (299, 27)

'=' - unexpected token C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (299, 27)

'Bid' - unexpected token C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (299, 29)

'(' - assignment expected C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (299, 32)

')' - unexpected token C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (299, 33)

';' - semicolon unexpected C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (299, 34)

'}' - unbalanced parentheses C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (301, 5)

'else' - unexpected token C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (301, 7)

'=' - left parenthesis expected C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (303, 27)

'=' - semicolon expected C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (303, 27)

'=' - unexpected token C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (303, 27)

'0' - unexpected token C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (303, 29)

';' - assignment expected C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (303, 30)

')' - left parenthesis expected C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (305, 34)

29 error(s), 2 warning(s)

I changed this at the top from int to double

extern double MagicNumber = 0;

I added this at the top

double AltOpenPrice = 0;

I added this to the exit logics

if (Buy1_1 <= CloseBuy1_2) AltOpenPrice = Ilevel();

and put this function new function at the bottom

I don't believe any of the error below the exit logics and above the new function..I didn't touch anything there.

what a mess.

ok i just read up on magic numbers...I think I'll take a fresh stab at this tomorrow. thanks for the assistance today.

 

fresh approach...one error

i'm still thinking my way thru this if it will do what I want...

//+-------------deep in position trailing stop initiation--------------+

void Ilevel()

{

double Buy1_1 = extern Buy1_1;

double CloseBuy1_2 = extern CloseBuy1_2;

double Sell1_1 = extern Sell1_1;

double CloseSell1_2 = extern CloseSell1_2;

int total = OrdersTotal();

for (int cnt = 0 ; cnt < total ; cnt++)

{

OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && Buy1_1 <= CloseBuy1_2 && Bid <= OrderOpenPrice () + TrailingStop)// Buy1_1 and CloseBuy1_2 are previously defined as moving averages in the variables section

{

Ilevel = Ask ;

}

if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && Sell1_1 >= CloseSell1_2 && Ask >= OrderOpenPrice () - TrailingStop)// Sell1_1 and CloseSell1_2 are previously defined as moving averages in the variables section

{

Ilevel = Bid ;

} else {

Ilevel = OrderOpenPrice ();

}

return (Ilevel);

}

}

//+----------end of deep in position trailing stop initiation---------+[/PHP]

here is where I call the function..

//Trailing stop for short position

if(TrailingStopMode && TrailingStop > 0) {

if((Ilevel() - Ask) > (Point * TrailingStop)) {

if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {

OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);

if (!EachTickMode) BarCount = Bars;

TrailingStopMode = False; //resets mode after each order

StopLossMode = False; //resets mode after each order

continue;[/PHP]

this returns one error from thecompiler...

'Ilevel' - function returns no result C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (168, 20)

then I put it in here too and...

[PHP]

//Trailing stop for long position

if(TrailingStopMode && TrailingStop > 0) {

if(Bid - Ilevel() > Point * TrailingStop) {

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

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

if (!EachTickMode) BarCount = Bars;

TrailingStopMode = False; //resets mode after each order

StopLossMode = False; //resets mode after each order

continue;

volia..

[PHP]

'Ilevel' - function returns no result C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (134, 25)

'else' - unexpected token C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (144, 12)

'continue' - 'break' or 'continue' used within some cycle only C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (164, 16)

'Ilevel' - function returns no result C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (168, 20)

'continue' - 'break' or 'continue' used within some cycle only C:\Program Files\Interbank FX Trader 4-live mini\experts\Gogetter.mq4 (174, 22)

5 error(s), 0 warning(s)

 

amazing

spent a couple hours in the library reading C++ books...came home...changed the 'void' to 'double' on the Ilevel function...

no errors...

Reason: