Profit Trailing (on close) ..how to convert tick by tick?!
23 replies
Gaudio Fx
8 years ago #115026
mikeyc
8 years ago #136500
//+------------------------------------------------------------------+ /** * manage trade - move SL to break even or trailing stop */ void manageStop() { if(!sqIsBarOpen) return; ... rest of code
If you comment out the:
if(!sqIsBarOpen) return;
line as:
// if(!sqIsBarOpen) return;
Then the manageStop function will run for every tick , and all types of stop loss move will occur every tick where required. This will make a much bigger CPU load on computer though.
geektrader
8 years ago #136504
…. and it will ruin your strategy. You´ll see that in 99% of the cases running a strategy with trailing by tick instead of bar open (how it was created by SQ) will result into a negative equity curve. It´s simply because the strategy was found by SQ with a on bar trailing and was created like that. Speeding up the trailing like that will change the whole strategy completely and will do you no good, but you´ll see what I mean when doing your tests in MT4 with trailing by tick instead of bar open;)
Gaudio Fx
8 years ago #136505
//+------------------------------------------------------------------+ /** * manage trade - move SL to break even or trailing stop */ void manageStop() { if(!sqIsBarOpen) return; ... rest of codeIf you comment out the:
if(!sqIsBarOpen) return;
line as:
// if(!sqIsBarOpen) return;
Then the manageStop function will run for every tick , and all types of stop loss move will occur every tick where required. This will make a much bigger CPU load on computer though.
thanks for the reply my friend.. so i have comment out “if(!sqlsBarOpen) return;” with ” // ” and the result is this :
from this standard code …
at this :
mikeyc
8 years ago #136510
Only the getProfitTrailingByTick function will having any affect if it is defined with a number of pips as the return value (this one is blank, so doesn’t do anything).
double getProfitTrailingByTick() { if (OrderType() == OP_BUY) { // long value here } else if (OrderType() == OP_SELL) { // short value here } return(0); }
The other type of stop trailing (which uses one or more indicators) will still trail on close, because the indicator value only changes every bar:
double getStopTrailingByClose() { double value = 0; if (OrderType() == OP_BUY) { value = iCustom(NULL,0, "Pivots", 0, 0, 15, 0, 0, 0, 0, 0, 0, 1) + (0.8) * (MathAbs(iIchimoku(NULL, 0, 9, 26, 52, MODE_KIJUNSEN, 1) - iMA(NULL, 0, 1872, 0, MODE_LWMA, PRICE_CLOSE, 1))); } else if (OrderType() == OP_SELL) { value = iCustom(NULL,0, "Pivots", 0, 0, 15, 0, 0, 0, 0, 0, 0, 1) + (-0.8) * (MathAbs(iIchimoku(NULL, 0, 9, 26, 52, MODE_KIJUNSEN, 1) - iMA(NULL, 0, 1872, 0, MODE_LWMA, PRICE_CLOSE, 1))); } return(value); }
Gaudio Fx
8 years ago #136513
Only the getProfitTrailingByTick function will having any affect if it is defined with a number of pips as the return value (this one is blank, so doesn’t do anything).
double getProfitTrailingByTick() { if (OrderType() == OP_BUY) { // long value here } else if (OrderType() == OP_SELL) { // short value here } return(0); }The other type of stop trailing (which uses one or more indicators) will still trail on close, because the indicator value only changes every bar:
double getStopTrailingByClose() { double value = 0; if (OrderType() == OP_BUY) { value = iCustom(NULL,0, "Pivots", 0, 0, 15, 0, 0, 0, 0, 0, 0, 1) + (0.8) * (MathAbs(iIchimoku(NULL, 0, 9, 26, 52, MODE_KIJUNSEN, 1) - iMA(NULL, 0, 1872, 0, MODE_LWMA, PRICE_CLOSE, 1))); } else if (OrderType() == OP_SELL) { value = iCustom(NULL,0, "Pivots", 0, 0, 15, 0, 0, 0, 0, 0, 0, 1) + (-0.8) * (MathAbs(iIchimoku(NULL, 0, 9, 26, 52, MODE_KIJUNSEN, 1) - iMA(NULL, 0, 1872, 0, MODE_LWMA, PRICE_CLOSE, 1))); } return(value); }
Sorry.. but not run by tick 🙁 i compile in this way but not have a trailing by tick .. it’s not correct ?
mikeyc
8 years ago #136514
Okay, it’s a bit more complicated by the fact that the manageStop function uses Close[1] as the current price. Of course this price only changes every bar, so the stop is only moved every bar.
//------------------------------ // profit trailing on TICK!!!!! trailingStopValue = getProfitTrailingByTick(); if(trailingStopValue > 0) { if(OrderType() == OP_BUY) { tsLevel = Bid - trailingStopValue; } else { tsLevel = Ask + trailingStopValue; }
Change the code as shown above, by replacing close with Bid and Ask.
Gaudio Fx
8 years ago #136515
Okay, it’s a bit more complicated by the fact that the manageStop function uses Close[1] as the current price. Of course this price only changes every bar, so the stop is only moved every bar.//------------------------------ // profit trailing on TICK!!!!! trailingStopValue = getProfitTrailingByTick(); if(trailingStopValue > 0) { if(OrderType() == OP_BUY) { tsLevel = Bid - trailingStopValue; } else { tsLevel = Ask + trailingStopValue; }Change the code as shown above, by replacing close with Bid and Ask.
i have change in Bid & Ask .. but never changed.. not trail 🙁
mikeyc
8 years ago #136516
I made those changes and the trailing by tick worked for me:
211 2015.08.24 02:45 buy 11 1.00 1.56723 0.00000 0.00000 0.00 9788.26
212 2015.08.24 02:45 modify 11 1.00 1.56723 1.54657 0.00000 0.00 9788.26
213 2015.08.24 02:50 modify 11 1.00 1.56723 1.56723 0.00000 0.00 9788.26
214 2015.08.24 02:52 modify 11 1.00 1.56723 1.56724 0.00000 0.00 9788.26
215 2015.08.24 02:52 modify 11 1.00 1.56723 1.56725 0.00000 0.00 9788.26
216 2015.08.24 02:52 modify 11 1.00 1.56723 1.56726 0.00000 0.00 9788.26
217 2015.08.24 02:52 modify 11 1.00 1.56723 1.56727 0.00000 0.00 9788.26
218 2015.08.24 02:52 modify 11 1.00 1.56723 1.56728 0.00000 0.00 9788.26
219 2015.08.24 02:53 modify 11 1.00 1.56723 1.56729 0.00000 0.00 9788.26
220 2015.08.24 02:53 modify 11 1.00 1.56723 1.56732 0.00000 0.00 9788.26
221 2015.08.24 02:53 modify 11 1.00 1.56723 1.56734 0.00000 0.00 9788.26
222 2015.08.24 02:53 modify 11 1.00 1.56723 1.56737 0.00000 0.00 9788.26
223 2015.08.24 02:53 modify 11 1.00 1.56723 1.56739 0.00000 0.00 9788.26
224 2015.08.24 02:53 modify 11 1.00 1.56723 1.56740 0.00000 0.00 9788.26
225 2015.08.24 02:56 s/l 11 1.00 1.56740 1.56740 0.00000 10.73 9798.99
Gaudio Fx
8 years ago #136519
geektrader
8 years ago #136530
You have to remove the “if (OrderOpenTime() >= Time[0])…” line as well, as it otherwise does exit the function at this point as long as the order isn´t opened for at least 1 bar. So it will only begin trailing (by tick in your case) 1 bar after the order was opened. If you remove that line, it will begin right away 1 tick after the order was opened.
I am still telling you: this will not lead to anything more profitable than your original strategy, it will be worse, much worse to trail by tick, if the strategy was created with on bar trailing. You kill the whole strategy this way as also the next orders will be affected by earlier closing of previous orders. It´s the perfect way to rid of your money to do that, but seems you have to learn the hard way:)
geektrader
8 years ago #136532
//+------------------------------------------------------------------+ /** * manage trade - move SL to break even or trailing stop */ void manageStop() { //if(!sqIsBarOpen) return; double trailingStopValue, moveSLValue; double orderSL, normalOrderSL, orderOpen; double close = Close[0]; double tsLevel, newSL; for(int i=0; i= Time[0]) continue; // exit if the order was just opened //------------------------------ // profit trailing on close trailingStopValue = getProfitTrailingByTick(); if(trailingStopValue > 0) { if(OrderType() == OP_BUY) { tsLevel = close - trailingStopValue; } else { tsLevel = close + trailingStopValue; } orderSL = OrderStopLoss(); normalOrderSL = getNormalSL(orderSL); newSL = getSpecialSL(tsLevel); if(OrderType() == OP_BUY) { if(isSLCorrect(tsLevel) && (orderSL == 0 || normalOrderSL tsLevel) && !doublesAreEqual(orderSL, newSL)) { rettmp = OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0); } } } //-------------------------------------------------------- // manage stop trailing on close trailingStopValue = getStopTrailingByClose(); if(trailingStopValue > 0) { orderOpen = OrderOpenPrice(); orderSL = OrderStopLoss(); normalOrderSL = getNormalSL(orderSL); newSL = getSpecialSL(trailingStopValue); if(OrderType() == OP_BUY) { if(isSLCorrect(trailingStopValue) && (orderSL == 0 || normalOrderSL trailingStopValue) && !doublesAreEqual(orderSL, newSL)) { rettmp = OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0); } } } //-------------------------------------------------------- // manage SL 2 BE (by tick) moveSLValue = getMoveSLValueByTick(); if(moveSLValue > 0) { orderSL = OrderStopLoss(); normalOrderSL = getNormalSL(orderSL); orderOpen = OrderOpenPrice(); newSL = getSpecialSL(orderOpen); if(OrderType() == OP_BUY) { if(isSLCorrect(orderOpen) && (close - orderOpen >= moveSLValue) && (orderSL == 0 || normalOrderSL = moveSLValue) && (orderSL == 0 || normalOrderSL > orderOpen) && !doublesAreEqual(orderSL, newSL)) { rettmp = OrderModify(OrderTicket(), orderOpen, newSL, OrderTakeProfit(), 0); } } } } } } //+------------------------------------------------------------------+
This is the the complete code to replace, this will trail your order tick by tick then.
Gaudio Fx
8 years ago #136542
Gaudio Fx
8 years ago #136545
Gaudio Fx
8 years ago #136616
geektrader
8 years ago #136634
NO, SQ will only create strategies with trailing on BAR CLOSE as explained several times here and in other threads. The only thing you can do is to export a EA for MT4 that trails ON TICK by modifying the MQL4 code in the way we´ve posted it for you already quite a few times in this thread. This will NOT change anything about the fact that SQ STILL will only create strategies with trailing on bar close only. And why that is the case, was also explained in the Forum (by me and others) a few times.