- ago
This is a feature request for trade entry and exit panel display annotations in the same triangle direction and color scheme as WL v6.

I much prefer seeing all the trade entries in gray; and the exits in the green/red (profit/loss) color scheme. This makes it much easier to visual instantly see if a developing strategy is working as desired, particularly strategies that open more than one position per symbol. I note it v7 the dot placed at the exact exit price is in the green/red color scheme, but this is obscured when there are multiple exits on the same bar (only the topmost dot is visible).

P.S. I hope I am entering wish list feature requests in the appropriate manner.
3
1,034
Solved
8 Replies

Reply

Bookmark

Sort
- ago
#1
Hi,

Below is the core code I use for my strategies. It colors the background as you ask. It uses a placeholder for the trading of a simple sma.

I hope this helps.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; //add your libraries here namespace WealthScript3 { public class HelloWorldStrategy : UserStrategyBase {       //declare parameter variables       int max_days;       double profit_target, stop_loss;       //declare other variables       //time series variables are used for calculated bar data       TimeSeries sma_values;       //the base procedure must have the same name as the class       public HelloWorldStrategy() : base()       {          //add parameters here          AddParameter("Profit Target", ParameterTypes.Double, 2, 1, 3, 1);          AddParameter("Stop Loss", ParameterTypes.Double, 2, 1, 3, 0.5);          AddParameter("Max Days", ParameterTypes.Int32, 2, 2, 5, 1);                    //add strategy specific parameters here       }                     //create indicators and other objects here, this is executed prior to the main trading loop     //also do plotting here       public override void Initialize(BarHistory bars) {                    //assign parameters to variables          profit_target = (double)Parameters[0].Value;          stop_loss = (double)Parameters[1].Value;          max_days = (int)Parameters[2].Value;          //create new series that can be pre-calculated here          sma_values = SMA.Series(bars.Close, 14);          //create new series that need to be calculated here          //ignore data before the StartIndex          StartIndex = 1;          //put headers on the chart here          DrawHeaderText("SMA days", Color.Red, 12);          //do all the plotting of anything that can be pre-calculated here     //   plot anything that needs to be calculated below in Cleanup          PlotTimeSeries(sma_values, sma_values.Description, "Price", Color.Red, PlotStyles.Line);       }       //color the background based on the level of profit/loss       public void WinLossBackground(BarHistory bars, int idx)       {          int profit_level = 0;          System.Drawing.Color color;          // use code_s to find the worst trade for overlapping positions          double profit = 0.0, code_s = 0.0;          // First determine the worst profit level at each bar based on the positions          List<Position> positions = GetPositions();          foreach (Position p in positions)          {             // foreach (Position p in OpenPositions) won't work because it skips the ExitBar             if (!(p.IsOpen || idx == p.ExitBar))                continue;             //can't use p.Profit because the profit isn't calculated yet so it just gives open cost             profit = p.ProfitPctAsOf(idx);             // Profit/Loss in percent             if ((profit > 5))                profit_level = (int)-1;             else if ((profit > 0.5))                profit_level = (int)-2;             else if ((profit > -0.5))                profit_level = (int)-3;             else if ((profit > -5))                profit_level = (int)-4;             else                // profit <= -5.0                profit_level = (int)-5;             double old_code = 0.0;             // keep most pessimistic in overlapping trades             old_code = (double)code_s;             if ((profit_level < old_code))                code_s = profit_level;          } // foreach position          // Now set the background color for the bar          int color_code = 0;          color_code = (int)Math.Floor(code_s);          switch (color_code)          {             case -1:                color = Color.LimeGreen;                break;             case -2:                color = Color.LightGreen;                break;             case -3:                color = Color.LightYellow;                break;             case -4:                color = Color.MistyRose;                break;             case -5:                color = Color.LightPink;                break;             default:                color = Color.White;                break;          } // switch   color_code          SetBackgroundColor(bars, idx, color);       } // WinLossBackground       double RoundPrice(double price, double decimal_points)       {          return Math.Round(price * Math.Pow(10, decimal_points)) / Math.Pow(10, decimal_points);       }       //use to fix price to sell short or buy long       double FloorPrice(double price, double decimal_points)       {          return Math.Floor(price * Math.Pow(10, decimal_points)) / Math.Pow(10, decimal_points);       }       //use to fix price to sell long or buy short       double CeilingPrice(double price, double decimal_points)       {          return Math.Ceiling(price * Math.Pow(10, decimal_points)) / Math.Pow(10, decimal_points);       }       public void SellStrategy(BarHistory bars, int idx, double ProfitTarget, int days)       {          //go through each position and sell          foreach (Position p in OpenPositions)          {             // Set Close signals             if (p.PositionType == PositionType.Long)             {                //End trade after a fixed number of days                if ((idx + 1) - p.EntryBar > days)                   ClosePosition(p, OrderType.Stop, bars.Close[idx] - 0.01, "DaysExpired");                else                {                   if (bars.Close[idx] > (double)(p.EntryPrice * (1 + profit_target / 100.0)))                      ClosePosition(p, OrderType.Stop, bars.Close[idx] - 0.01, "Close Gain");                   else                   {                      if (bars.Close[idx] < (double)(p.EntryPrice * (1 - stop_loss / 100.0)))                         ClosePosition(p, OrderType.Market, 0, "Close Loss");                      else                      {                         //when placing both orders this can be a conditional one-cancels-other                         ClosePosition(p, OrderType.Limit, CeilingPrice(p.EntryPrice * (1 + profit_target / 100.0), 2), "Limit");                         ClosePosition(p, OrderType.Stop, CeilingPrice(p.EntryPrice * (1 - stop_loss / 100.0), 2), "Stop Loss");                      }                   }                } // if expired long             } // if long             else             {                if ((idx + 1) - p.EntryBar > days)                   ClosePosition(p, OrderType.Stop, bars.Close[idx] + 0.01, "DaysExpired");                else                {                   if (bars.Close[idx] > (double)(p.EntryPrice * (1 - profit_target / 100.0)))                      ClosePosition(p, OrderType.Stop, bars.Close[idx] + 0.01, "Close Gain");                   else                   {                      if (bars.Close[idx] < (double)(p.EntryPrice * (1 + stop_loss / 100.0)))                         ClosePosition(p, OrderType.Market, 0, "Close Loss");                      else                      {                         //when placing both orders this can be a conditional one-cancels-other                         ClosePosition(p, OrderType.Limit, FloorPrice(p.EntryPrice * (1 - profit_target / 100.0), 2), "Limit");                         ClosePosition(p, OrderType.Stop, FloorPrice(p.EntryPrice * (1 + stop_loss / 100.0), 2), "Stop Loss");                      }                   }                } // if expired short             } // if short          } // OpenPositions       } // WaveRiderSell       public void BuyStrategy(BarHistory bars, int idx)       {          if ((OpenPositions.Count == 0) && (bars.Close[idx] < sma_values[idx]))             PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, "Initial");       }       public override void Cleanup(BarHistory bars)       {          //do all the plotting of any series that need to be calculated by bar here       }       //execute the strategy rules here, this is executed once for each bar in the backtest history       public override void Execute(BarHistory bars, int idx) {          // put the sell orders or sell procedure here          SellStrategy(bars, idx, profit_target, max_days);          //put the buy orders or buy procedure here          BuyStrategy(bars, idx);          //calculate series that need to be calculated by bar in Execute          //change background colors by bar here          WinLossBackground(bars, idx);           } // Execute           } // class } // namespace
1
- ago
#2
QUOTE:
I much prefer seeing all the trade entries in gray; and the exits in the green/red (profit/loss) color scheme.

