- ago
Can you explain why this.PlaceTrade(bars, TransactionType.Short, OrderType.Market) not open short positions ?

For tests I created TestStrategy that open position by MA CrossOver / CrossUder
this.PlaceTrade(bars, TransactionType.Buy OrderType.Market) works fine.
but this.PlaceTrade(bars, TransactionType.Short, OrderType.Market) doesn't work

0
721
Solved
17 Replies

Reply

Bookmark

Sort
- ago
#1
Not enough details to tell.
1
Glitch8
 ( 10.62% )
- ago
#2
It’s working at my end.
1
- ago
#3
CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using WealthLab.ECSXInvest.Indicator; namespace WealthLab.ECSXInvest.Strategy { public class ECSTestStrategy : UserStrategyBase { public ECSTestStrategy():base() { } public TimeSeries Wma_M1 { get; set; } //WMA Wma_M5; public TimeSeries Wma_M5_M1 { get; set; } //WMA Wma_M15; public TimeSeries Wma_M15_M1 { get; set; } //WMA Wma_H1; public TimeSeries Wma_H1_M1 { get; set; } public override void Initialize(BarHistory bars) { BarHistory _bars_M5 = WLHost.Instance.GetHistory(bars.Symbol, HistoryScale.Minute5, DateTime.MinValue, DateTime.MaxValue, 0, null); //BarHistoryCompressor.ToMinute(bars, 5); BarHistory _bars_M15 = WLHost.Instance.GetHistory(bars.Symbol, HistoryScale.Minute15, DateTime.MinValue, DateTime.MaxValue, 0, null); //BarHistoryCompressor.ToMinute(bars, 15); BarHistory _bars_H1 = WLHost.Instance.GetHistory(bars.Symbol, HistoryScale.Minute60, DateTime.MinValue, DateTime.MaxValue, 0, null); //BarHistoryCompressor.ToMinute(bars, 60); #region Wma this.Wma_M1 = new WMA(bars.Close, 16); this.PlotTimeSeriesLine(this.Wma_M1, "WMA_M1", color: WLColor.Blue, paneTag: "Price"); WMA Wma_M5 = new WMA(_bars_M5.Close, 16); this.Wma_M5_M1 = TimeSeriesSynchronizer.Synchronize(Wma_M5, bars); this.PlotTimeSeriesLine(this.Wma_M5_M1, "WMA_M5", color: WLColor.Green, paneTag: "Price"); WMA Wma_M15 = new WMA(_bars_M15.Close, 16); this.Wma_M15_M1 = TimeSeriesSynchronizer.Synchronize(Wma_M15, bars); this.PlotTimeSeriesLine(this.Wma_M15_M1, "WMA_M15", color: WLColor.Purple, paneTag: "Price"); WMA Wma_H1 = new WMA(_bars_H1.Close, 16); this.Wma_H1_M1 = TimeSeriesSynchronizer.Synchronize(Wma_H1, bars); this.PlotTimeSeriesLine(this.Wma_H1_M1, "WMA_H1", color: WLColor.Orange, paneTag: "Price"); #endregion } public override void Execute(BarHistory bars, int idx) { // IN SHORT if (this.Wma_M5_M1.CrossesUnder(this.Wma_M15_M1, idx)) this.PlaceTrade(bars, TransactionType.Short, OrderType.Market); // OUT SHORT if (this.Wma_M5_M1.CrossesOver(this.Wma_M15_M1, idx)) { foreach (Position _pos in this.OpenPositions) { this.ClosePosition(_pos, OrderType.Market, exitSignalName: "short out"); } } } } }
0
- ago
#4
Zero opened positions

0
- ago
#5
But IF works fine, you can see Red lines on screenshot
0
- ago
#6
Is there any checkbox in the settings that enable / disable open of short positions ?
Because when I replace TransactionType.Short by TransactionType.Buy all works fine and I have Long positions
0
Glitch8
 ( 10.62% )
- ago
#7
No. What about position sizing? Is there an NSF position count in the metrics report?

What if you try a basic building block moving average crossover short strategy, does it yield results?
0
- ago
#8
I found the problem it is in "position sizing" , how can I set custom = my own position size ?
Because I have my own risk / position size / SL calculation algorithm
0
Glitch8
 ( 10.62% )
- ago
#9
PlaceTrade returns an instance of the Transaction class. You can set the Quantity of this object.
0
- ago
#10
For Transaction.Quantity there is ok, what I need to set in the "position sizing" interface ?
0
Glitch8
 ( 10.62% )
- ago
#11
it does not matter, you are OVERRIDING whatever was set there.
0
- ago
#12
My tests show that it is not true

To use custom Quantity, especially for Shorts, I need to set :
- Capital : greater or equal to myQuantity x instrument market price
- Position size : greater or equal to instrument market price

Without these settings Shorts not opened


0
Glitch8
 ( 10.62% )
- ago
#13
You’re suffering from a misconception. There’s nothing different or special about how WL8 processes short vs long transactions. Why not post a minimal test case using a symbol we can all access so we can have something concrete to talk about here?
1
Cone8
 ( 4.98% )
- ago
#14
Here's a small modification of your posted strategy that will short 1 share of whatever you're trading. Probably the only way this won't work is if you're using Futures Mode for a futures symbol and have insufficient futures margin.

CODE:
//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 (this.Wma_M5_M1.CrossesUnder(this.Wma_M15_M1, idx))          {             Transaction t = PlaceTrade(bars, TransactionType.Short, OrderType.Market);             t.Quantity = 1;          }          // OUT SHORT          if (this.Wma_M5_M1.CrossesOver(this.Wma_M15_M1, idx))          {             foreach (Position _pos in this.OpenPositions)             {                ClosePosition(_pos, OrderType.Market, exitSignalName: "short out");             }          }       }
1
- ago
#15
Test conditions
1. In Preferences/Backtest : Future mode = is unchecked
2. Symbol US100
3. Test code = see bellow

To open short positions I need

To use custom Quantity for Shorts, I need to set :
- Capital : greater or equal to myQuantity x instrument market price
- Position size : greater or equal to instrument market price

Without these settings Shorts are not opened (see screenshots)

Price of US100 is about 10 800
Quantity set in the code is 20

When I set
- Capital >= 20 x 11 000 = 220 000
- Amount >= 11 000
All works fine

If I set Capital or Amount lower there is no shorts opened => Profit = 0

CODE:
public override void Execute(BarHistory bars, int idx) { // IN SHORT if (this.Wma_M5_M1.CrossesUnder(this.Wma_M15_M1, idx)) { Transaction _trans = this.PlaceTrade(bars, TransactionType.Short, OrderType.Market); _trans.Quantity = 20; this.SetBackgroundColorAllPanes(bars, idx, WLColor.Red); //Transaction _trans2 = this.PlaceTrade(bars, TransactionType.Buy, OrderType.Market); //_trans2.Quantity = 30; //this.SetBackgroundColorAllPanes(bars, idx, WLColor.Green); } // OUT SHORT if (this.Wma_M5_M1.CrossesOver(this.Wma_M15_M1, idx)) { foreach (Position _pos in this.OpenPositions) { this.ClosePosition(_pos, OrderType.Market, exitSignalName: "short out"); } } }


0
Glitch8
 ( 10.62% )
- ago
#16
Yes, you need at least enough to cover the trade in the position size, otherwise WL8 will ignore that trade and it won't make it into the backtest. Once it passes that threshold the amount established will be overridden by whatever Quantity you assign.

Note, this is nothing special about Shorts, it is the same for Long trades.
0
Cone8
 ( 4.98% )
- ago
#17
Alex, "Amount" for Fixed Value sizing is not the price of the instrument you're trading. It's the dollar amount for the entire position size.

Position size (Amount) = Shares (Quantity) x Price

You set 20 shares in your code, so that about a $220,000 size, which is far greater than the $11,000 "Amount".

Change your code to _trans.Quantity = 1; and set the amount to $12,000 so that you have some "leeway". If you set it to 11,000 and the price for 1 contract is 11,001, the trade will fail.
0
Best Answer

Reply

Bookmark

Sort