mrsic8
 ( 8.64% )
- ago
Hello,

is there a code snippet for exit with "CoverAtProfitTarget". I couldn't find one or can someone give me a hint how to correct the exit-part.

I'd like exit after 10% profit (using a slider). Something is wrong.

CODE:
using finantic.Indicators; using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; namespace WealthScript1 { public class srocLong : UserStrategyBase { public srocLong() { AddParameter("PeriodSROC", ParameterType.Int32, 15, 2, 100, 2); AddParameter("DownPercentage", ParameterType.Double, -20, -32, -2, 2); AddParameter("ProfitTargetLong", ParameterType.Double, 10, 2, 38, 4); } public override void Initialize(BarHistory bars) { _indicator = new SROC(bars.Close, Parameters[0].AsInt); PlotIndicator(_indicator, WLColor.Black, PlotStyle.ThickLine); StartIndex = 10; } public override void Execute(BarHistory bars, int idx) { { if (!HasOpenPosition(bars, PositionType.Long)) { if (_indicator[idx] < Parameters[1].AsDouble) { PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "BuyAtMarket"); } else { var isLastPositionAcitve = HasOpenPosition(bars, PositionType.Long); if (isLastPositionAcitve) { Position p = LastPosition; if (p.PositionType == PositionType.Long) { if (p.EntryBar >= p.EntryPrice * Parameters[2].AsDouble - (10.00 / 100.0)); } ClosePosition(p, OrderType.Limit, p.EntryPrice, "CoverAtProfitTarget"); } } } } } private IndicatorBase _indicator; } }
0
425
Solved
6 Replies

Reply

Bookmark

Sort
- ago
#1
A snippet for cover at profit target is always at your fingertips: knock out a strategy from Blocks and click "Open C# strategy code".

Damir, I notice you're putting the exit logic inside the "if(!HasOpenPosition...") block again. That block checks if an open position exists so you cannot get exits to work. Haven't we already gone through this on this forum before?

Here's an example of a correctly formatted logic in one of your previous topics:
https://www.wealth-lab.com/Discussion/WriteToDebugLog-Results-7974
0
mrsic8
 ( 8.64% )
- ago
#2
Hi Eugene,

yes, i have done this from the Blocks. But there working with "foundPosition0, bool condition and with no slider (addParameter)."
0
- ago
#3
Maybe at this point to make your learning curve less steep you stick to Blocks for now? There you can expose strategy parameters, utilize the SROC etc.
0
mrsic8
 ( 8.64% )
- ago
#4
I don't know if I understand you correctly, should I stop asking questions about programming?
0
Cone8
 ( 26.65% )
- ago
#5
There's no problem, but the answer is in the first line of Eugene's first response. He's just saying that a quick way to get an example is to put it in a Building Block and "Open C# Strategy Code". The result is somewhat verbose, but the programming is "correct".

Here's an edit of what you were trying to do... it should help you learn from the many mistakes. I added a time-based exit too and a few other bells and whistles.

CODE:
using finantic.Indicators; using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; namespace WealthScript3 {    public class srocLong : UserStrategyBase    {       public srocLong()       {          AddParameter("PeriodSROC", ParameterType.Int32, 15, 1, 100, 2);          AddParameter("DownPercentage", ParameterType.Double, -20, -32, -2, 2);          AddParameter("ProfitTargetLong", ParameterType.Double, 10, 2, 38, 4);       }       public override void Initialize(BarHistory bars)       {          StartIndex = Parameters[0].AsInt;                   _indicator = SROC.Series(bars.Close, Parameters[0].AsInt);          _pct = 1 + Parameters[2].AsDouble / 100d;                    PlotIndicator(_indicator, WLColor.Black, PlotStyle.ThickLine);          DrawHorzLine(Parameters[1].AsDouble, WLColor.Red, 1, LineStyle.Dashed, _indicator.PaneTag);                          PlotStopsAndLimits(3);                }       public override void Execute(BarHistory bars, int idx)       {                if (!HasOpenPosition(bars, PositionType.Long))          {             if (_indicator.CrossesUnder(Parameters[1].AsDouble, idx))             {                PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "BuyAtMarket");             }          }          else          {             Position p = LastPosition;             if (idx - p.EntryBar + 1 >= 30)                ClosePosition(p, OrderType.Market, 0, "Time-based");             else                ClosePosition(p, OrderType.Limit, p.EntryPrice * _pct, "SellAtProfitTarget");          }       }       private IndicatorBase _indicator;       private double _pct;    } }
1
Best Answer
mrsic8
 ( 8.64% )
- ago
#6
Hello Cone,

this is great for my understanding. Thanks.
1

Reply

Bookmark

Sort