Backtesting FOREX, problem with ApplyStop

Hello folks,

I'm new in Amibroker, and therefore it's a bit difficult to get into basic tasks yet.
I would like to backtest some signals on FOREX data, but unfortunately I'm not able to correctly setup an exit conditions - exit on stop loss with defined amount in USD and also exit on Profit Target defined in USD.

Here is my code:

// PATTERNS FUNCTIONS

function isDojiBar(pBarsAgo)
{
  //velmi nerozhodny trh. Telo usecky tvorilo jen 10% celkoveho range.
  //Na trhu neexistovala dominujici sila.
  return abs(Ref(O, pBarsAgo) - Ref(C,pBarsAgo) ) < 0.1 * (Ref(H, pBarsAgo) - Ref(L, pBarsAgo) );
}

function isThreeNewHighs_withHigherLows_Pattern(pBarsAgo)
{
  // Three new highs with three new higher lows
  local ret;
  ret = Ref(O, pBarsAgo) < Ref(C, pBarsAgo) AND Ref(O, pBarsAgo -1 ) < Ref(C, pBarsAgo -1) AND Ref(O, pBarsAgo-2) < Ref(C, pBarsAgo -2); // 3 rastuce usecky
  ret = ret AND Ref(H, pBarsAgo) > Ref(H, pBarsAgo -1) AND Ref(H, pBarsAgo) > Ref(H, pBarsAgo -2) AND Ref(H, pBarsAgo -1) > Ref(H, pBarsAgo -2); // vyssie HIGH
  ret = ret AND Ref(L, pBarsAgo) > Ref(L, pBarsAgo -1) AND Ref(L, pBarsAgo) > Ref(L, pBarsAgo -2) AND Ref(L, pBarsAgo -1) > Ref(L, pBarsAgo -2); // vyssie LOW
  return ret;
}

function isThreeWhiteSoldiersPattern(pBarsAgo)
{
  // Three White soldiers pattern
  local ret;
  ret = isThreeNewHighs_withHigherLows_Pattern(pBarsAgo) AND Ref(L, pBarsAgo -1) > Ref(O, pBarsAgo -2) AND Ref(C, pBarsAgo -1 ) > Ref(H, pBarsAgo -2) AND Ref(L, pBarsAgo) > Ref(O, pBarsAgo -1);
  ret = ret AND Ref(C, pBarsAgo) > Ref(H, pBarsAgo -1); // CLose na poslednom bare je vyssie ako high predposleneho baru
  return ret;
}

function isThreeNewLows_withLowerHighs_Pattern(pBarsAgo)
{
  // Three new highs with three new higher lows
  local ret;
  ret = Ref(O, pBarsAgo) > Ref(C, pBarsAgo) AND Ref(O, pBarsAgo -1 ) > Ref(C, pBarsAgo -1) AND Ref(O, pBarsAgo-2) > Ref(C, pBarsAgo -2); // 3 klesajuce usecky
  ret = ret AND Ref(H, pBarsAgo) < Ref(H, pBarsAgo -1) AND Ref(H, pBarsAgo) < Ref(H, pBarsAgo -2) AND Ref(H, pBarsAgo -1) < Ref(H, pBarsAgo -2); // nizsie HIGH
  ret = ret AND Ref(L, pBarsAgo) < Ref(L, pBarsAgo -1) AND Ref(L, pBarsAgo) < Ref(L, pBarsAgo -2) AND Ref(L, pBarsAgo -1) < Ref(L, pBarsAgo -2); // vyssie LOW
  return ret;
}


// GENERAL BUY, SELL, etc... SWITCH FUNCTIONS


function isOpenBuyLongSignal(pSignalID)
// Enter a long position
{
	local ret;
	switch (pSignalID)
	{
	case 0:
	    ret = False; break;
	case 1: 
		ret = isDojiBar(-1); break;
	case 2: 	
	    ret = isThreeWhiteSoldiersPattern(-1); 	break;	
	case 3: 
	    ret = isThreeNewLows_withLowerHighs_Pattern(-2) AND isDojiBar(-1); 	break;		    
	default:
        // default action
        ret = False;
        break;
	}
	
	return ret;
}


