RSI_now_sb_ok
RSISimple Buy/Sell Test, Default: Sell at 70–100, Buy at 0–30
RSISimple Buy/Sell Test, Default: Sell at 70–100, Buy at 0–30
Parameters
| Name | Default | What it controls |
|---|---|---|
| RSIPeriod | 14 | RSItimeframe |
| SlidePrice | 0.3 | Order sliding value |
| RSIBuyL | 50 | RSIBuying point (low) |
| RSIBuyH | 50 | RSIBuying point (high) |
| RSISellL | 50 | RSISelling point (low) |
| RSISellH | 50 | RSISelling point (high) |
| Interval | 1000 | Retry on error (milliseconds) |
Source JavaScript
function adjustFloat(v) {
return Math.floor(v*1000)/1000;
}
function cancelPendingOrders() {
while (true) {
var orders = exchange.GetOrders();
if (orders) {
if (orders.length == 0) {
break;
}
for (var j = 0; j < orders.length; j++) {
exchange.CancelOrder(orders[j].Id);
Sleep(Interval);
}
} else {
Sleep(Interval);
}
}
}
function LogProfitTotal(initAcnt, acnt, tck) {
var prft = acnt.Balance - initAcnt.Balance + (acnt.Stocks - initAcnt.Stocks) * tck.Last;
LogProfit(prft, "#ff0000");
}
var STATE_WAIT_IDLE = 0;
var STATE_BUY = 1;
var STATE_SELL = 2;
var STATE_WAIT_BUY = 3;
var STATE_WAIT_SELL = 4;
var STATE_BUY_COVER = 5;
var STATE_SELL_COVER = 6;
var State = STATE_WAIT_IDLE;
var InitAccount = null;
var EnterAcnt = null;
function MustGetAccount() {
var account;
while (!(account = exchange.GetAccount())) {
Sleep(Interval);
}
return account;
}
function onTick(exchange) {
var records = exchange.GetRecords();
if (!records || records.length < (RSIPeriod + 5)) {
return;
}
rsi = TA.RSI(records, RSIPeriod);
var rsiValue0 = rsi[records.length - 1];
var rsiValue1 = rsi[records.length - 2];
var rsiValue2 = rsi[records.length - 3];
//Open position condition judgment
if (State == STATE_WAIT_IDLE && rsiValue1 >= RSIBuyL && rsiValue2 <= RSIBuyH && rsiValue1 > rsiValue2) {
State = STATE_BUY;
EnterAcnt = MustGetAccount();
} else if (State == STATE_WAIT_IDLE && rsiValue2 >= RSISellL && rsiValue1 <= RSISellH && rsiValue2 > rsiValue1) {
State = STATE_SELL;
EnterAcnt = MustGetAccount();
} else if (State != STATE_BUY && State != STATE_SELL && State != STATE_WAIT_BUY && State != STATE_WAIT_SELL && State != STATE_BUY_COVER && State != STATE_SELL_COVER){
return;
}
// Buy or Sell, Cancel pending orders first
cancelPendingOrders();
var account = MustGetAccount();
var ticker = exchange.GetTicker();
if (!account || !ticker) {
return;
}
//Execute Open Position
if (State == STATE_BUY) {
var price = ticker.Last + SlidePrice;
var amount = adjustFloat(account.Balance / price);
if (amount >= exchange.GetMinStock()) {
exchange.Buy(price, amount);
}
else
{
// No money, wait sell and log profit
Log("<--- Buy");
State = STATE_WAIT_SELL;
return;
}
} else if(State == STATE_SELL) {
if (account.Stocks > exchange.GetMinStock()) {
exchange.Sell(ticker.Last - SlidePrice, account.Stocks);
}
else
{
// No stocks, wait buy and log profit
Log("<--- Sell");
State = STATE_WAIT_BUY;
return;
}
}
if(State != STATE_WAIT_BUY && State != STATE_WAIT_SELL && State != STATE_BUY_COVER && State != STATE_SELL_COVER)
return;
//Close position condition judgment
if (State == STATE_WAIT_BUY && rsiValue1 >= RSIBuyL && rsiValue2 <= RSIBuyH && rsiValue1 > rsiValue2) {
State = STATE_BUY_COVER;
} else if(State == STATE_WAIT_SELL && rsiValue2 >= RSISellL && rsiValue1 <= RSISellH && rsiValue2 > rsiValue1) {
State = STATE_SELL_COVER;
}else if(State != STATE_BUY_COVER && State != STATE_SELL_COVER) {
return;
}
//Execute Close Position
if (State == STATE_BUY_COVER) {
var price = ticker.Last + SlidePrice;
var stkTB = adjustFloat(EnterAcnt.Stocks - account.Stocks);
var stkCB = adjustFloat(account.Balance / price);
var amount = (stkTB < stkCB) ? stkTB : stkCB;
if (amount >= exchange.GetMinStock()) {
exchange.Buy(price, amount);
}
else
{
// No money, wait idle and log profit
Log("<--- covers");
LogProfitTotal(InitAccount, account, ticker);
Log(account);
State = STATE_WAIT_IDLE;
}
} else if(State == STATE_SELL_COVER) {
var stkS = account.Stocks - EnterAcnt.Stocks;
if (stkS > exchange.GetMinStock()) {
exchange.Sell(ticker.Last - SlidePrice, stkS);
}
else
{
// No stocks, wait idle and log profit
Log("<--- coverb");
LogProfitTotal(InitAccount, account, ticker);
Log(account);
State = STATE_WAIT_IDLE;
}
}
}
function main() {
InitAccount = MustGetAccount();
Log(InitAccount);
while (true) {
onTick(exchange);
Sleep(Interval);
}
}Published by tfboys on the FMZ strategy square · last modified 2016-03-08. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/RSI_now_sb_ok.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.