diff --git a/Strat_1NightStand.c b/Strat_1NightStand.c new file mode 100644 index 0000000..20aedf3 --- /dev/null +++ b/Strat_1NightStand.c @@ -0,0 +1,146 @@ +// #define FULLY_OPTIM +// #define STOP_OPTIM +// #define SMA_OPTIM +// #define SMA_AND_STOP_OPTIM +// #define ONLY_REINV_OPTIM +#define NOT_OPTIM + + +function tradeOneNightStand() + { + #ifdef FULLY_OPTIM + vars Price = series(price()); + vars SMAShort = series(SMA(Price, optimize(10,5,20))); + vars SMALong = series(SMA(Price, optimize(40,30,80,5))); + + Stop = optimize(100,100,500,10) * PIP; + var BuyStop = HH(10) + 1*PIP; + var SellStop = LL(10) - 1*PIP; + + if(between(tow(),42355,50005)) { // check the time interval around thursday midnight + if (SMAShort[0] > SMALong[0] && rising(SMAShort) && rising(SMALong) && NumOpenLong == 0 && NumPendingLong == 0) { + Margin = 0.1 * OptimalFLong * Capital * sqrt(1 + ProfitClosed/Capital); + enterLong(0,BuyStop); + } + else if (SMAShort[0] < SMALong[0] && falling(SMAShort) && falling(SMALong) && NumOpenShort == 0 && NumPendingShort == 0) { + Margin = 0.1 * OptimalFShort * Capital * sqrt(1 + ProfitClosed/Capital); + enterShort(0,SellStop); + } + } + #endif + + #ifdef STOP_OPTIM + vars Price = series(price()); + vars SMAShort = series(SMA(Price, 10)); + vars SMALong = series(SMA(Price, 40)); + + Stop = optimize(100,100,500,10) * PIP; + var BuyStop = HH(10) + 1*PIP; + var SellStop = LL(10) - 1*PIP; + + if(between(tow(),42355,50005)) { // check the time interval around thursday midnight + if (SMAShort[0] > SMALong[0] && rising(SMAShort) && rising(SMALong) && NumOpenLong == 0 && NumPendingLong == 0) { + enterLong(0,BuyStop); + } + else if (SMAShort[0] < SMALong[0] && falling(SMAShort) && falling(SMALong) && NumOpenShort == 0 && NumPendingShort == 0) { + enterShort(0,SellStop); + } + } + #endif + + #ifdef SMA_OPTIM + vars Price = series(price()); + vars SMAShort = series(SMA(Price, optimize(10,5,20))); + vars SMALong = series(SMA(Price, optimize(40,30,80,5))); + + Stop = 100 * PIP; + var BuyStop = HH(10) + 1*PIP; + var SellStop = LL(10) - 1*PIP; + + if(between(tow(),42355,50005)) { // check the time interval around thursday midnight + if (SMAShort[0] > SMALong[0] && rising(SMAShort) && rising(SMALong) && NumOpenLong == 0 && NumPendingLong == 0) { + enterLong(0,BuyStop); + } + else if (SMAShort[0] < SMALong[0] && falling(SMAShort) && falling(SMALong) && NumOpenShort == 0 && NumPendingShort == 0) { + enterShort(0,SellStop); + } + } + #endif + + #ifdef SMA_AND_STOP_OPTIM + vars Price = series(price()); + vars SMAShort = series(SMA(Price, optimize(10,5,20))); + vars SMALong = series(SMA(Price, optimize(40,30,80,5))); + + Stop = optimize(100,100,500,10) * PIP; + var BuyStop = HH(10) + 1*PIP; + var SellStop = LL(10) - 1*PIP; + + if(between(tow(),42355,50005)) { // check the time interval around thursday midnight + if (SMAShort[0] > SMALong[0] && rising(SMAShort) && rising(SMALong) && NumOpenLong == 0 && NumPendingLong == 0) { + enterLong(0,BuyStop); + } + else if (SMAShort[0] < SMALong[0] && falling(SMAShort) && falling(SMALong) && NumOpenShort == 0 && NumPendingShort == 0) { + enterShort(0,SellStop); + } + } + #endif + + #ifdef ONLY_REINV_OPTIM + vars Price = series(price()); + vars SMAShort = series(SMA(Price, 10)); + vars SMALong = series(SMA(Price, 40)); + + Stop = 100 * PIP; + var BuyStop = HH(10) + 1*PIP; + var SellStop = LL(10) - 1*PIP; + + if(between(tow(),42355,50005)) { // check the time interval around thursday midnight + if (SMAShort[0] > SMALong[0] && rising(SMAShort) && rising(SMALong) && NumOpenLong == 0 && NumPendingLong == 0) { + Margin = 0.1 * OptimalFLong * Capital * sqrt(1 + ProfitClosed/Capital); + enterLong(0,BuyStop); + } + else if (SMAShort[0] < SMALong[0] && falling(SMAShort) && falling(SMALong) && NumOpenShort == 0 && NumPendingShort == 0) { + Margin = 0.1 * OptimalFShort * Capital * sqrt(1 + ProfitClosed/Capital); + enterShort(0,SellStop); + } + } + #endif + + + #ifdef NOT_OPTIM + vars Price = series(price()); + vars SMAShort = series(SMA(Price, 10)); + vars SMALong = series(SMA(Price, 40)); + + Stop = 100 * PIP; + var BuyStop = HH(10) + 1*PIP; + var SellStop = LL(10) - 1*PIP; + + if(between(tow(),42355,50005)) { // check the time interval around thursday midnight + if (SMAShort[0] > SMALong[0] && rising(SMAShort) && rising(SMALong) && NumOpenLong == 0 && NumPendingLong == 0) { + enterLong(0,BuyStop); + } + else if (SMAShort[0] < SMALong[0] && falling(SMAShort) && falling(SMALong) && NumOpenShort == 0 && NumPendingShort == 0) { + enterShort(0,SellStop); + } + } + #endif + + if (dow() != 4 && dow() != 5 && dow() != 6) // exit on Monday morning's open, or Tuesday morning's open, if Monday is a holiday + { + exitShort(); + exitLong(); + } +} + +function run() + { + set(LOGFILE|PLOTNOW); + // set(PARAMETERS,FACTORS,LOGFILE,PLOTNOW); // generate and use optimized parameters and factors + + BarPeriod = 15; + // while(asset(loop("EUR/USD","USD/CHF","GBP/USD","USD/JPY"))) + asset("USD/CHF"); + tradeOneNightStand(); +} \ No newline at end of file diff --git a/Strat_BTFD.c b/Strat_BTFD.c index ff2890a..e456dfc 100644 --- a/Strat_BTFD.c +++ b/Strat_BTFD.c @@ -1,12 +1,15 @@ function run() { -set(PARAMETERS|TESTNOW|LOGFILE); // generate and use optimized parameters +set(TESTNOW|LOGFILE|PLOTNOW); +set(PARAMETERS); // generate and use optimized parameters +BarPeriod = 60; TimeFrame = 1; vars Price = series(price()); vars Trend = series(LowPass(Price,500)); // Like moving average but faster -Stop = optimize(4,2,10) * ATR(100); +Stop = ATR(100); +// Stop = optimize(4,2,10) * ATR(100); vars MMI_Raw = series(MMI(Price,300)); // Is market trending or not? Market Meanness Index diff --git a/Strat_BrokerArb.c b/Strat_BrokerArb.c index fe481a3..773bc91 100644 --- a/Strat_BrokerArb.c +++ b/Strat_BrokerArb.c @@ -1,4 +1,8 @@ // Broker arbitrage example //////////////////////////// +// you can compare currency or CFD prices between broker A and broker B ... +// ... and enter a long position with the cheaper broker and a short position with the other + +// There is special account list and asset list #define ASSET_A "EURUSD_A" #define ASSET_B "EURUSD_B" @@ -18,24 +22,16 @@ function tick() strdate(HMS,0),1000.*modf(second(),0),PriceA,PriceB); asset(ASSET_A); - if(NumOpenShort && Difference < 0) - exitShort(); - else if(NumOpenLong && Difference > 0) - exitLong(); - else if(!NumOpenShort && Difference > Threshold) // go short with the expensive asset - enterShort(); - else if(!NumOpenLong && Difference < -Threshold) // go long with the cheap asset - enterLong(); - + if(NumOpenShort && Difference < 0) exitShort(); + else if(NumOpenLong && Difference > 0) exitLong(); + else if(!NumOpenShort && Difference > Threshold) enterShort(); // go short with the expensive asset + else if(!NumOpenLong && Difference < -Threshold) enterLong(); // go long with the cheap asset + asset(ASSET_B); - if(NumOpenShort && Difference > 0) - exitShort(); - else if(NumOpenLong && Difference < 0) - exitLong(); - else if(!NumOpenShort && Difference < -Threshold) - enterShort(); - else if(!NumOpenLong && Difference > Threshold) - enterLong(); + if(NumOpenShort && Difference > 0) exitShort(); + else if(NumOpenLong && Difference < 0) exitLong(); + else if(!NumOpenShort && Difference < -Threshold) enterShort(); + else if(!NumOpenLong && Difference > Threshold) enterLong(); } function run() diff --git a/Strat_CNTR_with_EquityCurve.c b/Strat_CNTR_with_EquityCurve.c new file mode 100644 index 0000000..7413e09 --- /dev/null +++ b/Strat_CNTR_with_EquityCurve.c @@ -0,0 +1,43 @@ + +function checkEquity() { + vars EquityCurve = series(EquityLong+EquityShort); // generate equity curve including phantom trades + vars EquityLP = series(LowPass(EquityCurve,10)); + if(EquityLP[0] < LowPass(EquityLP,100) && falling(EquityLP)) // don't trade when the equity curve goes down & and is below its own lowpass filtered value + setf(TradeMode,TR_PHANTOM); // drawdown -> phantom trades + else + resf(TradeMode,TR_PHANTOM); // profitable -> normal trades + } + +function tradeCounterTrend() +{ + + BarPeriod = 240; +// Counter trend trading is affected by market cycles and more sensitive to the bar period than trend trading +// Bar periods that are in sync with the worldwide markets - such as 4 or 8 hours - ... +// ... are especially profitable with this type of trading + + LookBack = 500; + set(PARAMETERS); + StartDate = 2005; + + NumWFOCycles = 10; + + vars Price = series(price()); + vars Filtered = series(BandPass(Price,optimize(30,20,40),0.5)); + // BandPass is similar to LowPass + it dampens short cycles => curve with medium-period peaks and valleys + + vars Signal = series(FisherN(Filtered,500)); // Normalisation in order to compare with threshold + + var Threshold = optimize(1,0.5,1.5,0.1); + + checkEquity(); // equity curve trading + + // buy and sell + Stop = optimize(4,2,10) * ATR(100); + Trail = 4*ATR(100); // 4 average candles away from the current price +// Trailing often - not always - improves the profit of a strategy, ... +// but is almost always better than placing a profit target + + if(crossUnder(Signal,-Threshold)) enterLong(); // price is supposedly close to the bottom of the main cycle + else if(crossOver(Signal,Threshold)) enterShort(); // price is closed to peak +} \ No newline at end of file diff --git a/Strat_Ehlers_Trend.c b/Strat_Ehlers_Trend.c new file mode 100644 index 0000000..d35bde9 --- /dev/null +++ b/Strat_Ehlers_Trend.c @@ -0,0 +1,24 @@ +function trade_Ehlers() +{ +// simplier than in the Excel + +TimeFrame = 2; + +vars Price = series(price()); +vars Smoothed = series(LowPass(Price,optimize(100,50,200,25))); +vars Trendline = series(HTTrendline(Price)); +vars Trendmode = series(HTTrendMode(Price)); + +MaxLong = MaxShort = 1; + +Stop = optimize(30,3,21,3) * ATR(30); + +if(Trendmode == 1) { + if(crossOver(Smoothed, Trendline)) enterLong(); + else if(crossUnder(Smoothed, Trendline)) enterShort(); + } +else if(Trendmode == 0){ + if(Smoothed[0] > Trendline[0]) enterLong(); + else if(Smoothed[0] < Trendline[0]) enterShort(); + } +} \ No newline at end of file diff --git a/Strat_GridTrader.c b/Strat_GridTrader.c index 98eef18..b888855 100644 --- a/Strat_GridTrader.c +++ b/Strat_GridTrader.c @@ -22,20 +22,16 @@ int run() var Price, Grid = 200*PIP; // set grid distance to 200 pips var Close = priceClose(); -// place pending trades at 5 grid lines -// above and below the current price + // place pending trades at 5 grid lines + // above and below the current price for(Price = 0; Price < Close+5*Grid; Price += Grid) { -// find the lowest grid line - if(Price < Close-5*Grid) continue; -// place not more than 200 trades - if(NumOpenTotal + NumPendingTotal > 200) break; -// place short trades below the current price + if(Price < Close-5*Grid) continue; // find the lowest grid line + if(NumOpenTotal + NumPendingTotal > 200) break; // place not more than 200 trades if(Price < Close and !findTrade(Price,Grid,true)) - enterShort(1,Price,20*Grid,Grid); -// place long trades above the current price + enterShort(1,Price,20*Grid,Grid); // place short trades below the current price else if(Price > Close and !findTrade(Price,Grid,false)) - enterLong(1,Price,20*Grid,Grid); + enterLong(1,Price,20*Grid,Grid); // place long trades above the current price } } diff --git a/Strat_Intraday_2.c b/Strat_Intraday_2.c new file mode 100644 index 0000000..925e88c --- /dev/null +++ b/Strat_Intraday_2.c @@ -0,0 +1,16 @@ +void run() { + set(LOGFILE); // could print the hour and lhour to the log for checking + + BarPeriod = 15; + StartDate = 2010; + EndDate = 2019; + asset("USD/JPY"); + + int timezone = JST; + if(lhour(timezone) == 10 && minute() == 00) enterShort(); + if(lhour(timezone) == 12 && minute() == 30) exitShort(); + + BarZone = JST; + if(hour() == 10 && minute() == 00) enterShort(); + if(hour() == 12 && minute() == 30) exitShort(); + } \ No newline at end of file diff --git a/Strat_Intraday_hour-pairs.c b/Strat_Intraday_hour-pairs.c new file mode 100644 index 0000000..8fffc62 --- /dev/null +++ b/Strat_Intraday_hour-pairs.c @@ -0,0 +1,112 @@ + +// https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2613592 + +#define LONG 0 +#define SHORT 1 + +bool Reinvest = false; +bool UseOptimalF = false; + +var CalculateMargin(int side) + { + var value = 0.1 * Capital; + if (Reinvest) value *= sqrt(Balance / Capital); //value *= sqrt(1 + max(0, ProfitClosed/Capital)); + if (UseOptimalF) value *= ifelse(side == LONG, OptimalFLong, OptimalFShort); + return value; + } + +int hoursLookup[300]; // lookup table stores startHour - endHour combinations (300 combinations per day) +function Initialize() + { + int count = 0; + int startHour, endHour; + for (startHour = 0; startHour <= 23; ++startHour) + for (endHour = startHour + 1; endHour <= 24; ++endHour) + hoursLookup[count++] = (endHour % 24) * 24 + startHour; + } + +int NumOpenTrades(int side) + { + string currentAsset = Asset; + string currentAlgo = Algo; + int count = 0; + for (open_trades) + { + if (strcmp(TradeAsset, currentAsset) != 0 || strcmp(TradeAlgo, currentAlgo) != 0) continue; + if ((TradeIsLong && side == LONG) || (TradeIsShort && side == SHORT)) count++; + } + return count; + } + +TRADE* EnterTrade(int side) + { + if (side == LONG) return enterLong(); + else return enterShort(); + } + +void ExitTrade(int side) + { + if (side == LONG) exitLong(); + else exitShort(); + } + +function TradeIS(int side) + { + int index = (int)(optimize(0, 0, 254, 1) / 254 * 299); + // The optimize function computes an index into a lookup table. . + // It can only handle 255 steps, that is why it looks for a value between 0 and 254 + // ... and than scales that to [0, 299]. + + int startHour = hoursLookup[index] % 24; + int endHour = hoursLookup[index] / 24; + + if (dow() == FRIDAY && hour() >= 21) // Exit over the weekend + { + exitLong(); + exitShort(); + } + + Stop = 55 * PIP; + Trail = 10 * PIP; + + if (startHour == endHour) return; // Don't trade from 00:00 to 00:00 + + if (dow() >= 1 && dow() <= 5) // Trade during the week + { + Margin = CalculateMargin(side); + int numOpen = NumOpenTrades(side); + if (numOpen == 0 && hour() == startHour) EnterTrade(side); + if (numOpen > 0 && hour() == endHour) ExitTrade(side); + } + } + +#define METHOD ASCENT +//#define METHOD BRUTE +//#define METHOD GENETIC + +function run() + { + set(PARAMETERS + FACTORS); + setf(TrainMode,METHOD); + + StartDate = 2010; + // EndDate = 2016; + BarPeriod = 60; + Capital = 10000; + DataSplit = 80; // 80% training, 20% test period + WFOPeriod = (312 * 24) * DataSplit / (100 - DataSplit); // 718848 min => 500 days + + if (is(INITRUN)) + { + Initialize(); + NumCores = -1; + } + + // asset("EUR/USD"); + while (asset(loop("AUD/USD", "EUR/USD", "GBP/USD", "NZD/USD", "USD/CAD", "USD/CHF", "USD/JPY"))) + while (algo(loop("ISL", "ISS"))) + { + if (Algo == "ISL") TradeIS(LONG); + else if (Algo == "ISS") TradeIS(SHORT); + } + } \ No newline at end of file diff --git a/Strat_London_Breakout.c b/Strat_London_Breakout.c new file mode 100644 index 0000000..c2b0d28 --- /dev/null +++ b/Strat_London_Breakout.c @@ -0,0 +1,62 @@ +function run() +{ + StartDate = 20170101; + EndDate = 20181231; + BarPeriod = 10; + LookBack = 100; + + asset("EUR/USD"); + + vars Price = series(price()); + vars Highs = series(priceHigh()); + vars Lows = series(priceLow()); + + int fracTime = 7; //short term ... + var fHigh = FractalHigh(Highs, fracTime); // ... highs and ... + var fLow = FractalLow(Lows, fracTime); // ... lows + + static var longEntry, shortEntry; // Static is important + if(fHigh != 0 or fLow != 0) + { + shortEntry = Lows[0]; + longEntry = Highs[0]; + } + + MaxLong = MaxShort = 1; + Spread = Commission = Slippage = RollLong = RollShort = 0; + EntryTime = 3; + ExitTime = 12; + Stop = 4*ATR(40); + Trail = Stop; + + static var fHighCurrent, fLowCurrent; // Static is important + if(fHigh != 0) fHighCurrent = fHigh; + if(fLow != 0) fLowCurrent = fLow; + + vars trendFilter = series(LowPass(Price, 100)); + + if(lhour(WET) >= 8 and lhour(WET) <= 17 // London Session + and between(priceClose(0), fLowCurrent, fHighCurrent)) + { + if (Price[0] > trendFilter[0]) + { + Entry = longEntry; // go long on recent high + enterLong(); + } + if (Price[0] < trendFilter[0]) + { + Entry = shortEntry; + enterShort(); + } + } + + set(PLOTNOW); + setf(PlotMode, PL_FINE); + PlotBars = 400; // number of bars to plot in the chart. Default - all + + ColorDD = ColorEquity = 0; // don't plot Equty and drawdowns + + plot("Recent High", fHighCurrent, MAIN|DOT, BLUE); + plot("Recent Low", fLowCurrent, MAIN|DOT, RED); + plot("LowPass", trendFilter, MAIN, GREEN); +} \ No newline at end of file diff --git a/Strat_Luxor.c b/Strat_Luxor.c index 7b9439d..c2f548e 100644 --- a/Strat_Luxor.c +++ b/Strat_Luxor.c @@ -4,13 +4,15 @@ function run() { //StartDate = 2004; -set( PARAMETERS+FACTORS); +// set(PARAMETERS+FACTORS); NumYears = 2; BarPeriod = 15; LookBack = 80*4*24; Capital = 50000; //Margin = OptimalF * (Capital + sqrt(ProfitClosed)); -Margin = 0.02*(Capital + sqrt(ProfitClosed)); +// Margin = 0.02*(Capital + sqrt(ProfitClosed)); +Margin = 0.02*Capital; + while(asset(loop(SYMBOLS))) while(algo(loop("H1","H4"))) @@ -22,24 +24,23 @@ while(algo(loop("H1","H4"))) AlgoVar[3]=0; } TimeFrame = 1; - if(Algo == "H1") - TimeFrame = 4; - else if(Algo == "H4") - TimeFrame = 4*4; + if(Algo == "H1") TimeFrame = 4; + else if(Algo == "H4") TimeFrame = 4*4; vars Price = series(priceClose()); - var dFast=optimize(2,1,10); - var dSlow=optimize(18,18,60); + var dFast=2;//optimize(2,1,10); + var dSlow=18;//optimize(18,18,60); var dMulF=2;//optimize(2,1,10); var dMulS=5;//optimize(4,4,16); var dSignal=19;//optimize(9,9,27); - var dStop=optimize(1.5,1,5); - var dLimit=optimize(3,2,10); vars Fast = series(MACD(Price,dFast,dFast*dMulF,dSignal)); vars Slow = series(MACD(Price,dSlow,dSlow*dMulS,dSignal)); var BuyLimit=AlgoVar[0], SellLimit=AlgoVar[1], BuyStop=AlgoVar[2], SellStop=AlgoVar[3]; + var dStop=2;//optimize(1.5,1,5); + var dLimit=3;//optimize(3,2,10); + if(crossOver(Fast,Slow)) { BuyStop = priceHigh() + 1*PIP; // BuyStop = priceHigh() + dStop*ATRS(9); @@ -53,8 +54,8 @@ while(algo(loop("H1","H4"))) // SellLimit = priceLow() - dLimit*ATRS(27); } - TakeProfit = optimize(3,3,10)*ATRS(90); - Stop = optimize(3,3,10)*ATRS(60); + TakeProfit = ATRS(90); // *optimize(3,3,10) + Stop = ATRS(60); // *optimize(3,3,10) if(!NumOpenLong && Fast[0] > Slow[0] && Price[0] < BuyLimit ) enterLong(0,BuyStop); diff --git a/Strat_Mean_revers_Hurst.c b/Strat_Mean_revers_Hurst.c new file mode 100644 index 0000000..cc7f5d0 --- /dev/null +++ b/Strat_Mean_revers_Hurst.c @@ -0,0 +1,27 @@ +function run() // simple mean reversion system +{ +set(PLOTNOW); +// PlotBars = 800; // number of bars to plot in the chart. Default - all +vars Price = series(price()); + +// highpass filter dampens all cycles above 30 bars … +// ... and thus removes the trend from the price curve +vars Filtered = series(HighPass(Price,30)); +vars Signal = series(FisherN(Filtered,500)); // normalized by the Fisher transformation … +// This allows us to determine fixed thresholds at 1 and -1 for separating the tails from the resulting bell curve +var Threshold = 1.0; + +Stop = ATR(100); + +if(Hurst(Price,500) < 0.5 ) { // Above 0.5 begins momentum regime and below 0.5 mean reversion regime. + if(crossUnder(Signal,-Threshold)) // If the price enters a tail in any direction, … + enterLong(1); // … anticipation that it will soon return into the bell’s belly. + else if(crossOver(Signal,Threshold)) enterShort(1); + } + +// plot("Price",Price,1,RED); +// plot("No trend",Filtered,1,BLUE); +// plot("Normalized",Signal,1,GREEN); +// plot("Mean rev",Hurst(Price,500),1,BLACK); + +} \ No newline at end of file diff --git a/Strat_RSI_Tick.c b/Strat_RSI_Tick.c index 318efc9..e236d0b 100644 --- a/Strat_RSI_Tick.c +++ b/Strat_RSI_Tick.c @@ -1,56 +1,46 @@ function run() { - set(TICKS,FAST); + set(PLOTNOW, PARAMETERS); BarPeriod = 60; NumYears = 10; + LookBack = 160; assetList("AssetsFix"); asset("EUR/USD"); -// get the RSI series. RSI: Ratio of the recent upwards data movement to the total data movement; range 0..100 + // RSI: Ratio of the recent upwards data movement to the total data movement; range 0..100 vars Prices = series(priceClose()); - vars RSI12 = series(RSI(Prices,12)); - -// set up stop / profit levels - Stop = 200*PIP; - TakeProfit = 200*PIP; - MaxLong = MaxShort = 1; - -// if rsi crosses over buy level, exit short and enter long - if(crossOver(RSI12,75)) - enterLong(); -// if rsi crosses below sell level, exit long and enter short - if(crossUnder(RSI12,25)) - enterShort(); - -// measure the time - static var Time = 0; - if(is(INITRUN)) Time = timer(); - if(is(EXITRUN)) printf("\nTime needed: >>> %.3f sec <<<",(timer()-Time)/1000); - - - set(TICKS|FAST); - BarPeriod = 60; - NumYears = 10; - assetList("AssetsFix"); - asset("EUR/USD"); + vars RSI12 = series(RSI(Prices,12)); - vars Close = series(priceClose()); - vars Rsi12 = series(RSI(Close,12)); + int overbought = optimize(70, 60, 90,5); + int oversold = optimize(30, 10, 40, 5); - Stop = 200*PIP; - TakeProfit = 200*PIP; + Stop = 4*ATR(100); + TakeProfit = 2*ATR(100); MaxLong = MaxShort = 1; - if(crossOver(Rsi12,75)) - reverseLong(1); - if(crossUnder(Rsi12,25)) - reverseShort(1); -} - - - - - - + //set up trend filter + TimeFrame = 4; // 4-hourly trade filter + vars PriceH4 = series(price()); + int filtPeriod = optimize(5, 3, 10, 1)*24; + vars filter = series(LowPass(PriceH4, 200)); + + + if(crossOver(RSI12,overbought) and Prices[0] > filter[0] and NumOpenLong == 0) enterLong(); // if rsi crosses over buy level, exit short and enter long + if(crossUnder(RSI12,oversold) and Prices[0] < filter[0] and NumOpenShort == 0) enterShort(); // if rsi crosses below sell level, exit long and enter short + + if(crossOver(RSI12,0) && crossUnder(RSI12,0)) + {exitLong(); + exitShort(); + } + + //plots + PlotBars = 250; //plot first 250 bars only for better viewing + PlotHeight1 = 400; + PlotHeight2 = 125; + PlotWidth = 1200; + ColorEquity = ColorDD = 0; + plot("rsi", RSI12, NEW, BLUE); + plot("overbought", overbought, 0, BLACK); + plot("oversold", oversold, 0, BLACK); } \ No newline at end of file diff --git a/Strat_Seasonal.c b/Strat_Seasonal.c new file mode 100644 index 0000000..553352d --- /dev/null +++ b/Strat_Seasonal.c @@ -0,0 +1,45 @@ +/* +var predictSeason(vars Prices,int LookBack=252,int LifeTime=1,int Mode) +{ + var Val = 0.; // initial and final value + int i, Samples, Count = 0; + Samples = 252/(6*DAYBARS); break; // 42 + + var D = wdate(0); + for(i=LifeTime; i= wdate(i+1)) { + if(LifeTime) Val += Prices[i-LifeTime] - Prices[i]; // sample the difference only + else Val += Prices[i]; + if(++Count >= Samples) break; + D -= 7.; break; // same day previous week + } + } + return Val/Count; +} +*/ + +function tradeSeason() +{ + vars Prices = series(price()); + if(!is(LOOKBACK)) { + LifeTime = 1; + var Season = predictSeason(Prices,LookBack,LifeTime,2); + if(Season > 0) enterLong(); + else if(Season < 0) enterShort(); + } +} + +function run() + { + set(LOGFILE); // generate and use optimized parameters and factors + + StartDate = 2013; + EndDate=2018; + BarPeriod = 1440; + LookBack = 252; + + while(loop(Assets)) { + asset(Loop1); + tradeSeason(); + } +} \ No newline at end of file diff --git a/Strat_capture_breakout_TMF.c b/Strat_capture_breakout_TMF.c new file mode 100644 index 0000000..cc36595 --- /dev/null +++ b/Strat_capture_breakout_TMF.c @@ -0,0 +1,98 @@ +/* +capture an expected breakout of the hour prior to London open + +a buy stop limit above the current price and a sell stop below the current price +a sell limit above the current price and a buy limit below the current price +one-cancels-the-other orders, or OCO orders + +*/ + +#define HIGH AssetVar[0] +#define LOW AssetVar[1] +#define CANCELPENDING AssetVar[2] + +int ocoTMF() // one-cancels-the-other orders +{ + // When a pending order is executed, the other order is cancelled + if(CANCELPENDING == 1 and TradeIsPending) return 1; //exit the pending trade + if(TradeIsEntry) CANCELPENDING = 1; //trigger cancel pending + + return 0; +} + +int oco_sarTMF() // stop-and-reverse order enters an opposite order at market if a trade is stopped out +{ + if(CANCELPENDING == 1 and TradeIsPending) return 1; //exit the pending trade + if(TradeIsEntry) CANCELPENDING = 1; //trigger cancel pending + + if(TradeIsStop) + { + if(TradeIsLong) enterShort(oco_sarTMF); + else if(TradeIsShort) enterLong(oco_sarTMF); + } + return 0; + // we we don’t need to run our TMF at every tick ... + // – only when a trade hits its stop loss – ... + // ... so we can use 16 as the return value, which results in a slight increase in simulation speed + + +} + +function run() +{ + set(TICKS); + set(PLOTNOW); + + BarPeriod = 5; + StartDate = 20190101; + + if(is(INITRUN)) CANCELPENDING = 0; + + // price series + vars highs = series(priceHigh()); + vars lows = series(priceLow()); + vars Price = series(price()); + + // get the highest high and the lowest low of pre-open + int openHour = 8; + if (lhour(WET) == openHour and minute() == 0) + { + HIGH = MaxVal(highs, 12); + LOW = MinVal(lows, 12); + CANCELPENDING = 0; //reset CANCELPENDING each day => script is limited to only one trade per day + } + + if (lhour(WET) >= openHour) + { + if (between(priceClose(0), LOW, HIGH)) + { + // Trail = 3*ATR(40); // Somehow with trail the profit is smaller + Entry = HIGH; Stop = LOW; // places pending buy and sell stops at these levels + if (NumOpenLong == 0) enterLong(ocoTMF); // enterLong(oco_sarTMF); + + Entry = LOW; Stop = HIGH; + if (NumOpenShort == 0) enterShort(ocoTMF); // enterShort(oco_sarTMF); + + } + } + + + if (lhour(WET) >= 20) // UTC = WET, but WET has summer time + { + exitLong("*"); exitShort("*"); + } + + // PlotBars = 800; // PlotBars = 200; + // PlotDate = 20170105; + PlotHeight1 = 400; + PlotWidth = 1200; + ColorEquity = ColorDD = 0; + plot("High", HIGH, MAIN|DOT, BLUE); + plot("Low", LOW, MAIN|DOT, RED); + +} + + +//////////////////////////////////////////////////////////////////////////////////////////////////// + + diff --git a/Strat_intraday_15-45.c b/Strat_intraday_15-45.c new file mode 100644 index 0000000..7aa701d --- /dev/null +++ b/Strat_intraday_15-45.c @@ -0,0 +1,17 @@ +function run() +{ + set(TICKS,PLOTNOW,LOGFILE); + + StartDate = 2015; + LookBack = 0; + BarPeriod = 5; + + // asset("SPY"); + // asset("SPX500"); + asset("US30"); + + LifeTime = 3; + if(lhour(EST)==15 and minute() == 30) // Sunday 6:30pm Eastern (market has been open 90 minutes) + enterLong(); + +} \ No newline at end of file diff --git a/Strat_intraday_Enter_At_9.c b/Strat_intraday_Enter_At_9.c new file mode 100644 index 0000000..aee17e8 --- /dev/null +++ b/Strat_intraday_Enter_At_9.c @@ -0,0 +1,30 @@ +function run() +{ + StartDate = 2010; + LookBack = 0; + BarPeriod = 60; + FrameOffset = 9; // trade both assets at 9:00 of their local time + while(asset(loop("EUR/USD","USD/JPY"))) // trade two assets with different time zones + { + if(strstr(Asset,"EUR")) // Substring + AssetZone= WET; + else if(strstr(Asset,"JPY")) + AssetZone= JST; + TimeFrame=AssetFrame; // use a daily time frame changing at 9:00 local time + LifeTime = 8; + enterLong(); + } +} + + + + + + + + + + + + +} \ No newline at end of file diff --git a/Strat_intraday_overnight.c b/Strat_intraday_overnight.c new file mode 100644 index 0000000..f691285 --- /dev/null +++ b/Strat_intraday_overnight.c @@ -0,0 +1,23 @@ +/* OVERNIGHT EFFECT */ + +// define sessions +int timezone = JST; +int sessionStart = 8; // corresponds to 18 in ET +int sessionEnd = 18; // corresponds to 5 in ET + + +function run() +{ + set(PLOTNOW); + setf(PlotMode, PL_FINE); + StartDate = 2009; + EndDate = 2018; + BarPeriod = 60; + MaxShort = 1; + asset("USD/JPY"); + // Spread = Commission = Slippage = RollLong = RollShort = 0; + + if(lhour(timezone) == sessionStart) enterShort(); + if(lhour(timezone) == sessionEnd) exitShort(); + +} \ No newline at end of file