/*[[ Name := BMX_Unchained Author := Link := Use H1 charts Notes:= Lots := 1.0 Stop Loss := 0 Take Profit := 0 Trailing Stop := 0 ]]*/ // Defines; Defines: dMargin(5000); Defines: ProfitTarget(10000); Defines: Slippage(4); // Slippage Defines: StopYear(2005); Vars: i(0),STS(0),BTS(0),BuyClose(0),SellClose(0); Vars: BuyThisSymbol(0),SellThisSymbol(0),StartBal(0); Vars: LowestBuy(0),HighestSell(0),SetB(0),SetS(0); Vars: BuyNewLevel(0),SellNewLevel(0); Vars: CloseAll(0),TimeClock(0),TimeMin(0); Vars: LargestMarginUsed(0),LargestFloatingLoss(0); Vars: CloseSwitch(0),CloseSellFirst(0),CloseBuyFirst(0); Vars: LowestFreeMargin(0),HighestBuy(0),LowestSell(0); Vars: FiveEMA_0(0),FiveEMA_1(0),TwentyEMA_0(0),TwentyEMA_1(0),HundredEMA_0(0),HundredEMA_1(0); Vars: PipsNeeded(0),ProfitNeeded(0),OrderBalance(0),KeepBuys(0),KeepSells(0),BalGrid(0); // Presets; if year < stopyear then exit; If Curtime - LastTradeTime < 10 then Exit; If Margin > LargestMarginUsed then LargestMarginUsed = Margin; If TotalProfit < LargestFloatingLoss then LargestFloatingLoss = TotalProfit; If LowestFreeMargin == 0 then LowestFreeMargin = FreeMargin; If FreeMargin < LowestFreeMargin then LowestFreeMargin = FreeMargin; FiveEMA_0 = iMAEx(5,MODE_EMA,0,PRICE_CLOSE,0); FiveEMA_1 = iMAEx(5,MODE_EMA,0,PRICE_CLOSE,1); TwentyEMA_0 = iMAEx(20,MODE_EMA,0,PRICE_CLOSE,0); TwentyEMA_1 = iMAEx(20,MODE_EMA,0,PRICE_CLOSE,1); HundredEMA_0 = iMAEx(100,MODE_EMA,0,PRICE_CLOSE,0); HundredEMA_1 = iMAEx(100,MODE_EMA,0,PRICE_CLOSE,1); If (TotalTrades == 0) Then { StartBal= Balance; }; ////////////////////////////////////////// // Begin Market Positioning Arguments /////////////////////////////////////////// If CloseAll==0 Then { If ((FiveEMA_0 > TwentyEMA_0) and (FiveEMA_1 > TwentyEMA_1) and (FiveEMA_0 > HundredEMA_0) and (FiveEMA_1 > HundredEMA_1)) Then { KeepBuys=1; KeepSells=0; BalGrid=0; }; If ((FiveEMA_0 < TwentyEMA_0) and (FiveEMA_1 < TwentyEMA_1) and (FiveEMA_0 < HundredEMA_0) and (FiveEMA_1 < HundredEMA_1)) Then { KeepSells=1; KeepBuys=0; BalGrid=0; }; If ((FiveEMA_0 < TwentyEMA_0) and (FiveEMA_1 < TwentyEMA_1) and (FiveEMA_0 > HundredEMA_0) and (FiveEMA_1 > HundredEMA_1)) Then { BalGrid=1; KeepBuys=0; KeepSells=0; }; If ((FiveEMA_0 > TwentyEMA_0) and (FiveEMA_1 > TwentyEMA_1) and (FiveEMA_0 < HundredEMA_0) and (FiveEMA_1 < HundredEMA_1)) Then { BalGrid=1; KeepBuys=0; KeepSells=0; }; }; BuyThisSymbol = 0; SellThisSymbol = 0; for i = 1 to TotalTrades { if ord(i,VAL_SYMBOL) = Symbol and ord(i,VAL_TYPE) = OP_BUY then BuyThisSymbol = BuyThisSymbol + 1; if ord(i,VAL_SYMBOL) = Symbol and ord(i,VAL_TYPE) = OP_SELL then SellThisSymbol = SellThisSymbol + 1; }; Comment("Buy trades: ",BuyThisSymbol,","," Sell trades: ",SellThisSymbol,"Start Bal= ",StartBal, "\nLargest Margin Used: ",NumberToStr(LargestMarginUsed,2),","," Largest Floating Loss: ",NumberToStr(LargestFloatingLoss,2),","," Lowest Free Margin: ",NumberToStr(LowestFreeMargin,2), "\nBalance: ",NumberToStr(Balance,2),","," Equity: ",NumberToStr(Equity,2),","," TotalProfit: ",NumberToStr(TotalProfit,2), "\nHighestSell: ",HighestSell,","," LowestBuy: ",LowestBuy, "\nHighestBuy: ",HighestBuy,","," LowestSell: ",LowestSell, "Pips Needed to hit Target:","..",PipsNeeded); /////////////////////////////////////////////// // Calculations for pips needed to hit profit target //////////////////////////////////////////////// ProfitNeeded= ProfitTarget - TotalProfit; OrderBalance= (abs(BuyThisSymbol-SellThisSymbol)); PipsNeeded= (Round(abs(ProfitNeeded/OrderBalance))); STS = (Round(SellThisSymbol*1.3)); BTS = (Round(BuyThisSymbol*1.3)); //////////////// // Reset Variables ///////////////// If CloseBuyFirst==1 or CloseSellFirst==1 Then { If BuyThisSymbol==0 Then { CloseBuyFirst=0; }; If SellThisSymbol==0 Then { CloseSellFirst=0; }; }; /////////////////////////////////////////////// // If Profit is positive, close all open positions; //////////////////////////////////////////////// If TotalProfit > ProfitTarget and CloseSwitch==0 then { KeepBuys=0; KeepSells=0; BalGrid=0; SetB=0; SetS=0; CloseAll = 1; //////////////////////// TimeClock = Hour; //give time to close all orders TimeMin = Minute; ///////////////////////// CloseSwitch=1; If BuyThisSymbol>SellThisSymbol then { CloseSellFirst=1; }; If SellThisSymbol>BuyThisSymbol Then { CloseBuyFirst=1; }; }; ///////////////////// // Reset Close Order Variable ////////////////////// If ((TotalTrades == 0) And ((Hour-TimeClock)>=1)) then { If TimeMin<=20 OR Minute >=TimeMin Then { CloseAll = 0; CloseSwitch=0; SetS=0; SetB=0; }; }; ///////////////////////////////////////////////////////////// // Close Profitable Orders Routine to avoid S.T.U.C.K. [Stupid Trades Unite to Cause Khaos] ///////////////////////////////////////////////////////////// BuyClose=0; SellClose=0; For i=1 to TotalTrades Begin If Ord(i,VAL_SYMBOL)==Symbol and Ord(i,VAL_TYPE)==OP_BUY and Ord(i,VAL_PROFIT)>=10 Then BuyClose++; If Ord(i,VAL_SYMBOL)==Symbol and Ord(i,VAL_TYPE)==OP_SELL and Ord(i,VAL_PROFIT)>=10 Then SellClose++; End; If CloseAll==0 Then { If KeepBuys==1 OR KeepSells==1 OR BalGrid==1 And TotalProfit < ProfitTarget then { If BuyClose > 0 OR SellClose > 0 Then { For i = 1 to TotalTrades // Enter S.T.U.C.K. Relief mode { If KeepBuys==1 and BuyThisSymbol=10*Point Then { CloseOrder(Ord(i,VAL_TICKET),Ord(i,VAL_LOTS),Ask,Slippage,Yellow); SetB=1; Exit; }; }; }; If KeepSells==1 and SellThisSymbol=10*Point Then { CloseOrder(Ord(i,VAL_TICKET),Ord(i,VAL_LOTS),Bid,Slippage,Orange); SetS=1; Exit; }; }; }; If BalGrid==1 Then { If SellThisSymbol=10*Point Then { CloseOrder(Ord(i,VAL_TICKET),Ord(i,VAL_LOTS),Bid,Slippage,Purple); SetS=1; Exit; }; }; }; If BuyThisSymbol=10*Point Then { CloseOrder(Ord(i,VAL_TICKET),Ord(i,VAL_LOTS),Ask,Slippage,Purple); SetB=1; Exit; }; }; }; }; }; }; }; }; ///////////////////////////////////////// // Close All Orders Routine ////////////////////////////////////////// If CloseAll == 1 then { for i = 1 to TotalTrades { If ord(i,VAL_SYMBOL) = Symbol and ord(i,VAL_TYPE) = OP_BUY And (CloseBuyFirst==1)OR ((CloseBuyFirst==0) And (CloseSellFirst==0)) then { If Bid-OrderValue(i,VAL_OPENPRICE)<0 Then { CloseOrder(Ord(i,VAL_TICKET),Ord(i,VAL_LOTS),Bid,Slippage,LawnGreen); Exit; }; If Bid-OrderValue(i,VAL_OPENPRICE)>0 Then { CloseOrder(Ord(i,VAL_TICKET),Ord(i,VAL_LOTS),Bid,Slippage,Gold); Exit; }; }; If ord(i,VAL_SYMBOL) = Symbol and ord(i,VAL_TYPE) = OP_SELL And (CloseSellFirst==1)OR ((CloseBuyFirst==0) And (CloseSellFirst==0)) then { If OrderValue(i,VAL_OPENPRICE)-Ask <0 Then { CloseOrder(Ord(i,VAL_TICKET),Ord(i,VAL_LOTS),Ask,Slippage,HotPink); Exit; }; If OrderValue(i,VAL_OPENPRICE)-Ask >0 Then { CloseOrder(Ord(i,VAL_TICKET),Ord(i,VAL_LOTS),Ask,Slippage,Gold); Exit; }; }; }; }; ///////////////////////////////////// // Start up Routine ////////////////////////////////////// If (CloseAll==0) Then //If closing switch set to 1 we don't want any more new orders. { //////////////// // Open First Trades; ///////////////// If Close[1]>High[2] OR Close[1]0) OR (SellThisSymbol>0) Then { If BuyThisSymbol = 0 and FreeMargin > dMargin then { SetOrder(OP_BUY,Lots,Ask,Slippage,0,0,Blue); Exit; }; If SellThisSymbol = 0 and FreeMargin > dMargin then { SetOrder(OP_SELL,Lots,Bid,Slippage,0,0,Red); Exit; }; //////////////////////////////////////////////////////// // Determine Highest Buy and Lowest Sell; //////////////////////////////////////////////////////// LowestBuy = 10; HighestSell = 0; HighestBuy = 0; LowestSell = 10; For i = 1 to TotalTrades { If ord(i,VAL_SYMBOL) == Symbol and ord(i,VAL_TYPE) == OP_BUY and ord(i,VAL_OPENPRICE) < LowestBuy then LowestBuy = ord(i,VAL_OPENPRICE); If ord(i,VAL_SYMBOL) == Symbol and ord(i,VAL_TYPE) == OP_SELL and ord(i,VAL_OPENPRICE) > HighestSell then HighestSell = ord(i,VAL_OPENPRICE); If ord(i,VAL_SYMBOL) == Symbol and ord(i,VAL_TYPE) == OP_BUY and ord(i,VAL_OPENPRICE) > HighestBuy then HighestBuy = ord(i,VAL_OPENPRICE); If ord(i,VAL_SYMBOL) == Symbol and ord(i,VAL_TYPE) == OP_SELL and ord(i,VAL_OPENPRICE) < LowestSell then LowestSell = ord(i,VAL_OPENPRICE); }; ////////////////////////////////////// // Check to set new buy or sell level /////////////////////////////////////// BuyNewLevel = 0; SellNewLevel = 0; For i = 1 to TotalTrades { If ((ord(i,VAL_SYMBOL)== Symbol) and (ord(i,VAL_TYPE) == OP_BUY) and (ord(i,VAL_OPENPRICE) == Ask)) then BuyNewLevel = 1; If ((ord(i,VAL_SYMBOL)== Symbol) and (ord(i,VAL_TYPE) == OP_SELL) and (ord(i,VAL_OPENPRICE) == Bid)) then SellNewLevel = 1; }; //////////////////////////////////////////////////////// // Open additional trades on a grid; //////////////////////////////////////////////////////// If SetB==1 and Ask > HighestSell and BuyNewLevel == 0 and TotalProfit < 20 Then { If FreeMargin < dMargin Then Exit; SetOrder(OP_BUY,Lots,Ask,Slippage,0,0,Blue); SetB=0; Exit; }; If SetS==1 and Bid < LowestBuy and SellNewLevel == 0 and TotalProfit < 20 Then { If FreeMargin < dMargin Then Exit; SetOrder(OP_SELL,Lots,Bid,Slippage,0,0,Red); SetS=0; Exit; }; If Ask > LowestBuy and BuyNewLevel = 0 and TotalProfit < 20 then { If FreeMargin < dMargin then exit; SetOrder(OP_BUY,Lots,Ask,Slippage,0,0,Blue); Exit; }; If Bid < HighestSell and SellNewLevel = 0 and TotalProfit < 20 then { If FreeMargin < dMargin then exit; SetOrder(OP_SELL,Lots,Bid,Slippage,0,0,Red); Exit; }; }; }; Exit;