Quantstrat Position Accounting System: Netting vs Hedging Systems

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Quantstrat Position Accounting System: Netting vs Hedging Systems

Ayhan yuksel
Dear all,

In quantstrat, as described in package documentation, orders turn into
transactions,  transactions turn into positions, positions turn into
trades. And the underlying logic of how you define a position (using
netting or hedging system) or a trade (flat-to-flat or FIFO etc)
significantly affects the mechanics of money management rules (stop loss
etc) and trade statistics (profit factor etc).

For defining positions, the netting vs hedging systems are defined as
follows:
https://www.metatrader5.com/en/mobile-trading/iphone/help/trade/general_concept

The original implementation of quantstrat is based on a netting system
where after having a long position, each additional long trade increases
the current position amount (unless you have a position limit) and the
stop-loss/take profit is applied to the total position as a whole and not
to the individual long trades separately.

My question is that is there a way to apply stop loss and take profit to
each opened trade independently, regardless of order side (long or short)
and previous positions? If yes, would you provide an example of it?

Best regards
Ayhan

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Quantstrat Position Accounting System: Netting vs Hedging Systems

Ilya Kipnis
Ayhan,

If I'm understanding correctly:

Are you familiar with order sets?

By defining a stop loss that works on a particular parent rule, that
should do the trick.

See this post:
https://quantstrattrader.wordpress.com/2014/08/18/a-hammer-trading-system-demonstrating-custom-indicator-based-limit-orders-in-quantstrat/

-Ilya

On Sat, Nov 7, 2020 at 4:12 PM Ayhan yuksel <[hidden email]> wrote:

> Dear all,
>
> In quantstrat, as described in package documentation, orders turn into
> transactions,  transactions turn into positions, positions turn into
> trades. And the underlying logic of how you define a position (using
> netting or hedging system) or a trade (flat-to-flat or FIFO etc)
> significantly affects the mechanics of money management rules (stop loss
> etc) and trade statistics (profit factor etc).
>
> For defining positions, the netting vs hedging systems are defined as
> follows:
>
> https://www.metatrader5.com/en/mobile-trading/iphone/help/trade/general_concept
>
> The original implementation of quantstrat is based on a netting system
> where after having a long position, each additional long trade increases
> the current position amount (unless you have a position limit) and the
> stop-loss/take profit is applied to the total position as a whole and not
> to the individual long trades separately.
>
> My question is that is there a way to apply stop loss and take profit to
> each opened trade independently, regardless of order side (long or short)
> and previous positions? If yes, would you provide an example of it?
>
> Best regards
> Ayhan
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> [hidden email] 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]]

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Quantstrat Position Accounting System: Netting vs Hedging Systems

Ayhan yuksel
Ilya thanks for your reply, your help is much appreciated

Yes I am familiar with ordersets and orderchains, but my question is
related to how stop loss is applied to each trade

Below I put a minimal example of a strategy where we only open long trades
with a size of 100 lots using moving average crossover, and we have a stop
loss with a threshold of 0.001.

After running the strategy on GBPUSD data, please have a look at the 3rd,
4th and 5 th transactions. In these transactions, the strategy first have a
long trade with  100 lot, then another long trade with 100 lot (so now our
position is 200 lots), and then the stop loss rule is executed and the
whole 200 lots will be closed, which means that the stop loss rule is based
on the whole position that we have.

Every time the strategy adds new long positions to the existing ones (like
pyramiding), the stop loss rule is executed on the whole position and
position is always flattened.

My question is that is there a way to apply the stop loss rule
**seperately** for each of the 100 lot trades, which may have different
cost basis, so may have different stop loss prices? In this case, for
instance, each stop loss will be executed independently and each stop loss
transaction will have a size of -100 lots.

Regards
Ayhan

###########################################
# Example strategy

library(quantstrat)

source(paste0(path.package("quantstrat"),"/demo/luxor.include.R"))
.fast = 5
.slow = 10

source(paste0(path.package("quantstrat"),"/demo/luxor.getSymbols.R"))

### blotter

initPortf(portfolio.st, symbols='GBPUSD', currency='USD')
initAcct(account.st, portfolios=portfolio.st, currency='USD')

### quantstrat

initOrders(portfolio.st)

### define strategy

strategy(strategy.st, store=TRUE)


add.indicator(strategy.st, name = "SMA",
              arguments = list(
                x = quote(Cl(mktdata)[,1]),
                n = .fast
              ),
              label="nFast"
)

add.indicator(strategy.st, name="SMA",
              arguments = list(
                x = quote(Cl(mktdata)[,1]),
                n = .slow
              ),
              label="nSlow"
)

### signals