function isCloseLongSignal(pSignalID)
// Exit a long position, In amibroker it's called SELL
{
	local ret;
	switch (pSignalID)
	{
	case 0:
	    ret = False; break;	
	case 1: 
		ret = isDojiBar(-1); break;
	case 2: 
	    ret = Close > HHV( C , 5 );
		break;	
	case 3:
	    //ret = Cross( RSI(14), 80 ) ; break; 	
	    ret = RSI(14) > 80 ; break; 	
	default:
        // default action
        ret = False;
        break;
	}
	return ret;
}


/* INITIAL SETTINGS */
PosQty = 1;
SetOption("MaxOpenPositions", PosQty );
Account = 10000;
SetOption("InitialEquity", Account );
SetOption("CommissionMode", 3);  // 3 - $ per share/contract  
SetOption("CommissionAmount", 7); // fee 7USD per lot
SetOption("AllowSameBarExit", True );
SetOption("AllowPositionShrinking", True );
SetTradeDelays(0,0,0,0);

// LONG BUY CONDITIONS

// BUY ENTRY, Long position
pBuySignalID_Default = 3;
pBuySignalID = Optimize("pBuySignalID", pBuySignalID_Default, 1, 2, 1); 
BuySetup = isOpenBuyLongSignal(pBuySignalID); 
Buy = BuySetup;
BuyPrice = Open;      // BuyPrice definuje cenu, za kterou se obchod otevre. 

// EXIT LONG POSITION, sell 
pSellSignalID_Default = 0; // 0 means exit on SL or TP
pSellSignalID = Optimize("pSellSignalID", pSellSignalID_Default, 1, 10, 1); 
SellSetup = isCloseLongSignal(pSellSignalID); 
Sell = SellSetup;
SellPrice = Close;   // Uzaviraci cena - zde za Close.




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

// Stop Loss in USD
ApplyStop(stopTypeLoss, stopModePoint, 150 * TickSize, True, True); // is this set correctly?

// ProfitTarget in USD
ApplyStop(stopTypeProfit,stopModePoint, 200 * TickSize, True, True);  // is this set correctly?


I'm testing it on EURUSD data, and I'm not able to exit trade on predefined StopLoss resp ProfitTarget

Any suggestions regarding above mentioned issue?

It is unclear what you mean with

I'm not able to exit trade on predefined StopLoss resp ProfitTarget

See: How to ask a good question

We don't see your computer screen and can not guess what you see and what you think is wrong. Generally the 'amount' in ApplyStop is expressed either in percents or "points" which means actual price distance in underlying instrument. So if you want to exit trade when prices move say from 1.1500 to 1.1440 the distance (i.e. amount) should be set to 0.060 (1.1500-1.1440). Please note that actual trade may occur at different price when price gap between candles appears (for example on weekend).

Hello Tomasz,

thanks for your reply. By not exiting a trade I mean, that a StopLoss resp. ProfitTarget is not executed, even when it should be.

Here is my full code and also a picture of result which runs into max loss which is higher than StopLoss price. I'm sure it's some small mistake, or settings related issue, but have no idea how to solve it.

// PATTERNS FUNCTIONS

function isDojiBar(pBarsAgo)
{
  //velmi nerozhodny trh. Telo usecky tvorilo jen 10% celkoveho range.
  //Na trhu neexistovala dominujici sila.
  return abs(Ref(O, pBarsAgo) - Ref(C,pBarsAgo) ) < 0.1 * (Ref(H, pBarsAgo) - Ref(L, pBarsAgo) );
}

function isThreeNewHighs_withHigherLows_Pattern(pBarsAgo)
{
  // Three new highs with three new higher lows
  local ret;
  ret = Ref(O, pBarsAgo) < Ref(C, pBarsAgo) AND Ref(O, pBarsAgo -1 ) < Ref(C, pBarsAgo -1) AND Ref(O, pBarsAgo-2) < Ref(C, pBarsAgo -2); // 3 rastuce usecky
  ret = ret AND Ref(H, pBarsAgo) > Ref(H, pBarsAgo -1) AND Ref(H, pBarsAgo) > Ref(H, pBarsAgo -2) AND Ref(H, pBarsAgo -1) > Ref(H, pBarsAgo -2); // vyssie HIGH
  ret = ret AND Ref(L, pBarsAgo) > Ref(L, pBarsAgo -1) AND Ref(L, pBarsAgo) > Ref(L, pBarsAgo -2) AND Ref(L, pBarsAgo -1) > Ref(L, pBarsAgo -2); // vyssie LOW
  return ret;
}

