- ago
Trying a plane vanilla option Buy Call transaction using below code from the options blog (brilliant by the way!). The code is able to find the option symbol and displays the underlying symbol; however stops with an error at "placetrade (line 49). IB is connected in order manager. Obviously I must be doing something wrong - any suggestions ? Thank you




CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; using WealthLab.InteractiveBrokers; namespace WealthScript1 {    public class ATMOption : UserStrategyBase    {       public override void Initialize(BarHistory bars)       {          double price = bars.Close[bars.Count - 1];          DateTime currentDate = bars.DateTimes[bars.Count - 1];          int minDaysToExpiry = 5;          bool weeklies = false;          bool closestStrike = true;          bool allowExpired = false;          string optSym = IBHistorical.Instance.GetOptionsSymbol(bars, OptionType.Call, price, currentDate, minDaysToExpiry, weeklies, allowExpired, closestStrike);          DrawHeaderText("ATM Call: " + optSym, WLColor.NeonGreen, 14);          if (bars.Scale.IsIntraday)          {             _obars = GetHistory(bars, optSym);          }          else // assuming Daily          {             // request 3 months of 30 minute bars and synchronize with the non-intraday scale             _obars = WLHost.Instance.GetHistory(optSym, HistoryScale.Minute30, currentDate.AddMonths(-12), currentDate, 0, null);             if (_obars != null)             {                // compress to Daily bars                _obars = BarHistoryCompressor.ToDaily(_obars);                // sync the bars with the non-intraday data                _obars = BarHistorySynchronizer.Synchronize(_obars, bars);             }          }          if (_obars == null)          {             WriteToDebugLog("Could not get option data, check the Log Viewer for more info.");             return;          }          else          {             PlaceTrade(_obars, TransactionType.Buy, OrderType.Market, 0, 1, "Buy Call"); // Position Tag 1          }          PlotBarHistory(_obars, optSym);       }       //execute the strategy rules here, this is executed once for each bar in the backtest history       public override void Execute(BarHistory bars, int idx)       { }       BarHistory _obars; // the option's BarHistory       int _hash = -1;    } }
0
140
Solved
2 Replies

Reply

Bookmark

Sort
- ago
#1
never mind. all good , pls disregard the prior issue

I moved the trade to Execute and got the transaction through to IB

CODE:
         else          { //            PlaceTrade(_obars, TransactionType.Buy, OrderType.Market, 0, 1, "Buy Call"); // Position Tag 1          }          PlotBarHistory(_obars, optSym);       }       //execute the strategy rules here, this is executed once for each bar in the backtest history       public override void Execute(BarHistory bars, int idx)       {          PlaceTrade(_obars, TransactionType.Buy, OrderType.Market, 0, 1, "Buy Call"); // Position Tag 1       }       BarHistory _obars; // the option's BarHistory       int _hash = -1;
0
Best Answer
Cone8
 ( 25.44% )
- ago
#2
Okay, but but if you just want to Signal once without creating trades on every bar of the chart, check for the last bar of the chart -

CODE:
public override void Execute(BarHistory bars, int idx) { if (idx == bars.Count - 1) {             PlaceTrade(_obars, TransactionType.Buy, OrderType.Market, 0, 1, "Buy Call"); } }
0

Reply

Bookmark

Sort