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