function isThreeWhiteSoldiersPattern(pBarsAgo)
{
  // Three White soldiers pattern
  local ret;
  ret = isThreeNewHighs_withHigherLows_Pattern(pBarsAgo) AND Ref(L, pBarsAgo -1) > Ref(O, pBarsAgo -2) AND Ref(C, pBarsAgo -1 ) > Ref(H, pBarsAgo -2) AND Ref(L, pBarsAgo) > Ref(O, pBarsAgo -1);
  ret = ret AND Ref(C, pBarsAgo) > Ref(H, pBarsAgo -1); // CLose na poslednom bare je vyssie ako high predposleneho baru
  return ret;
}

function isThreeNewLows_withLowerHighs_Pattern(pBarsAgo)
{
  // Three new highs with three new higher lows
  local ret;
  ret = Ref(O, pBarsAgo) > Ref(C, pBarsAgo) AND Ref(O, pBarsAgo -1 ) > Ref(C, pBarsAgo -1) AND Ref(O, pBarsAgo-2) > Ref(C, pBarsAgo -2); // 3 klesajuce usecky
  ret = ret AND Ref(H, pBarsAgo) < Ref(H, pBarsAgo -1) AND Ref(H, pBarsAgo) < Ref(H, pBarsAgo -2) AND Ref(H, pBarsAgo -1) < Ref(H, pBarsAgo -2); // nizsie HIGH
  ret = ret AND Ref(L, pBarsAgo) < Ref(L, pBarsAgo -1) AND Ref(L, pBarsAgo) < Ref(L, pBarsAgo -2) AND Ref(L, pBarsAgo -1) < Ref(L, pBarsAgo -2); // vyssie LOW
  return ret;
}


// GENERAL BUY, SELL, etc... SWITCH FUNCTIONS


function isOpenBuyLongSignal(pSignalID)
// Enter a long position
{
	local ret;
	switch (pSignalID)
	{
	case 0:
	    ret = False; break;
	case 1: 
		ret = isDojiBar(-1); break;
	case 2: 	
	    ret = isThreeWhiteSoldiersPattern(-1); 	break;	
	case 3: 
	    ret = isThreeNewLows_withLowerHighs_Pattern(-2) AND isDojiBar(-1); 	break;		    
	default:
        // default action
        ret = False;
        break;
	}
	
	return ret;
}


function isCloseLongSignal(pSignalID)
// Exit a long position, In amibroker it's called SELL
{
	local ret;
	switch (pSignalID)
	{
	case 0:
	    ret = False; break;	
	case 1: 
		ret = isDojiBar(-1); break;
	case 2: 
	    ret = Close > HHV( C , 5 );
		break;	
	case 3:
	    //ret = Cross( RSI(14), 80 ) ; break; 	
	    ret = RSI(14) > 80 ; break; 	
	default:
        // default action
        ret = False;
        break;
	}
	return ret;
}


/* INITIAL SETTINGS */
PosQty = 1;

Account = 10000;
SetOption("InitialEquity", Account );
SetOption("MaxOpenPositions", PosQty );
SetOption("CommissionMode", 3);  // 3 - $ per share/contract  
SetOption("CommissionAmount", 7); // fee 7USD per lot
SetOption("AllowSameBarExit", True );
SetOption("AllowPositionShrinking", True );
SetTradeDelays(0,0,0,0);

// LONG BUY CONDITIONS

// BUY ENTRY, Long position
pBuySignalID_Default = 3;
pBuySignalID = Optimize("pBuySignalID", pBuySignalID_Default, 1, 2, 1); 
BuySetup = isOpenBuyLongSignal(pBuySignalID); 
Buy = BuySetup;
BuyPrice = Open;      // BuyPrice definuje cenu, za kterou se obchod otevre. 

// EXIT LONG POSITION, sell 
pSellSignalID_Default = 0; // 0 means exit on SL or TP
pSellSignalID = Optimize("pSellSignalID", pSellSignalID_Default, 1, 10, 1); 
SellSetup = isCloseLongSignal(pSellSignalID); 
Sell = SellSetup;
SellPrice = Close;   // Uzaviraci cena - zde za Close.




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

