Trailing StopBack to topics list |
|
guitara
2005.09.14 03:00
My expert doesn't seem to modify my stop loss correctly. However, I'm mirroring the Trailing Stop Implementation seen here:
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
Bar | 1 2 3 4 Price | 12 14 16 25 Stop | 4 6 15
|
|
guitara
2005.09.14 03:15
EDIT
|
|
Shimodax
2005.09.14 03:32
You should see each modification on the backtester Journal tab, also on the Results tab and it should make a dent in the Graph. And yes, it should update on every tick (every live tick or synthetic tick fed into the backtester), unless of course you only check on every full bar ( something like if (Volume[0]>) ... ). Also, the exits should give you an exit mark (triangle pointing left) on the Show Chart in the backtester. If you want to debug, add Print commands inside your if's ... they will appear in the Tester's Journal tab (or Terminal's Experts tab on real time). Markus |
|
guitara
2005.09.14 05:36
Here's an example. Perhaps you can help. This is during live testing.
![]() See how the SL is modified only once? the yellow line is for when I modify it based on certain parameters. The red, should be the beginning set TSL. I halve the value of the trailing stop with some logic during the trade, and then reset it for any new order. The red arrow is the first sell, the red line above is the first TSL. That doesn't seem to trail the price with each bar. Then at the yellow bar, i the SL, as I said, and it updates. I see no close. Then another order. But the first sell did close. The Yellow SL was breached, and it closed. Where's the close arrow? So, again, you can see with the second trade, we have the same thing. EDIT: I guess I should say, in the backtester, it appears to do the right thing, and it only shows the last modifying. Confirmed using Print commands, and reading log. EDIT2: The only "fix" is to not use any logic, and modify order every complete bar to reflect trailing stop. This is not pretty, but I've confirmed using backtesting, that modifying everytime, versus using the template code changes things. EDIT3: Of course, now running a 3 month backtest yields weird results. Ugh, maybe it's just me. EDIT4: Here's one of a different day, showing the problem perfectly. ![]() |
|
guitara
2005.09.14 06:29
Here's the exact code.
//################## BUY ORDER TRAILING STOP Adjustment #######################
total=OrdersTotal();
if(total>0)
{
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
{
if(TrailingStop>0)
{
if (Logic)
{
TrailingStop=SL;
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Yellow);
//Print("Stop Loss Set: ", TrailingStop," Price: ",Close[0]," Price minues SL: ",Close[0]-0.0012," SL: ",Ask+Point*TrailingStop);
}
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,DodgerBlue);
return(0);
}}}}}}
//################## SELL ORDER TRAILING STOP Adjustment #######################
total=OrdersTotal();
if(total>0)
{
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_SELL && OrderSymbol()==Symbol())
{
if(TrailingStop>0)
{
if (Logic)
{
TrailingStop=SL;
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Yellow);
}
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,DodgerBlue);
return(0);
}}}}}} //exit if not new bar
// if(BarTime == Time[0]) {return(0);}
//new bar, update bartime
//BarTime = Time[0]; |
|
guitara
2005.09.14 07:09
|
|
guitara
2005.09.14 07:14
|
|
guitara
2005.09.14 07:30
Here's a simple modification of the Trailing Stop 5.mq4 by MT. It *appears* to work. Just uses RSI>70 RSI<30 crosses as signals. Modding it with my logic indicators creates the same problem. I've removed all trouble code, just changed the Trailing Stop in here to 12, and I get the same result. Am I wrong about where it should SL? I'm really puzzled.
extern double TakeProfit = 0;
extern double StopLoss = 15;
extern double Lots = 1;
extern double TrailingStop = 5;
int start()
{
int total, ticket;
double rsi=iRSI(NULL,0,5,PRICE_CLOSE,0);
double orsi=iRSI(NULL,0,5,PRICE_CLOSE,1);
for (int i = 0; i < OrdersTotal(); i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderType() == OP_BUY) {
if (Bid - OrderOpenPrice() > TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
if (OrderStopLoss() < Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);
}
}
} else if (OrderType() == OP_SELL) {
if (OrderOpenPrice() - Ask > TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
if ((OrderStopLoss() > Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) ||
(OrderStopLoss() == 0)) {
OrderModify(OrderTicket(), OrderOpenPrice(),Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);
}
}
}
}
total=OrdersTotal();
if(total==0&&rsi>30&&orsi<=30)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-Point*TrailingStop,0,"MA Long",16384,0,Lime);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
total=OrdersTotal();
if(total==0&&rsi<70&&orsi>=70)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+Point*TrailingStop,0,"MA Short",16384,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
} |
|
guitara
2005.09.15 04:07
Figured it out. Whew. Turns out the shell code for the TSL is indeed incorrect. Using the MQ4 one achieves the desired results. Thanks.
|


