Help is fixing my code..

iTrade

Well-Known Member
#1
Hi Friends -- I am new to Amibroker AFL. I have gathereed some of
my code and I really need some help to get this code working...

Logic: Trade only LONGS on a candlestick chart...
When a bullish candle is formed, we enter trade on High+small filter
and set target to some fixed value from entryprice.
Initial stoploss set to low of candle on which trade was trigerred.
Thereafter we update stoploss to low of every new candle that is formed, ensuring that stop is never lowered compared to its previous value.

I have written some part of the code, and also added commnets...
Can someone please review, and also tell me how to put
the trailing stop loss logic here...


//Buy
stoploss=0;
whitebody=Ref(C,-1)>Ref(Open,-1); //last candle should be bullish
BuyCondition=whitebody AND (H>Ref(H,-1)+1); //and current high should cross high of last candle plus filter
Buyvalue=Ref(H,-1)+1; //variable to hold buy price

Buy=BuyCondition;
AlertIf( Buy, "", "Buy trigerred at" + WriteVal(C),1);
for( i = 1; i < BarCount; i++ )
{
if(Buy)
{
target=BuyValue+3;
SellCondition1=C>=target; //target price where we sell the stock. close should greater than buyvalue + rs.3 up
stoploss=L-1; //stoploss is set to low candle on which trade is trigerred
SellCondition2=C<Stoploss; //price goes below stoploss
Sell=SellCondition1 OR Cond SellCondition2; //We sell on either target hit or stoploss
AlertIf(Sell, "", "Sell trigerred at" + WriteVal(C),1);
"Stoploss set to " + WriteVal(stoploss); //some commentary
"Target set to " + WriteVal(target);
}
}

//trail stop to every new candles low with commentary. How do I do it?
 
Last edited:

GTji

Active Member
#3
This could work, I am not sure what exactly you want. Just wrote this without checking. You can tweak it.

BL=Valuewhen(Buy,L);// Low of the Buy candle
STL=iif(L>BL,cross(ref(L,-1),L),Cross(BL,L));// stoploss on buy candle or subsequent lows
 

Similar threads