- ago
Hi,
I have an old strategy that has already been converted from WL3 or 4 to WL6.
Can you help me transpose this to WL7?

CODE:
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Threading; // Sleep() using System.Windows.Forms; using WealthLab; using WealthLab.Indicators; using finantic.TL; // WealthScriptTL namespace WealthLabCompile2 {    class Strategy : WealthScriptTL    {       #region file ChartScript       // start of prolog: This is a comment at the top of each file       public new void Init(){          base.Init();          //optional settings          UseUpdatedEMA(true);          SetStdDevCalculation(StdDevCalculation.Population);       }       // end of prolog: This is a comment at the top of each file       /** (*Indicators...       I~SMA1~SMA~0~#Close,90~900~#Thick~       *) */       public /** (*Indicators... I~SMA1~SMA~0~#Close,90~900~#Thick~ *) */          int Bar = 0;       public bool shortOption = false;       public int avg, LastLongPosition, LastShortPosition = 0;       public double tolerance, avgValue, avgOverValue, avgUnderValue = 0.0;       public double newAvgOverValue, newAvgUnderValue = 0.0;       public int avgSeries, avgOverSeries, avgUnderSeries = 0;       public int curDay = 0;       public bool condition = false; /** This method contains the meat of your script, i.e, any statements that are not inside a user defined class or function/procedure. */       protected override void Execute(){          Init();          // [declaration for class ChartScript used to be here]          /** ----------- set variable values ------------------ */          avg = (int)20;          /** average in days */          tolerance = (double)0.034;          /** ----------- set variable values ------------------ */          avgValue = (double)0.00;          avgOverValue = (double)PriceClose(1);          avgUnderValue = (double)0.00;          newAvgOverValue = (double)0.00;          newAvgUnderValue = (double)0.00;          avgSeries = (int)SMASeries(WL_CLOSE, avg);          avgOverSeries = (int)CreateSeries();          avgUnderSeries = (int)CreateSeries();          for (Bar = 21; Bar <= (Bars.Count - 1); Bar++) {             /** calculate new average */             avgValue = (double)SMA(Bar, WL_CLOSE, avg);             newAvgOverValue = (double)((avgValue * ((1.00 + tolerance))));             newAvgUnderValue = (double)((avgValue * ((1.00 - tolerance))));             switch (MarketPosition) {                /** No position */                case 0:                {                   avgOverValue = (double)Math.Min(avgOverValue, newAvgOverValue);                   avgUnderValue = (double)Math.Max(avgUnderValue, newAvgUnderValue);                   /** long buy */                   if ((BuyAtStop((Bar + 1), avgOverValue, "Buy"))) {                      avgUnderValue = (double)newAvgUnderValue;                   }                   /** long buy */                }             break;             /** Current position is long */             case 1:             {             /** set the background color */             SetBackgroundColor(Bar, WL_BLUEBKG);             avgUnderValue = (double)Math.Max(avgUnderValue, newAvgUnderValue);             if ((SellAtStop((Bar + 1), avgUnderValue, LastPosition, "Sell"))) {                avgOverValue = (double)newAvgOverValue;                avgUnderValue = (double)newAvgUnderValue;             }          }             break;             default:             break;             } /** set PriceSeries */          SetSeriesValue(Bar, avgOverSeries, avgOverValue);          SetSeriesValue(Bar, avgUnderSeries, avgUnderValue);       }       /** plot avgs */       PlotSeries(avgSeries, 0, WL_PURPLE, WL_THIN);       PlotSeries(avgOverSeries, 0, WL_BLUE, WL_DOTTED);       PlotSeries(avgUnderSeries, 0, WL_FUCHSIA, WL_DOTTED);    }    // start of epilog: This is a comment at the end of each file } // class Strategy    } // namespace    // end of epilog    #endregion file ChartScript
0
1,025
Solved
8 Replies

Reply

Bookmark

Sort
Cone8
 ( 5.88% )
- ago
#1
I'll give it a try - but that finantic translator sure makes it more complicated. Do you still have the WL4 script - that would be easier!
0
- ago
#2
var Bar: integer;
var shortOption: boolean;
var avg, LastLongPosition, LastShortPosition: integer;
var tolerance, avgValue, avgOverValue, avgUnderValue: float;
var newAvgOverValue, newAvgUnderValue: float;
var avgSeries, avgOverSeries, avgUnderSeries: integer;
var curDay: integer;
var condition: boolean;

