100% time onmarket long and short strategy – unwanted partial close problem

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #95270 quote
    Finning
    Participant
    Veteran

    Hi all,

    I have a strategy that i want to stay on market 100% of the time. It’s either long or short – and switches from long to short in a single trade, by cancelling/netting off the entire of the previous position.

    The original code that I’ve been using is below to do this:

    if positionsize+countoflongshares>3150 then
    positionsize = 3150-countoflongshares
    endif
    
    if shortonmarket and countofshortshares+positionsize>6300 then
    positionsize = 6300
    endif
    
    BUY PositionSize CONTRACT AT MARKET

    The intent of the above code was that if I was not onmarket, buy positionsize shares to start with – and – if I was shortonmarket – buy out my short position with up to 6300 contracts to have 3150 contracts long net. Using cumulating orders.

    This code works well, and gives me the 100% onmarket switch that I wanted up to my max net positionsize of 3150 long or short – the code does what I want it to do.

    The problem I’m having, is that I’m right at the end of my testing phase, and went to launch into an onmarket test then live trading, and I got this message – see attachment.

    I am well aware of the partial close limitation problem with PRT. The thing is I’m not trying to partially close a position here – I’m trying to close all of my short/long position, and then trade in the opposite direction – all in the same move.

    I have tried the following code  to try and exit shorts (this example) totally first , to try and eliminate the partial closures problem. This didn’t seem to work but?

    if shortonmarket then
    exitshort at market
    endif
    
    if  positionsize+countoflongshares>3150 then
    positionsize = 3150-countoflongshares
    endif
    
    
    BUY PositionSize CONTRACT AT MARKET

    Bear in mind that the above code, like the first one listed above “if shortonmarket” comes after an “if” condition above has been met – not shown/listed.

    Any ideas on how I can beat this unwanted partial closures type problem? It is a bit annoying because I knew about no partial closures – I was trying to sell off entirely, and then re-enter the market in the new direction.

    I have found that the instantaneous direction change from long to short is very useful in backtest – it is a feature that I want to keep – IE – if possible I don’t want to have to exit the market, wait a bar, to then re-enter.

    Am really hoping to be able to keep this functionality – and would appreciate any ideas so I can make this happen.

    Thanks,

    Finning.

    Capture.jpg Capture.jpg
    #95279 quote
    Nicolas
    Keymaster
    Master

    I think that the quantity of shares could be slightly different from the ones that want to be set at market (because of rounding..). So inverting position by buying a new calculated amount of shares could not lead to close all active positions at market. I suggest you close ALL ORDERS with EXISHORT AT MARKET (or SELL AT MARKET ) before inverting the direction of your orders.

    #95313 quote
    Finning
    Participant
    Veteran

    Hi Nicolas,

    thanks for your reply.

    I will double check to make sure that positions are balanced because of rounding, and that this hasn’t caused a problem somehow.

    I do know what you mean about closing all orders, and I will try different variations of this.

    As I mentioned in the second block of code snippet in my initial post above , with my first attempt to fix this problem I did try to exit out of all positions first, before trading in the opposite direction – market exit code used as per below (for shorts):

    if shortonmarket then
    exitshort at market
    endif

    and then placed a buy order for a positionsize directly after it.

    The 2nd block of code in my initial post (with exitshort) works fine/gives the exact same backtest result as my initial code, but again when I try to set it up for live trading, the error message is displayed.

    In theory, this should counteract any out of balance positions, because you are starting with a clean book with no positions before you take on a new order, right?

    And by just saying “exitshort at market”, you are not specifying a quantity in the instructions for disposal – which seems to be the initial problem that I was dealing with. So this problem should be solved, but it still doesn’t seem to be?

    Is it possible – to exitshort at market – and then – buy new positions all in the same bar/candle/same run of code? This is what I’m aiming for.

    Many thanks,

    Finning.

    #95315 quote
    GraHal
    Participant
    Master

    Is it possible – to exitshort at market – and then – buy new positions all in the same bar/candle/same run of code? This is what I’m aiming for.

    If you use below as the first line of your code then this will achieve what you want, but maybe not in the way you want?

    DEFPARAM CumulateOrders = False
    #95316 quote
    Finning
    Participant
    Veteran

    Hi GraHal,

    Yes I have cumulate orders as true. I really do need to keep it that way as it greatly helps system performance.

    I will try with this setting as false and see what difference it makes.

    There aren’t any other ways that you can think of to get around this problem?

    Many thanks,

    Finning

    #95320 quote
    Nicolas
    Keymaster
    Master

    Try to don’t make test about already on market before closing the orders (do not use if shortonmarket, just liquidate the short orders without it and just before buying).

    #95442 quote
    Finning
    Participant
    Veteran

    Hi all,

    thanks for your help with this.

    I did manage to solve this problem – though the problem wasn’t  where I was initially looking/what I first asked for help with.

    Later on in my code, I had a device to exit over leveraged, under-performing positions. I didn’t look at this initially, because I didn’t have any numbers associated with it. Turns out though that this is where the problem was.

    The below code is what gave the error:

    If EQUITYCURVE < 1000 and (abs(countofposition))>1 and positionperf<-0.004 then
    sell Countoflongshares shares at market
    exitshort countofshortshares shares at market
    endif
    

    And by just getting rid of the “countoflongshares shares/countofshortshares shares” commands the error problem has now gone away.

    If EQUITYCURVE < 1000 and (abs(countofposition))>1 and positionperf<-0.004 then
    sell at market
    exitshort at market
    endif
    

    I didn’t think that countoflongshares/countofshortshares was a numerical command (though it deals with numbers) – more of a “total” command – so I didn’t look there to start with.

    By simplifying the code and getting rid of something I probably didn’t need in the first place the problem was eliminated.

    Cheers,

    Finning.

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

100% time onmarket long and short strategy – unwanted partial close problem


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Finning @finning Participant
Summary

This topic contains 6 replies,
has 3 voices, and was last updated by Finning
6 years, 11 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 04/02/2019
Status: Active
Attachments: 1 files
Logo Logo
Loading...