Userland Scripts development - worldexplorer/SquareOne GitHub Wiki

Intro to data structures

  1. any Strategy is a wrapper around Script + ScriptContext + GUI settings around the chart where this Script is running;
  2. by "Script" I mean your Script-derived implementation with trading rules;
  3. full Strategy thing which gets serialized and restored can be found here (and around) https://github.com/worldexplorer/SquareOne/blob/v1.0-dev/Data-debug/Strategies/Sq1.Strategies.Demo.dll/EnterEveryBarCompiled.json
  4. notice that for DLL-instantiated Scripts, the "ScriptSourceCode" is NULL, while for the Scripts that user typed into SourceCodeEditor within SquareOne wil contain whole SourceCode under "ScriptSourceCode" in JSON

Intro to Script lifecycle

  1. Script may want to implement the following methods (copy-paste from Script.cs base class that all userland implementations should be based off):
  • public virtual void InitializeBacktest();
  • public virtual void OnNewQuoteOfStreamingBarCallback(Quote quoteNewArrived);
  • public virtual void OnBarStaticLastFormedWhileStreamingBarWithOneQuoteAlreadyAppendedCallback(Bar barNewStaticArrived);
  • public virtual void OnAlertFilledCallback(Alert alertFilled);
  • public virtual void OnAlertKilledCallback(Alert alertKilled);
  • public virtual void OnAlertNotSubmittedCallback(Alert alertNotSubmitted, int barNotSubmittedRelno);
  • public virtual void OnPositionOpenedCallback(Position positionOpened);
  • public virtual void OnPositionOpenedPrototypeSlTpPlacedCallback(Position positionOpenedByPrototype);
  • public virtual void OnPositionClosedCallback(Position positionClosed);
  1. script gets created by the platform using default Script.ctor() after compilation in Editor by F5/F6 or right after DLL was loaded at application startup (if you see in Tools => Strategies => Sq1.Demo.Strategies then all the Scripts that Sq1.Demo.Strategies.DLL contained are already constructed by Activator.Instantiate()

  2. when it's time to backtest (controlled by Chart => Menu => Backtest), backtester loads the Bars and starts generating quotes (details here https://github.com/worldexplorer/SquareOne/wiki/How-the-backtester-works )

  3. so the backtester invokes these 3 methods for each quote/bar generated (method names are self-explanatory; please open any strategy in Tools => Strategies => Sq1.Demo.Strategies, set breakpoints there https://github.com/worldexplorer/SquareOne/wiki/How-to-set-a-breakpoint and click Chart => Backtest => Backtest Now); 4.1. InitializeBacktest(), 4.2. OnNewQuoteOfStreamingBarCallback(Quote quoteNewArrived), 4.3. OnBarStaticLastFormedWhileStreamingBarWithOneQuoteAlreadyAppendedCallback(Bar barNewStaticArrived)

  4. inside these methods, the strategy calculates whatever it needs using indicators, external DLLs and generates the orders for the platform to execute;

  5. orders get outside through BrokerProvider the user selected for the DataSource for the Symbol currently displayed on the Chart that runs the backtest;

  6. but during the backtest they stay in the platform and MarketSimStreaming.cs simulates order execution;

  7. order-related methods that the Script can invoke are accessible through Script.Executor and Script.Executor.PositionActivator; please take a look at https://github.com/worldexplorer/SquareOne/blob/v1.0-dev/Sq1.Strategies.Demo/TwoMAsCompiled.cs and https://github.com/worldexplorer/SquareOne/blob/v1.0-dev/Sq1.Strategies.Demo/StopLimitTestCompiled.cs respectively;

  8. your BuyAtMarket and ShortAtMarket aren't yet open positions; they are Alerts which may never become Positions if you've put a BuyAtLimit(tooFar);

  9. and these Script methods are invoked by the platform when it's time for the Alert to change its status: 10.1 OnAlertFilledCallback(Alert alertFilled); 10.2. OnPositionOpenedCallback(Position positionOpened);