/*[[ Name := TripleScreenDaily Author := Barcode Link := Notes := Based on Alexander Elder's Triple Screen system Lots := 1.00 Stop Loss := 50 Take Profit := 150 Trailing Stop := 45 ]]*/ ///////////////////////////////////////////////////// // Defines ///////////////////////////////////////////////////// Defines: Slippage(5),Force(2); // Slippage Defines: StopYear(2004); Defines: TodayD(11), TodayM(04), TodayY(2005); Defines: MM(0),Leverage(1),AcctSize(10000); ///////////////////////////////////////////////////// // 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), aaa(0); Var: ForcePos(0), ForceNeg(0), NewPrice(0), soldit(false), FlipFlop(False), FileName(""); ///////////////////////////////////////////////// // Initialisation ///////////////////////////////////////////////// if year < stopyear then exit; If Curtime - LastTradeTime < 10 then Exit; If FreeMargin < 500 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 < 0 then { LotMM = Round(Balance*Leverage/10000)/10; If LotMM > LotMax then LotMM = LotMax; }; If MM = 0 then LotMM = lots; if MM > 0 then { LotMM = Round(Balance*Leverage/10000)/10; If LotMM > 1 then LotMM = ceil(LotMM); if LotMM < 1 then LotMM = 1; If LotMM > LotMax then LotMM = LotMax; }; // Print("High1=",high[1]," Low1=",low[1]); ForcePos = iForce(Force,MODE_EMA,PRICE_CLOSE,1) > 0; ForceNeg = iForce(Force,MODE_EMA,PRICE_CLOSE,1) < 0; If Direction = "U" and ForceNeg 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 { SetOrder(OP_BUYSTOP, LotMM, PriceOpen, Slippage, Low[1] - 1 * Point, PriceOpen + TakeProfit * Point, BLUE); // print("Buy1"," Priceopen=",priceopen," ",priceopen-stoploss*point," ",priceopen+takeprofit*point); Exit; } Else { NewPrice = Ask + 16 * Point; SetOrder(OP_BUYSTOP, LotMM, NewPrice, Slippage, Low[1] - 1 * Point, NewPrice + TakeProfit * Point, BLUE); // print("Buy2",lots," ",newprice," ",slippage," ",newprice-stoploss*point," ",newprice+takeprofit*point); Exit; }; }; If Direction = "D" and ForcePos then { PriceOpen = Low[1] - 1 * Point; If PriceOpen < (Bid - 16 * Point) Then // Check if buy price is a least 16 points < Bid { SetOrder(OP_SELLSTOP, LotMM, PriceOpen, Slippage, High[1] + 1 * Point, PriceOpen - TakeProfit * Point, RED); // print("Sell1",lots," ",Priceopen," ",slippage," ",priceopen+stoploss*point," ",priceopen-takeprofit*point); // print("Low1=",low[0]," Bid=",bid); // soldit = true; Exit; } Else { NewPrice = Bid - 16 * Point; SetOrder(OP_SELLSTOP, LotMM, NewPrice, Slippage, High[1] + 1 * Point, NewPrice - TakeProfit * Point, 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"); ModifyOrder(Ord(1,VAL_TICKET), High[1] + 1 * Point, Low[1] - 1 * Point, High[1] + (TakeProfit + 1) * Point, White); // print("PB1 ",Ord(1,VAL_TICKET)," H1=",high[1]," L1=",low[1]); Exit; } Else { // print("Modify2"); ModifyOrder(Ord(1,VAL_TICKET), Ask + 16 * Point, Low[1] - 1 * Point, Ask + (TakeProfit + 16) * Point, 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"); ModifyOrder(Ord(1,VAL_TICKET), Low[1] - 1 * Point, High[1] + 1 * Point, Low[1] - (TakeProfit - 1) * Point, Gold); // print("PS1 ",Ord(1,VAL_TICKET)," H1=",high[1]," L1=",low[1]); Exit; } Else { // print("Modify4"); ModifyOrder(Ord(1,VAL_TICKET), Bid - 16 * Point, High[1] + 1 * Point, Bid - (TakeProfit - 16) * Point, Gold); // print("PS2 ",Ord(1,VAL_TICKET)," C0=",close[0]," H1=",high[1]); Exit; }; }; }; }; }; ///////////////////////////////////////////////// // Stop Loss Management ///////////////////////////////////////////////// For i=1 to TotalTrades { // if FlipFlop Then // // { // This is in here to try and detect a problem where the system seems to // FlipFlop = False; // be processing only about 100 modifies per trade. This causes it to // Exit; // modify only every second time thru. // }; // // FlipFlop = True; If Ord(i,VAL_TYPE) = OP_BUY Then { If (Ask-Ord(i,VAL_OPENPRICE)) > (TrailingStop * Point) Then { If Ord(i,VAL_STOPLOSS) < (Ask - TrailingStop * Point) Then { // print("ModifyA1"); ModifyOrder(Ord(i,VAL_TICKET), Ord(i,VAL_OPENPRICE), Ask - TrailingStop * Point, Ask + TakeProfit * Point, White); // if Ord(i,VAL_TICKET) = 7 or 27 then // { // print("Buy Close=",close[0]," Ask=",ask); // }; // print("S/LB ",Ord(i,VAL_TICKET)," Bid=",bid," Open=",Ord(i,VAL_OPENPRICE)," TS=",TrailingStop); Exit; }; }; }; If Ord(i,VAL_TYPE) = OP_SELL Then { If (Ord(i,VAL_OPENPRICE) - Bid) > (TrailingStop * Point) Then { If Ord(i,VAL_STOPLOSS) > (Bid + TrailingStop * Point) Then { // print("ModifyA2"); ModifyOrder(Ord(i,VAL_TICKET), Ord(i,VAL_OPENPRICE), Bid + TrailingStop * Point, Bid - TakeProfit * Point, Gold); // if Ord(i,VAL_TICKET) = 32 then // { // print("32 Sell Close=",close[0]," Bid=",bid); // }; // print("S/LS ",Ord(i,VAL_TICKET)," ASk=",ask," Open=",Ord(i,VAL_OPENPRICE)," TS=",TrailingStop); Exit; }; }; }; };