// Stop Loss in USD
// Stop loss
min_amount = 150 * 0.00001;// minimum SL amount e.g 1 tick change
//stop_amount = Max(min_amount, BuyPrice - Ref(L, -delay));
stop_amount = min_amount;
ApplyStop(stopTypeLoss, stopModePoint, stop_amount, exitatstop = 1);
//ApplyStop(stopTypeLoss, stopModePoint, 150 * TickSize, True, True); // is this set correctly?

// ProfitTarget in USD
ApplyStop(stopTypeProfit,stopModePoint, 100 * 0,00001, True, True);  // is this set correctly?


Amibroker%2C_SL_question

Check this post on ApplyStop by @fxshrat
I never use Applystop but I guess you should use 2 instead of 1

ApplyStop(stopTypeLoss, stopModePoint, stop_amount, ExitAtStop=2);

I changed it and also applied a set position size to 1 share to test it.

Minor modification within code, and applying a really simple entry condition for buy.
Unfortunately it does not work for me, returning no trades now.

I'm testing below code on 15min timeframe, EURUSD chart

// PATTERNS FUNCTIONS

function isDojiBar(pBarsAgo)
{
  //velmi nerozhodny trh. Telo usecky tvorilo jen 10% celkoveho range.
  //Na trhu neexistovala dominujici sila.
  return abs(Ref(O, pBarsAgo) - Ref(C,pBarsAgo) ) < 0.1 * (Ref(H, pBarsAgo) - Ref(L, pBarsAgo) );
}

function isThreeNewHighs_withHigherLows_Pattern(pBarsAgo)
{
  // Three new highs with three new higher lows
  local ret;
  ret = Ref(O, pBarsAgo) < Ref(C, pBarsAgo) AND Ref(O, pBarsAgo -1 ) < Ref(C, pBarsAgo -1) AND Ref(O, pBarsAgo-2) < Ref(C, pBarsAgo -2); // 3 rastuce usecky
  ret = ret AND Ref(H, pBarsAgo) > Ref(H, pBarsAgo -1) AND Ref(H, pBarsAgo) > Ref(H, pBarsAgo -2) AND Ref(H, pBarsAgo -1) > Ref(H, pBarsAgo -2); // vyssie HIGH
  ret = ret AND Ref(L, pBarsAgo) > Ref(L, pBarsAgo -1) AND Ref(L, pBarsAgo) > Ref(L, pBarsAgo -2) AND Ref(L, pBarsAgo -1) > Ref(L, pBarsAgo -2); // vyssie LOW
  return ret;
}

function isThreeWhiteSoldiersPattern(pBarsAgo)
{
  // Three White soldiers pattern
  local ret;
  ret = isThreeNewHighs_withHigherLows_Pattern(pBarsAgo) AND Ref(L, pBarsAgo -1) > Ref(O, pBarsAgo -2) AND Ref(C, pBarsAgo -1 ) > Ref(H, pBarsAgo -2) AND Ref(L, pBarsAgo) > Ref(O, pBarsAgo -1);
  ret = ret AND Ref(C, pBarsAgo) > Ref(H, pBarsAgo -1); // CLose na poslednom bare je vyssie ako high predposleneho baru
  return ret;
}

function isThreeNewLows_withLowerHighs_Pattern(pBarsAgo)
{
  // Three new highs with three new higher lows
  local ret;
  ret = Ref(O, pBarsAgo) > Ref(C, pBarsAgo) AND Ref(O, pBarsAgo -1 ) > Ref(C, pBarsAgo -1) AND Ref(O, pBarsAgo-2) > Ref(C, pBarsAgo -2); // 3 klesajuce usecky
  ret = ret AND Ref(H, pBarsAgo) < Ref(H, pBarsAgo -1) AND Ref(H, pBarsAgo) < Ref(H, pBarsAgo -2) AND Ref(H, pBarsAgo -1) < Ref(H, pBarsAgo -2); // nizsie HIGH
  ret = ret AND Ref(L, pBarsAgo) < Ref(L, pBarsAgo -1) AND Ref(L, pBarsAgo) < Ref(L, pBarsAgo -2) AND Ref(L, pBarsAgo -1) < Ref(L, pBarsAgo -2); // vyssie LOW
  return ret;
}


// GENERAL BUY, SELL, etc... SWITCH FUNCTIONS


