[R-SIG-Finance] Long Enter Position do not "block" Short Enter Orders
Ilya Kipnis
ilya.kipnis at gmail.com
Mon Apr 11 17:47:53 CEST 2016
Glad to be of help!
On Mon, Apr 11, 2016 at 11:42 AM, Diego Peroni <diegoperoni at vodafone.it>
wrote:
> Now it works,
> this is my custom function:
>
> osMaxPosLS = function(data, timestamp, orderqty, ordertype, orderside,
> portfolio, symbol, ruletype, ...) {
> maxpos = osMaxPos(data, timestamp, orderqty, ordertype, orderside,
> portfolio, symbol, ruletype, ...)
> curpos = getPosQty(portfolio, symbol, timestamp)
> if (orderside == 'long' & curpos<0)
> maxpos = 0
> if (orderside == 'short' & curpos>0)
> maxpos = 0
> return (maxpos)
> }
>
> Thank you for your help!
>
> Diego
>
>
>
>
>
> On 11/04/2016 17:13, Ilya Kipnis wrote:
>
> I see your question. If you want short orders not to fire when you have a
> long position, you should create a custom order sizing function that
> returns 0 if your current position isn't zero for both long and short
> positions.
>
> On Mon, Apr 11, 2016 at 11:07 AM, Diego Peroni <diegoperoni at vodafone.it>
> wrote:
>
>> Thank you Ilya,
>>
>> I've placed orderset='ocoall' in both ENTER orders (named "LE" and "SE")
>> as in the following code but testing them I have the same problem: long
>> positions are closed by short (enter) positions...
>> Can you please tell me what I'm doing wrong?
>>
>> Diego
>>
>>
>> #################################
>> # MAX POSITION (1 future)
>> #################################
>> addPosLimit(portfolio = qs.strategy, symbol = symbol, timestamp =
>> initDate, maxpos = 1)
>>
>> #################################
>> # LONG STRATEGY
>> #################################
>> add.rule(qs.strategy, name='ruleSignal',
>> arguments = list(sigcol="upTrend", sigval=TRUE,
>> replace=TRUE,
>> prefer='Open',
>> orderside='long',
>> ordertype='stoplimit',
>>
>> order.price=quote(mktdata$price.enter.long[timestamp]),
>> orderqty=1,
>> osFUN='osMaxPos',
>> orderset='ocoall',
>>
>> time.in.force=quote(timeInForceLong)),
>> type='enter',
>> label='LE')
>>
>> add.rule(qs.strategy, name='ruleSignal',
>> arguments=list(sigcol='exitLong', sigval=TRUE,
>> replace=FALSE,
>> orderside='long',
>> ordertype='market',
>> orderqty='all',
>> prefer='Open'
>> ),
>> type='exit',
>> label='ExitLong',
>> enabled=TRUE)
>>
>> #################################
>> # SHORT STRATEGY
>> #################################
>> add.rule(qs.strategy, name='ruleSignal',
>> arguments=list(sigcol="downTrend", sigval=TRUE,
>> replace=TRUE,
>> prefer='Open',
>> orderside='short',
>> ordertype='stoplimit',
>>
>> order.price=quote(mktdata$price.enter.short[timestamp]),
>> orderqty=-1,
>> osFUN='osMaxPos',
>> orderset='ocoall',
>> time.in.force=quote(timeInForceShort)),
>> type='enter',
>> label='SE')
>>
>> add.rule(qs.strategy, name='ruleSignal',
>> arguments=list(sigcol='exitShort', sigval=TRUE,
>> replace=FALSE,
>> orderside='short',
>> ordertype='market',
>> orderqty='all',
>> prefer='Open'
>> ),
>> type='exit',
>> label='ExitShort',
>> enabled=TRUE)
>>
>>
>>
>>
>>
>> On 11/04/2016 15:05, Ilya Kipnis wrote:
>>
>> Short orders do not by default cancel long orders. You need to have them
>> in the same order set.
>>
>> On Mon, Apr 11, 2016 at 6:39 AM, Diego Peroni < <diegoperoni at vodafone.it>
>> diegoperoni at vodafone.it> wrote:
>>
>>> Hi all,
>>>
>>> my simple strategy sets long/short ENTER orders and relative long/short
>>> EXIT orders.
>>>
>>> I don't understand why, if I have an open LONG position, SHORT enter
>>> order triggers (wrongly closing open position...).
>>>
>>> My desire is that if I already have an OPEN position (long) "opposite"
>>> orders (short enter) do not triggers.
>>>
>>> Thanks in advance for your help
>>>
>>> Diego
>>>
>>>
>>> #################################
>>> # MAX POSITION (1 future)
>>> #################################
>>> addPosLimit(portfolio = qs.strategy, symbol = symbol, timestamp =
>>> initDate, maxpos = 1)
>>>
>>> #################################
>>> # LONG STRATEGY
>>> #################################
>>> add.rule(qs.strategy, name='ruleSignal',
>>> arguments = list(sigcol="upTrend", sigval=TRUE,
>>> replace=TRUE,
>>> prefer='Open',
>>> orderside='long',
>>> ordertype='stoplimit',
>>> order.price=quote(mktdata$price.enter.long[timestamp]),
>>> orderqty=1,
>>> osFUN='osMaxPos',
>>> time.in.force=quote(timeInForceLong)),
>>> type='enter',
>>> label='LE')
>>> add.rule(qs.strategy, name='ruleSignal',
>>> arguments=list(sigcol='exitLong', sigval=TRUE,
>>> replace=FALSE,
>>> orderside='long',
>>> ordertype='market',
>>> orderqty='all',
>>> prefer='Open'
>>> ),
>>> type='exit',
>>> label='ExitLong',
>>> enabled=TRUE)
>>>
>>> #################################
>>> # SHORT STRATEGY
>>> #################################
>>> add.rule(qs.strategy, name='ruleSignal',
>>> arguments=list(sigcol="downTrend", sigval=TRUE,
>>> replace=TRUE,
>>> prefer='Open',
>>> orderside='short',
>>> ordertype='stoplimit',
>>> order.price=quote(mktdata$price.enter.short[timestamp]),
>>> orderqty=-1,
>>> osFUN='osMaxPos',
>>> time.in.force=quote(timeInForceShort)),
>>> type='enter',
>>> label='SE')
>>> add.rule(qs.strategy, name='ruleSignal',
>>> arguments=list(sigcol='exitShort', sigval=TRUE,
>>> replace=FALSE,
>>> orderside='short',
>>> ordertype='market',
>>> orderqty='all',
>>> prefer='Open'
>>> ),
>>> type='exit',
>>> label='ExitShort',
>>> enabled=TRUE)
>>>
>>> _______________________________________________
>>> R-SIG-Finance at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>> -- Subscriber-posting only. If you want to post, subscribe first.
>>> -- Also note that this is not the r-help list where general R questions
>>> should go.
>>>
>>
>>
>>
>
>
[[alternative HTML version deleted]]
More information about the R-SIG-Finance
mailing list