MarketPosition and alternate exit condition

TradeStation has a keyword "MarketPosition" that lets me know when I am in a position, and on which side of the market I am on. IE: Flat = 0, Long = 1, Short = -1.

What is the equivalent of that in Ami?

So far, I have been doing it like this:
InLongPosition = Flip(LongCondition, ExitCondition);

But what if I have an alternate exit condition that only applies if I have been in the trade for 7 bars? In other words I want one exit price for the first seven bars, and another exit price for every bar thereafter. So, I seem to be in a catch22. Because my exit has this condition, I cannot use it for my Flip, therefore I cannot determine what my MarketPosition is.....

1 Like

Something similar to another TS keyword: BarsSinceEntry

myBarsSinceEntry = BarsSince(InLongPosition);

Isn't working because I cannot determine the flip because the ExitPrice is conditional on how many bars since I've been in the trade.

Figured it out.

Need to use BarsSince(LongCondition, ExitCondition1 OR ExitCondition2);

2 Likes

Tradestation's "MarketPosition" variable is just the limitation of TS system - it is here because TS supports only single-symbol backtests.

In AmiBroker we have portfolio-level backtester and as such we deal with multiple positions at the same time (on many symbols) and AmiBroker maintains a list of all open positions. The list of open positions is available in custom backtester (GetFirstOpenPos/GetNextOpenPos methods).
A template code that queries open positions in the custom backtester looks as follows:

SetCustomBacktestProc( "" );

if ( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject();    //  Get backtester object
    bo.PreProcess();    //  Do pre-processing (always required)

    for ( i = 0; i < BarCount; i++ )  //  Loop through all bars
    {
        bo.ProcessTradeSignals( i );  //  Process trades at bar (always required)

        for ( openpos = bo.GetFirstOpenPos(); openpos; openpos = bo.GetNextOpenPos() )
        {
            // openpos variable now holds Trade object - you can query it

        }
    }    //  End of for loop over bars

    bo.PostProcess();    //  Do post-processing (always required)
}

For more details see:
http://www.amibroker.com/guide/a_custombacktest.html
and
http://www.amibroker.org/userkb/2008/03/16/amibroker-custom-backtester-interface-2/

Thanks to handling positions on portfolio-level you can change the sizes of postions based on sizes of other postions on other symbols (see rebalancing example), with Tradestation-style "MarketPosition" being limited to current (one and only one) symbol, it is simply not possible to do.
See:
http://www.amibroker.com/kb/2006/03/06/re-balancing-open-positions/

Also Tradestation codes sometimes have logic of

if( MarketPosition = 1 AND SellSignal )... do the sell

this is NOT needed at all in AmiBroker as AmiBroker does all that logic automatically (it matches signals,
so long exit is done only if you are already in position, etc)

See http://www.amibroker.com/portfolio.html

If for example you want just to draw a line when you are long, you can write a "position indicator" that way:

Buy = ..
Sell = ..

InLong = Flip( Buy, Sell );

Plot( InLong, "In long position", colorRed );

I hope that this explains how AmiBroker handles positions and why it is superior to Tradestation.

15 Likes

I am trying to do a simple backtest on a single chart and acquire MarketPosition from within this chart (not on a portfolio level.) I have to turn a variable to false upon entering long. Nothing else can turn that variable to false. In other posts others have suggested to use BarsSince of previous setups, but no matter what I do I have determined that it must be MarketPosition is long to turn this variable (array) to false. Could someone please give me an example of how to do this?

nevermind I figured out how to do it w/ barssince

This was a problem for me as well. Solved it by adding the below code at the end of the formula (after Buy,Sell,Short,Cover and Applystop). Please note this works only if you do not allow same bar entry exit and max open positions is set to 1. DO NOT USE ExRem to remove excess signals if you are using Applystop. Equity(1) will remove excess signals considering Applystop.

SetOption( "MaxOpenPositions", 1);
SetOption("AllowSameBarExit",0);
Equity(1);
MarketPosition = Flip(Buy,Sell) - Flip(Short,Cover); //Result : 1 is long, -1 is short, 0 is no position
Plot(MarketPosition, "MP", colorRed); //Optional if you want to view it on chart
3 Likes