Hi! How can I write custom loss stop for multiple signals for trading in 24-hr or specific time schedule? My code is a pattern trading strategy, is quite cumbersome. Please comment!
Buy1 = Ref(Pat1,-1) AND Close >= Ref(LL,-1);
Short1 = Ref(Pat2,-1) AND Close <= Ref(HH,-1);
//Short2 = Pat15 AND Close == LL;
/* Loss Stop */
for( i = 0; i < BarCount; i++ )
{
if ( PriceatBuy == 0 AND Buy1[i] )
{
PriceatBuy = BuyPrice[ i ];
PriceatBuyStop = L1[i-1];
}
if ( PriceatBuy > 0 AND SellPrice[i] < PriceatBuyStop )
{
Sell1[ i ] = 1;
//SellPrice[ i ] = Close[i];
PriceatBuy = 0;
}
else
{
Sell1[ i ] = 0;
PriceatBuyStop = 0;
}
if ( PriceatShort == 0 AND Short1[i] )
{
PriceatShort = ShortPrice[i];
PriceatShortStop = H1[i-1];
}
if ( PriceatShort > 0 AND CoverPrice[i] > PriceatShortStop )
{
Cover1[i] = 1;
//CoverPrice[i] = Close[i];
PriceatShort = 0;
}
else
{
Cover1[i] = 0;
PriceatShortStop = 999999;
}
}
/* trading in 24-hr or specific time schedule */
if ( TradeMode == 1 )
{
Buy = Buy1 AND tn >= TimeEnter;
Short = Short1 AND tn >= TimeEnter;
Sell = Sell1 OR Short OR Cross( tn, TimeQuit );
Cover = Cover1 OR Buy OR Cross( tn, TimeQuit );
}
else if ( TradeMode == 2 )
{
Buy = Buy1;
Short = Short1;
Sell = Sell1 or Short;
Cover = Cover1 or Buy;
}
ApplyStop function is intended to cover most “popular” kinds of stops. You can however code your own kind of stops and exits using looping code. For example the following re-implements profit target stop and shows how to refer to the trade entry price in your formulas:
/* a sample low-level implementation of Profit-target stop in AFL: */
Buy = Cross( MACD(), Signal() );
priceatbuy=0;
for( i = 0; i < BarCount; i++ )
{
if( priceatbuy == 0 && Buy[ i ] )
priceatbuy = BuyPrice[ i ];
if( priceatbuy > 0 && SellPrice[ i ] > 1.1 * priceatbuy )
{
Sell[ i ] = 1;
SellPrice[ i ] = 1.1 * priceatbuy;
priceatbuy = 0;
}
else
Sell[ i ] = 0;
}
I am sorry, but I can’t write code for everybody due to time constraints. I write sample codes for Traders’ Tips, sample codes for Knowledge Base, Users guide, function reference, Read-Mes and such. These examples are provided as starting point for your own coding. Or you may want to hire somebody if you don’t feel comfortable with doing it on your own.