Summary: This trading strategy script generates entry orders based on the signals of multiple analysts, and exit signals after a specified number of bars. Rules: 1. Buy Market - When the average analyst signal is higher than a specified threshold. 2. Sell Market - When the specified number of bars passed since the trade was opened. 3. Sell Short Market - When the average analyst signal is lower than a specified threshold. 4. Buy To Cover Market - When the specified number of bars passed since the trade was opened.
' Assign the script parameters to script variables. _buyAbove = buyAbove _sellBelow = sellBelow _barsToHold = barsToHold _enableShorting = enableShorting _stopLoss = stopLoss _takeProfit = takeProfit _trailingStop = trailingStop _isPercent = isPercent _analystKeyCount = analystKeys.Length ' Use for holding analyst keys based on the specified analyst key, indexed by symbol index. Redefine _analystKeys(SymbolCount() - 1, _analystKeyCount - 1) ' Iterate over all of the account symbols. For i As Integer = 0 To SymbolCount() - 1 ' Iterate over all of the analyst keys creating copies for the current symbol index. For j As Integer = 0 To _analystKeyCount - 1 ' Create an analyst for each account symbol, based on the current analyst key. _analystKeys(i,j) = AnalystCopy(analystKeys(j), i) ' Add the analyst to the symbol chart. ChartAnalyst(i, 0, _analystKeys(i,j),1, 2) Next 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 average analyst signel value. Define signalValue As Number ' Iterate over all of the analyst keys of the specified symbol index. For i As Integer = 0 To _analystKeyCount - 1 ' Add the analyst signal of the current analyst. signalValue += AnalystSignal(_analystKeys(symbolIndex,i)) Next ' Calculate the average analyst signal. signalValue = signalValue / _analystKeyCount ' If the analyst signal value is above the buy threshold, generate a buy order. If (signalValue >= _buyAbove) Then ' 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 signal " & signalValue) ' Set the maximum number of bars to hold the trade open. BrokerSetMaxBarHold(orderIndex, _barsToHold) ' 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, generate a sell order. ElseIf (signalValue <= _sellBelow) Then ' 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 signal " & signalValue) ' Set the maximum number of bars to hold the trade open. BrokerSetMaxBarHold(orderIndex, _barsToHold) ' 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