[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Newbee System Programming - Need some Help...



PureBytes Links

Trading Reference Links

/* For all of you who are more expert than I am I have finally
completed my first new system... However, it only selects 1 stock
with 100% winning ratio... The thing is, I am puzzeled because when
I look manually at a stock chart, I see these "requirements" being
met several times over. I expect many buy and sell and short and 
cover signals, but that isn't what I am getting. If someone would be
kind enough to run this code, look at it, and perhaps help me
understand why it is not working as expected, I would appreciate the
help! thank you! */




Price = (High+Low+(4*(Open+Close)))/6;


SetForeign("^RUT");

	TimeFrameSet(inWeekly);
		MarketWeekRSI = RSI(14);
		MarketWeekMACD = MACD();
		MarketWeekSignal = Signal();
		MarketWeekEMAOpen = EMA(Open,18);
		MarketWeekEMAClose = EMA(Close,14);
	TimeFrameRestore();

	TimeFrameSet(inMonthly);
		MarketMonthRSI = RSI(7);
		MarketMonthMACD = MACD();
		MarketMonthSignal = Signal();
		MarketMonthEMAOpen = EMA(Open,10);
		MarketMonthEMAClose = EMA (Close,7);
	TimeFrameRestore();

	MarketDailyRSI = RSI(21);
	MarketDailyMACD = MACD();
	MarketDailySignal = Signal();
	MarketDailyEMAOpen = EMA(Open,21);
	MarketDailyEMAClose = EMA (Close,20);

RestorePriceArrays(); 

TimeFrameSet(inWeekly);
	WeekRSI = RSI(14);
	WeekMACD = MACD();
	WeekSignal = Signal();
	WeekEMAOpen = EMA(Open,18);
	WeekEMAClose = EMA(Close,14);
TimeFrameRestore();

TimeFrameSet(inMonthly);
	MonthRSI = RSI(7);
	MonthMACD = MACD();
	MonthSignal = Signal();
	MonthEMAOpen = EMA(Open,10);
	MonthEMAClose = EMA (Close,7);
TimeFrameRestore();

Buy = 

	Close > Param("Minimum Stock Price 
(Long)",5.00,0.01,500,0.01)
	AND Cross(EMA(Close,20),EMA(Open,21))
	AND Cross(TimeFrameExpand
(WeekEMAClose,inWeekly),TimeFrameExpand(WeekEMAOpen,inWeekly))
	//AND Cross(TimeFrameExpand
(MonthEMAClose,inMonthly),TimeFrameExpand(MonthEMAOpen,inMonthly))
	AND Cross(TimeFrameExpand
(MarketWeekEMAClose,inWeekly),TimeFrameExpand
(MarketWeekEMAOpen,inWeekly))
	//AND Cross(TimeFrameExpand
(MarketMonthEMAClose,inMonthly),TimeFrameExpand
(MarketMonthEMAOpen,inMonthly))
	AND Cross(Close,MA(Low,2))
	AND Cross(RSI(21),50)
	AND Cross(TimeFrameExpand(WeekRSI ,inWeekly),50)
	//AND Cross(TimeFrameExpand(MonthRSI,inMonthly),50)
	//AND Cross(MACD(),Signal())
	AND Cross(TimeFrameExpand(WeekMACD,inWeekly),TimeFrameExpand
(WeekSignal,inWeekly))
	//AND Cross(TimeFrameExpand
(MonthMACD,inMonthly),TimeFrameExpand(MonthSignal,inMonthly))
	AND Cross(TimeFrameExpand(MarketWeekRSI,inWeekly),50)
	//AND Cross(TimeFrameExpand(MarketMonthRSI,inMonthly),50)
	//AND Cross(TimeFrameExpand
(MarketWeekMACD,inWeekly),TimeFrameExpand(MarketWeekSignal,inWeekly))
	//AND Cross(TimeFrameExpand
(MarketMonthMACD,inMonthly),TimeFrameExpand
(MarketMonthSignal,inMonthly))
;
	

BuyPrice = Max( Price, Low ); 

Sell = 

	Close > Param("Minimum Stock Price (Short)",5.00,5,500,0.01)
	AND Cross(EMA(Open,21),EMA(Close,20))
	AND Cross(TimeFrameExpand
(WeekEMAOpen,inWeekly),TimeFrameExpand(WeekEMAClose,inWeekly))
	//AND Cross(TimeFrameExpand
(MonthEMAOpen,inMonthly),TimeFrameExpand(MonthEMAClose,inMonthly))
	AND Cross(TimeFrameExpand
(MarketWeekEMAOpen,inWeekly),TimeFrameExpand
(MarketWeekEMAClose,inWeekly))
	//AND Cross(TimeFrameExpand
