I want it to buy/sell at the cross of Value4 and Trigger, when below/above the buylevel/selllevel.
This line of code works just fine out of another system
" BuyLevel = Optimize( "BuyLevel", 28, 5, 30, 1 );
Buy = RSI < BuyLevel;
Sell = RSI > 50;"
So whats the difference between that and what I had in-
"Buy = Cross (Value4, Trigger) < BuyLevel;
Sell = Cross (Trigger, Value4) > SellLevel;"
As was said earlier:
cross(a,b) is a boolean, it can be either true or false, 0 or 1, that's it.
So to say cross(a,b) < 40, makes no sense.
To say RS1 < 40 is fine, as RSI values can range from 0 to 100.
Buy = xxxx;
Sell = yyyy;
stoppc = 20; // percent
stoplevel = (100-stoppc)/100;
trailstop = 0;
trailARRAY = Null;
OpenPos = 0;
for (i=0; i<BarCount;i++)
{
if(trailstop==0 AND Buy[i]) // if buy today (trailstop will be 0 at this point. SellFilter must therefore be off)
{
OpenPos = 1;
}
else // if not initial buy, remove excess buy signals
{
Buy[i] = 0;
}
if(OpenPos == 1)
{
if(Sellfilter[i]=0) // if in trade and sellfilter is off
{
trailstop = 0; // reset trailstop
}
else // sellfilter must be on
{
if(trailstop == 0) // in trade, sell filter on but SL initially 0
{
trailstop == Low[i] * stoplevel; // set SL to % below low
trailARRAY[i] = trailstop;
}
else // in trade, sellfilter on, trailstop previously set
{
if(Close[i] < trailstop) // if close lower than stop - exit trade
{
// !!! gets here but trade never closes !!!
Sell[i] = 1; //exit trade -
OpenPos = 0; //not in trade anymore
trailstop = 0; //reset SL
}
else // in trade, sellfilter on, trailstop previously set, still in trade
{
trailstop = max(trailstop, Low[i] * trailstop; // SL is higher of the two - never lower
trailARRAY[i] = trailstop;
}
}
}
}
}
Hi guys,
Fantastic thread.
I'm having a problem with a Sell within a FOR loop. I use the FOR loop as a kind of trailing stop. I have the statement 'Sell = 1' within the loop but the trade never closes. Through some debugging, I know that it gets to that part of the code but I'm not sure what I'm doing wrong.
It looks a bit complicated but I think the problem lies near the end (the last if.else section)
Code:Buy = xxxx; Sell = yyyy; stoppc = 20; // percent stoplevel = (100-stoppc)/100; trailstop = 0; trailARRAY = Null; OpenPos = 0; for (i=0; i<BarCount;i++) { if(trailstop==0 AND Buy[i]) // if buy today (trailstop will be 0 at this point. SellFilter must therefore be off) { OpenPos = 1; } else // if not initial buy, remove excess buy signals { Buy[i] = 0; } if(OpenPos == 1) { if(Sellfilter[i]=0) // if in trade and sellfilter is off { trailstop = 0; // reset trailstop } else // sellfilter must be on { if(trailstop == 0) // in trade, sell filter on but SL initially 0 { trailstop == Low[i] * stoplevel; // set SL to % below low trailARRAY[i] = trailstop; } else // in trade, sellfilter on, trailstop previously set { if(Close[i] < trailstop) // if close lower than stop - exit trade { // !!! gets here but trade never closes !!! Sell[i] = 1; //exit trade - OpenPos = 0; //not in trade anymore trailstop = 0; //reset SL } else // in trade, sellfilter on, trailstop previously set, still in trade { trailstop = max(trailstop, Low[i] * trailstop; // SL is higher of the two - never lower trailARRAY[i] = trailstop; } } } } }
I'm pretty new to amibroker so I'm not sure whether its a simple coding error or my understanding is way off.
Haven't looked closely but possibly because sellprice is missing
Sell = 1; //exit trade -
SellPrice = trailstop;
Otherwise look here at sample code http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/
Thanks Trash,
I've tried those lines of code and unfortunately it still didn't work. I actually modelled my stop loss loop on the link you provided. The stop loss plot looks correct and there is even a red arrow over the bar where it should have exited. It just doesn't exit the trade. I've really hit a brick wall here.
I'm actually trying to write code for Nick Radge's Yearly High and the for loop is my implementation of the stop loss with the 75 day XAO filter. I could use the standard apply stop function but I want the stop to 'reset' each time the filter turns off.
I can post it all up but I thought just the 'for' loop would be good enough.
Then there are bugs elsewhere in your code
for example this line
trailstop == Low * stoplevel;
should be
trailstop = Low * stoplevel;
Then this is wrong
Sellfilter = 0
should be
Sellfilter == 0
and does that make sense?
trailstop = Max( trailstop, Low * trailstop );
should rather be
trailstop = Max( trailstop, Low * stoplevel );
Haven't looked at other stuff, could be more logical errors.
Thanks for your help Trash.
I'll have a look to see if those extra '=' are still in there. I remember making that mistake at least once so I may have left one in (conversely, I may have already fixed them up and posted early,bug-riddled code).
It seems from what you're saying its more than likely a coding/logic error. That's fine because at least I know I'm on the right track and just need to iron out a few bugs. I think if I was trying to do something in a way that wasn't in line with the way Amibroker works, you probably would've pointed it out already.
I'll have a look at the code again and let you know.
//'New Yearly Highs'
//Setup
InitEquity = 100000;
MaxPos = 20;
Tradesize = 5000;
SetOption("InitialEquity", InitEquity);
SetOption("MaxOpenPositions", MaxPos);
SetPositionSize(5, spsPercentOfEquity);
//SetPositionSize(5000, spsValue);
//SetTradeDelays(1,1,1,1);
SetTradeDelays(0,0,0,0);
SetOption("CommissionMode",2);
SetOption("CommissionAmount", 30);
SetOption("UsePrevBarEquityForPosSizing", True);
//Filter
SetForeign("XAO", True);
MAfilter = MA(C, 75);
Buyfilter = Close > MA( C, 75);
Sellfilter = Close < MA(C, 75);
RestorePriceArrays();
//Rules
HiLevel = Param("HiLevel", 100, 10, 500, 5);
LoLevel = Param("LoLevel", 100, 10, 500, 5);
HI = HHV(C,HiLevel);
LW = LLV(C,LoLevel);
DollarTurnover = MA(C*V, 7);
dollarTurnoverOK = dollarTurnover > 500000;
Liquidity = MA(V,7);
LiquidityOK = Liquidity > 500000;
OptMax = 20; //Optimize("MaxPrice", 20, 5, 30, 5);
MaxSP = Close < OptMax;
Buy = C >= HI AND LiquidityOK AND dollarTurnoverOK AND Buyfilter AND MaxSP;
Sell = C <= LW;
//With Filter
//Buy = C >= HI AND LiquidityOK AND dollarTurnoverOK AND Buyfilter;
//Sell = C <= LW OR SellFilter;
// No Filter
//Buy = C >= HI AND LiquidityOK AND dollarTurnoverOK;
//Sell = C <= LW;
//Buy = ExRem(Buy,Sell);
//Sell = ExRem(Sell,Buy);
//If Filter on, tighten SL to xx%
stoppc = Param("Stop %", 2, 2, 50, 2);
stoplevel = (100-stoppc)/100;
trailstop = 0;
OpenPos = 0;
trailARRAY = Null;
for (i=1; i<BarCount;i++)
{
if(trailstop==0 AND Buy[i]) // if buy today (trailstop will be 0 at this point. SellFilter must therefore be off)
{
OpenPos = 1;
}
else // if not initial buy, remove excess buy signals
{
Buy[i] = 0;
}
if(OpenPos == 1)
{
if(Sellfilter[i]==0) // if in trade and sellfilter is off
{
trailstop = 0; // reset trailstop
}
else // sellfilter must be on
{
if(trailstop == 0) // in trade, sell filter on but SL initially 0
{
trailstop = Low[i] * stoplevel; // set SL to % below low
trailARRAY[i] = trailstop;
}
else // in trade, sellfilter on, trailstop previously set
{
if(Close[i] < trailstop) // if close lower than stop - exit trade
{
Sell[i] = 1; //exit trade
SellPrice = trailstop; //sell on open
OpenPos = 0; //not in trade anymore
trailstop = 0; //reset SL
}
else // in trade, sellfilter on, trailstop previously set, still in trade
{
trailstop = Max(trailstop, Low[i] * stoplevel); // SL is higher of the two - never lower
trailARRAY[i] = trailstop;
}
}
}
}
}
Plot(LW, "LowestValue", colorRed, styleLine, 0, 0, 0, 0);
Plot(HI, "HighestValue", colorGreen, styleLine, 0, 0, 0, 0);
//Plot stop loss - stop loss applied using (max price - xx%)
Plot(trailARRAY, "stoploss", colorWhite, styleLine, 0, 0, 0, 0);
PlotShapes(Buy*shapeUpArrow, colorGreen, 0, Close);
PlotShapes(Sell*shapeDownArrow, colorRed, 0, Close);
//'New Yearly Highs'
//Setup
// version 1.1, edited by trash
InitEquity = 100000;
MaxPos = 20;
Tradesize = 5000;
tradedelay = 0;
if ( tradedelay > 0 )
{
array = Open;
flag = 1;
}
else
{
array = Close;
flag = 0;
}
SetOption( "InitialEquity", InitEquity );
SetOption( "MaxOpenPositions", MaxPos );
SetPositionSize( 5, spsPercentOfEquity );
//SetPositionSize(5000, spsValue);
SetTradeDelays( tradedelay, tradedelay, tradedelay, tradedelay );
SetOption( "CommissionMode", 2 );
SetOption( "CommissionAmount", 30 );
SetOption( "UsePrevBarEquityForPosSizing", True );
//Filter
SetForeign( "XAO", True );
MAfilter = MA( C, 75 );
Buyfilter = Close > MAfilter;
Sellfilter = Close < MAfilter;
RestorePriceArrays();
//Rules
HiLevel = Param( "HiLevel", 100, 10, 500, 5 );
LoLevel = Param( "LoLevel", 100, 10, 500, 5 );
HI = HHV( C, HiLevel );
LW = LLV( C, LoLevel );
DollarTurnover = MA( C * V, 7 );
dollarTurnoverOK = dollarTurnover > 500000;
Liquidity = MA( V, 7 );
LiquidityOK = Liquidity > 500000;
OptMax = 20; //Optimize("MaxPrice", 20, 5, 30, 5);
MaxSP = Close < OptMax;
Buy = C >= Ref( HI, -1 ) AND LiquidityOK AND dollarTurnoverOK AND Buyfilter AND MaxSP;
Sellcond = C <= Ref( LW, -1 ) /*OR Sellfilter*/;
BuyPrice = SellPrice = array;
Short = Cover = Sell = 0;
//If Filter on, tighten SL to xx%
stoppc = Param( "Stop %", 2, 0, 50, 2 );
stoplevel = ( 100 - stoppc ) / 100;
trailstop = 0;
trailARRAY = Null;
for ( i = 1; i < BarCount; i++ )
{
if ( trailstop == 0 AND Buy[i] )
{
trailstop = L[ i ] * stoplevel;
}
else
{
Buy[i] = 0; // remove excess buy signals
}
if ( Sellcond[ i ] )
{
Sell[ i ] = 1;
trailstop = 0;
}
if ( trailstop > 0 AND Low[ i ] < trailstop )
{
Sell[ i ] = 1;
SellPrice[ i ] = trailstop;
trailstop = 0;
}
if ( trailstop > 0 )
{
trailstop = Max( L[ i ] * stoplevel, trailstop );
trailARRAY[ i ] = trailstop;
}
}
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
// channel plot
Plot(LW, "LowestValue", colorRed, styleLine, 0, 0, 0, 0);
Plot(HI, "HighestValue", colorGreen, styleLine, 0, 0, 0, 0);
//Plot stop loss - stop loss applied using (max price - xx%)
Plot( trailarray, "stoploss", colorWhite, styleLine, 0, 0, 0, 0 );
/// plotting shapes ::::::::::::::::::::::::::::::::::::::::::::::::::::
Buycond = Ref( Buy, -flag );
Sellcond = Ref( Sell, -flag );
Buycolor = colorGreen;
Sellcolor = colorOrange;
Colorborder = colorLightGrey;
PlotShapes( Buy*shapeSmallUpTriangle, Buycolor, 0, L, -15 );
PlotShapes( Buy*shapeHollowSmallUpTriangle, Colorborder, 0, L, -15 );
PlotShapes( Buycond*shapeHollowSmallCircle, Buycolor, 0, BuyPrice, 0 );
PlotShapes( Sell*shapeDownArrow, Sellcolor, 0, H, -15 );
PlotShapes( Sell*shapeHollowDownArrow, Colorborder, 0, H, -15 );
PlotShapes( Sellcond*shapeHollowSmallCircle, Sellcolor, 0, SellPrice, 0 );
I suppose something similar to this is what you are after.
Code://'New Yearly Highs' //Setup // version 1.1, edited by trash InitEquity = 100000; MaxPos = 20; Tradesize = 5000; tradedelay = 0; if ( tradedelay > 0 ) { array = Open; flag = 1; } else { array = Close; flag = 0; } SetOption( "InitialEquity", InitEquity ); SetOption( "MaxOpenPositions", MaxPos ); SetPositionSize( 5, spsPercentOfEquity ); //SetPositionSize(5000, spsValue); SetTradeDelays( tradedelay, tradedelay, tradedelay, tradedelay ); SetOption( "CommissionMode", 2 ); SetOption( "CommissionAmount", 30 ); SetOption( "UsePrevBarEquityForPosSizing", True ); //Filter SetForeign( "XAO", True ); MAfilter = MA( C, 75 ); Buyfilter = Close > MAfilter; Sellfilter = Close < MAfilter; RestorePriceArrays(); //Rules HiLevel = Param( "HiLevel", 100, 10, 500, 5 ); LoLevel = Param( "LoLevel", 100, 10, 500, 5 ); HI = HHV( C, HiLevel ); LW = LLV( C, LoLevel ); DollarTurnover = MA( C * V, 7 ); dollarTurnoverOK = dollarTurnover > 500000; Liquidity = MA( V, 7 ); LiquidityOK = Liquidity > 500000; OptMax = 20; //Optimize("MaxPrice", 20, 5, 30, 5); MaxSP = Close < OptMax; Buy = C >= Ref( HI, -1 ) AND LiquidityOK AND dollarTurnoverOK AND Buyfilter AND MaxSP; Sellcond = C <= Ref( LW, -1 ) /*OR Sellfilter*/; BuyPrice = SellPrice = array; Short = Cover = Sell = 0; //If Filter on, tighten SL to xx% stoppc = Param( "Stop %", 2, 0, 50, 2 ); stoplevel = ( 100 - stoppc ) / 100; trailstop = 0; trailARRAY = Null; for ( i = 1; i < BarCount; i++ ) { if ( trailstop == 0 AND Buy[i] ) { trailstop = L[ i ] * stoplevel; } else { Buy[i] = 0; // remove excess buy signals } if ( Sellcond[ i ] ) { Sell[ i ] = 1; trailstop = 0; } if ( trailstop > 0 AND Low[ i ] < trailstop ) { Sell[ i ] = 1; SellPrice[ i ] = trailstop; trailstop = 0; } if ( trailstop > 0 ) { trailstop = Max( L[ i ] * stoplevel, trailstop ); trailARRAY[ i ] = trailstop; } } Buy = ExRem( Buy, Sell ); Sell = ExRem( Sell, Buy ); // channel plot Plot(LW, "LowestValue", colorRed, styleLine, 0, 0, 0, 0); Plot(HI, "HighestValue", colorGreen, styleLine, 0, 0, 0, 0); //Plot stop loss - stop loss applied using (max price - xx%) Plot( trailarray, "stoploss", colorWhite, styleLine, 0, 0, 0, 0 ); /// plotting shapes :::::::::::::::::::::::::::::::::::::::::::::::::::: Buycond = Ref( Buy, -flag ); Sellcond = Ref( Sell, -flag ); Buycolor = colorGreen; Sellcolor = colorOrange; Colorborder = colorLightGrey; PlotShapes( Buy*shapeSmallUpTriangle, Buycolor, 0, L, -15 ); PlotShapes( Buy*shapeHollowSmallUpTriangle, Colorborder, 0, L, -15 ); PlotShapes( Buycond*shapeHollowSmallCircle, Buycolor, 0, BuyPrice, 0 ); PlotShapes( Sell*shapeDownArrow, Sellcolor, 0, H, -15 ); PlotShapes( Sell*shapeHollowDownArrow, Colorborder, 0, H, -15 ); PlotShapes( Sellcond*shapeHollowSmallCircle, Sellcolor, 0, SellPrice, 0 );
Without testing but then change line
if ( trailstop > 0 AND Low[ i ] < trailstop )
to
if ( trailstop > 0 AND Low[ i ] < trailstop AND Sellfilter )
I does work for me (with my recent edited code)!! Chart results and backtests results are in line here! You should take care of time frame and params in backtester because they are independent! So rather use hard coded params to not get confused.
Here is proof that results are in line with each other (chart and backtest list). I'm using different symbols and time frame but that doesn't matter.
Results with taking additional Sellfilter of foreign symbol into account.
Click each picture to enlarge.
In the picture it does not exit at any trail stop because sell filter is not true together with it yet.
So it exits on the lower red channel sell condition every time here on that day. Sell filter is in line with that exit by accident so it has no influence on that other sell condition.
Here Results without taking any additional Sellfilter of a foreign symbol into account.
Here is a picture where SellFilter and trailstop are true so it exits on trail stop.
And if removing SellFilter then it is two trades.
Sorry, but my help ends here on this issue. Can't waste endless time on this since example is working. Wait for others if you don't get it done yourself.
Thanks anyway Trash. I really appreciate the amount of effort you've put in.
This below will show you that back testing results can be dramatically opposing to real time results. Additionally, simply participating in the market changes the outcome where as in a back test you are the 'invisible' trader trading unreal positions.Hi Guys,
I am 2-weeks new to Amibroker and I find this software is great! Powerful backtesting
Could anyone point me to any good AFL design of Amibroker Automated Trading ? I am trying to code a fully automated afl and could have benefit from any good template.
Thanks!
Hi Guys,
I am 2-weeks new to Amibroker and I find this software is great! Powerful backtesting
Could anyone point me to any good AFL design of Amibroker Automated Trading ? I am trying to code a fully automated afl and could have benefit from any good template.
I have read thru the official document but it only provide simple example of each function.
www.amibroker.com/at/
Thanks!
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?