/*[[ Name := BMX_Unchained Author := Copyright © 2005, Lifestatic Link := Use any chart Lots := 0.1 Stop Loss := 0 Take Profit := 0 Trailing Stop := 0 ]]*/ // Defines; Define: ProfitEntry(5); // Open New trades if Profit Less than this value Define: ProfitExit(100); Define: MaxGridSize(100); // Number of pips allowed between HighestBuy and LowestSell Define: dFreeMargin(2000); Define: Slippage(4); // Slippage Vars: i(0); Vars: NumBuys(0),NumSells(0); Vars: HighestSell(0),LowestBuy(0),HighestBuy(0),LowestSell(0); Vars: BuyNewLevel(0),SellNewLevel(0); Vars: LastTradeWasBuy(0),LastTradeWasSell(0); Vars: CloseAll(0); Vars: LargestMarginUsed(0),LargestFloatingLoss(0); Vars: CloseSwitch(0),CloseSellFirst(0),CloseBuyFirst(0); Vars: LowestFreeMargin(0); // Presets; 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; NumBuys = 0; NumSells = 0; for i = 1 to TotalTrades { if Ord(i,VAL_SYMBOL) == Symbol and Ord(i,VAL_TYPE) == OP_BUY then NumBuys++; if Ord(i,VAL_SYMBOL) == Symbol and Ord(i,VAL_TYPE) == OP_SELL then NumSells++; }; Comment("Buy trades: ",NumBuys,","," Sell trades: ",NumSells, "\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, "\nGridSize: ",(HighestBuy-LowestSell)/Point," pips"); if CloseBuyFirst == 1 or CloseSellFirst == 1 then { if NumBuys == 0 then CloseBuyFirst = 0; if NumSells == 0 then CloseSellFirst = 0; }; // if Profit is positive, close all open positions; if TotalProfit > ProfitExit and CloseSwitch == 0 then { CloseAll = 1; CloseSwitch = 1; if NumBuys > NumSells then CloseSellFirst = 1; if NumSells > NumBuys then CloseBuyFirst = 1; }; if TotalTrades == 0 then { CloseAll = 0; CloseSwitch = 0; }; 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-Ord(i,VAL_OPENPRICE) < 0 then { CloseOrder(Ord(i,VAL_TICKET),Ord(i,VAL_LOTS),Bid,Slippage,LawnGreen); Exit; }; if Bid-Ord(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 Ord(i,VAL_OPENPRICE)-Ask < 0 then { CloseOrder(Ord(i,VAL_TICKET),Ord(i,VAL_LOTS),Ask,Slippage,HotPink); Exit; }; if Ord(i,VAL_OPENPRICE)-Ask > 0 then { CloseOrder(Ord(i,VAL_TICKET),Ord(i,VAL_LOTS),Ask,Slippage,Gold); Exit; }; }; }; }; if (CloseAll == 0) then // if closing switch set to 1 we don't want any more new Orders. { // Open First Trades; if NumBuys >= 0 or NumSells >= 0 then { if NumBuys == 0 and FreeMargin > dFreeMargin then { SetOrder(OP_BUY,Lots,Ask,Slippage,0,0,Blue); LastTradeWasBuy = 1; LastTradeWasSell = 0; Exit; }; if NumSells == 0 and FreeMargin > dFreeMargin then { SetOrder(OP_SELL,Lots,Bid,Slippage,0,0,Red); LastTradeWasSell = 1; LastTradeWasBuy = 0; Exit; }; //////////////////////////////////////////////////////// // Determine Highest Buy and Lowest Sell; //////////////////////////////////////////////////////// LowestBuy = 1000; HighestSell = 0; HighestBuy = 0; LowestSell = 1000; 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); }; ///////////////////////////////////////// // If Grid Width > MaxGridSize Pips, Close Highest Buy If Last Trade Was A Sell // Or Close Lowest Sell If Last Trade Was A Buy ////////////////////////////////////////// for i = 1 to TotalTrades { if LastTradeWasSell == 1 and HighestBuy - LowestSell > MaxGridSize*Point and Ord(i,VAL_SYMBOL) == Symbol and Ord(i,VAL_TYPE) == OP_BUY and Ord(i,VAL_OPENPRICE) == HighestBuy then { CloseOrder(Ord(i,VAL_TICKET),Ord(i,VAL_LOTS),Bid,Slippage,Violet); Exit; }; if LastTradeWasBuy == 1 and HighestBuy - LowestSell > MaxGridSize*Point and Ord(i,VAL_SYMBOL) == Symbol and Ord(i,VAL_TYPE) == OP_SELL and Ord(i,VAL_OPENPRICE) == LowestSell then { CloseOrder(Ord(i,VAL_TICKET),Ord(i,VAL_LOTS),Ask,Slippage,Violet); Exit; }; }; // BuyNewLevel, SellNewLevel; 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 Ask > LowestBuy and BuyNewLevel == 0 and TotalProfit < ProfitEntry then { if FreeMargin < dFreeMargin then Exit; SetOrder(OP_BUY,Lots,Ask,Slippage,0,0,Blue); LastTradeWasBuy = 1; LastTradeWasSell = 0; Exit; }; if Bid < HighestSell and SellNewLevel == 0 and TotalProfit < ProfitEntry then { if FreeMargin < dFreeMargin then Exit; SetOrder(OP_SELL,Lots,Bid,Slippage,0,0,Red); LastTradeWasSell = 1; LastTradeWasBuy = 0; Exit; }; }; }; Exit;