Question: Related to the following question but looking for a PyQGIS method:

[QGIS Layer Order Panel - add layers at top of layer order](https://gis.stackexchange.com/questions/180943/qgis-layer-order-panel-add-layers-at-top-of-layer-order)

---

The following is a simple setup containing a group with three layers.

[![Simple setup](https://i.stack.imgur.com/GV5pZ.png)](https://i.stack.imgur.com/GV5pZ.png)

The code I use adds a new layer at the **end of this group**:

```
root = QgsProject.instance().layerTreeRoot()
group = root.findGroup('Main group')

vlayer = QgsVectorLayer('LineString?crs=epsg:27700', 'vlayer', 'memory')
QgsMapLayerRegistry.instance().addMapLayer(vlayer, False)
group.insertChildNode(-1, QgsLayerTreeLayer(vlayer))  

```

[![New layer added at end of group](https://i.stack.imgur.com/kK1Hq.png)](https://i.stack.imgur.com/kK1Hq.png)

In the *Layer Order Panel*, the newly added layer is at the end. 

[![Layer Order Panel](https://i.stack.imgur.com/pbexP.png)](https://i.stack.imgur.com/pbexP.png)

---

Is it possible to move this to the top **without moving the layer in the *Layers Panel***?

Answer: 
Based on this (old) question and some other research, I've successfully added a handler to my QGIS 3.10 project to move all newly added layers to the top of the custom layer order. Here's the relevant project macro code with v3 object references.

```
from qgis.core import QgsProject
from qgis.utils import iface

def openProject():
    global layersToTopHandler # keep so can disconnect() it in closeProject()
    def layersToTopHandler( layers ):
        # moves last len(layers) layers to top of custom layer order
        # note pays no attention to what is in the list given, always moves bottom layers
        # intended as a legendLayersAdded signal handler so the new layers have just been put at bottom! 
        rg = iface.layerTreeCanvasBridge().rootGroup()
        if rg.hasCustomLayerOrder():
            # Only do anything if custom layer order is turned on
            order = rg.customLayerOrder()
            for layer in layers: # How many layers we need to move
                order.insert( 0, order.pop() ) # Last layer to first position
            rg.setCustomLayerOrder( order )
    QgsProject.instance().legendLayersAdded.connect( layersToTopHandler )

def saveProject():
    pass

def closeProject():
    QgsProject.instance().legendLayersAdded.disconnect(layersToTopHandler)
    # needed since otherwise signal would go to slot even when change projects 

```

**Edit**: My answer originally didn't have the `global` line and just a `disconnect()` in `closeProject()`. This subtly failed in 3.10.2 (making it impossible to add layers in a subsequently opened project) since the argument-less `disconnect()` would also clobber some core slots on this signal. There may be more elegant ways to keep track of our own slot than to globalize the event handler itself but this seems to work. Note it's tempting to not bother disconnecting, assuming closing the project and opening a new one will reinitialize everything, but slots/signals stay persistent!
I am not so familiar with pyqgis, but when I changed the line in your code from 

```
group.insertChildNode(-1, QgsLayerTreeLayer(vlayer)) 

```

to 

```
group.insertChildNode(0, QgsLayerTreeLayer(vlayer)) 

```

It was intserted at the top of the group, not the bottom.