/*[[ Name := TripleScreenDaily Author := Barcode Link := Notes := Based on Alexander Elder's Triple Screen system Lots := 1.00 Stop Loss := 0 Take Profit := 999 Trailing Stop := 10 ]]*/ ///////////////////////////////////////////////////// // Defines ///////////////////////////////////////////////////// Defines: Slippage(5); Defines: StopYear(1990),WilliamsP(24),WilliamsL(-75),WilliamsH(-25); Defines: TodayD(20), TodayM(05), TodayY(2005); Defines: MM(0),Leverage(1),MarginChoke(500); ///////////////////////////////////////////////////// // Variables ///////////////////////////////////////////////////// var: PriceOpen(0); // Price Open var: I(0); // Misc Counter Var: NewBar(0),F1(0),First(True),D1(0),M1(0),Y1(0),LotMM(0),LotSize(10000),LotMax(50); Var: NewTime(0), NewDay(0), NewMth(0), NewYr(0), WeekDay(0), WeekMth(0), WeekYr(0); Var: Trash(0), WeekTime(0), PrevPos(0), PosInFile(0), BackAmt(0), d(0), FirstDate(True); Var: Direction(""), PosNeg(""), Zero(0), ProcessDay(0), LastDay(0), Back2(0); Var: WilliamsBuy(0), WilliamsSell(0), NewPrice(0), FileName(""); Var: buy_tp(0),sell_tp(0); ///////////////////////////////////////////////// // Initialisation ///////////////////////////////////////////////// if year < stopyear then exit; If Curtime - LastTradeTime < 10 then Exit; // The following statement stops the expert working after you attach it to a chart and before you press // Start to begin backtesting. Before you press start the CurTime (day, month & year) are todays date so // set the define statements to today to stop the expert processing. As soon as you press Start, CurTime // becomes the date of the first bar to be processed. This is not normally a problem except when you are // reading a file in the 'First Time' routine and you don't want this to happen until Start is pressed. if day = TodayD and month = TodayM and year = TodayY then exit; ///////////////////////////////////////////////////// // First Time and Once only processing ///////////////////////////////////////////////////// if First then { first = False; FileName = "TripleScreenWky"+Symbol+".csv"; F1 = FileOpen(Filename, ","); While WeekTime < CurTime Begin // Read weekly file until date is > or = to current bar date Back2 = Prevpos; // Save this position in file so we can go back to here PrevPos = FileTell(F1); // WeekDay = FileReadNumber(F1); WeekMth = FileReadNumber(F1); WeekYr = FileReadNumber(F1); WeekTime = FileReadNumber(F1); Direction = FileReadString(F1); PosNeg = FileReadString(F1); // print("1 curtime=",curtime," weektime=",weektime," prevpos=",prevpos); End; // If weekly date is > current date we need to go back to the previous weekly date as it is the // correct one for this current bar. If WeekTime > CurTime Then { PosInFile = FileTell(F1); BackAmt = Zero - (PosInFile - Back2); // Subtract from zero because we need BackAmt as a negative FileSeek(F1,BackAmt,Seek_Cur); // Go back 1 record (previous set of numbers) WeekDay = FileReadNumber(F1); WeekMth = FileReadNumber(F1); WeekYr = FileReadNumber(F1); WeekTime = FileReadNumber(F1); Direction = FileReadString(F1); PosNeg = FileReadString(F1); // print("Gone back one ",backamt); }; // print("2 ",day," ",month," ",year," ",weekday," ",weekmth," ",weekyr," "," WeekTime=",Weektime); }; ///////////////////////////////////////////////// // Get the next weekly bar details ///////////////////////////////////////////////// If NewBar != t[0] Then { // print("Day=",day," DayOfWeek=",dayofweek); NewBar = t[0]; ProcessDay = DayOfWeek; If LastDay = 6 and ProcessDay != 2 then ProcessDay = 2; // This is done in case a Monday is missing If ProcessDay = 2 then // Get the next Weekly History record as its a Monday { If FirstDate then { FirstDate = false; } else { WeekDay = FileReadNumber(F1); WeekMth = FileReadNumber(F1); WeekYr = FileReadNumber(F1); WeekTime = FileReadNumber(F1); Direction = FileReadString(F1); PosNeg = FileReadString(F1); }; }; LastDay = DayOfWeek; While Curtime - WeekTime > 346000 Begin // 345600 is 4 days. This is in here in case there // aaa=curtime-weektime; // is a week or more missing from the daily data. So // print("curtime-weektime=",aaa); // we need to read the weekly file until we get the WeekDay = FileReadNumber(F1); // right data. WeekMth = FileReadNumber(F1); WeekYr = FileReadNumber(F1); WeekTime = FileReadNumber(F1); Direction = FileReadString(F1); PosNeg = FileReadString(F1); If IsFileEnded(F1) Then { // Print("At end of Weekly File"); Exit; }; End; // print(day," ",month," ",year," ",weekday," ",weekmth," ",weekyr," "," WeekTime=",Weektime); ///////////////////////////////////////////////// // New positions management ///////////////////////////////////////////////// If TotalTrades < 1 Then { ///////////////////////////////////////////////// // Lot Management ///////////////////////////////////////////////// If MM < -1 then { If FreeMargin < 5 then Exit; LotMM = Floor(Balance*Leverage/1000); if LotMM < 1 then LotMM = 1; LotMM = LotMM/100; If LotMM > LotMax then LotMM = LotMax; }; If MM = -1 then { If FreeMargin < 50 then Exit; LotMM = Floor(Balance*Leverage/10000); if LotMM < 1 then LotMM = 1; LotMM = LotMM/10; If LotMM > LotMax then LotMM = LotMax; }; If MM = 0 then { If FreeMargin < MarginChoke then Exit; LotMM = lots; }; if MM > 0 then { If FreeMargin < 500 then Exit; LotMM = Floor(Balance*Leverage/100000); if LotMM < 1 then LotMM = 1; If LotMM > LotMax then LotMM = LotMax; }; // Print("High1=",high[1]," Low1=",low[1]); WilliamsSell = iWPR(WilliamsP,1) > WilliamsL; WilliamsBuy = iWPR(WilliamsP,1) < WilliamsH; If Direction = "U" and WilliamsBuy then { PriceOpen = High[1] + 1 * Point; // Buy 1 point above high of previous candle If PriceOpen > (Ask + 16 * Point) Then // Check if buy price is a least 16 points > Ask { if TakeProfit > 0 then buy_tp = PriceOpen + TakeProfit * Point else buy_tp = 0; SetOrder(OP_BUYSTOP, LotMM, PriceOpen, Slippage, Low[1] - 1 * Point, buy_tp, BLUE); // print("Buy1"," Priceopen=",priceopen," ",priceopen-stoploss*point," ",priceopen+takeprofit*point); Exit; } Else { NewPrice = Ask + 16 * Point; if TakeProfit > 0 then buy_tp = NewPrice + TakeProfit * Point else buy_tp = 0; SetOrder(OP_BUYSTOP, LotMM, NewPrice, Slippage, Low[1] - 1 * Point, buy_tp, BLUE); // print("Buy2",lots," ",newprice," ",slippage," ",newprice-stoploss*point," ",newprice+takeprofit*point); Exit; }; }; If Direction = "D" and WilliamsSell then { PriceOpen = Low[1] - 1 * Point; If PriceOpen < (Bid - 16 * Point) Then // Check if buy price is a least 16 points < Bid { if TakeProfit > 0 then sell_tp = PriceOpen - TakeProfit * Point else sell_tp = 0; SetOrder(OP_SELLSTOP, LotMM, PriceOpen, Slippage, High[1] + 1 * Point, sell_tp, RED); // print("Sell1",lots," ",Priceopen," ",slippage," ",priceopen+stoploss*point," ",priceopen-takeprofit*point); Exit; } Else { NewPrice = Bid - 16 * Point; if TakeProfit > 0 then sell_tp = NewPrice - TakeProfit * Point else sell_tp = 0; SetOrder(OP_SELLSTOP, LotMM, NewPrice, Slippage, High[1] + 1 * Point, sell_tp, RED); // print("Sell2",lots," ",newprice," ",slippage," ",newprice+stoploss*point," ",newprice-takeprofit*point); Exit; }; }; }; ///////////////////////////////////////////////// // Pending Order Management ///////////////////////////////////////////////// If TotalTrades > 0 Then { if Ord(1,VAL_TYPE) = OP_BUYSTOP then { if Direction = "D" then { // print("Delete1"); DeleteOrder(OrderValue(1,VAL_TICKET),Red); Exit; }; }; if Ord(1,VAL_TYPE) = OP_SELLSTOP then { if Direction = "U" then { // print("Delete2"); DeleteOrder(OrderValue(1,VAL_TICKET),Red); Exit; }; }; If Ord(1,VAL_TYPE) = OP_BUYSTOP Then { If High[1] < High[2] Then { If High[1] > (Ask + 16 * Point) Then { // print("Modify1"); if TakeProfit > 0 then buy_tp = High[1] + (TakeProfit + 1) * Point else buy_tp = 0; ModifyOrder(Ord(1,VAL_TICKET), High[1] + 1 * Point, Low[1] - 1 * Point, buy_tp, White); // print("PB1 ",Ord(1,VAL_TICKET)," H1=",high[1]," L1=",low[1]); Exit; } Else { // print("Modify2"); if TakeProfit > 0 then buy_tp = Ask + (TakeProfit + 16) * Point else buy_tp = 0; ModifyOrder(Ord(1,VAL_TICKET), Ask + 16 * Point, Low[1] - 1 * Point, buy_tp, White); // print("PB2 ",Ord(1,VAL_TICKET)," C0=",close[0]," L1=",low[1]); Exit; }; }; }; If Ord(1,VAL_TYPE) = OP_SELLSTOP Then { If Low[1] > Low[2] Then { If Low[1] < (Bid - 16 * Point) Then { // print("Modify3"); if TakeProfit > 0 then sell_tp = Low[1] - (TakeProfit - 1) * Point else sell_tp = 0; ModifyOrder(Ord(1,VAL_TICKET), Low[1] - 1 * Point, High[1] + 1 * Point, sell_tp, Gold); // print("PS1 ",Ord(1,VAL_TICKET)," H1=",high[1]," L1=",low[1]); Exit; } Else { // print("Modify4"); if TakeProfit > 0 then sell_tp = Bid - (TakeProfit - 16) * Point else sell_tp = 0; ModifyOrder(Ord(1,VAL_TICKET), Bid - 16 * Point, High[1] + 1 * Point, sell_tp, Gold); // print("PS2 ",Ord(1,VAL_TICKET)," C0=",close[0]," H1=",high[1]); Exit; }; }; }; }; }; ///////////////////////////////////////////////// // Stop Loss Management ///////////////////////////////////////////////// For i=1 to TotalTrades { If Ord(i,VAL_SYMBOL) = Symbol and Ord(i,VAL_TYPE) = OP_BUY and TrailingStop > 0 Then { If (Bid-Ord(i,VAL_OPENPRICE)) > (TrailingStop * Point) Then { If Ord(i,VAL_STOPLOSS) < (Bid - TrailingStop * Point) Then { // print("ModifyA1"); if TakeProfit > 0 then buy_tp = Bid + TakeProfit * Point else buy_tp = 0; ModifyOrder(Ord(i,VAL_TICKET), Ord(i,VAL_OPENPRICE), Bid - TrailingStop * Point, buy_tp, White); // print("S/LB ",Ord(i,VAL_TICKET)," Bid=",bid," Open=",Ord(i,VAL_OPENPRICE)," TS=",TrailingStop); Exit; }; }; }; If Ord(i,VAL_SYMBOL) = Symbol and Ord(i,VAL_TYPE) = OP_SELL and TrailingStop > 0 Then { If (Ord(i,VAL_OPENPRICE) - Ask) > (TrailingStop * Point) Then { If Ord(i,VAL_STOPLOSS) > (Ask + TrailingStop * Point) or OrderValue(i,VAL_STOPLOSS) = 0 Then { // print("ModifyA2"); if TakeProfit > 0 then sell_tp = Ask - TakeProfit * Point else sell_tp = 0; ModifyOrder(Ord(i,VAL_TICKET), Ord(i,VAL_OPENPRICE), Ask + TrailingStop * Point, sell_tp, Gold); Exit; }; }; }; };