once function combined with a stop

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #163141 quote
    Albaran
    Participant
    Average

    Hi there,

    Having trouble to understand why below code doesn’t behave like I would have it:

    c1 = barindex - tradeindex <= 45
    Once c11 = (close or high) >= (tradeindex + 14)
    
    If not onmarket then
    buy 5 contract at market
    elsif countoflongshares = 5 and c1 and c11 then
    sell at Fulllongstop stop
    endif

    Problem with above is that the stop doesn’t seem to kick in as soon as c1 and c11 are satisfied.

    C11 needs to be read only once as the stop should remain active even if c11 isn’t valid anymore. The stop needs to remain active once it has kicked in..

    Thanks for getting back to me,

    cheers.

    #163147 quote
    robertogozzi
    Moderator
    Master

    ONCE means that whatever follows on the same line is evaluated only when the strategy is first executed, not next bars.

    At that time TRADEINDEX is 0.

    Next, you assign c11 a logical value of a price compared to another logical value.

    c11 will ALWAYS be false if used on most currency pairs and will ALWAYS be false with indices. That’s why the SELL pending order can’t be placed when you want to.

    What do you want to achieve with the first two lines?

    #163195 quote
    Albaran
    Participant
    Average

    Well, the problem I’m having right now is that the STOP that I want to put in place seems to come and go without the “assumed wrong idea of of the ONCE function above”. That’s why I thought I’d give the “assumed wrong idea above” a try.

    The idea behind the first two lines is the following:

    c1 = barindex - tradeindex <= 45

    The first condition is there because I have different STOP levels in mind. See it as an inactivity, if TARGET PRICE is not reached within the next 45 bars then another type of STOP sets in.

    Once c11 = (close or high) >= (tradeindex + 14)

    With the second (wrong) condition I wanted to prevent that the stop comes and go (which is the case without the c11 idea). I didn’t put the whole code on display in my first post but you can assume that, because of various reasons, the stop comes and goes.

    See the condition as the TRAILINGSTART part. As soon as the POSITIONPRICE + TARGET PROFIT is reached, I want the stop to kick in.

    So the second condition only needs to be satisfied once and shouldn’t be recalculated bar after bar.

    My mistake here is that I thought that the ONCE function could be used for trading strategies as well and not only for indicators (or maybe it can be but from the explanation that you gave me, I understand it can’t be used in that way?)

    #163197 quote
    robertogozzi
    Moderator
    Master

    c1 should be written as:

    c1 = OnMarket AND (barindex - tradeindex <= 45)

    to make sure it’s not always evaluated.

    I still can’t understand what your goal is with:

    Once c11 = (close or high) >= (tradeindex + 14)

    let’s replace it the real values (for both DAX and EUR/GBP):

    //Once c11 = (close or high) >= (tradeindex + 14) becomes
    
      Once c11 = 13950 >= 14    //DAX       (always TRUE)
      Once c11 = 0.8650 >= 14   //EUR/GBP   (always FALSE)
    #163198 quote
    Albaran
    Participant
    Average

    Ow sorry, I meant TRADEPRICE instead of TRADEINDEX, my bad…

    I don’t know if you can understand what I mean now?

    #163209 quote
    robertogozzi
    Moderator
    Master

    Your code,line 7, will only be executed when c11 is TRUE, which can be at some bar,but cannot be the following bar.

    When it’s true for the first time, you need to keep placing the stoip order, even later when the porice drops down (that’s what it’s made for).

    So you should use a flag (a variable, no matter the name, that is ised to signal something). This flag should be rest to 0 when not on market, then set to 1 whenever c11 is true.

    Then you can place the stop order while this flag os true:

    if not OnMarket then
       Flag = 0
    endif
    c1 = OnMarket and (barindex - tradeindex <= 45)
    IF Flag = 0 then
       c11 = close >= (tradeprice + 14*pipsize) //you may use HIGH instead of CLOSE, PIPSIZES makes your code portable
       Flag = 1
    endif
    If not onmarket then
       buy 5 contract at market
    endif
    if Flag then
       sell at Fulllongstop stop
    endif
    Albaran thanked this post
    #163229 quote
    Albaran
    Participant
    Average

    I don’t really understand the lines 5 to 7 (what is the trigger in your example that changes flag into = 1?); shouldn’t it be like below?

    IF Flag = 0 then
        If c11 = close >= (tradeprice + 14*pipsize) then //you may use HIGH instead of CLOSE, PIPSIZES makes your code portable
            Flag = 1
        Endif
    Endif

    But I get the overall idea of the flag!

    I’ll give it a try and let you know if I still got any questions 🙂

    #163230 quote
    robertogozzi
    Moderator
    Master

    Yes, well spotted.

    I forgot to embed that within IF…ENDIF.

    #163469 quote
    Albaran
    Participant
    Average

    Works great!

    I got another (very silly) question though and I don’t know if there’s already a post covering this but here it goes…

    When is the cost of a guaranteed stop loss triggered? I would assume every time you use the stop function?

    Or is it only triggered when using the below kind of command:

    Set stop loss x

    and not when using this kind of command

    Sell at FullLongStop Stop

    ?

    #163475 quote
    robertogozzi
    Moderator
    Master

    This is something I cannot answer. I would guess when a STOP order yields a loss, not a profit, but I am not aware of it.

    I suggest that you post this question at https://www.prorealcode.com/forum/prorealtime-english-forum/prorealtime-support/, since it’s a PRT monitored forum and someone will surely reply asap.

    #163476 quote
    Albaran
    Participant
    Average

    Not so silly after all then (I thought it would be)!

    I’ll do that and keep you updated with the publication of a link in this topic then!

    robertogozzi thanked this post
    #163479 quote
    Vonasi
    Moderator
    Master

    IG only deduct their guaranteed stops fee when a stop is hit so if you have an order on a market attached to a position to close it at a worse price than the entry price and that order is hit you will get charged the fee.

    This is only my assumption as I don’t use guaranteed stops.

    #163498 quote
    Albaran
    Participant
    Average

    There its is:

    Cost trigger of a guaranteed stoploss

    It’s triggered in both cases!

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.

once function combined with a stop


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Albaran @albaran Participant
Summary

This topic contains 12 replies,
has 3 voices, and was last updated by Albaran
4 years, 11 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 03/04/2021
Status: Active
Attachments: No files
Logo Logo
Loading...