// ----------- set variable values ------------------
avg :=9; // average in days
tolerance := 0.026; // in percent (e.g. 0.4% = 0.004)
// ----------- set variable values ------------------

avgValue := 0.00;
avgOverValue := PriceClose(1);
avgUnderValue := 0.00;
newAvgOverValue := 0.00;
newAvgUnderValue := 0.00;
avgSeries := WMASeries(#Close,avg);
avgOverSeries := createSeries();
avgUnderSeries := createSeries();

Plotstops ;

// Trading rules
for Bar := 20 to BarCount - 1 do
begin
// calculate new average
avgValue := WMA(Bar, #Close, avg);
newAvgOverValue := (avgValue * (1.00 + tolerance));
newAvgUnderValue := (avgValue * (1.00 - tolerance));

case MarketPosition of
0: { No position }
begin
avgOverValue := Min(avgOverValue, newAvgOverValue);
avgUnderValue := Max(avgUnderValue, newAvgUnderValue);

// long buy
if (buyAtStop(Bar+1, avgOverValue, 'Buy')) then
begin
avgUnderValue := newAvgUnderValue;
end;
end;
1: { Current position is long }
begin
// set the background color
SetBackgroundColor(Bar, #bluebkg);

avgUnderValue := Max(avgUnderValue, newAvgUnderValue);

if (SellAtStop(Bar+1, avgUnderValue, LastPosition, 'Sell')) then
begin
avgOverValue := newAvgOverValue;
avgUnderValue := newAvgUnderValue;
end;
end;
end;
// set PriceSeries
setSeriesValue(Bar, avgOverSeries, avgOverValue);
setSeriesValue(Bar, avgUnderSeries, avgUnderValue);
end;

// plot avgs
plotSeries(avgSeries, 0, #purple, #thin);
plotSeries(avgOverSeries, 0, #blue, #dotted);
plotSeries(avgUnderSeries, 0, #fuchsia, #dotted);
0
Cone8
 ( 5.88% )
- ago
#3
It's actually a lot simpler than previously coded, which actually "peeked" when setting the avgOver/UnderSeries values. However, that didn't affect the trades, only the values displayed. Give this a try, complete with parameter sliders and ready for optimization.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript2 { public class Strategy612 : UserStrategyBase {                   IndicatorBase _avgSeries;       TimeSeries _avgOverSeries;       TimeSeries     _avgUnderSeries;       public Strategy612()       {          AddParameter("Period", ParameterTypes.Int32, 20, 5, 50, 5);          AddParameter("Tolerance", ParameterTypes.Double, 0.034, 0.025, 0.045, 0.001);       }        public override void Initialize(BarHistory bars) {          int avg = Parameters[0].AsInt;          double tolerance = Parameters[1].AsDouble;          _avgSeries = SMA.Series(bars.Close, avg);          _avgOverSeries = _avgSeries * (1.00 + tolerance);          _avgUnderSeries = _avgSeries * (1.00 - tolerance);          PlotIndicatorLine(_avgSeries, Color.Purple, 1);          PlotTimeSeriesLine(_avgOverSeries, "AvgOverSeries", "Price", Color.Blue, 1, LineStyles.Dotted);          PlotTimeSeriesLine(_avgUnderSeries, "AvgUnderSeries", "Price", Color.Fuchsia, 1, LineStyles.Dotted);       } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) {           if (!HasOpenPosition(bars, PositionType.Long)) {                         PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, _avgOverSeries[idx]);    } else {             /* position is long */             SetBackgroundColor(bars, idx, Color.FromArgb(40, Color.Blue));                          PlaceTrade(bars, TransactionType.Sell, OrderType.Stop, _avgUnderSeries[idx]);             } }        } }
1
Best Answer
- ago
#4
Great, THX
0
- ago
#5
Can this be accomplished by this Rule?
0
Cone8
 ( 5.88% )
- ago
#6
The Buy at Limit or Stop rule is all this one needs. The Indicator % condition isn't required because it's already part of the main rule.
2
- ago
#7
i found a problem at that what you sent.

There is a crucial point in my script from wl4. Position is flat: the indicator (SMA) must not become larger for a buy can only get smaller. (buy signal always at lowest low of indicator
same for sell: the indicator can only get bigger
0
- ago
#8
The solution in Post #2 employs a WMA; whereas, the solution in Post #3 employs an SMA instead. One might want to try stimulating both approaches to see which is most profitable.
1

Reply

Bookmark

Sort