{
  "name": "BYBIT Trading Bot",
  "nodes": [
    {
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "mode": "everyX",
              "unit": "minutes",
              "value": 5
            }
          ]
        }
      },
      "name": "Cron",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        100,
        300
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://api.bybit.com/v2/public/tickers?symbol=BTCUSDT"
      },
      "name": "Fetch Market Data",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        300,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const price = items[0].json.result[0].last_price;\nlet prices = [];\nprices.push(price);\nconst smaPeriod = 5;\nif (prices.length >= smaPeriod) {\n  const sma = prices.slice(-smaPeriod).reduce((a, b) => a + b) / smaPeriod;\n  return [{ json: { signal: price > sma ? 'buy' : 'sell' } }];\n}\nreturn [{ json: { signal: 'wait' } }];"
      },
      "name": "Generate Signal",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "{{$node[\"Generate Signal\"].json[\"signal\"]}}",
              "operation": "equals",
              "value2": "buy"
            }
          ]
        }
      },
      "name": "IF Buy",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        700,
        200
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "{{$node[\"Generate Signal\"].json[\"signal\"]}}",
              "operation": "equals",
              "value2": "sell"
            }
          ]
        }
      },
      "name": "IF Sell",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        700,
        400
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.bybit.com/v2/private/order/create",
        "options": {},
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "side",
              "value": "Buy"
            },
            {
              "name": "symbol",
              "value": "BTCUSDT"
            },
            {
              "name": "order_type",
              "value": "Market"
            },
            {
              "name": "qty",
              "value": 0.001
            },
            {
              "name": "time_in_force",
              "value": "GoodTillCancel"
            }
          ]
        },
        "headersUi": {
          "parameter": [
            {
              "name": "api_key",
              "value": "YOUR_API_KEY"
            },
            {
              "name": "timestamp",
              "value": "{{Math.floor(Date.now() / 1000)}}"
            },
            {
              "name": "sign",
              "value": "YOUR_SIGNATURE"
            }
          ]
        }
      },
      "name": "Execute Buy Order",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        900,
        100
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.bybit.com/v2/private/order/create",
        "options": {},
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "side",
              "value": "Sell"
            },
            {
              "name": "symbol",
              "value": "BTCUSDT"
            },
            {
              "name": "order_type",
              "value": "Market"
            },
            {
              "name": "qty",
              "value": 0.001
            },
            {
              "name": "time_in_force",
              "value": "GoodTillCancel"
            }
          ]
        },
        "headersUi": {
          "parameter": [
            {
              "name": "api_key",
              "value": "YOUR_API_KEY"
            },
            {
              "name": "timestamp",
              "value": "{{Math.floor(Date.now() / 1000)}}"
            },
            {
              "name": "sign",
              "value": "YOUR_SIGNATURE"
            }
          ]
        }
      },
      "name": "Execute Sell Order",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        900,
        500
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.bybit.com/v2/private/stop-order/create",
        "options": {},
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "side",
              "value": "Sell"
            },
            {
              "name": "symbol",
              "value": "BTCUSDT"
            },
            {
              "name": "order_type",
              "value": "Market"
            },
            {
              "name": "qty",
              "value": 0.001
            },
            {
              "name": "stop_px",
              "value": 30000
            },
            {
              "name": "base_price",
              "value": 31000
            }
          ]
        },
        "headersUi": {
          "parameter": [
            {
              "name": "api_key",
              "value": "YOUR_API_KEY"
            },
            {
              "name": "timestamp",
              "value": "{{Math.floor(Date.now() / 1000)}}"
            },
            {
              "name": "sign",
              "value": "YOUR_SIGNATURE"
            }
          ]
        }
      },
      "name": "Set Stop-Loss",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1100,
        300
      ]
    },
    {
      "parameters": {
        "path": "/path/to/log/file.log",
        "options": {}
      },
      "name": "Logger",
      "type": "n8n-nodes-base.writeBinaryFile",
      "typeVersion": 1,
      "position": [
        1300,
        300
      ]
    },
    {
      "parameters": {
        "chatId": "YOUR_CHAT_ID",
        "text": "Order executed: {{$node[\"Execute Buy Order\"].json}}",
        "additionalFields": {}
      },
      "name": "Telegram Notification",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        1500,
        300
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Cron": {
      "main": [
        [
          {
            "node": "Fetch Market Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Market Data": {
      "main": [
        [
          {
            "node": "Generate Signal",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Signal": {
      "main": [
        [
          {
            "node": "IF Buy",
            "type": "main",
            "index": 0
          },
          {
            "node": "IF Sell",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF Buy": {
      "main": [
        [
          {
            "node": "Execute Buy Order",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF Sell": {
      "main": [
        [
          {
            "node": "Execute Sell Order",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Buy Order": {
      "main": [
        [
          {
            "node": "Set Stop-Loss",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Sell Order": {
      "main": [
        [
          {
            "node": "Set Stop-Loss",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Stop-Loss": {
      "main": [
        [
          {
            "node": "Logger",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Logger": {
      "main": [
        [
          {
            "node": "Telegram Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}