While I don't necessarily agree on that legacy color scheme being superior, maybe a way to implement this could be to expose a new ChartDrawingOptions preference to set colors of trade arrows in C# Strategy code (exclusively). Of course provided there are enough votes to support this request. But let's wait for Dion's decision.
1
- ago
#3
I totally agree; I like how WL6 handles the colored trading arrows much better than WL7.

If you can either make the WL6 behavior the default or add a simple setting to make it work like WL6, that would be welcomed.

Jumping through hoops and writing a lot of code (as in Reply# 1) is not an ideal solution. Someone must really disliked the default WL7 color behavior to have developed all that overriding code.
0
Glitch8
 ( 10.62% )
- ago
#4
Adding an option to Chart Preferences is certainly feasible, but you all need to vote for this request!
3
- ago
#5
Thanks for the background color idea and code. I'll implement that for now. I like the 'expose a new ChartDrawingsOptions' idea the best so far. It's better (for me) than the background idea because it can handle multiple exits with mixed profitability on the same bar.
0
Glitch8
 ( 10.62% )
- ago
#6
Since this has some support now and I need a break from the mind-numbing optimization related work, adding this in for Build 30!
2
Glitch8
 ( 10.62% )
- ago
#7
Here's the old arrows styles you know and love ...

3
Best Answer
- ago
#8
Awesome! Those trade arrows are a thing of beauty. I especially like the green ones :). I think the general preferences checkbox implementation was an even better way to go too, it certainly works for my uses cases. Thank you.
4

Reply

Bookmark

Sort