function isOpenBuyLongSignal(pSignalID)
// Enter a long position
{
	local ret;
	switch (pSignalID)
	{
	case 0:
	    ret = False; break;
	case 1: 
		ret = isDojiBar(-1); break;
	case 2: 	
	    ret = isThreeWhiteSoldiersPattern(-1); 	break;	
	case 3: 
	    ret = isThreeNewLows_withLowerHighs_Pattern(-2) AND isDojiBar(-1); 	break;		
	case 4:
	    // simple condition to test  
	    ret = Ref(O, -1) > Ref(C, -1); 	break;		        
	default:
        // default action
        ret = False;
        break;
	}
	
	return ret;
}


function isCloseLongSignal(pSignalID)
// Exit a long position, In amibroker it's called SELL
{
	local ret;
	switch (pSignalID)
	{
	case 0:
	    ret = False; break;	
	case 1: 
		ret = isDojiBar(-1); break;
	case 2: 
	    ret = Close > HHV( C , 5 );
		break;	
	case 3:
	    //ret = Cross( RSI(14), 80 ) ; break; 	
	    ret = RSI(14) > 80 ; break; 	
	default:
        // default action
        ret = False;
        break;
	}
	return ret;
}


/* INITIAL SETTINGS */
PosQty = 1;

Account = 10000;
SetOption("InitialEquity", Account );
SetOption("MaxOpenPositions", PosQty );
SetOption("CommissionMode", 3);  // 3 - $ per share/contract  
SetOption("CommissionAmount", 7); // fee 7USD per lot
SetOption("AllowSameBarExit", True );
SetOption("AllowPositionShrinking", True );
SetTradeDelays(0,0,0,0);

SetPositionSize(1, spsShares);
//SetPositionSize(1, spsPercentOfEquity ); 

// LONG BUY CONDITIONS

// BUY ENTRY, Long position
pBuySignalID_Default = 4;
pBuySignalID = Optimize("pBuySignalID", pBuySignalID_Default, 1, 4, 1); 
BuySetup = isOpenBuyLongSignal(pBuySignalID); 
Buy = BuySetup;
BuyPrice = Open;      // BuyPrice definuje cenu, za kterou se obchod otevre. 

// EXIT LONG POSITION, sell 
pSellSignalID_Default = 0; // 0 means exit on SL or TP
pSellSignalID = Optimize("pSellSignalID", pSellSignalID_Default, 1, 10, 1); 
SellSetup = isCloseLongSignal(pSellSignalID); 
Sell = SellSetup;
SellPrice = Close;   // Uzaviraci cena - zde za Close.




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

// Stop Loss in USD
// Stop loss
min_amount = 150 * 0.00001;// minimum SL amount e.g 1 tick change
//stop_amount = Max(min_amount, BuyPrice - Ref(L, -delay));
stop_amount = min_amount;
ApplyStop(stopTypeLoss, stopModePoint, stop_amount, ExitAtStop = 2);
//ApplyStop(stopTypeLoss, stopModePoint, 150 * TickSize, True, True); // is this set correctly?

// ProfitTarget in USD
ApplyStop(stopTypeProfit,stopModePoint, (100 * 0.00001), ExitAtStop=1);  // is this set correctly?

/*
// Stops
buySignalBarLow = Ref( ValueWhen(buySignal, Low), -1 ); // use entry bar low
maxLossStopPoints = BuyPrice - buySignalBarLow - 0.1*ATR(10);

ApplyStop(stopTypeLoss, stopModePoint, maxLossStopPoints, ExitAtStop=2);
ApplyStop(stopTypeProfit,stopModePoint, 2*ATR(10), ExitAtStop=1);
*/


Could it be in setting something? Please let me know which settings screen to post, to be able to check it.

Then wait for expert to help you..........

@Tomeo,

First of all, these four functions

function isDojiBar(pBarsAgo)
{
  //velmi nerozhodny trh. Telo usecky tvorilo jen 10% celkoveho range.
  //Na trhu neexistovala dominujici sila.
  return abs(Ref(O, pBarsAgo) - Ref(C,pBarsAgo) ) < 0.1 * (Ref(H, pBarsAgo) - Ref(L, pBarsAgo) );
}

