// Downloaded From https://www.WiseStockTrader.com
 /* 
 Author: www.Think-Algo.com 
         www.facebook.com/ThinkAlgo
          
 Date: May 2016
             
 *** This program is for illustrative purposes only and
  not construed as specific advisory recommendations.
 *** It is not guaranteed to be free of errors. 
 *** Trades placed from this algorithm are taken at your     
 own risk for your own account. 
 *** Past performance is not necessarily indicative of     
 future results. 
 */  
 
 // Initial Capital  SetOption("InitialEquity", 1000000 ); // Commission is set to 0.1% SetOption("CommissionMode",1); 
 SetOption("CommissionAmount",0.1);
   
 // Buy/Sell at ATO one day after the signals are triggered SetTradeDelays(1,1,0,0);   
 RoundLotSize = 100; 
 BuyPrice = Open; 
 SellPrice = Open;  
  // Maximum position sizes  
  MaxPos = 30; 
 SetOption("MaxOpenPositions", MaxPos); 
 // Lookback periods   
 period1 = 20; 
 period2 = 60;  
 // Two Moving Averages  
 EMA_short = EMA(Close,period1); 
 EMA_long = EMA(Close,period2);
 //********** Conditions to BUY *************  
 // Liquidity is larger than 10 MB per day (on average) 
 Liquid = MA(C*V,period2) > 10e6; 
 // The short EMA crosses the longer EMA up in the past 3 days 
 BuyCon1 = BarsSince( Cross(EMA_short,EMA_long)) <= 3; 
 // ADX is trending up with +DMI > -DMI 
 BuyCon2 = ADX()> EMA(ADX(),period2) AND PDI() > MDI();  
 // The market is in a good condition trade 
 SetForeign("SET",True);     
 SETBuyCon =  RSI()>EMA(RSI(),period1) ;
 RestorePriceArrays();   
 
 Buy = Liquid AND BuyCon1 AND BuyCon2 AND SETBuyCon;    
 
 //********** Conditions to SELL *************  
 // The short EMA crosses the longer EMA down 
 SellCon1 = Cross(EMA_long,EMA_short);  
 // -DMI > +DMI indicates down trend 
 SellCon2 = PDI() < MDI();   
 
 Sell= SellCon1 OR SellCon2 ;//OR SellConMKC;  // Use ExRem to remove excessive Buy/Sell signals Buy = ExRem(Buy,Sell); 
 Sell = ExRem(Sell,Buy);    
 
 // ********  Position Size and Position Score ****  // Invest equally in each stock PositionSize = -100/MaxPos;    
 // If there are more than one buy signals at a given time, 
 // the priority of the Buy is based on the ranking from 
 // position score. Here, our criterion is the ratio of  
 // today's value with respect to the average past value PositionScore = C*V/MA(C*V,period2);
 
 // Stop Loss *************************************** 
 // Exit next day if the loss is larger than 10 % ApplyStop(stopTypeLoss,stopModePercent,10,2);
  Plot(C,"Test Think Algo system",colorDefault,styleCandle); 
PlotShapes(shapeUpArrow*Buy,ParamColor("UpArrow",10),0,L); 
PlotShapes(shapeDownArrow*Sell,ParamColor("DownArrow",11),0,H);

_SECTION_BEGIN("Automatic  Trend-line");

if (ParamToggle("Automatic Trend-line","Off|On",1))
{
////////  Created by Tomasz Janeczko & Modify By Chaiset setindex ///////
periodT = Param("period",50,1,250,1);
ColorLine = ParamColor("Color Trend-line",colorRed);
StyleTrend = ParamStyle("Style Trend line",styleLine,maskDefault);

x = Cum(1);

perchg = 0.3*LastValue( Highest( ROC( Low, periodT ) ));

startvalue = LastValue( Trough( Low, perchg, 1 ) );
endvalue1 = LastValue( Trough( Low, perchg, 2 ) );

startbar = LastValue( ValueWhen( Low == startvalue, x, 1 ) );
endbar = LastValue( ValueWhen( Low == endvalue1, x, 1 ) );

Aa = (endvalue1-startvalue)/(endbar-startbar);
b = startvalue;

trendline = Aa * ( x  - startbar ) + b;

Plot( IIf( x >= endbar, trendline, Null ), "Trendline", ColorLine,StyleTrend  );
}
_SECTION_END();