Using limit orders for targets in strategy

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #72176 quote
    Voldemort
    Participant
    New

    Hi!

    I’m new to the platform and I’m writing a strategy where I have multiple targets. So I figured I’d use SELL X SHARES AT Y LIMIT to setup my targets for a long. However, these never seem to execute when I place them after the market buy. Any ideas what I could be doing wrong?

    // Conditions to enter long positions
    IF NOT LongOnMarket AND c1 and c2 and c3 and c4 and c5 THEN
    BUY 1 CONTRACTS AT MARKET
    
    // 20 pip above entry target
    t1 = positionprice + (pipsize * 20)
    
    // Fibonacci target
    hh = highest[50](close)
    ll = lowest[50](close)
    t2 =  ll + ((hh - ll) * 1.272)
    
    SELL 1 CONTRACTS AT t1 LIMIT
    SELL 1 CONTRACTS AT t2 LIMIT
    ENDIF

    Any pointers are hugely helpful!

    #72192 quote
    robertogozzi
    Moderator
    Master

    In line 2 one of your conditions states that lines 3 through 14 have to be executed when NOT LongOnMarket, so, one you are on market they will NOT be executed.

    Moreover, closing partial positions is not allowed as of this version of ProOrder.

    #72222 quote
    Voldemort
    Participant
    New

    Aha! I’m not used to programming languages that evaluate the conditions for each line within the statement. I changed it around, now it looks like this. It seems to work for the exit but the stop loss never triggers. :/

    // Conditions to enter long positions
    IF NOT LongOnMarket AND c1 and c2 and c3 and c4 and c5 THEN
    BUY 1 CONTRACTS AT MARKET
    placeExitOrders = 1
    ENDIF
    
    // Conditions to exit long positions
    If LongOnMarket AND placeExitOrders THEN
    SET TARGET pPROFIT 60
    SELL 1 CONTRACTS AT positionprice + (pipsize * 20) LIMIT
    graph low[1]
    placeExitOrders = 0
    ENDIF
    #72232 quote
    Vonasi
    Moderator
    Master

    What stop loss? There is no stop loss in that code just two orders to sell at a higher price.

    #72233 quote
    robertogozzi
    Moderator
    Master

    This works only for the first bar after entry, because line 12 resets placeExitOrders  to zero, preventing the limit order from being placed on subsequent bars!

    Simply comment out that line.

    You could also remove or comment out line 4 and modify line 8 since placeExitOrders is not needed.

    All you have to check to place a SELL order is that your strategy is LONGONMARKET.

    #72247 quote
    Voldemort
    Participant
    New

    Sorry, I was a bit quick with the code. The below is what I meant.

    // Conditions to enter long positions
    IF NOT LongOnMarket AND c1 and c2 and c3 and c4 and c5 THEN
    BUY 1 CONTRACTS AT MARKET
    placeExitOrders = 1
    ENDIF
    
    // Conditions to exit long positions
    If LongOnMarket AND placeExitOrders THEN
    SET STOP $LOSS low[1]
    SELL 1 CONTRACTS AT positionprice + (pipsize * 20) LIMIT
    placeExitOrders = 0
    ENDIF

    This will immediately after the market buy; set a stop and a limit exit. But it doesn’t seem to work really. Are limit orders only valid one bar?

    #72249 quote
    Vonasi
    Moderator
    Master

    Your code buys at next bar open but at that point you are not long on market so it does not set the stop loss of limit order. It does this on the next bar closure and each bar after that.

    Yes limit and stop orders are only valid for one bar. Orders placed with SET are valid until triggered or the position is closed.

    Put your SET STOP and LIMIT order in with your IF NOT LONGONMARKET and remove the SET STOP from the IF LONGONMARKET.

    As Robert said the PlaceExitOrders condition is not needed so delete it.

    #72251 quote
    Voldemort
    Participant
    New

    Hmm. Sorry if I’m a bit slow. If they are only valid one bar, how can I refresh them and keep them at the value I first decided when I entered the trade? In this case the stop should always be the at previous bar low from the entry bar.

    Really appreciate the responses! Thanks people.

    #72258 quote
    Vonasi
    Moderator
    Master

    Do you have accumulating positions or just one position? In other words do you want the stop loss to be at the low before the first position bought or the adjusted to the low before the last position bought with each trade?

    #72260 quote
    Vonasi
    Moderator
    Master

    Forget that last question as the IF NOT OnMarket tells me my answer!

    Also just realised that in your last code you use SET to put the stoploss order on the market. As I said in the previous post this ‘Sets’ the order on the market so you do not need to repeat the instruction. Only Sell at Limit and Sell at Stop orders have to be repeated at each bar. What is wrong in your code is that you are setting the StopLoss at Market Price when it should be a value such as 50 or 60 pips or dollars. Use close – low[1] to calculate this value.

    #72275 quote
    Voldemort
    Participant
    New

    Thanks a lot, it’s actually working now. I must’ve misunderstood the documentation regarding the $LOSS, I read it as it should take the market price as an argument as you say. If I would like to set the initial limit order based on data from where the buy signal was generated, how would I in theory keep that order alive and kicking several bars further down the trade?

    #72317 quote
    Vonasi
    Moderator
    Master
    // Conditions to exit long positions
    If LongOnMarket THEN
    SELL 1 CONTRACTS AT positionprice + (pipsize * 20) LIMIT
    ENDIF

    This part of your code is already doing that. At every new bar if you are still long on the market it replaces the LIMIT order on the market. PositionPrice has not changed as you have not bought any new positions.

    In fact as your code is not accumulating positions then you do not really need the Limit order. Any of the following would do the same job:

    SET STOP LOSS x
    SET STOP pLoss x
    SET STOP $LOSS x
    #72334 quote
    Voldemort
    Participant
    New

    Indeed! But let’s say I calculate a Fibonacci level based on the previous 50 bars to my entry. Now I want to use one of these levels as my target and continously move this as we go. I could use SET TARGET, but is there any way to use a LIMIT instead? Or I guess if closing partial positions is not supported in ProOrder, then this kind of thing is not supposed to be supported?

    Anyways, thanks a lot. I’ve got a working strategy right now due to your help!

    #72343 quote
    Vonasi
    Moderator
    Master

    Save whatever levels you calculate at the time of opening the trade within the IF NOT ONMARKET and then use these levels later in the code to change your target or stop loss on each bar close with a limit or stop order.

    Partial closure is not possible at the moment but is due for release hopefully later this year.

    I’m actually out sailing at the moment and most likely to go out of 4G range soon so I may have to come back to this later if there are any other questions!

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

Using limit orders for targets in strategy


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Voldemort @voldemort Participant
Summary

This topic contains 13 replies,
has 3 voices, and was last updated by Vonasi
7 years, 9 months ago.

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