How to model buy/short for a system generating multiple limit orders

HI,

I have created a trading system that generates trade orders (limit orders with its entry, stop and target) but I need them not to be executed until the entry price is reached. That means that at any moment it is possible to have several orders pending execution.

All was going well with my coding until I got to the point where I need to store the orders pending to be executed, which would include stuff like entry price, stop, limit, quality and a few more. I am honestly in odds when it comes to how to model the buy/short instructions in this case.

The One idea i was thinking about, was to create a matrix and add the information of the signals generated by the system. Then, for each new candle, check if any outstanding order has been hit (checking entry price between high and low of the candle) and then removing it from the matrix.

Before doing that, I was wondering if anyone would have found himself with a similar issue and would like to chime in.

I would greatly appreciate any clue/contribution to what may be a more efficient way of managing those orders in amibroker .

Thanks,

Toni

This might help you out: Handling limit orders in the backtester.

1 Like

Thanks @TrendSurfer . The hold function is interesting. My main question though is how would be best to store multiple limit orders . Is a Matrix a good way to model it, or is there a more efficient way?
Thanks again
Toni

A matrix is as efficient as a normal array variable, 1D becomes 2D, generally speaking.
Unless you are comparing to Flat file or external DB etc

It really depends on the need, but another question is of persistence.

Are you going to need the data after you restart AB or not ?
Is the context in Live trading or Backtest bcos a few things tend to change.

1 Like

Hi @spanisharlem,

a couple years agor I posted a similar question in the old yahoo groups (https://groups.yahoo.com/neo/groups/amibroker/conversations/topics/193882) .
Below is a snippet suggested by Aron which worked well for what I wanted to do (store the pending orders in array). Maybe it can be usefull for what you need.

// pending orders example

Sell = short = Cover = pos = 0;
Buy = 1;

increment = 100 * TickSize ;
orders = 5;
PositionSize  = 10000;

for( i = 0 ; i < BarCount ; i ++ )
{
    if( pos )
    {
      
        count = 0;
        for( n = 1 ; n <= orders ; n ++ )
        {
            if( order[n] )
            {
                if( High [i] > order [n] )
                {
                    Buy [i] = sigScaleIn;
                    count ++;
                    order[n] = 0; // executed
                }
            }
        }

        PositionSize [i] = count * 10000;
    }
    else
    {
        if( Buy [i] )
        {
            order = Null ;

            for( n = 1 ; n <= orders  ; n ++ )
            {
                order [n] = Close [i] + n * increment;
            }

            pos  = True; 
        }
    }
}
1 Like

Hi @travick,

so initially its just for backtest the behaviour of the system, narrow down times of day with best/worst performance, and similar stuff. Then I will look into going live but honestly it looks way off my current AB skills.

Cheers!

Hi @pmxgs

brilliant, thanks for the code. That looks similar of what I was thinking in doing.

btw, is there a way to access to that old yahoo group?

Cheers,

Toni

I'm not sure, but I think it is still possible to read old posts from the old yahoo group, if you create an account with yahoo. (I'm not sure if the group admin @Tomasz has to grant some kind of authorization)

yeah, I would need to be authorised unfortunately.

Many thanks!