add.signal(strategy.st, name='sigCrossover',
           arguments = list(
             columns=c("nFast","nSlow"),
             relationship="gte"
           ),
           label='long'
)

### rules ############
# normal entry rules

add.rule(strategy.st, name = 'ruleSignal',
         arguments=list(sigcol='long' , sigval=TRUE,
                        replace=FALSE,
                        orderside='long' ,
                        ordertype='stoplimit',
                        prefer='High',
                        threshold=.threshold,
                        TxnFees=0,
                        orderqty=100,
                        orderset='ocolong'
         ),
         type='enter',
         timespan = .timespan,
         label='EnterLONG'
)


# stop-loss

add.rule(strategy.st, name = 'ruleSignal',
         arguments=list(sigcol='long' , sigval=TRUE,
                        replace=FALSE,
                        orderside='long',
                        ordertype='stoplimit', tmult=TRUE, threshold=0.001,
                        TxnFees=.txnfees,
                        orderqty='all',
                        orderset='ocolong'
         ),
         type='chain', parent='EnterLONG',
         label='StopLossLONG',
         enabled=TRUE
)



applyStrategy(strategy.st, portfolio.st)

View(getOrderBook(portfolio.st)[[portfolio.st]]$GBPUSD)

View(getTxns(portfolio.st,"GBPUSD"))














On Sun, 8 Nov 2020 at 01:40, Ilya Kipnis <[hidden email]> wrote:

> Ayhan,
>
> If I'm understanding correctly:
>
> Are you familiar with order sets?
>
> By defining a stop loss that works on a particular parent rule, that
> should do the trick.
>
> See this post:
> https://quantstrattrader.wordpress.com/2014/08/18/a-hammer-trading-system-demonstrating-custom-indicator-based-limit-orders-in-quantstrat/
>
> -Ilya
>
> On Sat, Nov 7, 2020 at 4:12 PM Ayhan yuksel <[hidden email]>
> wrote:
>
>> Dear all,
>>
>> In quantstrat, as described in package documentation, orders turn into
>> transactions,  transactions turn into positions, positions turn into
>> trades. And the underlying logic of how you define a position (using
>> netting or hedging system) or a trade (flat-to-flat or FIFO etc)
>> significantly affects the mechanics of money management rules (stop loss
>> etc) and trade statistics (profit factor etc).
>>
>> For defining positions, the netting vs hedging systems are defined as
>> follows:
>>
>> https://www.metatrader5.com/en/mobile-trading/iphone/help/trade/general_concept
>>
>> The original implementation of quantstrat is based on a netting system
>> where after having a long position, each additional long trade increases
>> the current position amount (unless you have a position limit) and the
>> stop-loss/take profit is applied to the total position as a whole and not
>> to the individual long trades separately.
>>
>> My question is that is there a way to apply stop loss and take profit to
>> each opened trade independently, regardless of order side (long or short)
>> and previous positions? If yes, would you provide an example of it?
>>
>> Best regards
>> Ayhan
>>
>>         [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> [hidden email] 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]]

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Quantstrat Position Accounting System: Netting vs Hedging Systems

Ilya Kipnis
Look at your stoploss rule and the orderqty it calls for. You actively
specify orderqty = 'all'. That means to flatten the entire position.

As far as a stop loss on a partial position, in your particular case, you
can simply set that with a better specified orderqty argument by specifying
100 lots instead of 'all'.

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Quantstrat Position Accounting System: Netting vs Hedging Systems

Ayhan yuksel
Thank you for the clarification.

On Sun, 8 Nov 2020 at 16:21, Ilya Kipnis <[hidden email]> wrote:

> Look at your stoploss rule and the orderqty it calls for. You actively
> specify orderqty = 'all'. That means to flatten the entire position.
>
> As far as a stop loss on a partial position, in your particular case, you
> can simply set that with a better specified orderqty argument by specifying
> 100 lots instead of 'all'.
>

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Quantstrat Position Accounting System: Netting vs Hedging Systems

Ayhan yuksel
One more question to solve my case

We can access the parent rule’s order price using chain.price argument  in
rule functions.

Is it also possible to access parent rule’s order size as well? I searched
but couldn't find it

Assume that the parent order may take variable position size for each
trade. It takes one long position and then adds a second long position
before hitting the stop loss for the first one. After that the stop loss
for the first position is hit. We need to know the position size of the
first trade in writing stop loss rule. Is it possible to access the
position size of the parent rule?

Thanks for your help
Regards





On 9 Nov 2020 Mon at 11:09 Ayhan yuksel <[hidden email]> wrote:

