- ago
Here is some code from a closed thread I found on Wealthlab. Can we create a building block or do we have to create code.

Looking to buy on Monday open and Sell on same day (5 minutes before close) with a stop loss.

Here is the older code. Line 15, 16 for PlotTimeSeries is not working.

Thank you,

Larry

CODE:
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; namespace WealthScript1 {    public class MyStrategy : UserStrategyBase    {       public override void Initialize(BarHistory bars)       {          indicator = bars.Low;          source = new Lowest(((bars.Low >> 1) * 0.988), 2);          stop = source * (1.0 - (4.00 / 100.0));          PlotTimeSeries(source, "Lowest", "Price", Color.Blue);          PlotTimeSeries(stop, "Lowest Stop", "Price", Color.Red);          //StartIndex = 8;       }       public override void Execute(BarHistory bars, int idx)       {          PlaceTrade(bars, TransactionType.Buy, OrderType.Market, source[idx]);          PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose);       }       //private members       private TimeSeries indicator, source, stop;    } }

0
207
Solved
3 Replies

Reply

Bookmark

Sort
Cone8
 ( 24.99% )
- ago
#1
No, it's not possible.

You can add your vote to the same-bar exit feature request and add a note about the MarketClose order. https://www.wealth-lab.com/Discussion/Same-Bar-Profit-Stops-for-Building-Blocks-9326
0
Best Answer
- ago
#2
I got the buy on open and sell on same day close working fine. However, I am getting an error on line 33 for my stop loss. Can you please take a look?

33: The name "EntryPrice" does not exist in the current context.

Thank you,
Larry

CODE:
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; namespace WealthScript3 {    public class MyStrategy : UserStrategyBase    {       public MyStrategy() : base()       {          AddParameter("Stop Loss", ParameterType.Double, 10, 2, 10, 2);          StartIndex = 290;       }                            public override void Initialize(BarHistory bars)       {          indicator = bars.Low;          source = new Lowest(((bars.Low >> 1) * 0.988), 2);          stop = source * (1.0 - (4.00 / 100.0));       }       public override void Execute(BarHistory bars, int idx)       {                PlaceTrade(bars, TransactionType.Buy, OrderType.Market);          Backtester.CancelationCode = 220;          value = 1.0 - (Parameters[0].AsDouble / 100.0);                    if(PlaceTrade(bars, TransactionType.Sell, OrderType.Stop, EntryPrice * value) == null)             PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose);       }       //private members       private double value;    } }

0
Cone8
 ( 24.99% )
- ago
#3
1. You have to declare all variables.
2. PlaceTrade will never return a null Transaction
3. That isn't the way to implement same-bar stops (or profits). See Transaction > AutoStopLossPrice
4. This type of same-bar trade interaction - a same-bar stop and/or sell at the close of the entry bar - is not currently supported by the Backtester.
0

Reply

Bookmark

Sort