function isThreeNewHighs_withHigherLows_Pattern(pBarsAgo)
{
  // Three new highs with three new higher lows
  local ret;
  ret = Ref(O, pBarsAgo) < Ref(C, pBarsAgo) AND Ref(O, pBarsAgo -1 ) < Ref(C, pBarsAgo -1) AND Ref(O, pBarsAgo-2) < Ref(C, pBarsAgo -2); // 3 rastuce usecky
  ret = ret AND Ref(H, pBarsAgo) > Ref(H, pBarsAgo -1) AND Ref(H, pBarsAgo) > Ref(H, pBarsAgo -2) AND Ref(H, pBarsAgo -1) > Ref(H, pBarsAgo -2); // vyssie HIGH
  ret = ret AND Ref(L, pBarsAgo) > Ref(L, pBarsAgo -1) AND Ref(L, pBarsAgo) > Ref(L, pBarsAgo -2) AND Ref(L, pBarsAgo -1) > Ref(L, pBarsAgo -2); // vyssie LOW
  return ret;
}

function isThreeWhiteSoldiersPattern(pBarsAgo)
{
  // Three White soldiers pattern
  local ret;
  ret = isThreeNewHighs_withHigherLows_Pattern(pBarsAgo) AND Ref(L, pBarsAgo -1) > Ref(O, pBarsAgo -2) AND Ref(C, pBarsAgo -1 ) > Ref(H, pBarsAgo -2) AND Ref(L, pBarsAgo) > Ref(O, pBarsAgo -1);
  ret = ret AND Ref(C, pBarsAgo) > Ref(H, pBarsAgo -1); // CLose na poslednom bare je vyssie ako high predposleneho baru
  return ret;
}

function isThreeNewLows_withLowerHighs_Pattern(pBarsAgo)
{
  // Three new highs with three new higher lows
  local ret;
  ret = Ref(O, pBarsAgo) > Ref(C, pBarsAgo) AND Ref(O, pBarsAgo -1 ) > Ref(C, pBarsAgo -1) AND Ref(O, pBarsAgo-2) > Ref(C, pBarsAgo -2); // 3 klesajuce usecky
  ret = ret AND Ref(H, pBarsAgo) < Ref(H, pBarsAgo -1) AND Ref(H, pBarsAgo) < Ref(H, pBarsAgo -2) AND Ref(H, pBarsAgo -1) < Ref(H, pBarsAgo -2); // nizsie HIGH
  ret = ret AND Ref(L, pBarsAgo) < Ref(L, pBarsAgo -1) AND Ref(L, pBarsAgo) < Ref(L, pBarsAgo -2) AND Ref(L, pBarsAgo -1) < Ref(L, pBarsAgo -2); // vyssie LOW
  return ret;
}

Can be simplified to

function isDojiBar(pBarsAgo)
{
  //velmi nerozhodny trh. Telo usecky tvorilo jen 10% celkoveho range.
  //Na trhu neexistovala dominujici sila.
  result = abs(O-C) < 0.1 * (H-L);
  return Ref(result, pBarsAgo); // 
}

function isThreeNewHighs_withHigherLows_Pattern(pBarsAgo)
{
  // Three new highs with three new higher lows
  local ret;
  ret = O < C AND H > Ref(H,-1) AND L > Ref(L,-1);
  ret = Sum(ret, 3) == 3; // 3 rastuce usecky
  return Ref(ret, pBarsAgo);
}

function isThreeWhiteSoldiersPattern(pBarsAgo)
{
  // Three White soldiers pattern
  local ret;
  cs = Sum(C > Ref(H,-1) AND L > Ref(O,-1), 2) == 2;
  ret = isThreeNewHighs_withHigherLows_Pattern(pBarsAgo) AND Ref(cs, pBarsAgo);
  return ret;
}

function isThreeNewLows_withLowerHighs_Pattern(pBarsAgo)
{
  // Three new highs with three new higher lows
  local ret;
  ret = O > C AND H < Ref(H,-1) AND L < Ref(L,-1);
  ret = Sum(ret, 3) == 3;// 3 klesajuce usecky
  return Ref(ret, pBarsAgo);
}

Much shorter, isn't it? (and easier to read)


Since you say that you do not get much trades....
You should check whether each of your patterns actually give that many signals. So do an exploration or chart the patterns using Plot with styleHistogram (or use PlotShapes).
How to debug formula.


As for stop amount being higher... As been said in this thread already... investigate whether it has been a gap there!


Another (additional) method of investigating results detailly:
Go to Analysis settings - Report tab and set to Detailed log.

46


As for ApplyStop please read here carefully. Also read about the example scenarios there.


BTW you can set TickSize (besides of others) in Window - Information-Contract Specification

08

So for 15 pip stop loss you may just use

