Making a system choose to go either in buy modus or short modus - not both same time

Hello,

I am currently struggling a bit with a system which I would like to create which needs to buy or short a couple of ETF’s. System will have 3 lots to be placed in either buy or short modus whichever comes first. So when it is buying an ETF it can scale in twice but also it can pick up another buy signal from another ETF. When it has done a buy it needs to ignore all short signals which comes in. When it’s out of buy positions its free to serve buy or short whichever comes first.

For short the opposite, so when it picked up a short it can scale in and it can do another short on another ETF but it needs to ignore buy signals, so it will not pick up buy positions (or do buy adds).

The code I wrote so far is ok but its mixing buy and short positions at the same time and thats not what I want.

Below the code with the loop for scaling in. Hope that someone is able to help me.

Rgds Swen

SystemName=“B S”;

BuyPrice=Close;
SellPrice=Close;
ShortPrice=Close;
CoverPrice=Close;

SetTradeDelays(0,0,0,0);

PositionSize = -33;

RestorePriceArrays();

Add1=1;
Add2=1;

Addshort1=1;
Addshort2=1;

Buy = RSI(2) < 10;

Sell = RSI(2) > 90;

Short = RSI(2) > 95;

Cover = RSI(2) < 10;

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell, Buy);

Short = ExRem(Short,Cover );
Cover = ExRem(Cover , Short);

fInTrade=False;
nEntry=0;
Clast=0;
ClastReeks1=0;
ClastReeks2=0;
inposition=0;

for (i=0;i<BarCount;i++)
{
if (Buy[i] AND !fInTrade)
{
fInTrade = True;
CLast = C[i];
iBuy = i;
nEntry = 1;
inposition=1;
}
else if (Sell[i] AND fInTrade and inposition==1 )
{
fInTrade = False;
nSize = 0;
}
else if (fInTrade and inposition==1 )
{
fScale1 = C[i] < CLastAdd1 AND nEntry==1;
fScale2 = C[i] < CLast
Add2 AND nEntry==2;

	if (fScale1)
	{
		CLast = C[i];
		Buy[i] = sigScaleIn;
		nEntry++;
		ScaleIn1[i] = True;
	}
	if (fScale2 )
	{
		CLast = C[i];
		Buy[i] = sigScaleIn;
		nEntry++;
		ScaleIn2[i] = True;
	}	
	

}
    else if (Short[i] AND !fInTrade)
{

	fInTrade = True;
	CLast = C[i];
	nEntry = 1;
            inposition=2;
}
else if (Cover[i] AND fInTrade and inposition==2)
{
	fInTrade = False;
	nSize = 0;
}
else if (fInTrade and inposition==2)
{
            fScale1 = C[i] > CLast*Addshort1 AND nEntry==1;
	fScale2 = C[i] > CLast*Addshort2 AND nEntry==2;
           
	if (fScale1)
	{
		CLast = C[i];
		Short[i] = sigScaleIn;
		nEntry++;
		ScaleIn1[i] = True;
	}
	if (fScale2 )
	{
		CLast = C[i];
		Short[i] = sigScaleIn;
		nEntry++;
		ScaleIn2[i] = True;
	}	


}
ClastReeks[i] = Clast;

}

I suppose you meant ‘mode’

A simple(r ) way to do this is by using IIf function
Ref: https://www.amibroker.com/guide/afl/iif.html

Define your Long/ Short/ Do nothing mode conditions --> Execute Buy/ Short coded logic as per evaluated condition in vogue

Suppose you want the system to go ‘long only’ when close is above 200 MA of close, and ‘short only’ when close is below the same

mode_long = C > MA( C, 200 );
mode_short = C < MA( C, 200 );

cond_long = RSI(2) < 10; // as per your coded logic
cond_short = RSI(2) > 95; // as per your coded logic

sys_mode = IIf( mode_long, cond_long, IIf( mode_short, cond_short, Null));

This is just a simple template for illustration. A far more comprehensive example of the same is available here -

http://www.amibroker.com/kb/2014/09/20/broad-market-timing-in-system-formulas/

Hi,
Thanks for replying! Really helpfull. However this works as far as I see it only on a single ETF for example. I have a list of about 6 ETF’s. So when the system is doing a short trade for a ETF, it can do a short trade for another ETF and can scale-in into both for short. What I want to prevent is that if the system is doing a short in one or two ETF’s, and if still a slot is available, I want to prevent that it is picking up a long signal for any of the 6 ETF’s the system is looking at.

Vice versa for long.

So it has to reqcognize on a portfolio level (watchlist) short is only short. When all slots are available again (3) it may to choose long or short whatever comes first.

Is that possible?

regards

Swen

1 Like

Difficult to find something which is not possible in amibroker! :slightly_smiling_face:

Level of expertise in its usage and imagination are the only limitations!

Its quite common to find both long and short opportunities in uncorrelated markets. So a system can be both Long and Short at the same time in different markets. If you want to implement unidirectional positions (hopefully with sound reason) then you have to define the global Long/ Short mode accordingly

There are two things - One is Mode and the other is Entry Signal

Your code only defines Entry Signals. And with such a small value of RSI, the signals for both long and short will be plentiful

As illustrated in the previous post, define the global mode as either exclusively long or short. For e.g if the close of the broad based index (upon which those ETFs are modelled) is above MA 200 (or any such average) then the System is in Global Long Mode and if the close be below, then in Global Short Mode

So when the system is in long mode code it for considering only long entries and vice versa for short (BTW the entries in your code have been coded as a condition. It may be better to code them as an event)

You can also implement the ‘mode’ concept by using ‘Switch’ and ‘Case’ structure

HTH