Summary: This trading strategy script generates orders based on the signals of two analysts. Rules: 1. Buy Market - When the first analyst signal is higher than a specified threshold. 2. Sell Market - When the second analyst signal is lower than a specified threshold. 3. Sell Short Market - When the second analyst signal is lower than a specified threshold. 4. Buy To Cover Market - When the first analyst signal is higher than a specified threshold.
' Assign the script parameters to script variables. _buyAbove = buyAbove _sellBelow = sellBelow _enableShorting = enableShorting _stopLoss = stopLoss _takeProfit = takeProfit _trailingStop = trailingStop _isPercent = isPercent ' Use for holding analyst keys based on the first specified analyst key, indexed by symbol index. Redefine _buyAnalystKeys(SymbolCount()-1) ' Use for holding analyst keys based on the second specified analyst key, indexed by symbol index. Redefine _sellAnalystKeys(SymbolCount()-1) ' Iterate over all of the account symbols. For i As Integer = 0 To SymbolCount() - 1 ' Create an analyst for each account symbol, based on the first specified analyst. _buyAnalystKeys(i) = AnalystCopy(buyAnalystKey, i) ' Add the analyst to the symbol chart. ChartAnalyst(i, 0, _buyAnalystKeys(i),1,2) ' Create an analyst for each account symbol, based on the second specified analyst. _sellAnalystKeys(i) = AnalystCopy(sellAnalystKey, i) ' Add the analyst to the symbol chart. ChartAnalyst(i, 0, _sellAnalystKeys(i),1,2) Next
' Check whether the current symbol is a futures contract which isn 't a front month contract If (SymbolInstrument(symbolIndex) = C_FUTURES And Not SymbolIsFrontMonthContract(symbolIndex)) Return End If ' Get the first analyst signal value for the specified symbol index. Define signalValue1 As Integer = AnalystSignal(_buyAnalystKeys(symbolIndex)) ' Get the second analyst signal value for the specified symbol index. Define signalValue2 As Integer = AnalystSignal(_sellAnalystKeys(symbolIndex)) ' If the first analyst signal value is above the buy threshold. If (signalValue1 >= _buyAbove) Then ' If a short position exists then generate a buy to cover order. If (_enableShorting And PositionExists(C_OPEN, symbolIndex, C_SHORT)) Then ' Generate a buy to cover market order. BrokerMarket(C_BUY_TO_COVER,symbolIndex, 0, C_DAY, "First analyst signal " & signalValue1) End If ' If no long open positions exist. If (Not PositionExists(C_OPEN, symbolIndex, C_LONG)) ' Generate a buy market order. Define orderIndex As Number = BrokerMarket(C_BUY,symbolIndex, 0, C_DAY, "First analyst signal " & signalValue1) ' Set a stop loss on the order. BrokerSetStopLossPercent(orderIndex, _stopLoss, True) ' Set a take profit on the order. BrokerSetTakeProfitPercent(orderIndex, _takeProfit, True) ' Set a trailing stop loss on the order. BrokerSetTrailingStopLoss(orderIndex, _isPercent, _trailingStop) End If ' If the second analyst signal value is below the sell threshold. ElseIf (signalValue2 <= _sellBelow) Then ' If a long open position exists. If (PositionExists(C_OPEN, symbolIndex, C_LONG)) ' Generate a sell market order. BrokerMarket(C_SELL,symbolIndex, 0, C_DAY, "Second analyst signal " & signalValue2) End If ' If no short open positions exist. If (_enableShorting And Not PositionExists(C_OPEN, symbolIndex, C_SHORT)) Then ' Generate a sell short market order. Define orderIndex As Number = BrokerMarket(C_SELL_SHORT,symbolIndex, 0, C_DAY, "Second analyst signal " & signalValue2) ' Set a stop loss on the order. BrokerSetStopLossPercent(orderIndex, _stopLoss, True) ' Set a take profit on the order. BrokerSetTakeProfitPercent(orderIndex, _takeProfit, True) ' Set a trailing stop loss on the order. BrokerSetTrailingStopLoss(orderIndex, _isPercent, _trailingStop) End If End If