> Thank you for the clarification.
>
> On Sun, 8 Nov 2020 at 16:21, Ilya Kipnis <[hidden email]> wrote:
>
>> Look at your stoploss rule and the orderqty it calls for. You actively
>> specify orderqty = 'all'. That means to flatten the entire position.
>>
>> As far as a stop loss on a partial position, in your particular case, you
>> can simply set that with a better specified orderqty argument by specifying
>> 100 lots instead of 'all'.
>>
>

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Quantstrat Position Accounting System: Netting vs Hedging Systems

braverock
The chained order quantity isn't directly passed to the triggered chain
rule right now.

Rules have access to the full current state, since they are path
dependent.  

So, you have two options here.  You can either 

- write a custom order sizing function that queries the order status to
check the chained rule and order to get the quantity

- propose a patch/pull request to rules.R and ruleSignal.R that could
process a chain.qty optional argument

see the code in rules.R to see how we pass chain.price, and it should
inform how you would take either of the above approaches.

Regards,

Brian

--
Brian G. Peterson
ph: +1.773.459.4973
im: bgpbraverock

On Thu, 2020-11-12 at 13:43 +0300, Ayhan yuksel wrote:

> One more question to solve my case
>
> We can access the parent rule’s order price using chain.price
> argument  in
> rule functions.
>
> Is it also possible to access parent rule’s order size as well? I
> searched
> but couldn't find it
>
> Assume that the parent order may take variable position size for each
> trade. It takes one long position and then adds a second long
> position
> before hitting the stop loss for the first one. After that the stop
> loss
> for the first position is hit. We need to know the position size of
> the
> first trade in writing stop loss rule. Is it possible to access the
> position size of the parent rule?
>
> Thanks for your help
> Regards
>
>
>
>
>
> On 9 Nov 2020 Mon at 11:09 Ayhan yuksel <[hidden email]>
> wrote:
>
> > Thank you for the clarification.
> >
> > On Sun, 8 Nov 2020 at 16:21, Ilya Kipnis <[hidden email]>
> > wrote:
> >
> > > Look at your stoploss rule and the orderqty it calls for. You
> > > actively
> > > specify orderqty = 'all'. That means to flatten the entire
> > > position.
> > >
> > > As far as a stop loss on a partial position, in your particular
> > > case, you
> > > can simply set that with a better specified orderqty argument by
> > > specifying
> > > 100 lots instead of 'all'.
> > >
> >
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> [hidden email] 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.
>

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Quantstrat Position Accounting System: Netting vs Hedging Systems

Ayhan yuksel
Thank you very much for the clarification
I will work on it

Regards
Ayhan

On 12 Nov 2020 Thu at 14:28 Brian G. Peterson <[hidden email]> wrote:

> The chained order quantity isn't directly passed to the triggered chain
> rule right now.
>
> Rules have access to the full current state, since they are path
> dependent.
>
> So, you have two options here.  You can either
>
> - write a custom order sizing function that queries the order status to
> check the chained rule and order to get the quantity
>
> - propose a patch/pull request to rules.R and ruleSignal.R that could
> process a chain.qty optional argument
>
> see the code in rules.R to see how we pass chain.price, and it should
> inform how you would take either of the above approaches.
>
> Regards,
>
> Brian
>
> --
> Brian G. Peterson
> ph: +1.773.459.4973
> im: bgpbraverock
>
> On Thu, 2020-11-12 at 13:43 +0300, Ayhan yuksel wrote:
> > One more question to solve my case
> >
> > We can access the parent rule’s order price using chain.price
> > argument  in
> > rule functions.
> >
> > Is it also possible to access parent rule’s order size as well? I
> > searched
> > but couldn't find it
> >
> > Assume that the parent order may take variable position size for each
> > trade. It takes one long position and then adds a second long
> > position
> > before hitting the stop loss for the first one. After that the stop
> > loss
> > for the first position is hit. We need to know the position size of
> > the
> > first trade in writing stop loss rule. Is it possible to access the
> > position size of the parent rule?
> >
> > Thanks for your help
> > Regards
> >
> >
> >
> >
> >
> > On 9 Nov 2020 Mon at 11:09 Ayhan yuksel <[hidden email]>
> > wrote:
> >
> > > Thank you for the clarification.
> > >
> > > On Sun, 8 Nov 2020 at 16:21, Ilya Kipnis <[hidden email]>
> > > wrote:
> > >
> > > > Look at your stoploss rule and the orderqty it calls for. You
> > > > actively
> > > > specify orderqty = 'all'. That means to flatten the entire
> > > > position.
> > > >
> > > > As far as a stop loss on a partial position, in your particular
> > > > case, you
> > > > can simply set that with a better specified orderqty argument by
> > > > specifying
> > > > 100 lots instead of 'all'.
> > > >
> > >
> >
> >         [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > [hidden email] 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]]

_______________________________________________
[hidden email] 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.