/*[[ AIP-gbpusd-1hr Lots := 1.00 Notes := Use in H1 timeframe. Backtests awesome with EURUSD and Cable. Trade other pairs at your own discretion, but be aware that some are too "weak" to provide the kind of momentum this strategy requires for profitability. The Stop Loss and Trailing Stop parameters are meaningless; the script calculates these, and can change them dynamically as trades progress. Lots are calculated via a risk formula. The risk define is set to 3, and influences the calculated lot size. Max possible simultaneously opened positions is only a function of how many currency pairs you attach this script to. The 2 value for risk allows you to trade four currency pairs quite safely. You need AccountIsMini = True if you are trading a real $ mini; otherwise, set it to False. Note that this works differently in backtesting than in real trading. See comments near AccountIsMini assignment under PYRAMIDING. This version supports trading of more than one currency pair at a time, but only one trade per pair. You can change the maxTradesPerPair parameter, but be aware it might not have the effect you desire. Modifications ------------- DATE MOD # DESCRIPTION ---------- -------- ------------------------------------------------------------ 2005-02-17 1 Autofx- AlwaysInPlay version modified by Autofx 2005-02-17 2 GaryH- Multi Pair coding, timer change & Code Cleanup 2005-02-17 3 Autofx- Changed cosmetics to my liking, simplified, enhanced comments, corrected spread math 2005-02-18 4 Autofx- Further cleanup 2005-02-23 5 Autofx- "invalid prices" fix 2005-02-24 6 Autofx- Changed to market orders 2005-02-27.. 2005-03-03 7 Autofx- Various turbocharging enhancements Update on every tick := Yes Enable Alerts := Yes Disable alert once hit := No Lots := 1 Stop Loss := 999 Take Profit := 55 Trailing Stop := 999 ]]*/ // ==================================================================================================== // DECLARATION AND ASSIGNMENT // ==================================================================================================== defines: Risk(3),mm(1),maxTradesPerPair(1); vars: spread(0), Slippage(0), sl(0),tp(0), mode(0), lastHigh(0),lastLow(0),lastOpen(0),lastClose(0), target(0), entryTS(0), cnt(0), first(0), lotMM(0), tHour(0), CurrentTrades(0), LiveTrading(True), AccountIsMini(False); // See comments near assignment statement below. entryTS = 4 * Point; // Entry trailing stop - points target = TakeProfit * Point; // Profit target - points lastHigh = High[1]; // Last bar high lastLow = Low[1]; // Last bar low lastOpen = Open[1]; // Last bar open lastClose = Close[1]; // Last bar close spread = Ask - Bid; // Spread // ==================================================================================================== // VALIDATION // ==================================================================================================== if CurTime - LastTradeTime < 300 Then Exit; if Bars < 100 or TakeProfit < 10 then Exit; if IsIndirect(Symbol) = True then Exit; if TrailingStop < 5 then { print("invalid Trailing Stop"); Exit; }; // ==================================================================================================== // DATE CHOKE - For testing purposes // ==================================================================================================== //if TimeYear(time[0]) < 2004 then Exit; // ==================================================================================================== // PYRAMIDING - LINEAR // Money management risk exposure compounding // ==================================================================================================== LiveTrading = True; AccountIsMini = False; // Change to False for real trading w/ 100k/regular account // or for all backtesting, since backtests allow // fractional lots. // Change to True for real trading w/ mini account if mm <> 0 then { lotMM = Ceil(Balance * risk / 10000) / 10; if lotMM < 0.1 then lotMM = Lots; if lotMM > 1.0 then lotMM = Ceil(lotMM); // Enforce lot size boundaries if LiveTrading then { if AccountIsMini then lotMM = lotMM * 10; if !AccountIsMini and lotMM < 1.0 then lotMM = 1.0; } if lotMM > 100 then lotMM = 100; } else { lotMM = Lots; // Change mm to 0 if you want the Lots parameter to be in effect }; // ==================================================================================================== // OPEN ORDER CHECK - // Each instance of a script is attached to one currency pair. // When this check executes, it sets CurrentTrades to 1 so that // only one trade for this symbol will be open, which is enforced // by "if CurrentTrades = 0". // ==================================================================================================== CurrentTrades = 0; for cnt = 1 to TotalTrades { if OrderValue(cnt,VAL_SYMBOL) = Symbol then { CurrentTrades = CurrentTrades + 1; }; }; // ==================================================================================================== // TRADE ENTRY // ==================================================================================================== if CurrentTrades < maxTradesPerPair then { //LONG TRADES ENTRY CRITERIA if iSAR(0.05,0.5,0) < Ask and // If SAR less than Ask, and Ask > lastHigh and // Ask above last bar's High, tHour != TimeHour(time[0]) then // then request order. { tHour = TimeMinute(time[0]); // Set stoploss to last bar low so that Bid must hit lastLow to exit. sl = lastLow; tp = Ask + target; SetOrder(OP_BUY, lotMM, Bid, Slippage, sl, tp, LIME); Exit; }; //SHORT TRADES ENTRY CRITERIA if iSAR(0.05,0.5,0) > Bid and // If SAR greater than Bid, and Bid < lastLow and // Bid below last bar's Low, tHour != TimeHour(time[0]) then // then request order. { tHour = TimeMinute(time[0]); // Set stoploss to last bar high so that Ask must hit lastHigh to exit. sl = lastHigh; tp = Bid - target; SetOrder(OP_SELL, lotMM, Ask, Slippage, sl, tp, RED); Exit; }; }; // end of if CurrentTrades < maxTradesPerPair // ==================================================================================================== // TRAILING STOP UPDATE // ==================================================================================================== if CurrentTrades = 0 then exit; for cnt = 1 to TotalTrades { if OrderValue(cnt,VAL_SYMBOL) = Symbol then { if OrderValue(cnt,VAL_TYPE) = OP_BUY then { // If Bid - Open is now higher than entryTS pips profit, // and the stop loss order is lower than // entryTS pips below the Bid, then adjust the stop loss part of // the order to the Bid - entryTS pips if Bid - OrderValue(cnt,VAL_OPENPRICE) > entryTS and OrderValue(cnt,VAL_STOPLOSS) < Bid - entryTS then { ModifyOrder(OrderValue(cnt,VAL_TICKET), OrderValue(cnt,VAL_OPENPRICE), Bid - entryTS, OrderValue(cnt,VAL_TAKEPROFIT), BLUE); Exit; }; // if Stop Loss > Open Price then Set Takeprofit if OrderValue(cnt,VAL_STOPLOSS) >= OrderValue(cnt,VAL_OPENPRICE) then { ModifyOrder(OrderValue(cnt,VAL_TICKET), OrderValue(cnt,VAL_OPENPRICE), OrderValue(cnt,VAL_STOPLOSS), Ask + TakeProfit * Point, BLUE); Exit; }; }; // end OP_BUY check if OrderValue(cnt,VAL_TYPE) = OP_SELL then { // If Open - Ask is now higher than entryTS pips profit, // and the stop loss order is higher than // entryTS pips above the Ask, then adjust the stop loss part of // the order to Ask + entryTS pips if OrderValue(cnt,VAL_OPENPRICE) - Ask > entryTS and OrderValue(cnt,VAL_STOPLOSS) > Ask + entryTS then { ModifyOrder(OrderValue(cnt,VAL_TICKET), OrderValue(cnt,VAL_OPENPRICE), Ask + entryTS, OrderValue(cnt,VAL_TAKEPROFIT), BLUE); Exit; }; // if Stop Loss < Open Price then Set Takeprofit if OrderValue(cnt,VAL_STOPLOSS) <= OrderValue(cnt,VAL_OPENPRICE) then { ModifyOrder(OrderValue(cnt,VAL_TICKET), OrderValue(cnt,VAL_OPENPRICE), OrderValue(cnt,VAL_STOPLOSS), Bid - TakeProfit * Point, BLUE); Exit; }; }; // end OP_SELL check }; // end Symbol check }; // end for cnt=1 to TotalTrades