• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 7:23am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 7:23am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Looking for Trailing Stop EA like default MT4 trailing stop 7 replies

Trailing stop after up some pips and question trailing step 3 replies

Trailing step and trailing stop optimization 0 replies

Buy Stop and Sell Stop orders with OCO and Trailing Stop 0 replies

To Trailing Stop or Not To Trailing Stop.... 36 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
Tags: Modified Trailing Stop Routine
Cancel

Modified Trailing Stop Routine

  • Post #1
  • Quote
  • First Post: Oct 5, 2009 8:05am Oct 5, 2009 8:05am
  •  Pipologist
  • Joined May 2009 | Status: testing/trading/testing/trading... | 1,799 Posts
Here's a routine that I am testing. It seems that the sell side stop is trailing on step one only, and the buy stop is not trailing whatsoever. I'm not sure if there is something wrong with the logic or syntax. What is the problem here?

*********

int step1 = 9, step2 = 18, step3 = 27;
for (int i = 0; i <= 0; i++) //scan all orders & positions...
{
OrderSelect (i, SELECT_BY_POS, MODE_TRADES);
{
if ((OrderSymbol() != Symbol()) && ((OrderMagicNumber() != magic + buy_id) || (OrderMagicNumber() != magic + sell_id))) continue;
{
if (OrderType() <= OP_SELL)
{
if ((short < (OrderOpenPrice() - step1 * Point)) && (short > (OrderOpenPrice() - step2 * Point)))
{
OrderModify (OrderTicket(), short, short + step1 * Point, OrderTakeProfit(), 0, Gold);
} else
if ((short < (OrderOpenPrice() - step2 * Point)) && (short > (OrderOpenPrice() - step3 * Point)))
{
OrderModify (OrderTicket(), short, short + step1 * Point, OrderTakeProfit(), 0, Tomato);
} else
if (short < (OrderOpenPrice() - step3 * Point))
{
OrderModify (OrderTicket(), short, short + step1 * Point, OrderTakeProfit(), 0, Coral);
}
} else
if (OrderType() >= OP_BUY)
{
if ((long > (OrderOpenPrice() + step1 * Point)) && (long < (OrderOpenPrice() + step2 * Point)))
{
OrderModify (OrderTicket(), long, long - step1 * Point, OrderTakeProfit(), 0, Gold);
} else
if ((long > (OrderOpenPrice() + step2 * Point)) && (long < (OrderOpenPrice() + step3 * Point)))
{
OrderModify (OrderTicket(), long, long - step1 * Point, OrderTakeProfit(), 0, Tomato);
} else
if (long > (OrderOpenPrice() + step3 * Point))
{
OrderModify (OrderTicket(), long, long - step1 * Point, OrderTakeProfit(), 0, Coral);
}
}
}
}
}

*********
  • Post #2
  • Quote
  • Oct 5, 2009 8:49am Oct 5, 2009 8:49am
  •  magnumfreak
  • Joined Nov 2007 | Status: Trying manual mode again | 2,210 Posts
for (int i = 0; i <= 0; i++)

this statement will only execute one time before i becomes larger than 0. Try it this way:

for (int i = 0; i <= Orderstotal(); i++)
 
 
  • Post #3
  • Quote
  • Oct 6, 2009 4:01am Oct 6, 2009 4:01am
  •  Pipologist
  • Joined May 2009 | Status: testing/trading/testing/trading... | 1,799 Posts
Thanks for that code adjustment. I implemented it and so far the results are exactly the same for a couple of backtests.

One thing that still happening is that long trades are not being stop adjusted, but short trades are.

Can anyone see where the code might be wrong logically that could cause buy side stop adjustments to not modify?

Thanks in advance.
 
 
  • Post #4
  • Quote
  • Oct 6, 2009 5:43am Oct 6, 2009 5:43am
  •  magnumfreak
  • Joined Nov 2007 | Status: Trying manual mode again | 2,210 Posts
it is best to post all of the code when asking for help. Without it simple things like where "long" and "short" variables are being declared and/or set cannot be determined.
 
 
  • Post #5
  • Quote
  • Edited 7:31am Oct 6, 2009 7:30am | Edited 7:31am
  •  sangmane
  • Joined Apr 2009 | Status: MT4 Programmer | 758 Posts
OP_SELL and OP_BUY is a constant which have the value:
OP_BUY = 0
OP_SELL = 1

so, this code:
if (OrderType() <= OP_SELL) which is identical to "if (OrderType() <= 1)"
will always be executed wether the OrderType() value is 0 for buying or 1 for selling.

that's why the code below
} else
if (OrderType() >= OP_BUY)
{

will never be executed.

you should using
if (OrderType() == OP_SELL)
and
if (OrderType() == OP_BUY).

regarding the program logic, i don't understand what the objective of your program, except gold, tomato and coral (it seems the objective of the if-else condition only to set the color of the trailing stop?) i think, like magnum said, it's better to post all the code.
 
 
  • Post #6
  • Quote
  • Last Post: Oct 7, 2009 4:38am Oct 7, 2009 4:38am
  •  Pipologist
  • Joined May 2009 | Status: testing/trading/testing/trading... | 1,799 Posts
Thanks Sangmane for the explanation.

I did try == before you replied and it did work and triggered the trailing stops on both sides, but the results were not as good as just sell orders trailing.

However, I appreciate very much your explanation as to why the sells were the only stops trailing. That makes complete sense.

Now a general question which maybe should be posted in the trading systems thread...

Has this happened to you...

Your EA performs better with just a trailing stop on buy orders or just a trailing stop on sell orders, but not on both sides. Interesting.
 
 
  • Platform Tech
  • /
  • Modified Trailing Stop Routine
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2023