using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; //based on bottomfisher 0.76aat code //added strategy parameters (profit only) //added inhours method //commented out constants //removed strategy parameters namespace WealthLab.Strategies { public class MyStrategy : WealthScript { //constants declarations and optimizable parameters double dRSIlowtarget=30; double dRSIhightarget=70; double dRSIbuylow=40; double dRSIbuyready=50; int dPatience=5; // int ivalleycounter=0; // int ipeakcounter=0; int Lastvalleybar=0; int Lastpeakbar=0; double dprofitpct = 3; double dstoploss = 3; double dPriceFudgeFactor = 0.5; //******************* //******************* //methods //GetTime -- converts bar to military time //inmarkethours -- bool if between 0931 and 1600 //Get time for the bar public int GetTime(int bar) { return Date[bar].Hour * 100 + Date[bar].Minute; } //bool if bar between 0931 and 1600 public bool inmarkethours(int bar) { bool inhours=false; if ((GetTime(bar) > 0930) && (GetTime(bar) <1601)) inhours=true; return inhours; } //color blue bars public int colorbluebars(int bar) { DataSeries rsiseries = RSI.Series(Close, 14); int lastvalleybar=Bars.Count; for (int blue = 3; blue <= (Bars.Count-1); blue++) { //*** Mark RSI valleys on chart if (((rsiseries[blue-1] < rsiseries[blue-2])&&(rsiseries[blue-1] rsiseries[orange-2])&&(rsiseries[orange-1]>dRSIhightarget) ) && (inmarkethours(orange) )) { SetBackgroundColor(orange-1, Color.Orange ); lastpeakbar=orange-1; } } return lastpeakbar; } //********************************************************************************************************************************************* //************************************** Actual program protected override void Execute() { //initialize global variables ClearDebug(); bool posactive=false; int posrecentclear=3; int selltimestop=0; int bar = Bars.Count - 1; DataSeries rsi = RSI.Series(Close, 14); DataSeries rsiseries = RSI.Series(Close, 14); DataSeries mfiseries = MFI.Series( Bars, 14 ); //draw chart panes ChartPane pane3 = CreatePane( 35, false, true ); PlotSeries(pane3, rsi, Color.Green, WealthLab.LineStyle.Solid, 1); SetPaneMinMax( pane3, 30, 70 ); ChartPane mfipane = CreatePane( 35, false, true ); PlotSeries(mfipane, mfiseries, Color.LightGreen, WealthLab.LineStyle.Solid, 1); SetPaneMinMax( mfipane, 30, 70 ); //*** Mark RSI valleys on chart Lastvalleybar=colorbluebars(Bars.Count-1); Lastpeakbar=colororangebars(Bars.Count-1); //Main loop - rsi valleys & peaks // for (int i = 3; i <= (Bars.Count-1); i++) if ( (Lastpeakbar(dRSIbuyready))&&(d>(runninglowbar+dPatience))) { readytobuy = true; } if ( (IsLastPositionActive) && (inmarkethours(d+2)) ) { Position p = LastPosition; if (SellAtStop( d+1, p, p.EntryPrice * (1-(dstoploss/100)), "% Stop" )) PrintDebug("sold "+LastPosition+" - $"+Close[d] +" at "+ Date[d] +" // RSI: "+ rsiseries[d] +"(Stop loss sell)"); // dprofitpct= (profitpct); if (SellAtLimit( d+1, LastPosition, p.EntryPrice*(1+((dprofitpct))/100), "% Profit Limit Sell" )) { PrintDebug("sold "+LastPosition+" - $"+Close[d] +" at "+ Date[d] +" // RSI: "+ rsiseries[d]+"(Target Profit sell)" ); i=Bars.Count-1; d=Bars.Count-1; } //if (Bars.IsLastBarOfDay(d+1)) if (GetTime(d+2)==1559) { SellAtMarket(d+1, p, "Day End Sell"); PrintDebug("sold "+LastPosition+" - $"+Close[d+1] +" at "+ Date[d+1] +" // RSI: "+ rsiseries[d+1] +"(End of day sell)" ); i=Bars.Count-1; d=Bars.Count-1; } } if ((Close[d] < (runninglow*(1+dPriceFudgeFactor/100) ) )&&((readytobuy==true)&&(rsiseries[d]5)) { BuyAtMarket(d+1, "bought at expected RSI divergence" ); PrintDebug("bought "+LastPosition+" - $"+Close[d] +" at "+ Date[d] +" // RSI: "+ rsiseries[d] ); readytobuy = false; } } } } } } } }