- ago
I'm having issues with setting a variable that I wish to remain set through multiple bars until a profit target or stop loss is triggered.

The variable is set to 1 based on some logic. As new bars are created, I test to see if the position has hit the profit/stop loss targets, and if so, I reset the variable to 0.

However, what's happening now is the variable resets to 0 on each bar.

I assume it's something simple but I'm stumped.
0
199
5 Replies

Reply

Bookmark

Sort
- ago
#1
Answer's in this blog post: Anatomy of a Wealth-Lab Strategy > Scoping Example.
1
- ago
#2
QUOTE:
However, what's happening now is the variable resets to 0 on each bar.

That's because you are declaring the variable in Execute, so it resets itself each time Execute loops. You need to declare the variable in the MyStrategy block so it becomes visible to each child block (such as Initialize and Execute) inside the MyStrategy block. You can then assign it in the Execute block.

Understand, if you declare "x" in both the MyStrategy block and the Execute block, then you will have two separate x's with different values, so don't do that. If you can't get it to work, post your code so someone can point out the "variable scoping" mistake.
2
- ago
#3
Thanks, I'm struggling with the classes, I guess.

Here's a snippet of the code. SLCounter is the variable I want to retain until the position is closed.

CODE:
amespace WealthScript6 { public class MyStrategy : UserStrategyBase {     public MyStrategy() : base() {          AddParameter("xxx1", ParameterType.Int32, 52, 40, 60, 2);          AddParameter("xxx2", ParameterType.Int32, 180, 180, 220, 2);          AddParameter("xxx3", ParameterType.Int32, 28, 10, 50, 2);          AddParameter("xxx4", ParameterType.Int32, 10, 10, 50, 5);          AddParameter("xxx5", ParameterType.Double, -4.5, -10, -0.5, 0.5);          AddParameter("Long OBV EMA Period", ParameterType.Int32, 150, 50, 200, 10);          AddParameter("Long Profit % Target - OBV above EMA", ParameterType.Double, 19, 1, 36, 1);          AddParameter("Long Stop Loss - OBV above EMA", ParameterType.Double, 7, 2, 15, 1);          AddParameter("Long Profit % Target - OBV below EMA", ParameterType.Double, 24, 1, 35, 1);          AddParameter("Long Stop Loss - OBV below EMA", ParameterType.Double, 14, 2, 15, 1);          AddParameter("HMA Price (chikou)", ParameterType.Int32, 160, 150, 220, 5);          AddParameter("HMA Price Period", ParameterType.Int32, 200, 180, 220, 2);          AddParameter("Post BE stop", ParameterType.Double, 1, 1, 10, 1);                            StartIndex = 196; } public override void Initialize(BarHistory bars) {          // Over Breakeven Stop-loss reset          PctOverBreakeven = Parameters[1].AsDouble; //Price level to initiate a trail-stop reset          SLcounter = 0;          foreach(IndicatorBase ib in _startIndexList) if (ib.FirstValidIndex > StartIndex) StartIndex = ib.FirstValidIndex; } public override void Execute(BarHistory bars, int idx) {          //LONG POSITIONS                       int index = idx;          Position foundPosition0 = FindOpenPosition(0);          bool condition0;          if (foundPosition0 == null)          {          //If there isn't a position, test if the indicators cross their parameters          //If one or more indicators cross, then test if the Wm%R is less than signal line             condition0 = false;             {                count = 0;                HMAconccount = 0;                                if (HMAx0.CrossesOver(0.00, index))                {                   count++;                   HMAconccount++;                }                if (count >= 1)                {                   condition0 = true;                                                               }             }             if (condition0)             //If one or more indicators cross, and Wm%R is below signal, the place the trade at market             //the different entry signals identify which indicator is the one that signals                          {                Backtester.CancelationCode = 1;                                if (HMAconccount == 1)                {                   _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (HMAConc)");                   DrawBarAnnotation(TextShape.HexagonFilled, index, true, WLColor.Red, 14, true, _fontCZ);                   PriceAtEntry = bars.Close[index];                }             }          }          else          //EXIT SIGNALS FOR LONG POSITIONS          {             //Set Stoploss counter for Breakeven calculations. This calculation sets the counter to 1 if the price breaks above the target profit percent.             //This needs to reset everytime a position is closed             DrawBarAnnotation(SLcounter.ToString("N0"), index, false, WLColor.Yellow, 14);             if (SLcounter == 0)             {                if (bars.Close[index] > foundPosition0.EntryPrice * (1 + (PctOverBreakeven / 100)))                {                   SLcounter = SLcounter + 1;                   DrawBarAnnotation(TextShape.SquareFilled, index, true, WLColor.Green, 14, true, _fontCZ);                }             }                          //Test to see if, once the price broke above the target level, it falls back to the open price             //if it does, then sell at breakeven                          if (SLcounter == 1)             {                if (bars.Close[index] <= foundPosition0.EntryPrice * 1.001)                {                   ClosePosition(foundPosition0, OrderType.Limit, PriceAtEntry, "Sell at Breakeven");                   DrawBarAnnotation(TextShape.SquareFilled, index, true, WLColor.Yellow, 14, true, _fontCZ);                   SLcounter = 0;                }             }                          //If the first test is false, then sell at profit/stop loss. OBV/EMA comparison will determine the profit/stop loss levels             condition0 = false;             {                                               if (OBVxLong[index] > EMAofOBVLong[index]) //IF OBV IS OVER EMA OF OBV                {                   condition0 = true;                }             }             if (condition0)             {                Backtester.CancelationCode = 1;                value = (Parameters[6].AsDouble / 100.0) + 1.0;                ClosePosition(foundPosition0, OrderType.Limit, foundPosition0.EntryPrice * value, "Sell at " + Parameters[6].AsDouble + "% profit tgt (OBV above)");                SLcounter = 0;             }          }        } public override void NewWFOInterval(BarHistory bars) {          OBVxLong = new OBV(bars);          EMAofOBVLong = new EMA(OBVxLong,Parameters[5].AsInt);          EMAofOBVLong.ParentIndicator = OBVxLong;          ChikouSpan = new ChikouSpan(bars,26);          HMAPrice = new HMA(bars.Close,Parameters[10].AsInt); }       public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice)       {          double price = 0;          if (t.PositionTag == 0)          {             price = executionPrice * (Parameters[6].AsDouble / 100.0 + 1.0);             t.AssignAutoProfitTargetPrice(price);          }          if (t.PositionTag == 0)          {             price = executionPrice * (1.0 - Parameters[7].AsDouble / 100.0);             t.AssignAutoStopLossPrice(price);          }          if (t.PositionTag == 0)          {             price = executionPrice * (Parameters[8].AsDouble / 100.0 + 1.0);             t.AssignAutoProfitTargetPrice(price);          }          if (t.PositionTag == 0)          {             price = executionPrice * (1.0 - Parameters[9].AsDouble / 100.0);             t.AssignAutoStopLossPrice(price);          }          if (t.PositionTag == 1)          {             price = executionPrice * (1.0 - Parameters[13].AsDouble / 100.0);             t.AssignAutoProfitTargetPrice(price);          }          if (t.PositionTag == 1)          {             price = executionPrice * (1.0 + Parameters[14].AsDouble / 100.0);             t.AssignAutoStopLossPrice(price);          }          if (t.PositionTag == 1)          {             price = executionPrice * (1.0 - Parameters[15].AsDouble / 100.0);             t.AssignAutoProfitTargetPrice(price);          }          if (t.PositionTag == 1)          {             price = executionPrice * (1.0 + Parameters[16].AsDouble / 100.0);             t.AssignAutoStopLossPrice(price);          }       }       private int count;       private int HMAconccount;       private bool mc;       private IndicatorBase HMA_Conc;          private IndicatorBase HMAx0;       private IndicatorBase OBVxLong;       private IndicatorBase EMAofOBVLong;       private double value;       private double value2;       private bool trailing;       private IndicatorBase smoother4;       private double value3;       private double value4;       private bool trailing2;       private IndicatorBase ChikouSpan;       private IndicatorBase HMAPrice;       private int count2;       private bool mc2;       private int savedIndex;       private bool flag;       private IndicatorBase HMAConcLower;       private double value5;       private bool trailing3;       private double value6;       private double PctOverBreakeven;       private double PriceAtEntry;       private double SLcounter;             private bool trailing4;       WLFont _fontCZ = new WLFont("Arial", 10, true); //Font (Bigger)       WLFont _fontCF = new WLFont("Arial", 12, true); //Font       private Transaction _transaction; private List<IndicatorBase> _startIndexList = new List<IndicatorBase>(); } }
0
- ago
#4
I didn't run your code, but examined it. The scoping looks OK. I'd take a look at:

CODE:
if (bars.Close[index] <= foundPosition0.EntryPrice * 1.001) { ClosePosition(foundPosition0, OrderType.Limit, PriceAtEntry, "Sell at Breakeven"); DrawBarAnnotation(TextShape.SquareFilled, index, true, WLColor.Yellow, 14, true, _fontCZ); SLcounter = 0; }


Check if it is closing the position after one bar. You're using a limit order with price at the signal bar's close.

Keep in mind, from the WL help:

QUOTE:
○ Limit - executes the simulated order if the bar's price penetrates below (for long entry or short exit) or above (for short entry or long exit) the order price. If the open price of the bar gaps beyond the limit price, the simulated order is filled at the bar's open price.
0
- ago
#5
Note this is a snippet of the code and may not run perfectly (there is more to the strategy that I can't disclose on the boards).

As for the limit, I've run variants of this code without this new wrinkle and it runs great. I'm trying to improve it because I'm giving back too much of the gains with trailing stop-loss targets, hence the interest to reset it once it hits a certain level of return.
0

Reply

Bookmark

Sort