/*[[ Name := GridMaster_03 Author := Copyright © 2005, Lifestatic, pip_seeker, autofx Link := http://autoforex.biz Notes:= This is a pure, 100% REACTIVE strategy, with no attempt at all to predict, anticipate or define a statistical edge. It limits grid size by keeping edges trimmed. This version (03) increases the profit target whenever an edge is trimmed. The ProfitIncrease define is used to indicate how much to increase the profit target. The MaxProfitExit define is used to limit the profit target to the defined value. Lots := 1.0 Stop Loss := 0 Take Profit := 0 Trailing Stop := 0 ]]*/ // Defines; Define: ProfitEntry(5); // Open new trades if TotalProfit less than this value Define: ProfitExit(100); // Stop opening new trades and start closing them all if TotalProfit greater than this value Define: ProfitIncrease(50); // Dollar amount to increase profit target each time an edge is trimmed Define: MaxProfitExit(500); // Maximum profit target Define: MaxTrades(50); // Number of TotalTrades allowed Define: BalanceDelta(20); // Number of trades by which buys can exceed sells and vice versa Define: BucksPerPip(1); Define: dFreeMargin(2000); Define: Slippage(4); // Slippage Vars: i(0); Vars: ProfitTarget(0); Vars: NumBuys(0),NumSells(0); Vars: CumulativeBuyRate(0),CumulativeSellRate(0); Vars: BuyPL(0),SellPL(0),CloseAl1(0),OverallPL(0),ExitRate(0); Vars: LastTradeWasBuy(0),LastTradeWasSell(0); Vars: HighestSell(0),LowestBuy(0),HighestBuy(0),LowestSell(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); // 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; CumulativeBuyRate = 0.0; CumulativeSellRate = 0.0; OverallPL = 0; for i = 1 to TotalTrades { if Ord(i,VAL_SYMBOL) == Symbol and Ord(i,VAL_TYPE) == OP_BUY then { NumBuys++; CumulativeBuyRate += Ord(i,VAL_OPENPRICE); } if Ord(i,VAL_SYMBOL) == Symbol and Ord(i,VAL_TYPE) == OP_SELL then { NumSells++; CumulativeSellRate += Ord(i,VAL_OPENPRICE); } }; if (NumBuys > NumSells and NumSells > 0) then { ExitRate = CumulativeBuyRate/NumBuys; while (OverallPL < ProfitTarget) { BuyPL = ((BucksPerPip*NumBuys)/Point)*(ExitRate - CumulativeBuyRate/NumBuys); SellPL = ((BucksPerPip*NumSells)/Point)*(CumulativeSellRate/NumSells - ExitRate); ExitRate += Point; OverallPL = BuyPL + SellPL; } if ProfitTarget < ProfitExit then ProfitTarget = ProfitExit; 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", "\nDollar Target: ",ProfitTarget, "\nLong Target: ",ExitRate, "\n",(ExitRate-Bid)/Point," pips from target"); } else if (NumSells > NumBuys and NumBuys > 0) then { ExitRate = CumulativeSellRate/NumSells; while (OverallPL < ProfitTarget) { BuyPL = ((BucksPerPip*NumBuys)/Point)*(ExitRate - CumulativeBuyRate/NumBuys); SellPL = ((BucksPerPip*NumSells)/Point)*(CumulativeSellRate/NumSells - ExitRate); ExitRate -= Point; OverallPL = BuyPL + SellPL; } if ProfitTarget < ProfitExit then ProfitTarget = ProfitExit; 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", "\nDollar Target: ",ProfitTarget, "\nShort Target: ",ExitRate, "\n",(Ask-ExitRate)/Point," pips from target"); } 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 > ProfitTarget 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; ProfitTarget = ProfitExit; }; if CloseAl1 == 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 is set to 0, we can open new orders and adjust TPs/SLs { // 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 TotalTrades > MaxTrades, 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 TotalTrades > MaxTrades and Ord(i,VAL_SYMBOL) == Symbol and Ord(i,VAL_TYPE) == OP_BUY and Ord(i,VAL_OPENPRICE) == HighestBuy then { ProfitTarget += ProfitIncrease; if ProfitTarget > MaxProfitExit then ProfitTarget = MaxProfitExit; CloseOrder(Ord(i,VAL_TICKET),Ord(i,VAL_LOTS),Bid,Slippage,Violet); Exit; }; if LastTradeWasBuy == 1 and TotalTrades > MaxTrades and Ord(i,VAL_SYMBOL) == Symbol and Ord(i,VAL_TYPE) == OP_SELL and Ord(i,VAL_OPENPRICE) == LowestSell then { ProfitTarget += ProfitIncrease; if ProfitTarget > MaxProfitExit then ProfitTarget = MaxProfitExit; 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 and NumBuys <= NumSells+BalanceDelta then { if FreeMargin < dFreeMargin then Exit; LastTradeWasBuy = 1; LastTradeWasSell = 0; SetOrder(OP_BUY,Lots,Ask,Slippage,0,0,Blue); Exit; }; if Bid < HighestSell and SellNewLevel == 0 and TotalProfit < ProfitEntry and NumSells <= NumBuys+BalanceDelta then { if FreeMargin < dFreeMargin then Exit; LastTradeWasSell = 1; LastTradeWasBuy = 0; SetOrder(OP_SELL,Lots,Bid,Slippage,0,0,Red); Exit; }; }; }; Exit;