(MarketMonthEMAOpen,inMonthly),TimeFrameExpand
(MarketMonthEMAClose,inMonthly))
	AND Cross(MA(High,2),Close)
	AND Cross(50,RSI(21))
	AND Cross(50,TimeFrameExpand(WeekRSI ,inWeekly))
	//AND Cross(50,TimeFrameExpand(MonthRSI,inMonthly))
	//AND Cross(Signal(),MACD())
	AND Cross(TimeFrameExpand
(WeekSignal,inWeekly),TimeFrameExpand(WeekMACD,inWeekly))
	//AND Cross(TimeFrameExpand
(MonthSignal,inMonthly),TimeFrameExpand(MonthMACD,inMonthly))
	AND Cross(50,TimeFrameExpand(MarketWeekRSI,inWeekly))
	//AND Cross(50,TimeFrameExpand(MarketMonthRSI,inMonthly))
	//AND Cross(TimeFrameExpand
(MarketWeekSignal,inWeekly),TimeFrameExpand(MarketWeekMACD,inWeekly))
	//AND Cross(TimeFrameExpand
(MarketMonthSignal,inMonthly),TimeFrameExpand
(MarketMonthMACD,inMonthly))
;

SellPrice = Min( Price, High ); 

Short = Sell;
ShortPrice = SellPrice;

Cover = Buy;
CoverPrice = BuyPrice;

e = Equity();

RoundLotSize = Param("Round Lot Size",1,1,100);
TickSize = Param("Tick Size",.01,.0001,1);

Capital = Param("Starting Capital",10000,1000,1000000);
/* IMPORTANT: Set it also in the Settings: Initial Equity */
CapitalRiskPct = 0.01;
/* Assumes Risking No More Than 0.5% of Capital*/
Risk = Param("% of Capital To Risk",CapitalRiskPct,0.001,0.03,0.001)
*Capital;

ProfitTargetRatio=Param("Profit Target Ratio",0.618,0.05,10,0.001);

/*Initialize Variables*/
TrailStopAmount = 0;
ProfitTarget = 0;
StopLoss = SAR();

for( i = 0; i < BarCount; i++ ) 
{ 
  if( Buy[i] == 1)
	{
		ProfitTarget[i] = abs(BuyPrice[i] - (BuyPrice[i] * 
ProfitTargetRatio[i]));
		TrailStopAmount[i] = abs(Price[i] - StopLoss[i]);
		PositionSize[i] = (Risk[i]/TrailStopAmount[i])
*BuyPrice[i];
	}
  else 
	if (Short[i] == 1)
	 {
			ProfitTarget[i] = abs(ShortPrice[i] - 
(ShortPrice[i] * ProfitTargetRatio[i]));
			TrailStopAmount[i] = abs(Price[i] - StopLoss
[i]);
			PositionSize[i] = (Risk[i]/TrailStopAmount
[i])*ShortPrice[i];
	 }
 	else if (Sell[i] == 1 OR Cover[i] == 1)
   			TrailStopAmount[i] = 0;
}


exitAtStop = Param("Apply Stop Exit At Stop", 1,0,1);
Volatile = Param("Apply Stop Volatile", 1,0,1);
ReEntryDelay = Param("Reentry Delay", 0,0,360);
PctRisk=Param("Max Loss %",10,1,20,0.5);

ProfitStop = IIf(ProfitTarget < Low, Low, IIf(ProfitTarget > High, 
High, ProfitTarget));

ApplyStop( stopTypeProfit, stopModePoint, ProfitStop, 
exitAtStop ,Volatile,ReEntryDelay);
ApplyStop( stopTypeTrailing, stopModePoint, 
TrailStopAmount,exitAtStop ,Volatile,ReEntryDelay);



Filter = Buy OR Sell OR Short OR Cover;
	AddColumn(DateTime(), "Date / Time", formatDateTime );
	AddColumn(Close,"Close");
	AddColumn(Buy,"Buy");
	AddColumn(Sell,"Sell");
	AddColumn(Short,"Short");
	AddColumn(Cover,"Cover");
	AddColumn(Price,"Price");
	AddColumn(SAR(),"SAR()");
	AddColumn(TrailStopAmount,"Trail Stop Amount");
	AddColumn(ProfitTarget,"Profit Target");
	AddColumn(PositionSize,"Position Size");
	AddColumn(BuyPrice,"Buy Price");
	AddColumn(MA(Volume,40),"Avg. Volume (40)");
	AddColumn(MA(Volume,5),"Avg. Volume (5)");
	AddColumn(Volume,"Volume");
	AddColumn(RSI(7),"RSI (7)");
	AddColumn(RSI(14),"RSI(14)");
	AddColumn(RSI(21),"RSI(21)");
	AddColumn( TimeFrameExpand(WeekRSI ,inWeekly), "weekly RSI 
14");
	AddColumn( TimeFrameExpand(MonthRSI ,inMonthly), "monthly 
RSI 7");
	AddColumn(MACD(),"MACD()");
	AddColumn(Signal(),"Signal()");
	AddColumn(Open,"Open");
	AddColumn(High,"High");
	AddColumn(Low,"Low");

/*