//+-------------------------------------------------------------------+ //| Made/Modified by Alejandro Galindo | //| | //| If this work/modification is helpful to you | //| send me a PayPal donation to ag@elcactus.com | //| any help is apreciated :) | //| Thanks. | //+-------------------------------------------------------------------+ /*[[ Name := Galindo Author := Copyright © 2005 Alejandro Galindo Lots := 1.00 Stop Loss := 35 Take Profit := 999 Trailing Stop := 25 ]]*/ Define: TimeZoneofData(0); // NY 7,15; Define: StartHour(0); Define: StartMinute(0); Define: EndHour(23); Define: EndMinute(55); Define: ClosePendingOrderHr(23); Define: ClosePendingOrderMin(59); Define: FridayEndHour(21); Define: Pips(1); Define: SecureProfit(10); Define: TrailSecureProfit(25); Define: DontTS(1); Define: Dont100(0); Define: Dont25(0); Define: DontReverse(0); Define: TradeFriday(1); // 0- dont open trades on friday, 1- Trade on friday Define: StartYear(2005); Define: StartMonth(1); Define: EndYear(2005); Define: EndMonth(8); Define: TradesSet(2); Define: TypeSet(0); // 0 = NONE, 1 = SELL, 2 = BUY Define: ReverseSet(0); // 0=False, 1=True Define: mm(1); Define: risk(12); var: Today(0), RefDate(0), LastBarOfDay(0), FirstBarOfDay(0), Loop(0); var: myH(0), myL(0), myC(0), myO(0); var: cnt(0); var: lotsi(0); var: OpenTrades(0), FilledOrders(0); var: Slippage(5); var: BuyPrice(0), SellPrice(0); var: TimetoOpen(0), TimetoClose(0), tmp(0), i(0); var: Trades(2), Type("NONE"), Reverse(False); var: mode(0); var: myEndHour(0), myFridayEndHour(0), myStartHour(0); Var: FirstTime(True); Var: text1(""), text2(""),text3(""), text4(""); If TimeYear(time)EndYear then Exit; If TimeMonth(Time)>EndMonth then Exit; if Curtime - LastTradeTime < 15 then exit; if Period>=1440 and TimeZoneOfData>0 then { alert("For TimeZoneOfData>0 you must run the script on a lower time frame. Not on Daily. I suggest M5"); Exit; }; if mm<>0 then lotsi=Ceil(Balance*risk/10000)/10 else lotsi=Lots; if lotsi>100 then lotsi=100; if (FirstTime) then { if (TradesSet<>2 OR TypeSet<>0 OR ReverseSet<>0) then { Trades = TradesSet; if TypeSet == 0 then Type = "NONE"; if TypeSet == 1 then Type = "SELL"; if TypeSet == 2 then Type = "BUY"; if ReverseSet ==0 then Reverse = False; if ReverseSet ==1 then Reverse = True; }; FirstTime=False; }; //print("day=",DayofWeek); Today=Floor(CurTime/86400)*86400; Cnt=1; If Today<>RefDate Then { // print("Today=",TimeToStr(Today)); RefDate=Today; While (Cnt<(Bars-1)) & (T[Cnt]-TimeZoneOfData*3600>=RefDate) { Cnt++; }; LastBarOfDay=Cnt; if DayofWeek==2 then RefDate=Today-86400*3 else RefDate=Today-86400; // print("RefDate=",TimeToStr(RefDate)); While (Cnt<(Bars-1)) & (T[Cnt]-TimeZoneOfData*3600>=RefDate) { Cnt++; }; FirstBarOfDay=Cnt-1; }; myH=High[highest(MODE_HIGH,FirstBarOfDay,FirstBarOfDay-LastBarOfDay+1)]; myL=Low[Lowest(MODE_LOW,FirstBarOfDay,FirstBarOfDay-LastBarOfDay+1)]; myC=Close[LastBarOfDay]; BuyPrice=myH+Pips*Point; SellPrice=myL-Pips*Point; if (!IsTesting) then { MoveObject("BuyPrice",OBJ_HLINE,time,BuyPrice,time,BuyPrice,BLUE,1,STYLE_dot); MoveObject("SellPrice",OBJ_HLINE,time,SellPrice,time,SellPrice,RED,1,STYLE_dot); }; text1="BuyPrice="+BuyPrice+"\nSellPrice="+SellPrice+"\nTrades="+Trades+", Type="+Type+", Reverse="+Reverse+"\nTime="+TimeToStr(curtime); OpenTrades=0; For cnt=1 to TotalTrades{ if OrderValue(cnt,VAL_SYMBOL)==Symbol then { OpenTrades++; If OrderValue(cnt,VAL_TYPE)==OP_SELL OR OrderValue(cnt,VAL_TYPE)==OP_BUY then { FilledOrders++; }; }; }; // We calculate the endHour based on the TimeZoneofData value cnt=0; i=TimeZoneofData; While (cnt<=EndHour) { if (i>23) then i=0; myEndHour=i; i++; cnt++; }; // Friday end of day offset //cnt=0; //i=TimeZoneofData; //While (cnt<=FridayEndHour) //{ // if (i>23) then i=0; // myFridayEndHour=i; // i++; // cnt++; //}; myFridayEndHour=FridayEndHour; if (Hour==myEndHour and Minute>=EndMinute) OR (DayOfWeek==6 AND Hour==myFridayEndHour AND Minute>=EndMinute) then { Trades=2; Type="NONE"; Reverse=False; //cnt=0;While cnt<=10000 { cnt++; }; For cnt=1 to TotalTrades { mode = OrderValue(cnt,VAL_TYPE); If OrderValue(cnt,VAL_SYMBOL)==Symbol then { If (mode == OP_BUYSTOP) then DeleteOrder(OrderValue(cnt,VAL_TICKET),Brown); If (mode == OP_SELLSTOP) then DeleteOrder(OrderValue(cnt,VAL_TICKET),Brown); If (mode == OP_BUY) then CloseOrder(ord(cnt,VAL_TICKET),ord(cnt,VAL_LOTS),bid,Slippage,Blue); If (mode == OP_SELL) then CloseOrder(ord(cnt,VAL_TICKET),ord(cnt,VAL_LOTS),ask,Slippage,red); Exit; }; }; }; if (Hour==ClosePendingOrderHr and Minute>=ClosePendingOrderMin) then { For cnt=1 to TotalTrades { mode = OrderValue(cnt,VAL_TYPE); If OrderValue(cnt,VAL_SYMBOL)==Symbol then { Trades=2; Type="NONE"; Reverse=False; If (mode == OP_BUYSTOP) then DeleteOrder(OrderValue(cnt,VAL_TICKET),Brown); If (mode == OP_SELLSTOP) then DeleteOrder(OrderValue(cnt,VAL_TICKET),Brown); Exit; }; }; }; // We get the StartHour offset cnt=0; i=TimeZoneofData; While (cnt<=StartHour) { if (i>23) then i=0; myStartHour=i; i++; cnt++; }; if (TradeFriday ==1 OR DayOfWeek<>6) then { if (Hour==myStartHour and Minute>=StartMinute) then { if ((myH-myL)>=100*Point OR Dont100==1)) then { Trades=Trades; } else { Trades=0; text4="\nNo conditions to open trades (100 rule)"; }; if (myH-myC>=25*Point OR Dont25==1) then { Trades=Trades; } else { Trades=0; text2="\nNo conditions to open BUYSTOP orders (25 rule)"; }; if (myC-myL>=25*Point OR Dont25==1) then { Trades=Trades; } else { Trades=0; text3="\nNo conditions to open SELLSTOP orders (25 rule)"; }; If OpenTrades<1 and Trades>0 then { Trades=1; Type="NONE"; Reverse=True; SetOrder(OP_BUYSTOP,Lotsi,BuyPrice,Slippage,BuyPrice-StopLoss*Point,BuyPrice+TakeProfit*Point,blue); Trades=1; Type="NONE"; Reverse=True; SetOrder(OP_SELLSTOP,Lotsi,SellPrice,Slippage,SellPrice+StopLoss*Point,SellPrice-TakeProfit*Point,Red); Exit; }; }; }; // if we only have one Stop order we place the another one to always have ready both sides If OpenTrades==1 and Trades>=1 then { for cnt=1 to TotalTrades { if (OrderValue(cnt,VAL_SYMBOL) == Symbol) then { if OrderValue(cnt,VAL_TYPE)==OP_SELLSTOP then { Trades=1; if (DontReverse==1) then Trades=0; Type="NONE"; Reverse=True; SetOrder(OP_BUYSTOP,Lotsi,BuyPrice,Slippage,BuyPrice-StopLoss*Point,BuyPrice+TakeProfit*Point,blue); Exit; }; if OrderValue(cnt,VAL_TYPE)==OP_BUYSTOP then { Trades=1; if (DontReverse==1) then Trades=0; Type="NONE"; Reverse=True; SetOrder(OP_SELLSTOP,Lotsi,SellPrice,Slippage,SellPrice+StopLoss*Point,SellPrice-TakeProfit*Point,Red); Exit; }; }; }; }; if (DontReverse==1) then Reverse=False; // if one order was closed we reverse (only once in a day) If OpenTrades<1 and Trades==1 and Reverse then { if Type=="BUY") then { Trades=0; Type="NONE"; Reverse=False; SetOrder(OP_BUY,Lotsi,Ask,Slippage,Ask-StopLoss*Point,Ask+TakeProfit*Point,Blue); Exit; }; if Type=="SELL") then { Trades=0; Type="NONE"; Reverse=False; SetOrder(OP_SELL,Lotsi,Bid,Slippage,Bid+StopLoss*Point,Bid-TakeProfit*Point,Red); Exit; }; }; // if we have opened positions we take care of them for cnt=1 to TotalTrades { if OrderValue(cnt,VAL_SYMBOL) == Symbol then { if OrderValue(cnt,VAL_TYPE) == OP_BUY then { for i=1 to TotalTrades { if (OrderValue(i,VAL_SYMBOL)==Symbol) then { if OrderValue(i,VAL_TYPE)==OP_SELLSTOP OR OrderValue(i,VAL_TYPE)==OP_BUYSTOP then { Trades=1; Type="SELL"; Reverse=True; DeleteOrder(OrderValue(i,VAL_TICKET),brown); Exit; }; }; }; //We secure SecureProfit (10) pips after TrailSecureProfit (16) pips if Ord(cnt,VAL_STOPLOSS)<=(Ord(cnt,VAL_OPENPRICE)-(StopLoss*Point)) and ((Bid-(Ord(cnt,VAL_OPENPRICE)))>=TrailSecureProfit*Point) then { ModifyOrder(Ord(cnt,VAL_TICKET),Ord(cnt,VAL_OPENPRICE),((Ord(cnt,VAL_OPENPRICE))+SecureProfit*Point),0,DeepSkyBlue); Trades=0; Type="NONE"; Reverse=False; tmp=(Ord(cnt,VAL_OPENPRICE))+SecureProfit*Point; Print("BUY Stoploss trailed to Openprice+SecureProfit=",tmp); Exit; }; // Trailing stop after profit secured If TrailingStop>0 and DontTS=0 then { If (Bid-OrderValue(cnt,VAL_OPENPRICE))>(SecureProfit+TrailingStop)*Point then { If OrderValue(cnt,VAL_STOPLOSS)<(Bid-Point*(TrailingStop+SecureProfit)) then { ModifyOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_OPENPRICE),Bid-Point*(SecureProfit+TrailingStop),0,Yellow); Trades=0; Type="NONE"; Reverse=False; Exit; }; }; }; }; if OrderValue(cnt,VAL_TYPE) == OP_SELL then { for i=1 to TotalTrades { if (OrderValue(i,VAL_SYMBOL)==Symbol) then { if OrderValue(i,VAL_TYPE)==OP_SELLSTOP OR OrderValue(i,VAL_TYPE)==OP_BUYSTOP then { Trades=1; Type="BUY"; Reverse=True; DeleteOrder(OrderValue(i,VAL_TICKET),brown); Exit; }; }; }; //We secure SecureProfit (10) pips after TrailSecureProfit (16) pips if Ord(cnt,VAL_STOPLOSS)>=(Ord(cnt,VAL_OPENPRICE)+(StopLoss*Point)) and (((Ord(cnt,VAL_OPENPRICE)-Ask))>=TrailSecureProfit*Point) then { ModifyOrder(Ord(cnt,VAL_TICKET),Ord(cnt,VAL_OPENPRICE),((Ord(cnt,VAL_OPENPRICE))-SecureProfit*Point),0,DeepSkyBlue); Trades=0; Type="NONE"; Reverse=False; tmp=(Ord(cnt,VAL_OPENPRICE))-SecureProfit*Point; Print("SELL Stoploss trailed to Openprice-SecureProfit=",tmp); Exit; }; If TrailingStop>0 and DontTS=0 then { If (OrderValue(cnt,VAL_OPENPRICE)-Ask)>(Point*(TrailingStop+SecureProfit)) then { if OrderValue(cnt,VAL_STOPLOSS)>(Ask+Point*(TrailingStop+SecureProfit)) then { ModifyOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_OPENPRICE),Ask+Point*(TrailingStop+SecureProfit),0,Purple); Trades=0; Type="NONE"; Reverse=False; Exit; }; }; }; }; }; }; if (!IsTesting) then Comment(text1,text2,text3,text4); Exit;