stop_amount = 15 * TickSize;

etc.

And if you trade via margin account you should activate Futures mode.
AmiBroker and Forex here.

7 Likes

Many thanks for your help.

Simplified code is perfect, I understand it and agree with you. Much easier to read and understand.

I'll go through the rest and will post back if issue will persist or I'll identify any problems.

One note and two changes/alternatives to two of your four pattern functions..

If you want to do three white soldiers as in this picture of this website
747

then you should change isThreeNewHighs_withHigherLows_Pattern and isThreeNewLows_withLowerHighs_Pattern functions to

function isThreeNewHighs_withHigherLows_Pattern(pBarsAgo)
{
  // three bullish bars with two new higher highs and two new higher lows
  local ret;
  ret = H > Ref(H,-1) AND L > Ref(L,-1);
  ret = Sum(O<C, 3) == 3 AND Sum(ret, 2) == 2; // 3 rastuce usecky
  return Ref(ret, pBarsAgo);
}

function isThreeNewLows_withLowerHighs_Pattern(pBarsAgo)
{
  // three bearish bars and two new lower highs and two new lower lows
  local ret;
  ret = H < Ref(H,-1) AND L < Ref(L,-1);
  ret = Sum(O>C, 3) == 3 AND Sum(ret, 2) == 2;// 3 klesajuce usecky
  return Ref(ret, pBarsAgo);
}

Otherwise if you want to have three new higher highs and three new lower lows then use the ones of previous post.

MaxLoss is applied correctly. You specified 0.0015 as stop loss amount in your code

min_amount = 150 * 0.00001;// minimum SL amount e.g 1 tick change
//stop_amount = Max(min_amount, BuyPrice - Ref(L, -delay));
stop_amount = min_amount;
ApplyStop(stopTypeLoss, stopModePoint, stop_amount, exitatstop = 1);

and precisely this was used:
Amibroker%2C_SL_question

See the difference between entry price (1.09055) and exit price (1.08900) is 0.00155 which is exactly in line with your declared amount of stop

Stop loss amount as documented is expressed in underlying instrument PRICE (price movement required to trigger stop). And you are pointing out to "Profit" incorrectly because NOT profit but PRICE movement triggers the stop. Profit is a result of MULTIPLYING price difference by NUMBER OF CONTRACTS AND PointValue !
That is not the "stop amount" in ApplyStop which is just price movement.

Recommended reading: http://www.amibroker.com/kb/2006/08/09/amibroker-for-forex/

2 Likes

Hello Tomasz and Fxshrat,

many thanks for your input. It helped me a lot.

I'll set up a shares to 1 lot (1 share) for testing, as at the moment it calculated 1236 shares based on initial equity.

If any issue will occur, I'll create a new topic, as this one was related to ApplyStop which is not an issue anymore.

Tomasz, please one more look into this section, as I really don't know why it's not triggered.

Please see this code:

// Stop Loss in pips
pipsSL = 5; // x10 in USD per 1 lot
price_diff_SL = pipsSL * 0.0001; // minimum SL amount e.g 1 tick change
ApplyStop(stopTypeLoss, stopModePoint, price_diff_SL, ExitAtStop = 2);

This is how I understand it should work. I want 5 pips price movement, so in my case it would be price difference of 5 * 0.0001 = 0.0005.

Unfortunately this is not triggered.

I have a similar code for TargetProfit and it works correctly, so no clue why SL is not working.

This is how I have set a TP, and it works:

// ProfitTarget in pips
pipsTP = 20;  // 10 pips = 100USD per 1 lot
price_diff_TP = pipsTP * 0.0001; // minimum TP amount e.g 1 tick change
ApplyStop(stopTypeProfit,stopModePoint, price_diff_TP, ExitAtStop=1);

Please see also a results of trades, from which we can see that SL was not triggered. Could it be some other settings? I really tried to check everything.

AmiBroker-SL-issue

Thank you

I think I found why it's like that:

ApplyStop(stopTypeLoss, stopModePoint, price_diff_SL, ExitAtStop = 1);

In SL I had ExitAtStop = 2 instead of ExitAtStop = 1.

Can you please explain me when is good to use ExitAtStop = 2 option?

The answer to your question is in Users Guide http://www.amibroker.com/f?applystop
Nothing more to add beyond what is already described in great detail the manual. Just read it carefully and SLOWLY. Every sentence and every word.