<span style="font-family:Verdana, Geneva, Helvetica, Arial, sans-serif;font-size:13px"><span>Normally, I would calculate a spread ahead of time and treat it like a single stock, but in the interest of creating examples for quantstrat, I created a strategy that will enter each leg separately.   </span><br>

<br><span>The idea is to calculate a <b style="background-color:rgb(255, 255, 102);background-repeat:initial initial">ratio</b> of the 2 stocks (StockA / StockB).  If that <b style="background-color:rgb(255, 255, 102);background-repeat:initial initial">ratio</b> goes above it's 2 stdev band, then, when it crosses back through it, sell StockA and buy Stock B.  If the <b style="background-color:rgb(255, 255, 102);background-repeat:initial initial">ratio</b> goes below it's 2 stdev band, then, when it crosses back above it, buy StockA and sell Stock B.  When the <b style="background-color:rgb(255, 255, 102);background-repeat:initial initial">ratio</b> cross it's moving average, flatten out. </span><br>

<br><span>I needed to make a <b style="background-color:rgb(255, 255, 102);background-repeat:initial initial">rule</b> act differently for different symbols.  i.e. when the <b style="background-color:rgb(255, 255, 102);background-repeat:initial initial">ratio</b> crosses it's upper bound, the <b style="background-color:rgb(255, 255, 102);background-repeat:initial initial">rule</b> for StockA should be buy and the <b style="background-color:rgb(255, 255, 102);background-repeat:initial initial">rule</b> for StockB should be sell.  But, that would only work if the <b style="background-color:rgb(255, 255, 102);background-repeat:initial initial">rule</b> knew which stock it was operating on.  So, the plan was to have an order sizing function check the symbol and return the opposite quantity if it is the second symbol.  (e.g. -100 instead of 100).  </span></span><div>
<span style="font-family:Verdana, Geneva, Helvetica, Arial, sans-serif;font-size:13px"><span><br></span></span></div><div><span style="font-family:Verdana, Geneva, Helvetica, Arial, sans-serif;font-size:13px"><span>But, it is not as easy to change the orderside.</span><br>

<br></span><div><div><span style="font-family:Verdana, Geneva, Helvetica, Arial, sans-serif;font-size:13px"><span><span>I modified osMaxPos and <b style="background-color:rgb(255, 255, 102);background-repeat:initial initial">ruleSignal</b>, but that's not enough.   </span><br>

<br><span>What function writes to the orderbook object? </span><br><span>Is there a better way to do this? </span><br><span>I tried setting orderside with the <<- operator, but that doesn't help. </span><br><br>

<span>My code is attached.  Sorry it's so long, but it has to include custom osMaxPos and <b style="background-color:rgb(255, 255, 102);background-repeat:initial initial">ruleSignal</b> functions.  You'll see in the orderbook for XOM that where orderside is "short" I have a positive orderqty and vice versa. </span></span></span></div>

<div><span style="font-family:Verdana, Geneva, Helvetica, Arial, sans-serif;font-size:13px"><span><span></span><br><span>thanks. </span></span></span></div></div>
</div>