Not logged in
Viewing 15 posts - 1 through 15 (of 23 total)

Forums>StrategyQuant>Application Support>Profit Trailing (on close) ..how to convert tick by tick?!

  • #115026 |
    Participant
    22 Posts
    I understand that to have an empirical study to be reported in the live trading is great to have the function that SQ, but we should have the ability to better work on the strategy, once you find a good
    These functions allow you to use profit-trailing, breakeven, trailing stop-etc etc .. but for many strategies, especially those more aggressive, wait for the closing of the candle is a function that we do not need.
    all these functions (for some strategies) are useless if I have to wait until the closing of the candle!
    I state that I work only with tick-real!
    but each of breakeven and trailing parameter is set waiting for the closure of the entry candle..why?
    hundreds and hundreds of strategies implemented by me are set with the “trailing on close” .. and would be much more efficient with a trailing tick by tick from the entry to the market
    surely, wait for the closing of the candle, will help in not fall into overfitting or see good strategies that do not work then live
    but it should have the possibility of eliminating this feature once you find a good strategy and then work better output in trailing not waiting for the closing of the candle
    does anyone know how to do? I have to change the fil mql? I have to delete or replace some code inside? I have to set the SQ?
    Thank you all
    Giorgio
    #136500
    Customer
    557 Posts
    //+------------------------------------------------------------------+
    /**
     * 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.

    #136504
    Customer
    432 Posts

    …. 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;)

    #136505
    Participant
    22 Posts
    //+------------------------------------------------------------------+
    /**
     * 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.

    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 :

     

    but after this change nothing has changed
    I have to change something else? I tried to set up a simple trailing by 1 pip, but does not work !!  :angry:
    I tried and tried several times while changing the type of trailing … no way!
    the trailing only activates with the closing of the candle ..
    I’m sure you can solve this problem  :D  :D ..pleaseeee
    thanks again 
    Giorgio
    #136510
    Customer
    557 Posts

    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);
    }
    
    #136513
    Participant
    22 Posts

    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 ?

     

    #136514
    Customer
    557 Posts

    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.

    #136515
    Participant
    22 Posts
    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 :(

    it could be that I have to change “double close = Close [1]” ??
    thanks for all the help you are giving me :)
    #136516
    Customer
    557 Posts

    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
     

    #136519
    Participant
    22 Posts
    I tried many times but nothing!
    I thought maybe the problem is that my orders are buystop and sellstop while I noticed that your orders are market orders
    could this be the cause?
    to set the exact command to buystop and sellstop orders, maybe I should change other things?
     
    Thanks again
     
    Giorgio
    #136530
    Customer
    432 Posts

    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:)

    #136532
    Customer
    432 Posts
    //+------------------------------------------------------------------+
    /**
     * 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.

    #136542
    Participant
    22 Posts

    ..many thanks geektrader :D .. I’ll try this once and I’ll let you know

    Thanks again
    #136545
    Participant
    22 Posts
    then:
    I tried in every way, either by changing the code within the strategy (as indicated by you) and even creating a strategy manually, by StrategyQuant, in the “Strategy Editor”, I can not get nesun trailing tick-by- tick
    I noticed in fact, that even if I create any simple strategy with the StrategyQuant, if I enter a simple “profit-trailing” is created, automatically, the setting of the trailing “on close” candle and never tick-by-tick.
    Even if you use the “Build Strategies”, by setting real tick, never finds any trailing tick by tick
    always always always “(on close)” .. in any way, I can not seem to have the fastest trailing on the price tick-by-tick.
    it’s incredible :(
    some of you can tell me what’s wrong? you have ever created a strategy with trailing tick-by-tick?
    my “StrategyQuant” has never created any strategy with trailing tick-by-tick .. but only “on close”
    why?
    #136616
    Participant
    22 Posts
    no one can help me?
    someone can confirm that the STRATEGYQUANT create strategies with dynamic trailing tick-by-tick?
    why, he never even created a trailing with tick-by-tick?
    I do not mean to force profitable, even unprofitable … has never created any strategy with trailing other than “trailing (on close)”
    why?
    if i tray to create manually ,it’s always the same (ON CLOSE)!! :(
Viewing 15 posts - 1 through 15 (of 23 total)

You must be logged in to reply to this topic.