Summary: This trading strategy script generates orders based on the average signal from two analysts. Rules: 1. Buy Market - When the average analyst signal is higher than a specified threshold. 2. Sell Market - When the average analyst signal is lower than a specified threshold. 3. Sell Short Market - When the average analyst signal is lower than a specified threshold. 4. Buy To Cover Market - When the average 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 _analystKeys1(SymbolCount() - 1) ' Use for holding analyst keys based on the second specified analyst key, indexed by symbol index. Redefine _analystKeys2(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. _analystKeys1(i) = AnalystCopy(analystKey1, i) ' Add the analyst to the symbol chart. ChartAnalyst(i, 0, _analystKeys1(i),1, 2) ' Create an analyst for each account symbol, based on the second specified analyst. _analystKeys2(i) = AnalystCopy(analystKey2, i) ' Add the analyst to the symbol chart. ChartAnalyst(i, 0, _analystKeys2(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(_analystKeys1(symbolIndex)) ' Get the second analyst signal value for the specified symbol index. Define signalValue2 As Integer = AnalystSignal(_analystKeys2(symbolIndex)) ' Use for the average signal value. Define signal As Number = (signalValue1 + signalValue2) / 2 ' If the average analyst signal value is above the buy threshold. If (signal >= _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, "Analyst signals average " & signal) 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, "Analyst signals average " & signal) ' 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 ' If the average analyst signal value is below the sell threshold. If (signal <= _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, "Analyst signals average " & signal) 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, "Analyst signals average " & signal) ' 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