1/2 POSITION ON TARGET

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #124950 quote
    murryis
    Participant
    New

    Hi all

    Within the last couple weeks, i have downloaded ProRealTime and have started to learn how to programme a simple idea i have had.  Ive learn alot since starting and looking through the library of code on here and really helped but I am currently stuck on 2 points and wondered if anyone would be able to help me with these please.

    These are quite basic questions but i just cant figure out how you would write these scenarios in code:

    • If my code opens a position (either long or short) with 2 contracts on an action, how can i set the code to close 1 of the contracts on a profit target and leave the remaining running until another action closes it?  Is that possible?  This would really help to maximise profits but limit risk.
    • Also, how can I get my code to close a position (long or short) if there are say 4 candles in the row that have only moved a maximum of say 2 points in either direction?

    Thanks in advance

    #124957 quote
    Vonasi
    Moderator
    Master

    Use LIMIT orders. You have to place one at the time you enter the trade (you can only use the closing price to calculate the exit at this point and hope there is no big gap!) so that it is on the market from the moment the 2 positions are opened. After the first bar closes you can calculate the exit price based on the actual price you bought at. Limit orders last only one bar so you then have to keep sending them to the market on every candle.

    You should be aware that at the current time it is not possible to run a strategy either live in demo or in real live trading that uses partial position closure. You can back test ideas though.

    In this example the first position is closed out at 1% profit.

    if not onmarket and (your long entry conditions) then
    buy 2 contracts at market
    profitprice = close * 1.01
    sell 1 contract at profitprice limit
    endif
    
    if longonmarket and countofposition = 2 then
    profitprice = tradeprice * 1.01
    sell 1 contract at profitprice limit
    endif
    
    if longonmarket and countofposition = 1 and (your long exit conditions) then
    sell at market
    endif
    
    if not onmarket and (your short entry conditions) then
    sellshort 2 contracts at market
    profitprice = close * 0.99
    exitshort 1 contract at profitprice limit
    endif
    
    if shortonmarket and countofposition = -2 then
    profitprice = tradeprice * 0.99
    exitshort 1 contract at profitprice limit
    endif
    
    if shortonmarket and countofposition = -1 and (your short exit conditions) then
    exitshort at market
    endif
    
    
    
    
    #124958 quote
    Vonasi
    Moderator
    Master

    To answer your second question:

    if summation[4](high-open<=2) = 4 and if summation[4](open-low<=2) = 4 then
    sell at market
    exitshort at market
    endif
    #125005 quote
    murryis
    Participant
    New

    Hi Vonasi

    Thanks very much for coming back to me with the above, much appreciated!

    Oh right, is there a reason that we are not currently able to do partial position closure?  Thats a shame….so the code you’ve sent me above for the limit order wouldnt actually work on the live or live demo system, only on back test?

    Thanks again

    #125009 quote
    Vonasi
    Moderator
    Master

    Correct.

    The PRT platform has the ability to do partial position closure but the broker IG doesn’t allow it via automated trading at the moment. The closest we can get is to use SET STOP and SET PROFIT instructions that allow positions to be closed separately. All positions however have exactly the same stoploss and takeprofit distances from their opening price set to whatever the last SET instruction values were.

    #125019 quote
    murryis
    Participant
    New

    Hi Vonasi

    Thanks again, i would never have worked all this out without speaking to you! 🙂

    I know this is going to be a dumb question but i just want to make sure i understand it correctly….how exactly do we use the SET STOP and SET PROFIT instructions to close positions separately?

    #125022 quote
    Vonasi
    Moderator
    Master
    set target pprofit 2
    set stop ploss 2
    
    set set target %profit 2
    set stop %loss 2
    
    set target profit 2
    set stop loss 2

     

    Whenever one of the above instructions is read at the close of a candle then the stop loss and target orders are sent to the market and effect all open positions. The orders stay on the market until the position they are attached to are closed or until a new SET order is sent. If you change the 2 to a 3 then all positions will have their exit orders changed to 3 pips or 3%.

    #125035 quote
    murryis
    Participant
    New

    Thanks again for explaining, this makes it alot easier to understand.

    One last question i promise, then i’ll leave you alone 🙂

    Within my code, my buy instructions are looking at when exponential lines cross but also to then count 2 candles after the lines cross to ensure that the market is still heading that way at that point….the code i have used is below:

    Example of buy order:

    c1 = (indicator1 CROSSES OVER indicator2)
    
    IF c1 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND open < close AND open[1] < close[1] THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF

     

    But i’m still getting buys (or shorts) in my backtesting when the candles arn’t as i’ve set in the code, i think i’ve set it so it doesn’t buy before the indicators cross and there are 2 subsequent candles that are green before buying? Or have i got the code wrong?  I have done the same but in reverse for shorts.

    Thanks again

    #125043 quote
    Vonasi
    Moderator
    Master

    There are some simple rules that everyone using the forums is expected to follow. Your post has broken one or more of these rules.

    The forum rules are as follows. I have highlighted in bold the rule/rules that you have not followed:

    Post your topic in the correct forum.

    ProRealTime Platform Support          only platform related issues.
    ProOrder                                                only strategy topics.
    ProBuilder                                              only indicator topics.
    ProScreener                                           only screener topics
    General Discussion                               any other topics.
    Welcome New Members                      for new forum members to introduce themselves.

    Only post in the language of the forum that you are posting in. For example English only in the English speaking forums and French only in the French speaking forums.
    Always use the ‘Insert PRT Code’ button when putting code in your posts to make it easier for others to read.
    Do not double post. Ask your question only once and only in one forum. All double posts will be deleted anyway so posting the same question multiple times will just be wasting your own time and will not get you an answer any quicker. Double posting just creates confusion in the forums.
    Be careful when quoting others in your posts. Only use the quote option when you need to highlight a particular bit of text that you are referring to or to highlight that you are replying to a particular member if there are several involved in a conversation. Do not include large amounts of code in your quotes. Just highlight the text you want to quote and then click on ‘Quote’.
    Give your topic a meaningful title. Describe your question or your subject in your title. Do not use meaningless titles such as ‘Coding Help Needed’.
    Do not include personal information such as email addresses or telephone numbers in your posts. If you would like to contact another forum member directly outside of the forums then contact the forums administrator via ‘Contact Us’ and they will pass your details on to the member that you wish to contact.
    Always be polite and courteous to others.
    Have fun.

    I have edited your post where required. Please ensure that your future posts meet these few simple forum rules. 🙂

    #125044 quote
    Vonasi
    Moderator
    Master

    You need to check C1 two candles back.

    c1 = (indicator1 CROSSES OVER indicator2)
    
    IF c1[2] AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND open < close AND open[1] < close[1] THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF

    So this will now need a cross followed by two green candles.

    murryis thanked this post
    #125047 quote
    murryis
    Participant
    New

    Ah, sorry about that, i’ll make sure i do that from now on 🙂

    #125052 quote
    murryis
    Participant
    New

    As, so close!

    Thanks for your help Vonasi, that seems to have worked but now thats given me a new thing to look at tomorrow 🙂

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

1/2 POSITION ON TARGET


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
murryis @murryis Participant
Summary

This topic contains 11 replies,
has 2 voices, and was last updated by murryis
5 years, 10 months ago.

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