- ago
I figured I would make a single thread to go over some issues and questions I had about my strategy that I can not seem to figure out.


1. why is this code executing when the next bar is not the last bar of the day?
CODE:
            if (bars.IsLastBarOfDay(idx + 1))             {                SetBarColor(bars, idx, WLColor.Orange);                ClosePositionEod();             }


2. foreach (Position pos in OpenPositions) , will OpenPositions return all positions for all Symbols or only positions for the symbol currently being processed?

3. Is OpenPositions actually checking to see if I have a position with my broker or it is assuming because I see it trying to run logic for positions that were never actually filled on my broker end.


CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; //If n > time and price is lower or higher (meaning it continues to make high or low) then take position or extend time for breakout namespace WealthScript4 {    public class ORB : UserStrategyBase    {       public ORB()       {          AddParameter("Range", ParameterType.Int32, 15, 30, 60, 1);          AddParameter("Profit Target %", ParameterType.Double, 3, 1, 10, 1);          AddParameter("Stop Loss %", ParameterType.Double, 2, 1, 10, 1);       }       //create indicators and other objects here, this is executed prior to the main trading loop       public override void Initialize(BarHistory bars)       {          if(!bars.IsIntraday)             return;          //PlotStopsAndLimits();          range = Parameters[0].AsInt;          target = Parameters[1].AsDouble;          stop = Parameters[2].AsDouble;          int n = bars.Scale.Interval;          num = (range / n) - 1; // the N-minute bar          firstIntradayBar = -1;          lastChartBar = bars.Count - 1;          nextToLastBarTime = Convert.ToInt32(bars.Market.GetCloseTimeMarketTime().ToString("HHmm")).AddIntegerTime(-bars.Scale.Interval);          choppiness = Choppiness.Series(bars, 14);          //PlotIndicator(choppiness,WLColor.Blue);       }       //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 (bars.IntradayBarNumber(idx) == 0)          {             firstIntradayBar = idx;             canEnterToday = true; //   one position per day          }          if (bars.IntradayBarNumber(idx) <= num) // highlight the opening range             SetBarColor(bars, idx, WLColor.Silver);          if (bars.IntradayBarNumber(idx) == num) // get the highest high and the lowest low after first hour          {             HighRange = Highest.Series(bars.High, num + 1)[idx];             LowRange = Lowest.Series(bars.Low, num + 1)[idx];             if (firstIntradayBar > -1)             {                DrawLine(idx, HighRange, firstIntradayBar, HighRange, WLColor.Blue, 1, LineStyle.Dashed);                DrawLine(idx, LowRange, firstIntradayBar, LowRange, WLColor.Red, 1, LineStyle.Dashed);             }          }          if (HasOpenPosition(bars, PositionType.Long) || HasOpenPosition(bars, PositionType.Short))          {             canEnterToday = false;             var dt = bars.DateTimes[idx].Date;             //   Exit last bar of day             if (bars.IsLastBarOfDay(idx + 1))             {                SetBarColor(bars, idx, WLColor.Orange);                ClosePositionEod();             }             else             {                //   Stop Loss (optimizable)                //   Profit Target (optimizable)                ClosePositionProfitStop();             }          }          else          {             if (bars.IntradayBarNumber(idx) >= num && (!bars.IsLastBarOfDay(idx) || !bars.IsLastBarOfDay(idx +1)) && canEnterToday && choppiness[idx] < 65)             {                PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, HighRange + .01,1,"long entry");                PlaceTrade(bars, TransactionType.Short, OrderType.Stop, LowRange - .01,1,"short entry");             }          }       }       public void ClosePositionProfitStop()       {          foreach (Position pos in OpenPositions)          {             if (pos.PositionType == PositionType.Long)             {                ProfitTgt = pos.EntryPrice * (1 + target / 100.0d);                StopPrice = pos.EntryPrice * (1 - stop / 100.0d);                                WLHost.Instance.AddLogItem($"{this.StrategyName}", $"ClosePositionProfitStop for {pos.Symbol} initial {pos.PositionType} entry at {pos.EntryPrice}", WealthLab.Core.WLColor.LightYellow);                                ClosePosition(pos, OrderType.Stop, StopPrice, "Stop Loss");                ClosePosition(pos, OrderType.Limit, ProfitTgt, "Profit Target");             }             else             {                ProfitTgt = pos.EntryPrice * (1 - target / 100.0d);                StopPrice = pos.EntryPrice * (1 + stop / 100.0d);                                WLHost.Instance.AddLogItem($"{this.StrategyName}", $"ClosePositionProfitStop for {pos.Symbol} initial {pos.PositionType} entry at {pos.EntryPrice}", WealthLab.Core.WLColor.LightYellow);                                ClosePosition(pos, OrderType.Stop, StopPrice, "Stop Loss");                ClosePosition(pos, OrderType.Limit, ProfitTgt, "Profit Target");             }          }       }       public void ClosePositionEod()       {          foreach (Position pos in OpenPositions)          {             WLHost.Instance.AddLogItem($"{this.StrategyName}", $"ClosePositionEod for {pos.Symbol} initial {pos.PositionType}", WealthLab.Core.WLColor.LightYellow);             ClosePosition(pos, OrderType.Market,0,"Exit at open of last bar");          }       } public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice) {          if (t.SignalStatus == SignalStatus.Filled || t.SignalStatus == SignalStatus.PartialFilled)          {             WLHost.Instance.AddLogItem($"{this.StrategyName}", $"AssignAutoStopTargetPrices for {t.Symbol} initial {t.TransactionType} entry at {executionPrice}", WealthLab.Core.WLColor.LightYellow);             if (t.PositionType == PositionType.Long)             {                t.AutoProfitTargetPrice= executionPrice * (1 + target / 100.0d);                t.AutoStopLossPrice = executionPrice * (1 - stop / 100.0d);             }             else             {                t.AutoProfitTargetPrice = executionPrice * (1 - target / 100.0d);                t.AutoStopLossPrice = executionPrice * (1 + stop / 100.0d);             }          } } //declare private variables below int lastChartBar, firstIntradayBar, range, num, nextToLastBarTime;       bool canEnterToday = false;       double HighRange = 0, LowRange = 0, target = 0, stop = 0, ProfitTgt = 0, StopPrice = 0;       Choppiness choppiness;    } }



0
106
4 Answers

Reply

Bookmark

Sort
- ago
#1
1. It's only intended to be used on intraday bar scales. If your strategy is not applied to an EOD chart, your question lacks some context to answer it.

2. If you type OpenPositions in the QuickRef, you will find two methods there. I'm sure if you just look at their names this is going to give an instant answer.

3. OpenPositions checks backtested positions.
0
- ago
#2
1. I am running intraday 15m and it is still hitting that code block as you can see in the logs at 7:45

2. I have checked the references prior to messaging In the docs it says for that BarHistory being processed however I noticed it is returning all positions and not just for that symbol.

3. Is there a way to check for actually open positions as opposed to what the backtest assumes?
0
- ago
#3
2. Do you mean to say that OpenPositions is acting as OpenPositionsAllSymbols should?
0
- ago
#4
That is my observation, that is why I asked the question. I can paste my logs in here if that would be helpful but I have 12 symbols for this strategy. And on each iteration it returns all positions for all symbols.

Obviously I am going based on my logs so I might be wrong but you can see it calls ClosePositionProfitStop for each symbol then a millisecond later does it again?

0

Reply

Bookmark

Sort