Cumulate orders and close with barindex-tradeindex

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #157903 quote
    FinningFinning
    Participant
    Veteran

    Hi there,

    just something I’ve noticed, and I’m not sure why it’s happening.

    I have a strategy, and it should exit after 2 full bars on the market. I have this set up as:

    if barindex-tradeindex=2 then
    sell at market
    endif

    Now, there are two different ways that I could run this strategy. Both with

    cumulateorders=true

    I can either run a 1 position max,

    countoflongshares<1

    or a 2 position max,

    countoflongshares<2

    The thing I’ve noticed, is that if I run the countoflongshares<1, then it exits the position after 2 full bars. This is what is intended.

    If I run it as countoflongshares<2 and if I buy a second position while the first position is still on, then it doesn’t do 2 separate exits but a combined single exit, and it pushes the first exit out, so that the first trade is on the market for 3 full bars, and not 2.

    Please see the attached picture that shows this.

    What I want to know, is this normal/is this to be expected?

    What will the broker (IG) see and do?

    Will it execute as backtested, or will I have two separate exits, and not the single exit that I’m seeing?

    Thanks,

    Finning

    icharttop thanked this post
    Countoflongshares-and-barindex.jpg Countoflongshares-and-barindex.jpg
    #157910 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    TRADEINDEX(1), or simply TRADEINDEX, always refers to the most recent entry, so 2 bars will be computed from THAT barindex.

    Refer to TRADEINDEX(2) for the previous entry and use the partial closure support to exit only the first position entered.

    (not tested)

    i = CountOfLongShares
    if barindex-tradeindex(i)=2 then
       sell 1 contracts at market
    endif
    icharttop thanked this post
    #157944 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    The correct code is:

    If Not OnMarket then
       i = 0
    Endif
    i = max(i,CountOfLongShares)
    if barindex-tradeindex(i)=2 then
       sell 1 contracts at market
    endif
    Finning and icharttop thanked this post
    #158001 quote
    FinningFinning
    Participant
    Veteran

    Hi Roberto,

    Thanks for that!

    will test, but it looks good and makes sense.

    Cheers,

    Finning

    #158010 quote
    FinningFinning
    Participant
    Veteran

    Tested, and it works as intended – thanks!

    robertogozzi thanked this post
    #158030 quote
    GraHalGraHal
    Participant
    Legend

    Roberto correct code above added as Log 269 to here …

    Snippet Link Library

    robertogozzi thanked this post
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Cumulate orders and close with barindex-tradeindex


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Finning @finning Participant
Summary

This topic contains 5 replies,
has 3 voices, and was last updated by GraHalGraHal
5 years, 9 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 01/15/2021
Status: Active
Attachments: 1 files
ProRealCode ProRealCode
Loading...